<?php ob_start(); ?>

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

<!-- Filter bar -->
<form method="GET" action="/admin/downloads" class="filter-bar">
    <input type="text" name="search" value="<?= e($search ?? '') ?>" placeholder="<?= __('admin.downloads.search_placeholder') ?>">

    <select name="status">
        <option value=""><?= __('admin.downloads.all_statuses') ?></option>
        <option value="draft"     <?= ($status ?? '') === 'draft'     ? 'selected' : '' ?>><?= __('admin.common.draft') ?></option>
        <option value="published" <?= ($status ?? '') === 'published' ? 'selected' : '' ?>><?= __('admin.common.published') ?></option>
    </select>

    <select name="category_id">
        <option value=""><?= __('admin.downloads.all_categories') ?></option>
        <?php foreach ($categories as $cat): ?>
        <option value="<?= (int) $cat['id'] ?>" <?= (int) ($categoryId ?? 0) === (int) $cat['id'] ? 'selected' : '' ?>>
            <?= e($cat['name']) ?>
        </option>
        <?php endforeach; ?>
    </select>

    <button type="submit" class="btn btn-secondary btn-sm"><?= __('admin.common.filter') ?></button>
    <?php if (($search ?? '') !== '' || ($status ?? '') !== '' || ($categoryId ?? 0) > 0): ?>
    <a href="/admin/downloads" class="btn btn-secondary btn-sm"><?= __('admin.common.clear') ?></a>
    <?php endif; ?>
</form>

<?php
$bulkUrl = '/admin/downloads/bulk';
$bulkActions = [
    ['action' => 'publish', 'label' => __('admin.common.bulk_publish'), 'confirm' => __('admin.common.bulk_confirm_status')],
    ['action' => 'draft',   'label' => __('admin.common.bulk_draft'),   'confirm' => __('admin.common.bulk_confirm_status')],
    ['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.common.title') ?></th>
            <th><?= __('admin.download_categories.title') ?></th>
            <th><?= __('admin.downloads.file') ?></th>
            <th><?= __('admin.common.status') ?></th>
            <th><?= __('admin.common.actions') ?></th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($downloads)): ?>
        <?php admin_empty_state(__('admin.downloads.no_downloads'), 'download', '/admin/downloads/create', __('admin.downloads.create_first'), true, 6); ?>
        <?php else: ?>
        <?php foreach ($downloads as $dl): ?>
        <tr>
            <td class="bulk-check-cell">
                <input type="checkbox" data-bulk-item value="<?= (int) $dl['id'] ?>" aria-label="Select <?= e($dl['title']) ?>">
            </td>
            <td>
                <strong><?= e($dl['title']) ?></strong>
                <?php if (!empty($dl['version'])): ?>
                <span class="badge badge-success">v<?= e($dl['version']) ?></span>
                <?php endif; ?>
                <?php if (!empty($dl['excerpt'])): ?>
                <br><small class="text-muted"><?= e(mb_substr($dl['excerpt'], 0, 80)) ?><?= mb_strlen($dl['excerpt']) > 80 ? '…' : '' ?></small>
                <?php endif; ?>
            </td>
            <td><?= e($dl['category_name'] ?? '—') ?></td>
            <td>
                <?php if (!empty($dl['file_path'])): ?>
                <?php
                    $ext = strtoupper(pathinfo($dl['file_path'], PATHINFO_EXTENSION));
                    $displayName = !empty($dl['file_name']) ? $dl['file_name'] : basename($dl['file_path']);
                ?>
                <span class="badge badge-info"><?= e($ext) ?></span>
                <small><?= e(mb_substr($displayName, 0, 30)) ?><?= mb_strlen($displayName) > 30 ? '…' : '' ?></small>
                <?php else: ?>
                <span class="text-muted">—</span>
                <?php endif; ?>
            </td>
            <td>
                <span class="badge <?= $dl['status'] === 'published' ? 'badge-success' : 'badge-draft' ?>">
                    <?= e($dl['status']) ?>
                </span>
            </td>
            <td class="actions">
                <?php if ($dl['status'] === 'published'): ?>
                <a href="/download/<?= e($dl['slug']) ?>" class="btn btn-sm btn-secondary" target="_blank" title="<?= __('admin.common.view') ?>"><?= __('admin.common.view') ?></a>
                <?php endif; ?>
                <a href="/admin/downloads/edit/<?= (int) $dl['id'] ?>" class="btn btn-sm btn-secondary"><?= __('admin.common.edit') ?></a>
                <form method="POST" action="/admin/downloads/delete/<?= (int) $dl['id'] ?>" class="inline-form"
                      onsubmit="return confirm('<?= __('admin.downloads.confirm_delete', [':title' => e($dl['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'; ?>
