<?php
$assetBase =
    basename(rtrim(FCPATH, "\\/")) === "public" ? "assets/" : "public/assets/";
$isEdit = $mode === "edit";
$value = static function ($field, $default = "") use ($article) {
    return old($field, $article[$field] ?? $default);
};
$publishedAt = $value("published_at");
if ($publishedAt) {
    $publishedAt = date("Y-m-d\TH:i", strtotime($publishedAt));
}
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= $isEdit
        ? "Edit Article"
        : "Create Article" ?> · 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 href="<?= base_url(
    "admin/invoices",
) ?>"><i>▤</i> Invoices</a><a href="<?= base_url(
    "admin/industries",
) ?>"><i>◆</i> Industries</a><a class="active" 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></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 quotation-form-main article-form-main">
    <header class="admin-heading"><div><a class="back-to-templates" href="<?= $isEdit
        ? base_url("admin/articles/" . $article["id"])
        : base_url("admin/articles") ?>">← Article library</a><h1><?= $isEdit
    ? "Edit Brand Guide article"
    : "Create Brand Guide article" ?></h1><p>Write useful content first, then optimise the search appearance and publishing controls.</p></div></header>
    <?php if (session("error")): ?><div class="alert error"><?= esc(
    session("error"),
) ?></div><?php endif; ?>
    <form method="post" class="quotation-form"><?= csrf_field() ?>
        <section class="quotation-form-card"><div class="quotation-form-section-title"><span>01</span><div><h2>Article identity</h2><p>Title, URL, category, authorship, and visual treatment.</p></div></div>
            <div class="quotation-details-grid article-identity-grid">
                <label class="article-wide-field">Article title<input required maxlength="180" name="title" value="<?= esc(
                    $value("title"),
                ) ?>" placeholder="A clear, useful title matching a real search question"></label>
                <label>URL slug<input required maxlength="180" pattern="[a-z0-9]+(?:-[a-z0-9]+)*" name="slug" value="<?= esc(
                    $value("slug"),
                ) ?>" placeholder="how-to-choose-a-logo"><small>/brand-guide/your-article-slug</small></label>
                <label>Category<select required name="category"><?php foreach (
                    $categories
                    as $category
                ): ?><option value="<?= esc($category) ?>" <?= $value(
    "category",
    "Logo Planning",
) === $category
    ? "selected"
    : "" ?>><?= esc($category) ?></option><?php endforeach; ?></select></label>
                <label>Author<input required maxlength="120" name="author_name" value="<?= esc(
                    $value("author_name", "BrandSquare Team"),
                ) ?>"></label>
                <label>Reading time<input required type="number" min="1" max="60" name="reading_minutes" value="<?= esc(
                    $value("reading_minutes", "6"),
                ) ?>"><small>Estimated minutes</small></label>
                <label>Card theme<select required name="theme"><?php foreach (
                    $themes
                    as $theme => $themeLabel
                ): ?><option value="<?= esc($theme) ?>" <?= $value(
    "theme",
    "orange",
) === $theme
    ? "selected"
    : "" ?>><?= esc(
    $themeLabel,
) ?></option><?php endforeach; ?></select></label>
            </div>
        </section>
        <section class="quotation-form-card"><div class="quotation-form-section-title"><span>02</span><div><h2>Article content</h2><p>Give readers a complete answer. Use descriptive headings, short paragraphs, and practical lists.</p></div></div>
            <div class="industry-copy-grid article-copy-grid">
                <label>Card excerpt<textarea required minlength="40" maxlength="700" rows="4" name="excerpt" placeholder="A concise promise explaining what the reader will learn."><?= esc(
                    $value("excerpt"),
                ) ?></textarea></label>
                <label>Article body (limited HTML)<textarea class="article-content-editor" required minlength="300" maxlength="60000" rows="24" name="content" placeholder="<p>Opening answer...</p>&#10;<h2>Useful section heading</h2>&#10;<p>Detailed guidance...</p>"><?= esc(
                    $value("content"),
                ) ?></textarea><small>Allowed tags: p, h2, h3, h4, ul, ol, li, strong, em, blockquote, a, hr.</small></label>
            </div>
        </section>
        <section class="quotation-form-card"><div class="quotation-form-section-title"><span>03</span><div><h2>Search appearance</h2><p>Create a unique, accurate result that matches the reader’s search intent.</p></div></div>
            <div class="industry-copy-grid article-seo-grid">
                <label>SEO title<input required minlength="20" maxlength="180" name="meta_title" value="<?= esc(
                    $value("meta_title"),
                ) ?>"></label>
                <label>Meta description<textarea required minlength="60" maxlength="320" rows="3" name="meta_description"><?= esc(
                    $value("meta_description"),
                ) ?></textarea></label>
                <label>Primary search phrase<input maxlength="160" name="focus_keyword" value="<?= esc(
                    $value("focus_keyword"),
                ) ?>" placeholder="e.g. logo design brief checklist"><small>Use naturally in the title, opening, and useful headings.</small></label>
            </div>
        </section>
        <section class="quotation-form-card"><div class="quotation-form-section-title"><span>04</span><div><h2>Publishing</h2><p>Save a private draft or make the article visible in the Brand Guide.</p></div></div>
            <div class="article-publishing-grid">
                <label><span>Published date</span><input type="datetime-local" name="published_at" value="<?= esc(
                    $publishedAt,
                ) ?>"><small>Leave empty to use the first publication time.</small></label>
                <label class="article-check"><input type="checkbox" name="is_featured" value="1" <?= (string) $value(
                    "is_featured",
                    "0",
                ) === "1"
                    ? "checked"
                    : "" ?>><span><b>Feature this article</b><small>Give it priority on the Brand Guide page.</small></span></label>
                <label class="article-check"><input type="checkbox" name="is_published" value="1" <?= (string) $value(
                    "is_published",
                    "0",
                ) === "1"
                    ? "checked"
                    : "" ?>><span><b>Publish article</b><small>Make the public URL and sitemap entry active.</small></span></label>
            </div>
        </section>
        <div class="quotation-form-actions"><a href="<?= $isEdit
            ? base_url("admin/articles/" . $article["id"])
            : base_url(
                "admin/articles",
            ) ?>">Cancel</a><button type="submit">Save article <span>→</span></button></div>
    </form>
</main>
</body>
</html>
