<?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>Clients · 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 class="active" 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/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 clients-main">
        <header class="admin-heading"><div><span>Relationship management</span><h1>Clients</h1><p>Keep every client’s contact information organised and easy to reach.</p></div><a class="primary-admin-action" href="<?= base_url(
            "admin/clients/new",
        ) ?>">Add new client <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="client-metrics">
            <article><span>All clients</span><b><?= esc(
                $metrics["total"],
            ) ?></b><small>Saved profiles</small></article>
            <article><span>Added this month</span><b><?= esc(
                $metrics["thisMonth"],
            ) ?></b><small>New relationships</small></article>
            <article><span>Complete profiles</span><b><?= esc(
                $metrics["complete"],
            ) ?></b><small>Ready to contact</small></article>
        </section>

        <section class="admin-panel client-panel">
            <div class="panel-toolbar">
                <div><h2>Client directory</h2><span>Showing up to 250 client records</span></div>
                <form method="get" class="client-search"><input name="search" value="<?= esc(
                    $search,
                ) ?>" placeholder="Search name, email, mobile..."><button type="submit">Search</button><?php if (
    $search
): ?><a href="<?= base_url("admin/clients") ?>">Clear</a><?php endif; ?></form>
            </div>
            <div class="table-wrap">
                <table class="client-table">
                    <thead><tr><th>Client</th><th>Contact</th><th>Address</th><th>Added</th><th>Actions</th></tr></thead>
                    <tbody>
                    <?php if (!$clients): ?>
                        <tr><td colspan="5" class="empty-state"><b><?= $search
                            ? "No matching clients."
                            : "No clients yet." ?></b><span><?= $search
    ? "Try a different search term."
    : "Add your first client to start building the directory." ?></span></td></tr>
                    <?php endif; ?>
                    <?php foreach ($clients as $client): ?>
                        <?php
                        $fullName =
                            $client["first_name"] . " " . $client["last_name"];
                        $initials =
                            substr($client["first_name"], 0, 1) .
                            substr($client["last_name"], 0, 1);
                        ?>
                        <tr>
                            <td><a class="client-identity" href="<?= base_url(
                                "admin/clients/" . $client["id"],
                            ) ?>"><span><?= esc(
    strtoupper($initials),
) ?></span><div><b><?= esc($fullName) ?></b><small>Client #<?= esc(
    $client["id"],
) ?></small></div></a></td>
                            <td><a href="mailto:<?= esc(
                                $client["email"],
                            ) ?>"><?= esc(
    $client["email"],
) ?></a><a href="tel:<?= esc(
    preg_replace("/[^0-9+]/", "", $client["mobile"]),
) ?>"><?= esc($client["mobile"]) ?></a></td>
                            <td><p class="client-address"><?= nl2br(
                                esc($client["address"]),
                            ) ?></p></td>
                            <td><span><?= date(
                                "d M Y",
                                strtotime($client["created_at"]),
                            ) ?></span><small><?= date(
    "h:i A",
    strtotime($client["created_at"]),
) ?></small></td>
                            <td><div class="client-row-actions"><a href="<?= base_url(
                                "admin/clients/" . $client["id"],
                            ) ?>">View</a><a href="<?= base_url(
    "admin/clients/" . $client["id"] . "/edit",
) ?>">Edit</a></div></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </section>
    </main>
</body>
</html>
