const Stand = {
    schedule: [],
    init: async () => {
        let html = await fetch('/lib/pages/stand/index.html').then((response) => response.text());
        app.innerHTML = html;

        let listDate = [1, 4];

        function generateAvailableDates() {
            let select = document.getElementById("dateSelect");
            select.innerHTML = "";

            let today = new Date();
            today.setHours(0, 0, 0, 0);
            let months = [today.getMonth(), today.getMonth() + 1];
            let year = today.getFullYear();

            months.forEach(month => {
                let date = new Date(year, month, 1);
                while (date.getMonth() === month) {
                    if (date >= today) {
                        let day = date.getDay();
                        if (listDate.includes(day)) {
                            let option = document.createElement("option");
                            option.value = date.toISOString().split("T")[0];
                            option.textContent = date.toLocaleDateString("uk-UA", {
                                weekday: "long", year: "numeric", month: "long", day: "numeric"
                            });
                            select.appendChild(option);
                        }
                    }
                    date.setDate(date.getDate() + 1);
                }
            });
        }

        // generateAvailableDates();
        Stand.generator();
    },
    generator: () => {
        let block_schedule = document.getElementById('stand-schedule');

        let html = "";

        let stand = {
            id: 1,
            title: "Універсам",
            geo: { lat: 0, lng: 0 },
            hour_start: 9,
            hour_end: 14,
            quantity_sheep: 2,
            week_days: [0, 2, 4, 6],
            processing_time: 0.5,
            updated_at: null
        }

        Stand.schedule = [];

        // Кількість годин служіння
        let stand_length = (stand.hour_end - stand.hour_start) / stand.processing_time;

        for (let z = 0; z < stand.week_days.length; z++) {
            Stand.schedule.push([]);

            let date = new Date();
            date.setDate(date.getDate() + stand.week_days[z]);
            let dayName = date.toLocaleDateString('uk-UA', { weekday: 'long' });

            html += `
            <div class="block-day" id="day-${z}">
                <h3>${date.toLocaleDateString()} • ${dayName}</h3>
        `;

            let stand_date = 1 + stand.week_days[z];

            for (let i = 0; i < stand_length; i++) {
                let time_now = stand.hour_start + (stand.processing_time * i);
                let timeFormat = (a) => a > 9 ? a : `0${a}`;

                function formatTime(hours) {
                    let h = Math.floor(hours);
                    let m = (hours % 1 === 0.5) ? "30" : "00";
                    let hh = h.toString().padStart(2, "0");
                    return `${hh}:${m}`;
                }

                html += `
                <div id="hour-${z}-${i}">
                    <span class="time">${formatTime(time_now)}-${formatTime(time_now + stand.processing_time)}</span>
            `;

                for (let q = 0; q < stand.quantity_sheep; q++) {
                    html += `
                    <select id="name">
                        <option value=""></option>
                        <option value="Test">Test</option>
                    </select>
                `;

                    Stand.schedule[z].push({
                        id: (i + z) * stand.quantity_sheep + q,
                        hour: stand.hour_start + (stand.processing_time * i),
                        number_sheep: q,
                        date: stand_date,
                        sheep_id: null,
                        stand_id: stand.id,
                        updated_at: Date.now()
                    });
                }

                html += `</div>`; // закриваємо hour
            }

            html += `</div>`; // закриваємо day
        }

        document.getElementById('stand-info-title').innerText = stand.title;
        document.getElementById('stand-info-geo').innerHTML = '';
        document.getElementById('stand-info-image').setAttribute('src', '');

        block_schedule.innerHTML = html;
    }
}