<main id="main-content">

    <div class="admin-list-toolbar">
        <h1>Accès étudiant·e</h1>
        <div class="admin-list-toolbar__right">
            <button type="button" class="btn btn--primary btn--sm" id="open-create-dialog">
                ＋ Créer un lien
            </button>
        </div>
    </div>

    <?php if (empty($links)): ?>
        <p class="admin-empty">Aucun lien d'accès créé. Cliquez sur « Créer un lien » pour générer un lien partageable.</p>
    <?php else: ?>
    <table>
        <thead>
            <tr>
                <th scope="col">Lien</th>
                <th scope="col">Objet</th>
                <th scope="col">Statut</th>
                <th scope="col">Mot de passe</th>
                <th scope="col">Utilisations</th>
                <th scope="col">Expiration</th>
                <th scope="col">Créé le</th>
                <th scope="col">Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($links as $link): ?>
                <?php
                    $isExpired = $link['expires_at'] !== null && strtotime($link['expires_at']) < time();
                    $isActive  = (bool)$link['is_active'] && !$isExpired;
                    $statusLabel = $isExpired ? 'Expiré' : ($link['is_active'] ? 'Actif' : 'Désactivé');
                    if ($isExpired) {
                        $statusClass = 'status-badge status-pending';
                    } elseif ($link['is_active']) {
                        $statusClass = 'status-badge status-published';
                    } else {
                        $statusClass = 'status-badge';
                        $statusClass .= ' style="background:var(--error-muted-bg);color:var(--error);"';
                    }
                    $fullUrl = $baseUrl . '/partage/' . htmlspecialchars($link['slug']);
                    $created = date('d/m/Y H:i', strtotime($link['created_at']));
                    $expires = $link['expires_at'] ? date('d/m/Y H:i', strtotime($link['expires_at'])) : '—';
                    $hasPassword = !empty($link['password_hash']);
                ?>
                <tr>
                    <td>
                        <code style="font-size:var(--step--2);color:var(--text-secondary);"><?= htmlspecialchars($link['slug']) ?></code>
                        <input type="hidden" id="url-<?= $link['id'] ?>" value="<?= $fullUrl ?>">
                    </td>
                    <td>
                        <?php if ($link['objet_restriction']): ?>
                            <span class="status-badge"><?= htmlspecialchars($link['objet_restriction']) ?></span>
                        <?php else: ?>
                            <span style="color:var(--text-secondary);font-size:var(--step--2);">Tous</span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <?php if ($isExpired): ?>
                            <span class="status-badge status-pending"><?= $statusLabel ?></span>
                        <?php elseif ($link['is_active']): ?>
                            <span class="status-badge status-published"><?= $statusLabel ?></span>
                        <?php else: ?>
                            <span style="display:inline-block;padding:var(--space-3xs) var(--space-2xs);border-radius:3px;font-size:var(--step--2);font-weight:500;letter-spacing:0.04em;background:var(--error-muted-bg);color:var(--error);"><?= $statusLabel ?></span>
                        <?php endif; ?>
                    </td>
                    <td><?= $hasPassword ? 'Oui' : 'Non' ?></td>
                    <td style="text-align:center;"><?= intval($link['usage_count']) ?></td>
                    <td><?= $expires ?></td>
                    <td><?= $created ?></td>
                    <td class="admin-actions-col">
                        <div class="admin-actions">
                            <a href="/partage/<?= urlencode($link['slug']) ?>" target="_blank" rel="noopener"
                               class="admin-icon-btn admin-icon-btn--view" title="Visiter le formulaire">
                                <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M232.4,114.49,88.32,26.35a16,16,0,0,0-16.2-.3A15.86,15.86,0,0,0,64,39.87V216.13A15.94,15.94,0,0,0,80,232a16.07,16.07,0,0,0,8.36-2.35L232.4,141.51a15.81,15.81,0,0,0,0-27ZM80,215.94V40l143.83,88Z"></path></svg>
                            </a>
                            <button type="button" class="admin-icon-btn admin-icon-btn--copy" title="Copier l'URL"
                                    onclick="copyUrl(<?= $link['id'] ?>)">
                                <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M216,32H88a8,8,0,0,0-8,8V80H40a8,8,0,0,0-8,8V216a8,8,0,0,0,8,8H168a8,8,0,0,0,8-8V176h40a8,8,0,0,0,8-8V40A8,8,0,0,0,216,32ZM160,208H48V96H160Zm48-48H176V88a8,8,0,0,0-8-8H96V48H208Z"></path></svg>
                            </button>
                            <form method="post" action="actions/acces-etudiante.php" class="publish-form">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                                <input type="hidden" name="action" value="toggle">
                                <input type="hidden" name="id" value="<?= $link['id'] ?>">
                                <button type="submit"
                                        class="admin-icon-btn <?= $link['is_active'] ? 'admin-icon-btn--unpublish' : 'admin-icon-btn--publish' ?>"
                                        title="<?= $link['is_active'] ? 'Désactiver' : 'Activer' ?>">
                                    <?php if ($link['is_active']): ?>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M200,32H160a16,16,0,0,0-16,16V208a16,16,0,0,0,16,16h40a16,16,0,0,0,16-16V48A16,16,0,0,0,200,32Zm0,176H160V48h40ZM96,32H56A16,16,0,0,0,40,48V208a16,16,0,0,0,16,16H96a16,16,0,0,0,16-16V48A16,16,0,0,0,96,32Zm0,176H56V48H96Z"></path></svg>
                                    <?php else: ?>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M232.4,114.49,88.32,26.35a16,16,0,0,0-16.2-.3A15.86,15.86,0,0,0,64,39.87V216.13A15.94,15.94,0,0,0,80,232a16.07,16.07,0,0,0,8.36-2.35L232.4,141.51a15.81,15.81,0,0,0,0-27ZM80,215.94V40l143.83,88Z"></path></svg>
                                    <?php endif; ?>
                                </button>
                            </form>
                            <button type="button" class="admin-icon-btn admin-icon-btn--key" title="Modifier le mot de passe"
                                    onclick="openPasswordDialog(<?= $link['id'] ?>, <?= $hasPassword ? 'true' : 'false' ?>)">
                                <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" fill="currentColor" viewBox="0 0 256 256"><path d="M216.57,39.43A80,80,0,0,0,83.91,120.78L28.69,176A15.86,15.86,0,0,0,24,187.31V216a16,16,0,0,0,16,16H72a8,8,0,0,0,8-8V208H96a8,8,0,0,0,8-8V184h16a8,8,0,0,0,5.66-2.34l9.56-9.57A79.73,79.73,0,0,0,160,176h.1A80,80,0,0,0,216.57,39.43ZM224,98.1c-1.09,34.09-29.75,61.86-63.89,61.9H160a63.7,63.7,0,0,1-23.65-4.51,8,8,0,0,0-8.84,1.68L116.69,168H96a8,8,0,0,0-8,8v16H72a8,8,0,0,0-8,8v16H40V187.31l58.83-58.82a8,8,0,0,0,1.68-8.84A63.72,63.72,0,0,1,96,95.92c0-34.14,27.81-62.8,61.9-63.89A64,64,0,0,1,224,98.1ZM192,76a12,12,0,1,1-12-12A12,12,0,0,1,192,76Z"></path></svg>
                            </button>
                            <form method="post" action="actions/acces-etudiante.php" class="publish-form"
                                  id="delete-link-form-<?= $link['id'] ?>">
                                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
                                <input type="hidden" name="action" value="delete">
                                <input type="hidden" name="id" value="<?= $link['id'] ?>">
                                <button type="button" class="admin-icon-btn admin-icon-btn--delete" title="Supprimer"
                                        onclick="openDeleteLinkDialog(<?= $link['id'] ?>)">
                                    <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; ?>
        </tbody>
    </table>
    <?php endif; ?>
