<?php
/**
 * Formulaire galerie (Tabler).
 * @var array<string,mixed> $g
 * @var array<string,string> $errors
 * @var ?string $flash
 */

use App\Core\Csrf;

$isEdit = !empty($g['id']);
$action = $isEdit ? base_url('admin/galleries/' . $g['id']) : base_url('admin/galleries');
?>
<h2 class="h2 mb-3"><?= $isEdit ? 'Modifier la galerie' : 'Nouvelle galerie' ?></h2>

<?php if (!empty($errors)): ?><div class="alert alert-danger"><?php foreach ($errors as $er): ?><div><?= e($er) ?></div><?php endforeach; ?></div><?php endif; ?>

<form method="post" action="<?= e($action) ?>">
    <?= Csrf::field() ?>
    <div class="row row-cards">
        <div class="col-lg-8">
            <div class="card"><div class="card-body">
                <div class="mb-3">
                    <label class="form-label required">Titre</label>
                    <input type="text" name="title" class="form-control" value="<?= e($g['title'] ?? '') ?>" required>
                </div>
                <div class="mb-1">
                    <label class="form-label">Texte de présentation</label>
                    <textarea id="intro_html" name="intro_html" class="form-control" rows="8"><?= e($g['intro_html'] ?? '') ?></textarea>
                </div>
            </div></div>
        </div>
        <div class="col-lg-4">
            <div class="card mb-3"><div class="card-body">
                <div class="<?= $isEdit && !empty($g['images']) ? 'mb-3' : 'mb-0' ?>">
                    <label class="form-label">Statut</label>
                    <label class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" name="status_published" value="1" <?= ($g['status'] ?? 'draft') === 'published' ? 'checked' : '' ?>>
                        <span class="form-check-label">Publié</span>
                    </label>
                </div>
                <?php if ($isEdit && !empty($g['images'])): ?>
                    <div class="mb-0">
                        <label class="form-label">Image de couverture</label>
                        <select name="cover_image_id" class="form-select">
                            <option value="">— Aucune —</option>
                            <?php foreach ($g['images'] as $img): ?>
                                <option value="<?= (int) $img['id'] ?>" <?= (int) ($g['cover_image_id'] ?? 0) === (int) $img['id'] ? 'selected' : '' ?>><?= e($img['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                <?php endif; ?>
            </div></div>
            <div class="card"><div class="card-header"><h3 class="card-title">SEO</h3></div><div class="card-body">
                <div class="mb-3"><label class="form-label">Title SEO</label><input type="text" name="seo_title" class="form-control" value="<?= e($g['seo_title'] ?? '') ?>"></div>
                <div class="mb-3"><label class="form-label">Meta description</label><textarea name="seo_description" class="form-control" rows="2"><?= e($g['seo_description'] ?? '') ?></textarea></div>
                <label class="form-check"><input type="checkbox" class="form-check-input" name="seo_noindex" value="1" <?= !empty($g['seo_noindex']) ? 'checked' : '' ?>><span class="form-check-label">Ne pas indexer (noindex)</span></label>
            </div></div>
        </div>
    </div>
    <div class="d-flex gap-2 mt-3 justify-content-end">
        <a class="btn" href="<?= e(base_url('admin/galleries')) ?>">Annuler</a>
        <button type="submit" class="btn btn-primary"><i class="ti ti-device-floppy me-1"></i><?= $isEdit ? 'Enregistrer' : 'Créer' ?></button>
    </div>
</form>

<script src="https://cdn.jsdelivr.net/npm/tinymce@7.3.0/tinymce.min.js"></script>
<script>
if (window.tinymce) {
    tinymce.init({ selector: '#intro_html', menubar: false, plugins: 'lists link',
        toolbar: 'styles | bold italic superscript | bullist numlist | link | removeformat',
        style_formats: [{ title: 'Paragraphe', format: 'p' }, { title: 'Titre 1', block: 'h1' }, { title: 'Titre 2', block: 'h2' }, { title: 'Titre 3', block: 'h3' }],
        height: 300, branding: false, license_key: 'gpl' });
}
</script>
