<?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>Quotations · 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 class="active" href="<?= base_url(
    "admin/quotations",
) ?>"><i>₹</i> Quotations</a><a 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>Sales documents</span><h1>Quotations</h1><p>Create clear, itemised proposals for every BrandSquare client.</p></div><a class="primary-admin-action" href="<?= base_url(
            "admin/quotations/new",
        ) ?>">Create quotation <span>＋</span></a></header>

        <?php if (session("error")): ?><div class="alert error"><?= esc(
    session("error"),
) ?></div><?php endif; ?>
        <?php if (session("success")): ?><div class="alert success"><?= esc(
    session("success"),
) ?></div><?php endif; ?>

        <section class="quotation-metrics">
            <article><span>All quotations</span><b><?= esc(
                $metrics["total"],
            ) ?></b><small>Created documents</small></article>
            <article><span>Drafts</span><b><?= esc(
                $metrics["draft"],
            ) ?></b><small>Still being prepared</small></article>
            <article><span>Accepted</span><b><?= esc(
                $metrics["accepted"],
            ) ?></b><small>Approved by clients</small></article>
            <article><span>Total quoted</span><b>₹<?= number_format(
                $metrics["value"],
                2,
            ) ?></b><small>Across all quotations</small></article>
        </section>

        <section class="admin-panel quotation-panel">
            <div class="panel-toolbar">
                <div><h2>Quotation register</h2><span>Showing up to 250 latest quotations</span></div>
                <form method="get" class="quotation-filters">
                    <input name="search" value="<?= esc(
                        $search,
                    ) ?>" placeholder="Number, client or email...">
                    <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/quotations",
) ?>">Clear</a><?php endif; ?>
                </form>
            </div>
            <div class="table-wrap">
                <table class="quotation-table">
                    <thead><tr><th>Quotation</th><th>Client</th><th>Date</th><th>Total</th><th>Status</th><th>Actions</th></tr></thead>
                    <tbody>
                    <?php if (!$quotations): ?>
                        <tr><td colspan="6" class="empty-state"><b>No quotations found.</b><span>Create a quotation or adjust your filters.</span></td></tr>
                    <?php endif; ?>
                    <?php foreach ($quotations as $quotation): ?>
                        <tr>
                            <td><a class="quotation-number" href="<?= base_url(
                                "admin/quotations/" . $quotation["id"],
                            ) ?>"><?= esc(
    $quotation["quotation_number"],
) ?></a><small>Created <?= date(
    "d M Y",
    strtotime($quotation["created_at"]),
) ?></small></td>
                            <td><b><?= esc(
                                $quotation["first_name"] .
                                    " " .
                                    $quotation["last_name"],
                            ) ?></b><small><?= esc(
    $quotation["email"],
) ?></small></td>
                            <td><span><?= date(
                                "d M Y",
                                strtotime($quotation["quotation_date"]),
                            ) ?></span><?php if (
    $quotation["valid_until"]
): ?><small>Valid to <?= date(
    "d M Y",
    strtotime($quotation["valid_until"]),
) ?></small><?php endif; ?></td>
                            <td><b class="quotation-total">₹<?= number_format(
                                (float) $quotation["total"],
                                2,
                            ) ?></b></td>
                            <td><span class="quote-status status-<?= esc(
                                $quotation["status"],
                            ) ?>"><?= esc(
    ucfirst($quotation["status"]),
) ?></span></td>
                            <td><div class="client-row-actions"><a href="<?= base_url(
                                "admin/quotations/" . $quotation["id"],
                            ) ?>">View</a><a href="<?= base_url(
    "admin/quotations/" . $quotation["id"] . "/edit",
) ?>">Edit</a></div></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </main>
</body>
</html>
