<?php
$sortParams = array_filter([
    'search'      => $searchQuery,
    'year'        => $yearFilter ?: '',
    'orientation' => $orientationFilter ?: '',
    'ap'          => $apFilter ?: '',
]);

$sortLink = function(string $col) use ($sortCol, $sortDir, $sortParams): string {
    $params = $sortParams;
    $params['sort'] = $col;
    $params['dir']  = ($sortCol === $col && $sortDir === 'desc') ? 'asc' : 'desc';
    return '/admin/?' . http_build_query($params);
};

$sortArrow = function(string $col) use ($sortCol, $sortDir): string {
    if ($sortCol !== $col) return '';
    return $sortDir === 'asc' ? ' ↑' : ' ↓';
};
?>

<div id="admin-table-wrap">

<!-- Hidden state for HTMX to preserve sort across filter changes -->
<input type="hidden" name="sort" value="<?= htmlspecialchars($sortCol) ?>">
<input type="hidden" name="dir"  value="<?= htmlspecialchars($sortDir) ?>">

<!-- Meta bar: shows either nothing (default) or bulk actions on selection -->
<div id="bulk-actions" class="admin-bulk-actions" style="display:none">
    <strong><span id="selected-count">0</span> TFE(s) sélectionné(s)</strong>
    <div class="admin-bulk-btns">
        <button type="button" class="btn btn--sm btn--green admin-btn-publish" onclick="confirmBulk('publish')">Publier</button>
        <button type="button" class="btn btn--sm btn--muted admin-btn-unpublish" onclick="confirmBulk('unpublish')">Dépublier</button>
        <button type="button" class="btn btn--sm btn--red admin-btn-delete" onclick="confirmBulk('delete')">Supprimer</button>
    </div>
</div>

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

