<?php
/**
 * Liste des galeries (Tabler).
 * @var list<array<string,mixed>> $galleries
 * @var ?string $flash
 */
?>
<div class="d-flex justify-content-between align-items-center mb-3">
    <h2 class="h2 m-0">Galeries</h2>
    <a href="<?= e(base_url('admin/galleries/new')) ?>" class="btn btn-primary"><i class="ti ti-plus me-1"></i>Nouvelle galerie</a>
</div>


<?php if (empty($galleries)): ?>
    <div class="card"><div class="card-body text-secondary">Aucune galerie pour le moment. Créez-en une.</div></div>
<?php else: ?>
    <div class="card bg-transparent border-0 shadow-none">
        <div class="table-responsive">
            <table class="table table-vcenter rows-spaced" data-sortable="galleries" data-reorder-url="<?= e(base_url('admin/galleries/reorder')) ?>">
                <thead>
                    <tr>
                        <th style="width: 2.5rem; padding: 0;"></th>
                        <th>Titre</th>
                        <th class="text-center">Images</th>
                        <th class="text-center">Publié</th>
                        <th class="w-1"></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($galleries as $g): ?>
                    <tr data-id="<?= (int) $g['id'] ?>">
                        <td class="sortable-handle align-middle text-muted" title="Glisser pour réordonner" style="width: 2.5rem; padding: 0; cursor: grab; background: #f1f5f9 radial-gradient(circle, #cbd5e1 1.2px, transparent 1.5px) center / 8px 8px !important; border-right: 1px solid #e9edf2 !important;"></td>
                        <td><a href="<?= e(base_url('admin/galleries/' . $g['id'] . '/edit')) ?>" class="text-reset fw-bold"><?= e($g['title']) ?></a></td>
                        <td class="text-center"><span class="badge bg-blue-lt"><?= (int) $g['image_count'] ?></span></td>
                        <td class="text-center">
                            <label class="form-check form-switch m-0 d-flex justify-content-center">
                                <input class="form-check-input toggle-status" type="checkbox"
                                       data-url="<?= e(base_url('admin/galleries/' . $g['id'] . '/status')) ?>"
                                       <?= $g['status'] === 'published' ? 'checked' : '' ?>>
                            </label>
                        </td>
                        <td class="text-end">
                            <div class="d-flex gap-2 justify-content-end align-items-center">
                                <a href="<?= e(base_url('admin/galleries/' . $g['id'] . '/images')) ?>" class="btn btn-sm btn-outline-primary"><i class="ti ti-photo me-1"></i>Gérer les images</a>
                                <a href="<?= e(base_url('admin/galleries/' . $g['id'] . '/edit')) ?>" class="btn btn-sm btn-outline-secondary"><i class="ti ti-edit me-1"></i>Modifier</a>
                                <a href="#" class="btn btn-sm btn-icon btn-ghost-danger" title="Supprimer"
                                   data-bs-toggle="modal" data-bs-target="#deleteModal"
                                   data-delete-url="<?= e(base_url('admin/galleries/' . $g['id'] . '/delete')) ?>"
                                   data-delete-label="<?= e($g['title']) ?>"><i class="ti ti-trash"></i></a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
    <p class="text-secondary small mt-2"><i class="ti ti-info-circle me-1"></i>Glissez les lignes par la poignée à gauche pour réordonner (enregistré automatiquement).</p>
<?php endif; ?>
