<?php ob_start(); ?>

<?php admin_content_header(__('admin.gallery.title'), 'image', [
    ['url' => '/admin/gallery/create', 'label' => __('admin.gallery.new'), 'class' => 'btn-primary'],
]); ?>

<?php
$bulkUrl = '/admin/gallery/bulk';
$bulkActions = [
    ['action' => 'delete', 'label' => __('admin.common.bulk_delete'), 'class' => 'btn-bulk-danger', 'confirm' => __('admin.common.bulk_confirm_delete')],
];
include dirname(__DIR__) . '/layouts/_bulk_bar.php';
?>

<div class="table-responsive">
<table class="data-table" data-bulk-table>
    <thead>
        <tr>
            <th class="bulk-check-cell"><input type="checkbox" data-bulk-select-all aria-label="Select all"></th>
            <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)): ?>
        <?php admin_empty_state(__('admin.gallery.no_albums'), 'image', '/admin/gallery/create', __('admin.gallery.create_first'), true, 8); ?>
        <?php else: ?>
        <?php foreach ($albums as $album): ?>
        <tr>
            <td class="bulk-check-cell">
                <input type="checkbox" data-bulk-item value="<?= (int) $album['id'] ?>" aria-label="Select <?= e($album['title']) ?>">
            </td>
            <td class="table-thumb-cell">
                <?php if (!empty($album['cover_image'])): ?>
                <img src="<?= e(thumb_url($album['cover_image'])) ?>"
                     alt=""
                     class="table-thumb">
                <?php else: ?>
                <div class="table-thumb-placeholder"><?= icon('image', 18) ?></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'; ?>