<table>
    <thead>
        <tr>
            <th scope="col"><input type="checkbox" onchange="toggleAll(this)"></th>
            <th scope="col"><a href="<?= $sortLink('identifier') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('identifier')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">ID<?= $sortArrow('identifier') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('title') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('title')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Titre<?= $sortArrow('title') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('authors') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('authors')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Auteur(s)<?= $sortArrow('authors') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('year') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('year')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Année<?= $sortArrow('year') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('orientation') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('orientation')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Orientation<?= $sortArrow('orientation') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('ap_program') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('ap_program')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">AP<?= $sortArrow('ap_program') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('is_published') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('is_published')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Publié<?= $sortArrow('is_published') ?></a></th>
            <th scope="col"><a href="<?= $sortLink('access_type') ?>" class="admin-sort-link" hx-get="<?= htmlspecialchars($sortLink('access_type')) ?>" hx-target="#admin-table-wrap" hx-swap="innerHTML" hx-indicator="#admin-search-indicator" hx-push-url="true">Accès<?= $sortArrow('access_type') ?></a></th>
            <th scope="col">Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php if (empty($theses)): ?>
        <tr>
            <td colspan="10" class="admin-empty">Aucun TFE trouvé.</td>
        </tr>
        <?php else: ?>
        <?php foreach ($theses as $thesis): ?>
        <tr class="admin-table-row" onclick="event.stopPropagation(); window.location='/admin/recapitulatif.php?id=<?= $thesis['id'] ?>'" style="cursor:pointer">
            <td onclick="event.stopPropagation()"><input type="checkbox" name="selected_theses[]" value="<?= $thesis['id'] ?>"></td>
            <td class="admin-table-id"><?= htmlspecialchars($thesis['identifier'] ?? $thesis['id']) ?></td>
            <td>
                <div class="thesis-title" title="<?= htmlspecialchars($thesis['title']) ?>"><?= htmlspecialchars($thesis['title']) ?></div>
            </td>
            <td><?= htmlspecialchars($thesis['authors'] ?? 'N/A') ?></td>
            <td><?= $thesis['year'] ?></td>
            <td class="admin-na"><?= htmlspecialchars($thesis['orientation'] ?? 'N/A') ?></td>
            <td class="admin-na admin-ap-col"><?= htmlspecialchars($thesis['ap_program'] ?? 'N/A') ?></td>
            <td>
                <?php $badgeType = 'publish'; $badgeValue = $thesis['is_published']; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
            </td>
            <td>
                <?php if (!empty($thesis['access_type'])): ?>
                    <?php $badgeType = 'access'; $badgeValue = $thesis['access_type']; include APP_ROOT . '/templates/partials/status-badge.php'; ?>
                <?php endif; ?>
            </td>
            <td class="admin-actions-col">
                <div class="admin-actions">
                    <a href="/admin/edit.php?id=<?= $thesis['id'] ?>" class="admin-icon-btn admin-icon-btn--edit" title="Éditer" onclick="event.stopPropagation()">
                        <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>
                    </a>
                    <form method="post" action="actions/publish.php" class="publish-form" onclick="event.stopPropagation()">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                        <input type="hidden" name="thesis_id" value="<?= $thesis['id'] ?>">
                        <?php if ($thesis['is_published']): ?>
                            <input type="hidden" name="action" value="unpublish">
                            <button type="submit" class="admin-icon-btn admin-icon-btn--unpublish" title="Dépublier">
                                <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M53.92,34.62A8,8,0,1,0,42.08,45.38L61.32,66.55C25,88.84,9.38,123.2,8.69,124.76a8,8,0,0,0,0,6.5c.35.79,8.82,19.57,27.65,38.4C61.43,194.74,93.12,208,128,208a127.11,127.11,0,0,0,52.07-10.83l22,24.21a8,8,0,1,0,11.84-10.76Zm47.33,75.84,41.67,45.85a32,32,0,0,1-41.67-45.85ZM128,192c-30.78,0-57.67-11.19-79.93-33.25A133.16,133.16,0,0,1,25,128c4.69-8.79,19.66-33.39,47.35-49.38l18,19.75a48,48,0,0,0,63.66,70l14.73,16.2A112,112,0,0,1,128,192Zm6-95.43a8,8,0,0,1,3-15.72,48.16,48.16,0,0,1,38.77,42.64,8,8,0,0,1-7.22,8.71,6.39,6.39,0,0,1-.75,0,8,8,0,0,1-8-7.26A32.09,32.09,0,0,0,134,96.57Zm113.28,34.69c-.42.94-10.55,23.37-33.36,43.8a8,8,0,1,1-10.67-11.92A132.77,132.77,0,0,0,231.05,128a133.15,133.15,0,0,0-23.12-30.77C185.67,75.19,158.78,64,128,64a118.37,118.37,0,0,0-19.36,1.57A8,8,0,1,1,106,49.79,134,134,0,0,1,128,48c34.88,0,66.57,13.26,91.66,38.35,18.83,18.83,27.3,37.62,27.65,38.41A8,8,0,0,1,247.31,131.26Z"></path></svg>
                            </button>
                        <?php else: ?>
                            <input type="hidden" name="action" value="publish">
                            <button type="submit" class="admin-icon-btn admin-icon-btn--publish" title="Publier">
                                <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M247.31,124.76c-.35-.79-8.82-19.58-27.65-38.41C194.57,61.26,162.88,48,128,48S61.43,61.26,36.34,86.35C17.51,105.18,9,124,8.69,124.76a8,8,0,0,0,0,6.5c.35.79,8.82,19.57,27.65,38.4C61.43,194.74,93.12,208,128,208s66.57-13.26,91.66-38.34c18.83-18.83,27.3-37.61,27.65-38.4A8,8,0,0,0,247.31,124.76ZM128,192c-30.78,0-57.67-11.19-79.93-33.25A133.47,133.47,0,0,1,25,128,133.33,133.33,0,0,1,48.07,97.25C70.33,75.19,97.22,64,128,64s57.67,11.19,79.93,33.25A133.46,133.46,0,0,1,231.05,128C223.84,141.46,192.43,192,128,192Zm0-112a48,48,0,1,0,48,48A48.05,48.05,0,0,0,128,80Zm0,80a32,32,0,1,1,32-32A32,32,0,0,1,128,160Z"></path></svg>
                            </button>
                        <?php endif; ?>
                    </form>
                    <form method="post" action="actions/delete.php" id="delete-form-<?= $thesis['id'] ?>" class="publish-form" onclick="event.stopPropagation()">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                        <input type="hidden" name="thesis_id" value="<?= $thesis['id'] ?>">
                        <button type="button" class="admin-icon-btn admin-icon-btn--delete" title="Supprimer"
                                onclick="event.stopPropagation(); confirmDelete(<?= $thesis['id'] ?>, <?= htmlspecialchars(json_encode($thesis['title']), 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>

<script>
document.querySelectorAll('input[name="selected_theses[]"]').forEach(cb => cb.addEventListener('change', updateBulk));
</script>
</div>
