
$(document).ready(function () {
    checkEvent($('#fk_product').val());
    $('#fk_product').on('change', function () {
        checkEvent($(this).val());
    })
});
function checkEvent(value) {
    console.log('checkEvent');
    if (value == -1) {
        $('#eventDateSelector').addClass("TDhideClass");
        $('#eventTimeSelector').addClass("TDhideClass");
    } else {
        $("#date").val("");
        $('#eventTimeSelector').addClass("TDhideClass");
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "class/voucerhistory_assistant.class.php?product_id=" + value + "&function=checkEvent", true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                if (xhr.responseText == 'true') {
                    GetAllEventsOfTheSelectedProduct(value, function (allowedDates) {
                        console.log(allowedDates);
                        const dateInput = document.getElementById('date');
                        const errorMessage = document.getElementById('error-message');
                        dateInput.addEventListener('change', function () {
                            const selectedDate = dateInput.value;
                            if (!allowedDates.includes(selectedDate)) {
                                errorMessage.style.display = 'block';
                                dateInput.value = '';
                                $("#eventTimeSelector").addClass("TDhideClass");
                            } else {
                                errorMessage.style.display = 'none';
                                generateTimeOptions(value, dateInput.value);
                                $("#eventTimeSelector").removeClass("TDhideClass");
                            }
                        });
                        if(allowedDates !== '' ){
                            $("#eventDateSelector").removeClass("TDhideClass");
                        }
                    });
                } else {
                    $("#eventDateSelector").val("");
                    $("#date-range").attr("disabled", true);
                    $('#eventDateSelector').addClass("TDhideClass");
                    $('#eventTimeSelector').addClass("TDhideClass");
                }
            }
        };
        xhr.send();
    }

}

function generateTimeOptions(value, selectedDate) {
    var eventId = document.getElementById("eventId");
    eventId.innerHTML = '';
    GetAllTimesOfTheSelectedEvent(value, selectedDate, function (timeSlots) {
        console.log(timeSlots);
        Object.entries(timeSlots).forEach(function ([key, time]) {
            var option = document.createElement("option");
            option.value = key;
            option.text = time;
            eventId.appendChild(option);
        });
    });
}

function GetAllTimesOfTheSelectedEvent(value, selectedDate, callback) {
    if (value == -1) {
        $('#eventDateSelector').addClass("TDhideClass");
    } else {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "class/voucerhistory_assistant.class.php?product_id=" + value + "&selectedDate=" + selectedDate + "&function=GetAllTimesOfTheSelectedEvent", true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                try {
                    var responseArray = JSON.parse(xhr.responseText);
                    if (typeof callback === "function") {
                        callback(responseArray);
                    } else {
                        console.error("A callback nem függvény.");
                    }
                } catch (error) {
                    console.error("Hiba a válasz feldolgozása során:", error);
                }
            }
        };
        xhr.send();
    }
}

function GetAllEventsOfTheSelectedProduct(value, callback) {
    if (value == -1) {
        $('#eventDateSelector').addClass("TDhideClass");
    } else {
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "class/voucerhistory_assistant.class.php?product_id=" + value + "&function=GetAllEventsOfTheSelectedProduct", true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                try {
                    if (xhr.responseText != '') {
                        var responseArray = JSON.parse(xhr.responseText);
                        if (typeof callback === "function") {
                            callback(responseArray);
                        } else {
                            console.error("A callback nem függvény.");
                        }
                    }else{
                        $('#eventDateSelector').addClass("TDhideClass");
                    }
                } catch (error) {
                    console.error("Hiba a válasz feldolgozása során:", error);
                }
            }
        };
        xhr.send();
    }
}