
<?php if (!empty($showTypeFilter)):
    $typeLabels = Video::typeLabels();
    $typeTabUrl = function(string $type) use ($search, $currentSort, $currentVideoFilter) {
        $p = [];
        if ($type !== '') $p['type'] = $type;
        if (($search ?? '') !== '') $p['q'] = $search;
        if (($currentSort ?? 'name') !== 'name') $p['sort'] = $currentSort;
        if (($currentVideoFilter ?? '') !== '') $p['videos'] = $currentVideoFilter;
        return '/admin/channels' . (!empty($p) ? '?' . http_build_query($p) : '');
    };
?>
<div class="mb-4 border-b border-gray-200">
    <nav class="flex gap-0 -mb-px">
        <?php $isAll = ($currentType ?? '') === ''; ?>
        <a href="<?= $typeTabUrl('') ?>" class="px-4 py-2.5 text-xs font-medium border-b-2 <?= $isAll ? 'border-primary-500 text-primary-700' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' ?> transition-colors">
            All <span class="<?= $isAll ? 'text-primary-400' : 'text-gray-400' ?>"><?= array_sum($typeCounts) ?></span>
        </a>
        <?php foreach ($availableTypes as $t): ?>
        <?php $isActive = ($currentType ?? '') === $t; ?>
        <a href="<?= $typeTabUrl($t) ?>" class="px-4 py-2.5 text-xs font-medium border-b-2 <?= $isActive ? 'border-primary-500 text-primary-700' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300' ?> transition-colors whitespace-nowrap">
            <?= $typeLabels[$t] ?? ucfirst($t) ?> <span class="<?= $isActive ? 'text-primary-400' : 'text-gray-400' ?>"><?= $typeCounts[$t] ?? 0 ?></span>
        </a>
        <?php endforeach; ?>
    </nav>
</div>
<?php endif; ?>


<?php
$filterStyle = 'px-2 py-1.5 border border-gray-300 rounded-lg text-xs bg-white focus:ring-1 focus:ring-blue-500 focus:border-blue-500 outline-none appearance-none cursor-pointer';
$hasFilters = ($currentVideoFilter ?? '') !== '' || ($currentSort ?? 'name') !== 'name';
?>
<div class="mb-3 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
    <div class="flex items-center gap-3">
        <input type="text" data-search-table="channels-table" placeholder="Search channels..." class="w-full sm:w-64 px-3 py-1.5 border border-gray-300 rounded-lg text-xs focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none">
        <span id="channels-table-count" class="hidden sm:inline text-xs text-gray-500 whitespace-nowrap"></span>
    </div>
    <div class="flex items-center gap-2 flex-wrap">
        <select onchange="adminFilter('videos',this.value)" class="<?= $filterStyle ?>">
            <option value="">All channels</option>
            <option value="with" <?= ($currentVideoFilter ?? '') === 'with' ? 'selected' : '' ?>>With videos</option>
            <option value="empty" <?= ($currentVideoFilter ?? '') === 'empty' ? 'selected' : '' ?>>Empty (0 videos)</option>
        </select>
        <select onchange="adminFilter('sort',this.value)" class="<?= $filterStyle ?>">
            <option value="name" <?= ($currentSort ?? 'name') === 'name' ? 'selected' : '' ?>>Sort: Name</option>
            <option value="videos" <?= ($currentSort ?? 'name') === 'videos' ? 'selected' : '' ?>>Sort: Most videos</option>
            <option value="newest" <?= ($currentSort ?? 'name') === 'newest' ? 'selected' : '' ?>>Sort: Newest</option>
        </select>
        <?php if ($hasFilters): ?>
            <?php
            $clearParams = [];
            if (!empty($search)) $clearParams['q'] = $search;
            if (($currentType ?? '') !== '') $clearParams['type'] = $currentType;
            $clearUrl = '/admin/channels' . (!empty($clearParams) ? '?' . http_build_query($clearParams) : '');
            ?>
            <a href="<?= $clearUrl ?>" class="text-xs text-red-500 hover:text-red-700 whitespace-nowrap">Clear filters</a>
        <?php endif; ?>
    </div>
</div>


<div id="bulkBar" class="hidden mb-2 px-3 py-2 bg-primary-50 border border-primary-200 rounded-lg flex items-center gap-3 text-xs">
    <span class="font-medium text-primary-700"><span id="bulkCount">0</span> selected</span>
    <button type="button" onclick="AT.translateSelected()" class="inline-flex items-center gap-1 px-2.5 py-1 bg-sky-600 text-white text-xs font-medium rounded hover:bg-sky-700 transition-colors">
        <i class="fa-solid fa-language"></i>
        Translate
    </button>
    <button type="button" onclick="bulkDeleteSelected()" class="inline-flex items-center gap-1 px-2.5 py-1 bg-red-600 text-white text-xs font-medium rounded hover:bg-red-700 transition-colors">
        <svg class="w-3 h-3" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
        Delete
    </button>
    <button type="button" onclick="bulkClearAll()" class="text-xs text-gray-500 hover:text-gray-700">Clear</button>