</main>

<!-- ═══════════════════════ CREATE DIALOG ═══════════════════════ -->
<dialog id="create-dialog" class="admin-dialog" aria-labelledby="create-dialog-title">
    <div class="admin-dialog__header">
        <h2 id="create-dialog-title">Créer un lien d'accès</h2>
        <button type="button" class="admin-dialog__close" aria-label="Fermer"
                onclick="document.getElementById('create-dialog').close()">&#x2715;</button>
    </div>
    <form method="post" action="actions/acces-etudiante.php" class="admin-form">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
        <input type="hidden" name="action" value="create">
        <div>
            <label for="create-objet">Type d'objet (optionnel)</label>
            <select id="create-objet" name="objet_restriction">
                <option value="">— Tous les types —</option>
                <option value="tfe">TFE</option>
                <option value="thèse">Thèse</option>
                <option value="frart">Frart</option>
            </select>
            <small>Restreint ce lien à un seul type de soumission.</small>
        </div>
        <div>
            <label for="create-password">Mot de passe (optionnel)</label>
            <input type="password" id="create-password" name="password" autocomplete="new-password">
            <small>Laissez vide pour un lien sans mot de passe.</small>
        </div>
        <div>
            <label for="create-expires">Expiration (optionnel)</label>
            <input type="datetime-local" id="create-expires" name="expires_at">
            <small>Laissez vide pour qu'il n'expire jamais.</small>
        </div>
        <div class="admin-form-footer">
            <button type="submit" class="btn btn--primary">Créer le lien</button>
            <button type="button" class="btn btn--secondary"
                    onclick="document.getElementById('create-dialog').close()">Annuler</button>
        </div>
    </form>
