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

declare(strict_types=1);

/** @var array<string, mixed>|null $project */
/** @var array<string, string> $errors */
$p = $project ?? [];
$isEdit = isset($p['id']);
$action = $isEdit ? url('/admin/projects/edit/' . (int) $p['id']) : url('/admin/projects/create');
$catSlugs = \App\Models\Project::categorySlugs();
$currentCat = (string) ($p['category'] ?? $catSlugs[0]);
?>
<h1 class="text-2xl font-semibold text-white"><?= $isEdit ? 'Edit project' : 'New project' ?></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><label class="text-xs text-silver-500">Client</label><input name="client" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['client'] ?? '')) ?>"></div>
        <div><label class="text-xs text-silver-500">Year</label><input name="year" type="number" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['year'] ?? date('Y'))) ?>"></div>
        <div><label class="text-xs text-silver-500">Sector</label><input name="sector" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['sector'] ?? '')) ?>"></div>
        <div>
            <label class="text-xs text-silver-500">Category</label>
            <select name="category" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white">
                <?php if ($currentCat !== '' && ! in_array($currentCat, $catSlugs, true)): ?>
                    <option value="<?= e($currentCat) ?>" selected><?= e($currentCat) ?> (legacy)</option>
                <?php endif; ?>
                <?php foreach ($catSlugs as $slug): ?>
                    <option value="<?= e($slug) ?>" <?= $currentCat === $slug ? 'selected' : '' ?>><?= e(__(\App\Models\Project::categoryMessageKey($slug))) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    <div><label class="text-xs text-silver-500">Services</label><textarea name="services" rows="2" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white"><?= e((string) ($p['services'] ?? '')) ?></textarea></div>
    <div><label class="text-xs text-silver-500">Overview</label><textarea name="summary" rows="5" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" placeholder="Short case overview for the project page"><?= e((string) ($p['summary'] ?? '')) ?></textarea></div>
    <div class="grid gap-4 md:grid-cols-3">
        <div><label class="text-xs text-silver-500">Cover type</label><select name="cover_type" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white"><option value="image" <?= (($p['cover_type'] ?? '') === 'image') ? 'selected' : '' ?>>image</option><option value="video" <?= (($p['cover_type'] ?? '') === 'video') ? 'selected' : '' ?>>video</option></select></div>
        <div class="md:col-span-2"><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>
    <div><label class="text-xs text-silver-500">Gallery (one per line, optional type prefix video|)</label><textarea name="gallery_paths" rows="4" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white"><?= e((string) ($p['gallery_paths'] ?? '')) ?></textarea></div>
    <div class="grid gap-4 md:grid-cols-2">
        <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>
        <div><label class="text-xs text-silver-500">Sort order</label><input name="sort_order" type="number" class="mt-1 w-full rounded border border-white/10 bg-charcoal px-3 py-2 text-white" value="<?= e((string) ($p['sort_order'] ?? '0')) ?>"></div>
    </div>
    <button type="submit" class="rounded-full bg-white px-6 py-2 text-xs font-semibold text-carbon">Save</button>
</form>
