<?php $this->extend("layouts/main"); $this->section("content"); $title = "Quotes";
$sym = ["GBP"=>"£","EUR"=>"€","USD"=>"$"][$settings["regional"]["currency"]??"GBP"] ?? "£";
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <?php if (in_array("edit_quotes", $permissions)): ?><a href="/fieldly/public/index.php/quotes/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i> New Quote</a><?php endif; ?>
</div>
<div class="fieldly-tabs mb-3">
    <a class="fieldly-tab <?php echo $tab==="all"?"active":"" ?>" href="/fieldly/public/index.php/quotes?tab=all">All <span class="badge bg-secondary ms-1"><?php echo $counts["all"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="sent"?"active":"" ?>" href="/fieldly/public/index.php/quotes?tab=sent&status=sent">Sent <span class="badge bg-secondary ms-1"><?php echo $counts["sent"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="deleted"?"active":"" ?>" href="/fieldly/public/index.php/quotes?tab=deleted">Deleted <span class="badge bg-secondary ms-1"><?php echo $counts["deleted"] ?></span></a>
</div>
<form method="GET" class="row g-2 mb-3">
    <input type="hidden" name="tab" value="<?php echo esc($tab) ?>"/>
    <div class="col-md-6"><input type="text" name="search" class="form-control" placeholder="Search quotes..." value="<?php echo esc($search ?? "") ?>"/></div>
    <div class="col-md-4"><select name="status" class="form-select"><option value="">All Statuses</option><?php foreach (["draft","sent","accepted","declined","converted"] as $s): ?><option value="<?php echo $s ?>" <?php echo ($status??"")===$s?"selected":"" ?>><?php echo ucfirst($s) ?></option><?php endforeach; ?></select></div>
    <div class="col-md-2"><button type="submit" class="btn btn-outline-secondary w-100"><i class="bi bi-search"></i></button></div>
</form>
<div class="card"><div class="card-body p-0"><table class="table table-hover mb-0">
<thead><tr><th>Quote</th><th>Customer</th><th>Status</th><th>Total</th><th>Valid Until</th><th></th></tr></thead>
<tbody>
<?php if (empty($quotes)): ?><tr><td colspan="6" class="text-center text-muted py-4">No quotes found</td></tr>
<?php else: foreach ($quotes as $q): ?>
<tr>
    <td><a href="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>" class="fw-semibold text-decoration-none"><?php echo esc($q["number"]) ?></a><?php if ($q["title"]): ?><div class="text-muted small"><?php echo esc($q["title"]) ?></div><?php endif; ?></td>
    <td style="font-size:13px"><?php echo esc($q["company_name"]?:$q["first_name"]." ".$q["last_name"]) ?></td>
    <td><span class="badge bg-<?php echo ["draft"=>"secondary","sent"=>"primary","accepted"=>"success","declined"=>"danger","converted"=>"info"][$q["status"]]??"secondary" ?>"><?php echo ucfirst($q["status"]) ?></span></td>
    <td class="fw-bold"><?php echo $sym.number_format((float)$q["total"],2) ?></td>
    <td style="font-size:13px"><?php echo $q["valid_until"] ? date("d M Y",strtotime($q["valid_until"])) : "—" ?></td>
    <td><div class="table-actions">
        <a href="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a>
        <?php if (in_array("edit_quotes", $permissions) && $tab!=="deleted"): ?>
        <a href="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/delete" data-confirm="Move to recycle bin?" class="d-inline"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button></form>
        <?php elseif ($tab==="deleted"): ?>
        <form method="POST" action="/fieldly/public/index.php/quotes/<?php echo $q["id"] ?>/restore" class="d-inline"><?php echo csrf_field() ?><button class="btn btn-sm btn-outline-success"><i class="bi bi-arrow-counterclockwise"></i></button></form>
        <?php endif; ?>
    </div></td>
</tr>
<?php endforeach; endif; ?>
</tbody>
</table></div></div>
<?php $this->endSection(); ?>