<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calendrier avec réservations</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .calendar th, .calendar td {
            width: 14.28%;
            text-align: center;
            vertical-align: top;
            cursor: pointer;
            padding: 5px;
        }
        .calendar .day-number {
            font-weight: bold;
        }
        .selected-date {
            background: #007bff;
            color: white;
            border-radius: 50%;
        }
    </style>
</head>
<body>

<div class="container">
    <h1 class="text-center mt-4">Calendrier des réservations</h1>
    <div class="calendar card mt-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <button id="prevMonth" class="btn btn-outline-primary btn-sm">&lt;</button>
            <h5 id="monthYear"></h5>
            <button id="nextMonth" class="btn btn-outline-primary btn-sm">&gt;</button>
        </div>
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>Lun</th>
                    <th>Mar</th>
                    <th>Mer</th>
                    <th>Jeu</th>
                    <th>Ven</th>
                    <th>Sam</th>
                    <th>Dim</th>
                </tr>
            </thead>
            <tbody id="calendarBody"></tbody>
        </table>
    </div>
    <div id="selectedDate" class="text-center mt-3"></div>
</div>

<script>
    const monthNames = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
                        "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
    let currentDate = new Date();
    let selectedCell = null;

    function renderCalendar(date, reservations = {}) {
        const year = date.getFullYear();
        const month = date.getMonth();
        const firstDay = new Date(year, month, 1);
        const lastDay = new Date(year, month + 1, 0);
        const startDay = (firstDay.getDay() + 6) % 7;
        const daysInMonth = lastDay.getDate();

        document.getElementById('monthYear').textContent = `${monthNames[month]} ${year}`;

        const calendarBody = document.getElementById('calendarBody');
        calendarBody.innerHTML = '';

        let row = document.createElement('tr');
        for (let i = 0; i < startDay; i++) {
            row.appendChild(document.createElement('td'));
        }

        for (let day = 1; day <= daysInMonth; day++) {
            if ((startDay + day - 1) % 7 === 0 && day !== 1) {
                calendarBody.appendChild(row);
                row = document.createElement('tr');
            }

            const cell = document.createElement('td');
            cell.innerHTML = `<div class="day-number">${day}</div>
                              <div class="small text-muted">${reservations[day] ? reservations[day] + ' réservation(s)' : ''}</div>`;
            cell.onclick = () => selectDate(day, month, year, cell);
            row.appendChild(cell);
        }
        calendarBody.appendChild(row);
    }

    function selectDate(day, month, year, cell) {
        if (selectedCell) selectedCell.classList.remove('selected-date');
        cell.querySelector('.day-number').classList.add('selected-date');
        selectedCell = cell.querySelector('.day-number');
        document.getElementById('selectedDate').textContent =
            `Vous avez sélectionné le ${day} ${monthNames[month]} ${year}`;
    }

    async function loadReservations() {
        const year = currentDate.getFullYear();
        const month = currentDate.getMonth() + 1;
        const response = await fetch(`http://127.0.0.1:5000/api/reservations?month=${month}&year=${year}`);
        const data = await response.json();
        renderCalendar(currentDate, data);
    }

    document.getElementById('prevMonth').addEventListener('click', () => {
        currentDate.setMonth(currentDate.getMonth() - 1);
        loadReservations();
    });

    document.getElementById('nextMonth').addEventListener('click', () => {
        currentDate.setMonth(currentDate.getMonth() + 1);
        loadReservations();
    });

    loadReservations();
</script>

</body>
</html>
