<?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 des équipes
$sql = "SELECT
            Equipe.nomEquipe AS nom,
            Equipe.numEquipe AS numero,
            Pays.nomPays AS pays
        FROM Equipe
        LEFT JOIN Pays ON Equipe.codePays = Pays.codePays";

$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; width: 70px; height: 70px; }
        .main { margin-left: 148px; padding: 20px; }
        .header { text-align: center; font-size: 24px; font-weight: bold; margin-bottom: 20px; }
        .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; }
        .cards {
            display: flex;
            gap: 20px;
            margin-bottom: 20px;
        }

        .card {
            flex: 1;
            height: 150px;
            background-color: #ccc;
            border-radius: 10px;
        }
    </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">directions_bike</a>
        <a href="Etape.php" class="material-icons icon">terrain</a>
        <a href="Team.php" class="material-icons icon active">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">Équipes</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_equipe.php"><button class="filter">Créer une équipe</button></a>
        <?php endif;?>

        <div class="table-container">
            <table>
                <thead>
                    <tr>
                        <th>Nom d'équipe</th>
                        <th>Numéro d'équipe</th>
                        <th>Pays</th>
                        <th>Maillot</th>
                        <?php if ($isAdminLoggedIn): ?>
                            <th>Modification</th>
                        <?php endif;?>
                    </tr>
                </thead>
                <tbody>
                    <?php
if ($result->num_rows > 0) {
    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>";

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

        echo "</tr>";
    }
} else {
    echo "<tr><td colspan='4'>Aucune équipe trouvée</td></tr>";
}
?>
                </tbody>
            </table>
        </div>
    </div>

    <script>
        const icons = document.querySelectorAll('.sidebar .icon');
        icons.forEach(icon => {
            icon.addEventListener('click', () => {
                icons.forEach(i => i.classList.remove('active'));
                icon.classList.add('active');
            });
        });
    </script>
</body>
</html>

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