<?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>Leads · 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 class="active" 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 href="<?= base_url(
    "admin/invoices",
) ?>"><i>▤</i> Invoices</a><a href="<?= base_url(
    "admin/industries",
) ?>"><i>◆</i> Industries</a><a href="<?= base_url(
    "admin/articles",
) ?>"><i>▤</i> Articles</a><a href="<?= base_url(
    "admin/email-templates",
) ?>"><i>✦</i> Email templates</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">
        <header class="admin-heading"><div><span>Enquiry management</span><h1>Logo design leads</h1><p>Review every homepage brief and keep the sales conversation moving.</p></div><div class="heading-date"><?= date(
            "D, d M Y",
        ) ?></div></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="metric-grid">
            <article><span>All leads</span><b><?= esc(
                $metrics["total"],
            ) ?></b><i>Total enquiries</i></article>
            <article><span>New</span><b><?= esc(
                $metrics["new"],
            ) ?></b><i>Awaiting response</i></article>
            <article><span>Qualified</span><b><?= esc(
                $metrics["qualified"],
            ) ?></b><i>Strong opportunities</i></article>
            <article><span>Closed</span><b><?= esc(
                $metrics["closed"],
            ) ?></b><i>Completed conversations</i></article>
        </section>

        <section class="admin-panel">
            <div class="panel-toolbar"><div><h2>Recent enquiries</h2><span>Showing up to 100 latest leads</span></div><form method="get" class="lead-filters"><input name="search" value="<?= esc(
                $search,
            ) ?>" placeholder="Search name, email, business..."><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></form></div>
            <div class="table-wrap">
                <table class="lead-table">
                    <thead><tr><th>Lead</th><th>Business</th><th>Project</th><th>Contact</th><th>Received</th><th>Status</th></tr></thead>
                    <tbody>
                    <?php if (!$leads): ?>
                        <tr><td colspan="6" class="empty-state"><b>No leads found.</b><span>New homepage enquiries will appear here automatically.</span></td></tr>
                    <?php endif; ?>
                    <?php foreach ($leads as $lead): ?>
                        <tr>
                            <td><div class="lead-person"><span><?= esc(
                                strtoupper(substr($lead["name"], 0, 2)),
                            ) ?></span><div><b><?= esc(
    $lead["name"],
) ?></b><small>#<?= esc($lead["id"]) ?> · <?= esc(
     ucfirst($lead["source"]),
 ) ?></small></div></div></td>
                            <td><b><?= esc(
                                $lead["business_name"],
                            ) ?></b><small><?= esc(
    $lead["industry"],
) ?></small></td>
                            <td><div class="project-tags"><?php
                            if ($lead["style"]): ?><span><?= esc(
    $lead["style"],
) ?></span><?php endif;
                            if ($lead["budget"]): ?><span><?= esc(
    $lead["budget"],
) ?></span><?php endif;
                            ?></div><?php if (
    $lead["message"]
): ?><details><summary>View brief</summary><p><?= nl2br(
    esc($lead["message"]),
) ?></p></details><?php endif; ?></td>
                            <td><?php
                            if ($lead["email"]): ?><a href="mailto:<?= esc(
    $lead["email"],
) ?>"><?= esc($lead["email"]) ?></a><?php endif;
                            if ($lead["phone"]): ?><a href="tel:<?= esc(
    $lead["phone"],
) ?>"><?= esc($lead["phone"]) ?></a><?php endif;
                            ?></td>
                            <td><span><?= date(
                                "d M Y",
                                strtotime($lead["created_at"]),
                            ) ?></span><small><?= date(
    "h:i A",
    strtotime($lead["created_at"]),
) ?></small></td>
                            <td><form method="post" action="<?= base_url(
                                "admin/leads/" . $lead["id"] . "/status",
                            ) ?>" class="status-form"><?= csrf_field() ?><select name="status" class="status-<?= esc(
    $lead["status"],
) ?>" onchange="this.form.submit()"><?php foreach (
    $statuses
    as $status
): ?><option value="<?= esc($status) ?>" <?= $lead["status"] === $status
    ? "selected"
    : "" ?>><?= esc(
    ucfirst($status),
) ?></option><?php endforeach; ?></select></form></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </main>
</body>
</html>
