document.addEventListener('DOMContentLoaded', function () {

    const calendarEl = document.getElementById('calendar');

    let selectedDates = []; // YYYY-MM-DD (max 14)

  
    const calendar = new FullCalendar.Calendar(calendarEl, {
        initialView: 'dayGridMonth',

        /* 🔥 timezone + start today */
        timeZone: 'local',
        initialDate: new Date(),

        /* 🔥 disable past dates */
        validRange: {
            start: new Date()
        },

        selectable: true,
        selectMirror: true,

        /* ❌ Prevent selecting blocked dates */
selectAllow: function(selectInfo) {
        const day = new Date(selectInfo.start).getDay();
        // 0 = Sunday
        if(day === 0) return false; // disable Sundays

        // Prevent already selected dates
        return !selectedDates.includes(selectInfo.startStr);
    },

        /* 🟩 User selects a day */
        select: function (info) {

            const date = info.startStr;

            if (selectedDates.length >= 14) {
                alert('Maximum 14 dates allowed');
                calendar.unselect();
                return;
            }

            if (!selectedDates.includes(date)) {
                selectedDates.push(date);

                /* 🔴 Add red background event */
                calendar.addEvent({
                    start: date,
                    allDay: true,
                    display: 'background',
                    backgroundColor: '#ff4d4f'
                });
            }

            calendar.unselect();
        }
    });

    calendar.render();
});
