<?php
/**
 * tags-fragment.php
 *
 * HTMX fragment: returns the tags table, optionally filtered by search query.
 * Uses lazy-load pattern: the wrapping div already shows a spinner via hx-indicator.
 */
require_once __DIR__ . '/../../bootstrap.php';
require_once __DIR__ . '/../../src/AdminAuth.php';
AdminAuth::requireLogin();

if (empty($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

require_once __DIR__ . '/../../src/Database.php';

$searchQuery = trim($_GET['q'] ?? '');

try {
    $db   = new Database();
    $tags = ($searchQuery !== '') ? $db->searchTags($searchQuery) : $db->getAllTagsWithCount();
    $totalCount = count($tags);
} catch (Exception $e) {
    die('<div class="flash-error">Erreur : ' . htmlspecialchars($e->getMessage()) . '</div>');
}
?>
<span id="tags-total-count" hx-swap-oob="true"><?= $searchQuery !== '' ? htmlspecialchars($totalCount) . ' résultat(s)' : htmlspecialchars($totalCount) . ' mot(s)-clé(s)' ?></span>
<div id="tags-bulk-actions" class="admin-bulk-actions" style="display:none">
    <strong><span id="tags-selected-count">0</span> mot(s)-clé(s) sélectionné(s)</strong>
    <div class="admin-bulk-btns">
        <button type="button" class="btn btn--sm btn--warning admin-btn-merge"
                onclick="tagsConfirmBulkMerge()"
                title="Fusionner les mots-clés sélectionnés">
            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M224,152V96a8,8,0,0,0-8-8H168V40a8,8,0,0,0-8-8H40a8,8,0,0,0-8,8v64h0v56a8,8,0,0,0,8,8H88v48a8,8,0,0,0,8,8H216a8,8,0,0,0,8-8V152Zm-68.69,56L48,100.69V59.31L196.69,208Zm-96-160h41.38L208,155.31v41.38ZM208,132.69,179.31,104H208Zm-56-56L123.31,48H152ZM48,123.31,76.69,152H48Zm56,56L132.69,208H104Z"></path></svg>
            Fusionner
        </button>
    </div>
</div>

<form id="tags-bulk-form" method="post" action="actions/tag.php">
    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
    <input type="hidden" name="action" value="merge_bulk">
    <input type="hidden" name="target_id" id="tags-bulk-target" value="">
    <div id="tags-bulk-checkboxes"></div>
</form>

<table>
    <thead>
        <tr>
            <th scope="col" style="width:1%"><input type="checkbox" onchange="tagsToggleAll(this)"></th>
            <th scope="col">Nom</th>
            <th scope="col" style="width:1%;white-space:nowrap">TFE associés</th>
            <th scope="col" style="width:1%">Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($tags)): ?>
        <tr><td colspan="4" class="admin-empty">Aucun mot-clé trouvé.</td></tr>
        <?php else: ?>
        <?php foreach ($tags as $tag): ?>
        <tr>
            <td style="width:1%"><input type="checkbox" name="selected_tags[]" value="<?= (int)$tag['id'] ?>" onchange="tagsUpdateBulk()"></td>
            <td id="tag-name-<?= (int)$tag['id'] ?>" data-name="<?= htmlspecialchars($tag['name']) ?>">
                <span class="tag-name-cell"><?= htmlspecialchars($tag['name']) ?></span>
                <button type="button" class="admin-icon-btn admin-icon-btn--edit" title="Renommer"
                        onclick="tagsStartRename(<?= (int)$tag['id'] ?>)">
                    <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M248,92.68a15.86,15.86,0,0,0-4.69-11.31L174.63,12.68a16,16,0,0,0-22.63,0L123.57,41.11l-58,21.77A16.06,16.06,0,0,0,55.35,75.23L32.11,214.68A8,8,0,0,0,40,224a8.4,8.4,0,0,0,1.32-.11l139.44-23.24a16,16,0,0,0,12.35-10.17l21.77-58L243.31,104A15.87,15.87,0,0,0,248,92.68Zm-69.87,92.19L63.32,204l47.37-47.37a28,28,0,1,0-11.32-11.32L52,192.7,71.13,77.86,126,57.29,198.7,130ZM112,132a12,12,0,1,1,12,12A12,12,0,0,1,112,132Zm96-15.32L139.31,48l24-24L232,92.68Z"></path></svg>
                </button>
            </td>
            <td class="admin-tags-count" style="width:1%;white-space:nowrap"><?= (int)$tag['thesis_count'] ?></td>
            <td class="admin-actions-col" style="width:1%">
                <div class="admin-actions">
                    <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="button" class="admin-icon-btn admin-icon-btn--delete" title="Supprimer"
                                onclick="tagsConfirmDelete(this, <?= htmlspecialchars(json_encode($tag['name']), ENT_QUOTES) ?>)">
                            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M216,48H176V40a24,24,0,0,0-24-24H104A24,24,0,0,0,80,40v8H40a8,8,0,0,0,0,16h8V208a16,16,0,0,0,16,16H192a16,16,0,0,0,16-16V64h8a8,8,0,0,0,0-16ZM96,40a8,8,0,0,1,8-8h48a8,8,0,0,1,8,8v8H96Zm96,168H64V64H192ZM112,104v64a8,8,0,0,1-16,0V104a8,8,0,0,1,16,0Zm48,0v64a8,8,0,0,1-16,0V104a8,8,0,0,1,16,0Z"></path></svg>
                        </button>
                    </form>
                </div>
            </td>
        </tr>
        <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
