const createReceiptLoc = $("#CreateReceiptLOC").val();
const createInvoiceFromSelectedLoc = $("#CreateInvoiceFromSelectedLOC").val();

const createInvoiceGetUrl = $("#PerformedTasksCreateInvoiceUrl").val();

function updateActionButton() {
    const checkboxes = document.querySelectorAll('.task-checkbox');
    const createInvoiceButton = document.getElementById('createInvoiceButton');
    let anyChecked = false;

    checkboxes.forEach(checkbox => {
        if (checkbox.checked) {
            anyChecked = true;
        }
    });

    createInvoiceButton.innerHTML = anyChecked ? createInvoiceFromSelectedLoc : createReceiptLoc;
}

function submitPerformedTasksFilterForm() {
    $("#performed-tasks-filter-form").submit();
}

let selectedEventItems = [];

function updateSelectedTasks(checkbox) {
    const eventId = checkbox.value;

    if (checkbox.checked) {
        if (!selectedEventItems.includes(eventId)) {
            selectedEventItems.push(eventId);
        }
    } else {
        selectedEventItems = selectedEventItems.filter(id => id !== eventId);
    }
}

function openCreateInvoice() {
    const params = new URLSearchParams();
    if (selectedEventItems.length > 0) {
        selectedEventItems.forEach((id, index) => {
            params.append(`selectedEventInvoiceItems[${index}]`, id);
        });
    }

    const url = `${createInvoiceGetUrl}?${params.toString()}`;

    window.location.href = url;
}