<?php ob_start(); ?>

<div class="content-header">
    <h2><?= __('admin.gallery.title') ?></h2>
    <div class="header-actions">
        <a href="/admin/gallery/create" class="btn btn-primary"><?= __('admin.gallery.new') ?></a>
    </div>
</div>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <th><?= __('admin.gallery.cover') ?></th>
            <th><?= __('admin.common.title') ?></th>
            <th><?= __('admin.common.slug') ?></th>
            <th><?= __('admin.gallery.visibility') ?></th>
            <th><?= __('admin.pages.order') ?></th>
            <th><?= __('admin.gallery.images') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($albums)): ?>
        <tr>
            <td colspan="7" class="text-center text-muted">
                <?= __('admin.gallery.no_albums') ?> <a href="/admin/gallery/create"><?= __('admin.gallery.create_first') ?></a>
            </td>
        </tr>
        <?php else: ?>
        <?php foreach ($albums as $album): ?>
        <tr>
            <td style="width:60px;">
                <?php if (!empty($album['cover_image'])): ?>
                <img src="<?= e(thumb_url($album['cover_image'])) ?>"
                     alt=""
                     style="width:50px;height:50px;object-fit:cover;border-radius:4px;display:block;">
                <?php else: ?>
                <div style="width:50px;height:50px;background:#eee;border-radius:4px;display:flex;align-items:center;justify-content:center;font-size:1.2rem;color:#aaa;">▦</div>
                <?php endif; ?>
            </td>
            <td><strong><?= e($album['title']) ?></strong></td>
            <td class="text-muted"><?= e($album['slug']) ?></td>
            <td>
                <span class="badge <?= $album['is_visible'] ? 'badge-success' : 'badge-draft' ?>">
                    <?= $album['is_visible'] ? __('admin.common.visible') : __('admin.common.hidden') ?>
                </span>
            </td>
            <td><?= (int) $album['sort_order'] ?></td>
            <td><?= (int) $album['image_count'] ?></td>
            <td class="actions">
                <a href="/admin/gallery/album/<?= (int) $album['id'] ?>/images"
                   class="btn btn-sm btn-primary" title="Manage images"><?= __('admin.gallery.images') ?></a>
                <a href="/admin/gallery/edit/<?= (int) $album['id'] ?>"
                   class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <?php if ($album['is_visible']): ?>
                <a href="/gallery/<?= e($album['slug']) ?>"
                   class="btn btn-sm btn-secondary" target="_blank" title="View on site"><?= __('admin.common.view') ?></a>
                <?php endif; ?>
                <form method="POST" action="/admin/gallery/delete/<?= (int) $album['id'] ?>"
                      class="inline-form"
                      onsubmit="return confirm('<?= e(__('admin.gallery.confirm_delete', [':title' => $album['title']])) ?>')">
                    <?= $csrf->field() ?>
                    <button type="submit" class="btn btn-sm btn-danger"><?= __('admin.common.delete') ?></button>
                </form>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
</div>

<?php $content = ob_get_clean(); include dirname(__DIR__) . '/layouts/admin.php'; ?>
