<?php

// Démarrer la session
session_start();

// Vérifie si l'utilisateur est connecté
$isAdminLoggedIn = isset($_SESSION['user_id']);

// Inclusion de la configuration
include 'config.php';

// Requête pour récupérer les données
$sql = "SELECT
            Coureur.nom,
            Coureur.numDossard AS numero,
            Pays.nomPays AS pays,
            Equipe.nomEquipe AS equipe,
            YEAR(CURDATE()) - YEAR(Coureur.DN) AS age,
            COUNT(Performance.idPerformance) AS nb_participation,
            COALESCE(SUM(Bonification.reductionTemps), 0) AS bonifications
        FROM Coureur
        LEFT JOIN Pays ON Coureur.codePays = Pays.codePays
        LEFT JOIN Equipe ON Coureur.numEquipe = Equipe.numEquipe
        LEFT JOIN Performance ON Coureur.numDossard = Performance.numDossard
        LEFT JOIN Bonification ON Coureur.numDossard = Bonification.numDossard
        GROUP BY Coureur.numDossard";

$result = $conn->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teams Dashboard</title>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet">

    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        .sidebar {
            position: fixed;
            width: 148px;
            height: 100%;
            background-color: #f8e4a6;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding-top: 20px;
        }

        .sidebar .logo {
            width: 40px;
            height: 40px;
            margin-bottom: 100px;
        }

        .sidebar .icon {
            font-size: 36px;
            margin: 20px 0;
            color: #333;
            cursor: pointer;
            position: relative;
            text-decoration: none;
            width: 50px;
            height: 50px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .sidebar .active {
            background-color: #fff;
            border-radius: 15%;
            color: #000;
            padding: 0;
            width: 70px;
            height: 70px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .main {
            margin-left: 148px;
            padding: 20px;
        }

        .header {
            text-align: center;
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
        }

        .cards {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }

        .card {
            flex: 1;
            height: 150px;
            background-color: #ccc;
            border-radius: 10px;
        }

        .filter {
            background-color: #b8a99c;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            margin-bottom: 20px;
            cursor: pointer;
        }

        .table-container {
            background-color: #eaeff3;
            padding: 20px;
            border-radius: 10px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        table th, table td {
            text-align: left;
            padding: 10px;
            border-bottom: 1px solid #ddd;
        }

        table th {
            font-weight: bold;

        }
        .bx-trash{
            color:red;
        }
    </style>
</head>
<body>
    <div class="sidebar">
        <img src="logo.png" alt="Logo" class="logo">
        <a href="index.php" class="material-icons icon">home</a>
        <a href="Coureur.php" class="material-icons icon active">directions_bike</a>
        <a href="Etape.php" class="material-icons icon">terrain</a>
        <a href="Team.php" class="material-icons icon">groups</a>
        <?php if ($isAdminLoggedIn): ?>
            <a href="admin.php" class="material-icons icon">admin_panel_settings</a>
                        <?php endif;?>
    </div>

    <div class="main">
        <div class="header">Coureurs</div>
        

        <div class="cards">
            <div class="card"></div>
            <div class="card"></div>
            <div class="card"></div>
        </div>

        <button class="filter">Filtre</button>
        <?php if ($isAdminLoggedIn): ?>
            <a href="create_coureur.php"><button class="filter">Crée un coureur</button></a>
                        <?php endif;?>

        <div class="table-container">
            <table>
                <thead>
                    <tr>
                        <th>Nom</th>
                        <th>Numéro</th>
                        <th>Pays</th>
                        <th>Équipe</th>
                        <th>Âge</th>
                        <th>Nb de participation</th>
                        <th>Bonifications</th>
                        <?php if ($isAdminLoggedIn): ?>
                            <th>Modification</th>
                        <?php endif;?>
                    </tr>
                </thead>
                <tbody>
                    <?php
if ($result->num_rows > 0) {
    // Affichage des résultats
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . htmlspecialchars($row['nom']) . "</td>";
        echo "<td>" . htmlspecialchars($row['numero']) . "</td>";
        echo "<td>" . htmlspecialchars($row['pays']) . "</td>";
        echo "<td>" . htmlspecialchars($row['equipe']) . "</td>";
        echo "<td>" . htmlspecialchars($row['age']) . "</td>";
        echo "<td>" . htmlspecialchars($row['nb_participation']) . "</td>";
        echo "<td>" . htmlspecialchars($row['bonifications']) . "</td>";

        if ($isAdminLoggedIn) {
            echo "<td>";
            echo "<a href='delete_coureur.php?numDossard=" . urlencode($row['numero']) . "' title='Supprimer'><i class='bx bx-trash'></i></a>";
            echo " ";
            echo "<a href='update_coureur.php?numDossard=" . urlencode($row['numero']) . "' title='Modifier'><i class='bx bxs-pencil'></i></a>";
            echo "</td>";
        }

        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='7'>Aucun coureur trouvé</td></tr>";
}
?>
</tbody>
            </table>
        </div>
    </div>

    <script>
        const icons = document.querySelectorAll('.sidebar .icon');

        icons.forEach(icon => {
            icon.addEventListener('click', () => {
                // Supprimer la classe active de tous les icônes
                icons.forEach(i => i.classList.remove('active'));

                // Ajouter la classe active à l'icône cliquée
                icon.classList.add('active');
            });
        });
    </script>
</body>
</html>

<?php
$conn->close();
?>
