<?php
$assetBase =
    basename(rtrim(FCPATH, "\\/")) === "public" ? "assets/" : "public/assets/";
$isEdit = $mode === "edit";
$value = static function ($field) use ($client) {
    return old($field, $client[$field] ?? "");
};
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $isEdit
        ? "Edit Client"
        : "New Client" ?> · 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 client-form-main">
        <header class="admin-heading"><div><a class="back-to-templates" href="<?= $isEdit
            ? base_url("admin/clients/" . $client["id"])
            : base_url("admin/clients") ?>">← <?= $isEdit
    ? "Client profile"
    : "Client directory" ?></a><h1><?= $isEdit
    ? "Edit client"
    : "Add a new client" ?></h1><p><?= $isEdit
    ? "Keep contact details accurate and ready for every conversation."
    : "Create a complete contact record for a new BrandSquare client." ?></p></div></header>

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

        <form method="post" class="client-form-card">
            <?= csrf_field() ?>
            <div class="client-form-intro"><span><?= $isEdit
                ? "EDIT PROFILE"
                : "NEW PROFILE" ?></span><h2>Client information</h2><p>All fields are required. The email address must be unique.</p></div>
            <div class="client-form-grid">
                <label>First name<input name="first_name" required maxlength="100" autocomplete="given-name" value="<?= esc(
                    $value("first_name"),
                ) ?>" placeholder="First name"></label>
                <label>Last name<input name="last_name" required maxlength="100" autocomplete="family-name" value="<?= esc(
                    $value("last_name"),
                ) ?>" placeholder="Last name"></label>
                <label>Email address<input name="email" type="email" required maxlength="190" autocomplete="email" value="<?= esc(
                    $value("email"),
                ) ?>" placeholder="client@company.com"></label>
                <label>Mobile number<input name="mobile" type="tel" required maxlength="40" autocomplete="tel" value="<?= esc(
                    $value("mobile"),
                ) ?>" placeholder="+91 98765 43210"></label>
                <label class="client-address-field">Address<textarea name="address" required maxlength="2000" rows="6" autocomplete="street-address" placeholder="Complete postal address"><?= esc(
                    $value("address"),
                ) ?></textarea></label>
            </div>
            <div class="client-form-actions"><a href="<?= $isEdit
                ? base_url("admin/clients/" . $client["id"])
                : base_url(
                    "admin/clients",
                ) ?>">Cancel</a><button type="submit"><?= $isEdit
    ? "Save changes"
    : "Create client" ?> <span>→</span></button></div>
        </form>
    </main>
</body>
</html>
