<?php
/// file: app/View/admin/journal/form.php

declare(strict_types=1);

/** @var array<string, mixed>|null $post */
/** @var array<string, string> $errors */
$p = $post ?? [];
$isEdit = isset($p['id']);
$action = $isEdit ? url('/admin/journal/edit/' . (int) $p['id']) : url('/admin/journal/create');
?>
<h1 class="text-2xl font-semibold text-white"><?= $isEdit ? 'Edit post' : 'New post' ?></h1>
<?php foreach ($errors as $msg): ?><p class="mt-4 text-sm text-red-400"><?= e((string) $msg) ?></p><?php endforeach; ?>
<form method="post" action="<?= e($action) ?>" class="mt-8 max-w-3xl space-y-4">
    <?= \App\Core\Csrf::field() ?>
    <div class="grid gap-4 md:grid-cols-2">
        <div><label class="text-xs text-silver-500">Title</label><input name="title" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['title'] ?? '')) ?>" required></div>
        <div><label class="text-xs text-silver-500">Slug</label><input name="slug" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['slug'] ?? '')) ?>" required></div>
    </div>
    <div><label class="text-xs text-silver-500">Excerpt</label><textarea name="excerpt" rows="2" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" required><?= e((string) ($p['excerpt'] ?? '')) ?></textarea></div>
    <div><label class="text-xs text-silver-500">Body (HTML)</label><textarea name="body" rows="10" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white font-mono text-sm" required><?= e((string) ($p['body'] ?? '')) ?></textarea></div>
    <div class="grid gap-4 md:grid-cols-3">
        <div><label class="text-xs text-silver-500">Category</label><input name="category" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['category'] ?? 'studio')) ?>"></div>
        <div><label class="text-xs text-silver-500">Reading time</label><input name="reading_time" type="number" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['reading_time'] ?? '5')) ?>"></div>
        <div><label class="text-xs text-silver-500">Published</label><input name="published_at" type="date" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['published_at'] ?? date('Y-m-d'))) ?>"></div>
    </div>
    <div><label class="text-xs text-silver-500">Cover path</label><input name="cover_path" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['cover_path'] ?? '')) ?>"></div>
    <div><label class="text-xs text-silver-500">Status</label><select name="status" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white"><option value="published" <?= (($p['status'] ?? '') === 'published') ? 'selected' : '' ?>>published</option><option value="draft" <?= (($p['status'] ?? '') === 'draft') ? 'selected' : '' ?>>draft</option></select></div>
    <button type="submit" class="rounded-full bg-white px-6 py-2 text-xs font-semibold text-carbon">Save</button>
</form>
