<?php
// app/view/admin/pedagogie/inscription_module/list/index.php

require_once __DIR__ . '/../../../../../../model/service/InscriptionModuleService.php';
require_once __DIR__ . '/../../../../../../model/service/FiliereService.php';
require_once __DIR__ . '/../../../../../../model/service/ModuleService.php';
require_once __DIR__ . '/../../../../../../model/service/SemestreService.php';
require_once __DIR__ . '/../../../../../../model/service/EmployeeService.php';

$inscriptionService = new InscriptionModuleService();
$filiereService = new FiliereService();
$moduleService = new ModuleService();
$semestreService = new SemestreService();
$employeeService = new EmployeeService();

$stats = $inscriptionService->getStats();
$filieres = $filiereService->getAllFiliere();
$semestres = $semestreService->getAllSemestres();
$annees = $inscriptionService->getAnneesUniversitaires();
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des Inscriptions Modules - EST</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <link rel="stylesheet" href="../../../../../css/admin-styles.css">
</head>
<body>
<?php include __DIR__ . '/../../../../../layouts/admin/header.php'; ?>
<?php include __DIR__ . '/../../../../../layouts/admin/pedagogie/nav-second-level.php'; ?>

<main class="main-content">
    <div class="container">

        <!-- ========================================= -->
        <!-- STATISTIQUES - IDs: statTotal, statModules, statEtudiants -->
        <!-- ========================================= -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon">📋</div>
                <div class="stat-info">
                    <span class="stat-label">Total Inscriptions</span>
                    <span class="stat-value" id="statTotal"><?= $stats['total_inscriptions'] ?? 0 ?></span>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">📚</div>
                <div class="stat-info">
                    <span class="stat-label">Modules</span>
                    <span class="stat-value" id="statModules"><?= $stats['total_modules'] ?? 0 ?></span>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">👨‍🎓</div>
                <div class="stat-info">
                    <span class="stat-label">Étudiants inscrits</span>
                    <span class="stat-value" id="statEtudiants"><?= $stats['total_etudiants'] ?? 0 ?></span>
                </div>
            </div>
        </div>

        <!-- ========================================= -->
        <!-- SECTION RECHERCHE -->
        <!-- IDs: searchForm, searchSearch, searchAnnee, searchFiliere -->
        <!-- ========================================= -->
        <div class="search-section">
            <form id="searchForm" class="search-form">
                <div class="form-group">
                    <label>Recherche</label>
                    <input type="text" id="searchSearch" placeholder="Rechercher par module ou filière...">
                </div>
                <div class="form-group">
                    <label>Année universitaire</label>
                    <select id="searchAnnee">
                        <option value="">Toutes les années</option>
                        <?php foreach ($annees as $annee): ?>
                            <option value="<?= htmlspecialchars($annee['annee_universitaire']) ?>">
                                <?= htmlspecialchars($annee['annee_universitaire']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Filière</label>
                    <select id="searchFiliere">
                        <option value="">Toutes les filières</option>
                        <?php foreach ($filieres as $filiere): ?>
                            <option value="<?= $filiere['id'] ?>">
                                <?= htmlspecialchars($filiere['libelle']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="search-actions">
                    <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> Rechercher</button>
                    <button type="reset" class="btn btn-secondary" onclick="resetSearch()"><i class="fas fa-undo"></i> Réinitialiser</button>
                    <button type="button" class="btn btn-success" onclick="openCreateModal()"><i class="fas fa-plus"></i> Nouvelle Inscription</button>
                </div>
            </form>
        </div>

        <!-- ========================================= -->
        <!-- TABLEAU - IDs: tableBody, paginationInfo, pageNumbers, prevBtn, nextBtn -->
        <!-- ========================================= -->
        <div class="table-container">
            <div class="table-wrapper">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Module</th>
                            <th>Filière</th>
                            <th>Année</th>
                            <th>Nb Étudiants</th>
                            <th>Date</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        <tr>
                            <td colspan="7" style="text-align:center;padding:40px;">
                                <div class="loading active">
                                    <div class="spinner"></div>
                                    <p style="margin-top:10px;">Chargement...</p>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="pagination">
                <div class="pagination-info" id="paginationInfo">Affichage de 0 à 0 sur 0</div>
                <div class="pagination-buttons">
                    <button onclick="changePage('prev')" id="prevBtn">◀ Précédent</button>
                    <span id="pageNumbers"></span>
                    <button onclick="changePage('next')" id="nextBtn">Suivant ▶</button>
                </div>
            </div>
        </div>
    </div>
</main>

<?php include __DIR__ . '/../../../../../layouts/admin/footer.php'; ?>

<!-- ========================================= -->
<!-- MODALS - IDs: inscriptionModal, detailModal, detailContent, toastContainer -->
<!-- ========================================= -->
<?php include __DIR__ . '/../create-edit/modal.php'; ?>
<?php include __DIR__ . '/../view/modal.php'; ?>

<div class="toast-container" id="toastContainer"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/js/all.min.js"></script>
<script src="main.js"></script>
</body>
</html>