<?php $assetBase =
    basename(rtrim(FCPATH, "\\/")) === "public"
        ? "assets/"
        : "public/assets/"; ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Invoices · BrandSquare Admin</title>
    <link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;600;700&family=Manrope:wght@600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?= base_url($assetBase . "css/admin.css") ?>">
</head>
<body class="admin-page">
<aside class="admin-sidebar"><a href="<?= base_url(
    "admin/leads",
) ?>" class="admin-logo">Brand<span>Square.</span></a><nav><a href="<?= base_url(
    "admin/leads",
) ?>"><i>◎</i> Leads</a><a href="<?= base_url(
    "admin/clients",
) ?>"><i>◉</i> Clients</a><a href="<?= base_url(
    "admin/quotations",
) ?>"><i>₹</i> Quotations</a><a class="active" href="<?= base_url(
    "admin/invoices",
) ?>"><i>▤</i> Invoices</a><a href="<?= base_url(
    "admin/industries",
) ?>"><i>◆</i> Industries</a><a href="<?= base_url(
    "admin/email-templates",
) ?>"><i>✦</i> Email templates</a><a href="<?= base_url(
    "admin/articles",
) ?>"><i>▤</i> Articles</a><a href="<?= base_url(
    "admin/settings",
) ?>"><i>⚙</i> Settings</a><a href="<?= base_url() ?>" target="_blank"><i>↗</i> View website</a></nav><div class="admin-user"><span><?= esc(
    strtoupper(substr((string) session("admin_name"), 0, 2)),
) ?></span><div><b><?= esc(session("admin_name")) ?></b><small><?= esc(
    session("admin_email"),
) ?></small></div></div><form action="<?= base_url(
    "admin/logout",
) ?>" method="post"><?= csrf_field() ?><button class="logout" type="submit">Sign out →</button></form></aside>
<main class="admin-main quotations-main">
    <header class="admin-heading"><div><span>Accounts & billing</span><h1>Invoices</h1><p>Create invoices from accepted quotations or bill clients directly.</p></div><a class="primary-admin-action" href="<?= base_url(
        "admin/invoices/new",
    ) ?>">Create invoice <span>＋</span></a></header>
    <?php
    if (session("error")): ?><div class="alert error"><?= esc(
    session("error"),
) ?></div><?php endif;
    if (session("success")): ?><div class="alert success"><?= esc(
    session("success"),
) ?></div><?php endif;
    ?>
    <section class="quotation-metrics invoice-metrics">
        <article><span>All invoices</span><b><?= esc(
            $metrics["total"],
        ) ?></b><small>Created invoices</small></article>
        <article><span>Paid</span><b><?= esc(
            $metrics["paid"],
        ) ?></b><small>Completed payments</small></article>
        <article><span>Total invoiced</span><b>₹<?= number_format(
            $metrics["value"],
            2,
        ) ?></b><small>Across all invoices</small></article>
        <article><span>Outstanding</span><b>₹<?= number_format(
            $metrics["outstanding"],
            2,
        ) ?></b><small>Draft, sent and overdue</small></article>
    </section>
    <section class="admin-panel quotation-panel"><div class="panel-toolbar"><div><h2>Invoice register</h2><span>Showing up to 250 latest invoices</span></div><form method="get" class="quotation-filters"><input name="search" value="<?= esc(
        $search,
    ) ?>" placeholder="Invoice, quotation or client..."><select name="status"><option value="">All statuses</option><?php foreach (
    $statuses
    as $status
): ?><option value="<?= esc($status) ?>" <?= $activeStatus === $status
    ? "selected"
    : "" ?>><?= esc(
    ucfirst($status),
) ?></option><?php endforeach; ?></select><button type="submit">Filter</button><?php if (
    $search ||
    $activeStatus
): ?><a href="<?= base_url(
    "admin/invoices",
) ?>">Clear</a><?php endif; ?></form></div>
    <div class="table-wrap"><table class="quotation-table"><thead><tr><th>Invoice</th><th>Client</th><th>Based on</th><th>Due date</th><th>Total</th><th>Status</th><th>Actions</th></tr></thead><tbody>
    <?php if (
        !$invoices
    ): ?><tr><td colspan="7" class="empty-state"><b>No invoices found.</b><span>Create one from a quotation or directly for a client.</span></td></tr><?php endif; ?>
    <?php foreach ($invoices as $invoice): ?><tr>
        <td><a class="quotation-number" href="<?= base_url(
            "admin/invoices/" . $invoice["id"],
        ) ?>"><?= esc($invoice["invoice_number"]) ?></a><small><?= date(
    "d M Y",
    strtotime($invoice["invoice_date"]),
) ?></small></td>
        <td><b><?= esc(
            $invoice["first_name"] . " " . $invoice["last_name"],
        ) ?></b><small><?= esc($invoice["email"]) ?></small></td>
        <td><span><?= $invoice["quotation_number"]
            ? esc($invoice["quotation_number"])
            : "Standalone" ?></span></td>
        <td><span><?= $invoice["due_date"]
            ? date("d M Y", strtotime($invoice["due_date"]))
            : "On receipt" ?></span></td>
        <td><b class="quotation-total">₹<?= number_format(
            (float) $invoice["total"],
            2,
        ) ?></b></td>
        <td><span class="quote-status status-<?= esc(
            $invoice["status"],
        ) ?>"><?= esc(ucfirst($invoice["status"])) ?></span></td>
        <td><div class="client-row-actions"><a href="<?= base_url(
            "admin/invoices/" . $invoice["id"],
        ) ?>">View</a><a href="<?= base_url(
    "admin/invoices/" . $invoice["id"] . "/edit",
) ?>">Edit</a></div></td>
    </tr><?php endforeach; ?></tbody></table></div></section>
</main></body></html>
