setTimeout(() => {
    $("#jobform").submit();
}, 200);

$("#jobform").submit(function (e) {

    e.preventDefault();

    var form = $(this);
    var url = form.attr('action');

    $.ajax({
        type: "POST",
        url: url,
        data: form.serialize(),
        beforeSend: function () { // Before we send the request, remove the .hidden class from the spinner and default to inline-block.
            $('#loader').removeClass('hidden')
        },
        success: function (data) {
            $('#result #list').html(data);
        },
        complete: function () { // Set our complete callback, adding the .hidden class and hiding the spinner.
            $('#loader').addClass('hidden')
        },
    });


});


for (const dropdown of document.querySelectorAll(".select-wrapper")) {
    dropdown.addEventListener('click', function () {
        this.querySelector('.select').classList.toggle('open');
    })
}


window.addEventListener('click', function (e) {
    for (const select of document.querySelectorAll('.select')) {
        if (!select.contains(e.target)) {
            select.classList.remove('open');
        }
    }
});
for (const option of document.querySelectorAll(".type_select .custom-option")) {
    option.addEventListener('click', function () {
        if (!this.classList.contains('selected')) {
            this.parentNode.querySelector('.custom-option.selected').classList.remove('selected');
            this.classList.add('selected');
            this.closest('.select').querySelector('.select__trigger span').textContent = this.textContent;
            document.getElementById('typefield').value = this.getAttribute("data-value");
        }
    })
}
for (const option of document.querySelectorAll(".hotel_select .custom-option")) {
    option.addEventListener('click', function () {
        if (!this.classList.contains('selected')) {
            this.parentNode.querySelector('.custom-option.selected').classList.remove('selected');
            this.classList.add('selected');
            this.closest('.select').querySelector('.select__trigger span').textContent = this.textContent;
            document.getElementById('outletfield').value = this.getAttribute("data-value");
        }
    })
}

const areaSelect = document.querySelector('.area_select');
const areaUrlParam = new URLSearchParams(window.location.search).get('area');


if (areaUrlParam) {
    const selectedOption = areaSelect.querySelector(`.custom-option[data-value="${areaUrlParam}"]`);
    // Remove "selected" class from all options by default
    const allOptions = areaSelect.querySelectorAll('.area_select .custom-option');
    for (const option of allOptions) {
        option.classList.remove('selected');
    }
    if (selectedOption) {
        selectedOption.classList.add('selected');
        document.getElementById('areasfield').value = areaUrlParam;
        document.getElementById('area_title').textContent = selectedOption.textContent;
    }
}
for (const option of document.querySelectorAll(".area_select .custom-option")) {
    option.addEventListener('click', function () {
        if (!this.classList.contains('selected')) {
            this.parentNode.querySelector('.custom-option.selected').classList.remove('selected');
            this.classList.add('selected');
            this.closest('.select').querySelector('.select__trigger span').textContent = this.textContent;
            document.getElementById('areasfield').value = this.getAttribute("data-value");
            $("#jobform").submit();
        }
    })
}

document.addEventListener('click', function (e) {
    const option = e.target.closest('.custom-option');

    if (option && option.dataset.url) {
        window.location.href = option.dataset.url;
    }
});