<?php
$translationData = $translationData ?? ['defaultLang' => 'en', 'enabledLangs' => ['en'], 'transMap' => []];
$tdDefaultLang = $translationData['defaultLang'];
$tdEnabledLangs = $translationData['enabledLangs'];
$tdTransMap = $translationData['transMap'];
?>

<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="pages-table" placeholder="Search pages..." 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="pages-table-count" class="hidden sm:inline text-xs text-gray-500 whitespace-nowrap"></span>
    </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="pages-table" class="w-full" data-bulk-delete="/admin/pages/bulk-delete" data-bulk-noun="page">
            <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">Title</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500 hide-mobile">Slug</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500 hide-mobile">Languages</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Status</th>
                    <th class="px-2 py-2 text-left text-[11px] font-semibold uppercase tracking-wider text-gray-500">Footer</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($pages)): ?>
                    <?php foreach ($pages as $pg): $pgId = (int)$pg['id']; ?>
                        <tr class="hover:bg-gray-50 transition-colors" data-searchable="<?= htmlspecialchars($pg['title'] . ' ' . $pg['slug']) ?>">
                            <td class="px-2 py-1.5"><input type="checkbox" data-bulk-id="<?= $pgId ?>" class="rounded border-gray-300 text-primary-600 focus:ring-primary-500"></td>
                            <td class="px-2 py-1.5 text-xs">
                                <a href="/admin/pages/<?= $pgId ?>/edit" class="text-blue-600 hover:text-blue-800 font-medium"><?= htmlspecialchars($pg['title']) ?></a>
                            </td>
                            <td class="px-2 py-1.5 text-xs hide-mobile">
                                <code class="text-[11px] bg-gray-100 text-gray-700 px-1.5 py-0.5 rounded"><?= htmlspecialchars($pg['slug']) ?></code>
                            </td>
                            <td class="px-2 py-1.5 text-xs hide-mobile">
                                <div class="flex items-center gap-1">
                                    <?php foreach ($tdEnabledLangs as $lc):
                                        $flag = Translator::flagCode($lc);
                                        if ($lc === $tdDefaultLang):
                                            $badgeClass = 'bg-blue-100 text-blue-700 border-blue-200';
                                            $title = strtoupper($lc) . ' (default)';
                                        elseif (isset($tdTransMap[$pgId][$lc])):
                                            $badgeClass = 'bg-green-100 text-green-700 border-green-200';
                                            $title = strtoupper($lc) . ' (translated)';
                                        else:
                                            $badgeClass = 'bg-gray-100 text-gray-400 border-gray-200';
                                            $title = strtoupper($lc) . ' (missing)';
                                        endif;
                                    ?>
                                        <span class="inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[10px] font-medium border <?= $badgeClass ?>" title="<?= $title ?>">
                                            <span class="fi fi-<?= $flag ?> fis" style="font-size:10px;"></span>
                                        </span>
                                    <?php endforeach; ?>
                                </div>
                            </td>
                            <td class="px-2 py-1.5 text-xs">
                                <?php if (($pg['status'] ?? '') === 'published'): ?>
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-medium bg-green-100 text-green-800">Published</span>
                                <?php else: ?>
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-medium bg-gray-100 text-gray-600">Draft</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-2 py-1.5 text-xs">
                                <?php if (!empty($pg['show_in_footer'])): ?>
                                    <span class="inline-flex items-center px-1.5 py-0.5 rounded-full text-[10px] font-medium bg-blue-100 text-blue-800">Yes</span>
                                <?php else: ?>
                                    <span class="text-gray-400">No</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-2 py-1.5 text-xs text-right">
                                <div class="flex items-center justify-end gap-1">
                                    <?php if (($pg['status'] ?? '') === 'published'): ?>
                                    <a href="/<?= htmlspecialchars($pg['slug']) ?>" target="_blank" class="px-2 py-1 text-[11px] font-medium rounded border bg-white text-gray-600 border-gray-300 hover:bg-gray-50" title="View page">View</a>
                                    <?php endif; ?>
                                    <button type="button" onclick="AT.translateOne(<?= $pgId ?>)" 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/pages/<?= $pgId ?>/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/pages/<?= $pgId ?>/delete" onsubmit="return confirm('Delete this page?');">
                                        <?= 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="7" class="text-center py-8 text-gray-500 text-xs">No pages found.</td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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

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