<?php $this->extend("layouts/main"); $this->section("content"); $title = "Invoices";
$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_invoices", $permissions)): ?>
    <a href="/fieldly/public/index.php/invoices/create" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i> New Invoice</a>
    <?php endif; ?>
</div>
<div class="fieldly-tabs mb-3">
    <a class="fieldly-tab <?php echo $tab==="all"?"active":"" ?>" href="/fieldly/public/index.php/invoices?tab=all">All <span class="badge bg-secondary ms-1"><?php echo $counts["all"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="unpaid"?"active":"" ?>" href="/fieldly/public/index.php/invoices?tab=unpaid&status=sent">Unpaid <span class="badge bg-warning text-dark ms-1"><?php echo $counts["unpaid"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="overdue"?"active":"" ?>" href="/fieldly/public/index.php/invoices?tab=overdue&status=overdue">Overdue <span class="badge bg-danger ms-1"><?php echo $counts["overdue"] ?></span></a>
    <a class="fieldly-tab <?php echo $tab==="deleted"?"active":"" ?>" href="/fieldly/public/index.php/invoices?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 invoices..." 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","part_paid","paid","overdue","void"] as $s): ?><option value="<?php echo $s ?>" <?php echo ($status??"")===$s?"selected":"" ?>><?php echo ucfirst(str_replace("_"," ",$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>Invoice</th><th>Customer</th><th>Status</th><th>Total</th><th>Balance</th><th>Due</th><th></th></tr></thead>
<tbody>
<?php if (empty($invoices)): ?><tr><td colspan="7" class="text-center text-muted py-4">No invoices found</td></tr>
<?php else: ?>
<?php foreach ($invoices as $inv): ?>
<tr>
    <td><a href="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>" class="fw-semibold text-decoration-none"><?php echo esc($inv["number"]) ?></a><?php if ($inv["title"]): ?><div class="text-muted small"><?php echo esc($inv["title"]) ?></div><?php endif; ?></td>
    <td style="font-size:13px"><?php echo esc($inv["company_name"]?:$inv["first_name"]." ".$inv["last_name"]) ?></td>
    <td><span class="badge bg-<?php echo ["paid"=>"success","overdue"=>"danger","draft"=>"secondary","sent"=>"primary","part_paid"=>"warning","void"=>"dark"][$inv["status"]]??"secondary" ?>"><?php echo ucfirst(str_replace("_"," ",$inv["status"])) ?></span></td>
    <td class="fw-bold"><?php echo $sym.number_format((float)$inv["total"],2) ?></td>
    <td><?php echo $sym.number_format((float)$inv["balance_due"],2) ?></td>
    <td style="font-size:13px;<?php echo $inv["status"]==="overdue"?"color:#ef4444":"" ?>"><?php echo $inv["due_date"] ? date("d M Y",strtotime($inv["due_date"])) : "—" ?></td>
    <td>
        <div class="table-actions">
            <a href="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a>
            <?php if (in_array("edit_invoices", $permissions) && $tab!=="deleted"): ?>
            <a href="/fieldly/public/index.php/invoices/<?php echo $inv["id"] ?>/edit" class="btn btn-sm btn-outline-primary"><i class="bi bi-pencil"></i></a>
            <form method="POST" action="/fieldly/public/index.php/invoices/<?php echo $inv["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/invoices/<?php echo $inv["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; ?>
<?php endif; ?>
</tbody>
</table>
</div></div>
<?php $this->endSection(); ?>