<?php
// Récupérer les données pour les filtres
require_once __DIR__ . '/../../../../../../model/service/ModuleService.php';
require_once __DIR__ . '/../../../../../../model/service/SemestreService.php';
$moduleService = new ModuleService();
$semestreService = new SemestreService();

$stats = $moduleService->getStats();
$filieres = $moduleService->getFilieres();
$semestres = $semestreService->getAllSemestres();
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des 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 -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon">📖</div>
                <div class="stat-info">
                    <span class="stat-label">Total Modules</span>
                    <span class="stat-value" id="statTotal"><?= $stats['total'] ?? 0 ?></span>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">🏫</div>
                <div class="stat-info">
                    <span class="stat-label">Filières</span>
                    <span class="stat-value" id="statFilieres"><?= $stats['total_filieres'] ?? 0 ?></span>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon">⏰</div>
                <div class="stat-info">
                    <span class="stat-label">Total Heures</span>
                    <span class="stat-value" id="statTotalHeures">0</span>
                </div>
            </div>
        </div>

        <!-- Section Recherche -->
        <div class="search-section">
            <form id="searchForm" class="search-form">
                <div class="form-group">
                    <label>Libellé</label>
                    <input type="text" id="searchLibelle" placeholder="Rechercher par libellé...">
                </div>
                <div class="form-group">
                    <label>Code</label>
                    <input type="text" id="searchCode" placeholder="Rechercher par code...">
                </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="<?= htmlspecialchars($filiere['filiere']) ?>">
                                <?= htmlspecialchars($filiere['filiere']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group">
                    <label>Semestre</label>
                    <select id="searchSemestre">
                        <option value="">Tous les semestres</option>
                        <?php foreach ($semestres as $semestre): ?>
                            <option value="<?= $semestre['id'] ?>">
                                <?= htmlspecialchars($semestre['libelle']) ?> (<?= htmlspecialchars($semestre['code']) ?>)
                            </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="openModuleModal()"><i class="fas fa-plus"></i> Nouveau Module</button>
                    <button type="button" class="btn btn-info" onclick="exportModules()"><i class="fas fa-file-export"></i> Exporter</button>
                </div>
            </form>
        </div>

        <!-- Tableau -->
        <div class="table-container">
            <div class="table-wrapper">
                <table>
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Libellé</th>
                            <th>Code</th>
                            <th>Filière</th>
                            <th>Semestre</th>
                            <th>Année universitaire </th>
                            <th>Heures (C/TP/TD)</th>
                            <th>Total</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody id="tableBody">
                        <tr>
                            <td colspan="8" 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'; ?>
<?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>