<?php ob_start(); ?>

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

<!-- 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>

<div class="table-responsive">
<table class="data-table">
    <thead>
        <tr>
            <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)): ?>
        <tr><td colspan="5" class="text-center text-muted"><?= __('admin.downloads.no_downloads') ?> <a href="/admin/downloads/create"><?= __('admin.downloads.create_first') ?></a></td></tr>
        <?php else: ?>
        <?php foreach ($downloads as $dl): ?>
        <tr>
            <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 style="color:var(--gray-400);">—</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'; ?>
