<main id="main-content">
    <h1>Mots-clés (<?= count($tags) ?>)</h1>

    <table>
        <thead>
            <tr>
                <th scope="col">Nom</th>
                <th scope="col">TFE associés</th>
                <th scope="col">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($tags as $tag): ?>
            <tr>
                <td><?= htmlspecialchars($tag['name']) ?></td>
                <td class="admin-tags-count"><?= (int)$tag['thesis_count'] ?></td>
                <td>
                    <!-- Rename -->
                    <form method="post" action="actions/tag.php" class="admin-inline-form">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                        <input type="hidden" name="action" value="rename">
                        <input type="hidden" name="tag_id" value="<?= (int)$tag['id'] ?>">
                        <input class="admin-input--inline" type="text" name="new_name"
                               value="<?= htmlspecialchars($tag['name']) ?>" required>
                        <button type="submit" class="admin-btn admin-btn--sm">Renommer</button>
                    </form>

                    <!-- Merge into another tag -->
                    <form method="post" action="actions/tag.php" class="admin-inline-form">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                        <input type="hidden" name="action" value="merge">
                        <input type="hidden" name="source_id" value="<?= (int)$tag['id'] ?>">
                        <select name="target_id" class="admin-select--inline" required>
                            <option value="">— Fusionner dans… —</option>
                            <?php foreach ($tags as $other): ?>
                                <?php if ($other['id'] !== $tag['id']): ?>
                                <option value="<?= (int)$other['id'] ?>"><?= htmlspecialchars($other['name']) ?></option>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </select>
                        <button type="submit" class="admin-btn admin-btn--sm admin-btn--warning"
                                onclick="return confirm('Fusionner ce tag dans la cible ? Le tag source sera supprimé.')">
                            Fusionner
                        </button>
                    </form>

                    <!-- Delete -->
                    <form method="post" action="actions/tag.php" class="admin-inline-form">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                        <input type="hidden" name="action" value="delete">
                        <input type="hidden" name="tag_id" value="<?= (int)$tag['id'] ?>">
                        <button type="submit" class="admin-btn admin-btn--sm admin-btn--danger"
                                onclick="return confirm('Supprimer le tag « <?= htmlspecialchars(addslashes($tag['name'])) ?> » ? Cette action est irréversible.')">
                            Supprimer
                        </button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</main>
