
<?php if (!empty($showTypeFilter)):
    $typeLabels = Video::typeLabels();
    $typeTabUrl = function(string $type) use ($search, $currentSort, $currentVideoFilter, $currentPhoto, $currentCountry) {
        $p = [];
        if ($type !== '') $p['type'] = $type;
        if (($search ?? '') !== '') $p['q'] = $search;
        if (($currentSort ?? 'name') !== 'name') $p['sort'] = $currentSort;
        if (($currentVideoFilter ?? '') !== '') $p['videos'] = $currentVideoFilter;
        if (($currentPhoto ?? '') !== '') $p['photo'] = $currentPhoto;
        if (($currentCountry ?? '') !== '') $p['country'] = $currentCountry;
        return '/admin/performers' . (!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 ?? '') !== '' || ($currentPhoto ?? '') !== '' || ($currentCountry ?? '') !== '' || ($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="performers-table" placeholder="Search performers..." 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="performers-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 pornstars</option>
            <option value="with" <?= ($currentVideoFilter ?? '') === 'with' ? 'selected' : '' ?>>With videos</option>
            <option value="empty" <?= ($currentVideoFilter ?? '') === 'empty' ? 'selected' : '' ?>>Empty (0 videos)</option>
        </select>
        <select onchange="adminFilter('photo',this.value)" class="<?= $filterStyle ?>">
            <option value="">All photos</option>
            <option value="yes" <?= ($currentPhoto ?? '') === 'yes' ? 'selected' : '' ?>>With photo</option>
            <option value="no" <?= ($currentPhoto ?? '') === 'no' ? 'selected' : '' ?>>No photo</option>
        </select>
        <?php if (!empty($filterCountries)): ?>
        <select onchange="adminFilter('country',this.value)" class="<?= $filterStyle ?>">
            <option value="">All countries</option>
            <?php foreach ($filterCountries as $fc): ?>
                <option value="<?= htmlspecialchars($fc['country']) ?>" <?= ($currentCountry ?? '') === $fc['country'] ? 'selected' : '' ?>><?= htmlspecialchars($fc['country']) ?></option>
            <?php endforeach; ?>
        </select>
        <?php endif; ?>
        <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/performers' . (!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="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="performers-table" class="w-full" data-bulk-delete="/admin/performers/bulk-delete" data-bulk-noun="performer">
            <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">Photo</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Name</th>
                    <th class="hide-mobile px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Country</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($performers)): ?>
                    <?php foreach ($performers as $performer): ?>
                        <tr class="hover:bg-gray-50 transition-colors" data-searchable="<?= htmlspecialchars($performer['name'] . ' ' . ($performer['country'] ?? '')) ?>">
                            <td class="px-2 py-1.5"><input type="checkbox" data-bulk-id="<?= (int)$performer['id'] ?>" class="rounded border-gray-300 text-primary-600 focus:ring-primary-500"></td>
                            <td class="px-2 py-1.5">
                                <?php
                                    $perfThumbUrl = '';
                                    if (!empty($performer['photo'])) {
                                        $perfThumbUrl = str_starts_with($performer['photo'], 'http') ? $performer['photo'] : '/uploads/performers/' . $performer['photo'];
                                    } else {
                                        $perfThumbUrl = VideoAsset::thumbUrl([
                                            'video_folder' => $performer['best_video_folder'] ?? '',
                                            'storage_id'   => $performer['best_video_storage_id'] ?? null,
                                            'thumbnail'    => $performer['best_video_thumbnail'] ?? '',
                                        ]);
                                    }
                                ?>
                                <?php if (!empty($perfThumbUrl)): ?>
                                    <img src="<?= htmlspecialchars($perfThumbUrl) ?>" alt="" class="w-8 h-8 rounded-full object-cover" data-zoom-src="<?= htmlspecialchars($perfThumbUrl) ?>">
                                <?php else: ?>
                                    <div class="w-8 h-8 rounded-full 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/performers/<?= (int)$performer['id'] ?>/edit" class="text-blue-600 hover:text-blue-800 font-medium"><?= htmlspecialchars($performer['name']) ?></a>
                            </td>
                            <td class="hide-mobile px-2 py-1.5 text-xs text-gray-600"><?= htmlspecialchars($performer['country'] ?? '-') ?></td>
                            <td class="px-2 py-1.5 text-xs text-gray-600"><?= (int)($performer['video_count'] ?? 0) ?></td>
                            <td class="px-2 py-1.5 text-xs text-right">
                                <div class="flex items-center justify-end gap-1">
                                    <a href="/admin/performers/<?= (int)$performer['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/performers/<?= (int)$performer['id'] ?>/delete" onsubmit="return confirm('Delete this pornstar?');">
                                        <?= 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="6" class="text-center py-8 text-gray-500 text-xs">No pornstars 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>