</div>


<div class="bg-white rounded-lg shadow-sm border border-gray-200">
    <div class="overflow-x-auto">
        <table id="channels-table" class="w-full" data-bulk-delete="/admin/channels/bulk-delete" data-bulk-noun="channel">
            <thead>
                <tr class="bg-gray-50">
                    <th class="px-2 py-2 w-8"><input type="checkbox" data-bulk-select-all class="rounded border-gray-300 text-primary-600 focus:ring-primary-500"></th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Logo</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Name</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Videos</th>
                    <th class="px-2 py-2 text-right text-[11px] font-semibold uppercase tracking-wider text-gray-500">Actions</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-100">
                <?php if (!empty($channels)): ?>
                    <?php foreach ($channels as $channel): ?>
                        <tr class="hover:bg-gray-50 transition-colors" data-searchable="<?= htmlspecialchars($channel['name']) ?>">
                            <td class="px-2 py-1.5"><input type="checkbox" data-bulk-id="<?= (int)$channel['id'] ?>" class="rounded border-gray-300 text-primary-600 focus:ring-primary-500"></td>
                            <td class="px-2 py-1.5">
                                <?php
                                    $channelThumbUrl = '';
                                    if (!empty($channel['logo'])) {
                                        $channelThumbUrl = str_starts_with($channel['logo'], 'http') ? $channel['logo'] : '/uploads/channels/' . $channel['logo'];
                                    } else {
                                        $channelThumbUrl = VideoAsset::thumbUrl([
                                            'video_folder' => $channel['best_video_folder'] ?? '',
                                            'storage_id'   => $channel['best_video_storage_id'] ?? null,
                                            'thumbnail'    => $channel['best_video_thumbnail'] ?? '',
                                        ]);
                                    }
                                ?>
                                <?php if (!empty($channelThumbUrl)): ?>
                                    <img src="<?= htmlspecialchars($channelThumbUrl) ?>" alt="" class="w-8 h-8 rounded object-cover" data-zoom-src="<?= htmlspecialchars($channelThumbUrl) ?>">
                                <?php else: ?>
                                    <div class="w-8 h-8 rounded bg-gray-200 flex items-center justify-center text-[10px] text-gray-400">N/A</div>
                                <?php endif; ?>
                            </td>
                            <td class="px-2 py-1.5 text-xs">
                                <a href="/admin/channels/<?= (int)$channel['id'] ?>/edit" class="text-blue-600 hover:text-blue-800 font-medium"><?= htmlspecialchars($channel['name']) ?></a>
                            </td>
                            <td class="px-2 py-1.5 text-xs text-gray-600"><?= (int)($channel['video_count'] ?? 0) ?></td>
                            <td class="px-2 py-1.5 text-xs text-right">
                                <div class="flex items-center justify-end gap-1">
                                    <button type="button" onclick="AT.translateOne(<?= (int)$channel['id'] ?>)" class="px-2 py-1 text-[11px] font-medium rounded border bg-white text-sky-600 border-sky-200 hover:bg-sky-50" title="Translate this one with AI"><i class="fa-solid fa-language"></i></button>
                                    <a href="/admin/channels/<?= (int)$channel['id'] ?>/edit" class="px-2 py-1 text-[11px] font-medium rounded border bg-white text-gray-600 border-gray-300 hover:bg-gray-50">Edit</a>
                                    <form method="POST" action="/admin/channels/<?= (int)$channel['id'] ?>/delete" onsubmit="return confirm('Delete this channel?');">
                                        <?= Router::csrfField() ?>
                                        <button type="submit" class="px-2 py-1 text-[11px] font-medium rounded border bg-red-600 text-white border-red-600 hover:bg-red-700">Del</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="5" class="text-center py-8 text-gray-500 text-xs">No channels found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php require APP_ROOT . '/templates/admin/_admin_pagination.php'; ?>

<script>
function adminFilter(k,v){var u=new URL(location);var d={'sort':'name'};if(v&&v!==(d[k]||''))u.searchParams.set(k,v);else u.searchParams.delete(k);u.searchParams.delete('page');location=u;}
</script>

<?php $skipMpBanner = true; $isConnected = CatalogueClient::isConfigured(); if (!defined('AT_MP_MODAL')) { define('AT_MP_MODAL', 1); include __DIR__ . '/_mp_modal.php'; } ?>
<?php $aiTranslateEntityType = 'channel'; $aiTranslateTypeFilter = $currentType ?? ''; include __DIR__ . '/_ai_translate_modal.php'; ?>