</dialog>

<!-- ═══════════════════════ PASSWORD DIALOG ═══════════════════════ -->
<dialog id="password-dialog" class="admin-dialog" aria-labelledby="password-dialog-title">
    <div class="admin-dialog__header">
        <h2 id="password-dialog-title">Mot de passe</h2>
        <button type="button" class="admin-dialog__close" aria-label="Fermer"
                onclick="document.getElementById('password-dialog').close()">&#x2715;</button>
    </div>
    <form method="post" action="actions/acces-etudiante.php" class="admin-form">
        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION['csrf_token']) ?>">
        <input type="hidden" name="action" value="set_password">
        <input type="hidden" name="id" id="password-link-id" value="">
        <div>
            <label for="password-input">Nouveau mot de passe</label>
            <input type="password" id="password-input" name="password" autocomplete="new-password">
            <small>Laissez vide pour supprimer le mot de passe.</small>
            <p id="password-current-info" style="font-size:var(--step--2);color:var(--text-secondary);margin-top:var(--space-2xs);"></p>
        </div>
        <div class="admin-form-footer">
            <button type="submit" class="btn btn--primary">Enregistrer</button>
            <button type="button" class="btn btn--secondary"
                    onclick="document.getElementById('password-dialog').close()">Annuler</button>
        </div>
    </form>
</dialog>

<script>
document.getElementById('open-create-dialog').addEventListener('click', () => {
    document.getElementById('create-dialog').showModal();
});

let _pendingDeleteLinkId = null;
function openDeleteLinkDialog(id) {
    _pendingDeleteLinkId = id;
    document.getElementById('delete-link-dialog').showModal();
}
function _executeDeleteLink() {
    const form = document.getElementById('delete-link-form-' + _pendingDeleteLinkId);
    if (form) form.submit();
}

function copyUrl(id) {
    const input = document.getElementById('url-' + id);
    navigator.clipboard.writeText(input.value).then(() => {
        const btn = event.target.closest('button');
        const orig = btn.textContent;
        btn.textContent = '✓ Copié';
        setTimeout(() => { btn.textContent = orig; }, 1200);
    });
}

function openPasswordDialog(id, hasPassword) {
    document.getElementById('password-link-id').value = id;
    const info = document.getElementById('password-current-info');
    info.textContent = hasPassword
        ? 'Un mot de passe est actuellement configuré. Entrez-en un nouveau ou laissez vide pour le supprimer.'
        : 'Aucun mot de passe configuré.';
    document.getElementById('password-dialog').showModal();
}
</script>

<!-- Delete link confirm -->
<dialog id="delete-link-dialog" class="admin-dialog admin-dialog--sm" aria-labelledby="delete-link-title">
    <div class="admin-dialog__header">
        <h2 id="delete-link-title">Supprimer ce lien</h2>
        <button type="button" class="admin-dialog__close" aria-label="Fermer"
                onclick="this.closest('dialog').close()">&#x2715;</button>
    </div>
    <div class="admin-dialog__alert">
        <p>Supprimer ce lien ? Les soumissions via ce lien seront bloquées.</p>
    </div>
    <div class="admin-dialog__footer">
        <button type="button" class="btn btn--danger" onclick="this.closest('dialog').close(); _executeDeleteLink()">Supprimer</button>
        <button type="button" class="btn btn--secondary" onclick="this.closest('dialog').close()">Annuler</button>
    </div>
</dialog>
