const updateBtn = document.getElementById("update-button");
const nameInput = document.getElementById("name-input");
const applicantInput = document.getElementById("applicant-input");
const defendantInput = document.getElementById("defendant-input");
const annotationInput = document.getElementById("annotation-input");
const dateInput = document.getElementById("date-input");
const specializationSelect = document.getElementById("specialization-select");
const caseId = document.getElementById("vb-id").dataset.id;

var specializations = [];

window.addEventListener("load", async () => {
    try {
        await $.ajax({
            url: `/case/getallspecializations`,
            type: "GET",
            contentType: "json"
        }).done((result) => {
            specializations = result;
            specializations.forEach((specialization) => {
                const option = document.createElement("option");
                option.value = specialization.id;
                option.innerHTML = specialization.name;
                specializationSelect.appendChild(option);
                specializationSelect.selectedIndex = -1;
            });
        });
    } catch (error) {
        console.error(error);
    }
});

updateBtn.addEventListener("click", () => {
    if (!correctData()) {
        return;
    }
    if (!validate()) {
        return;
    }
});

const correctData = function () {

    return true;
};

const validate = function () {

    return true;
};

updateBtn.addEventListener("click", () => {
    let caseModel = {
        "Id": parseInt(caseId),
        "Name": nameInput.value,
        "Applicant": applicantInput.value,
        "Defendant": defendantInput.value,
        "Date": new Date(dateInput.value),
        "Annotation": annotationInput.value,
        "SpecializationId": parseInt(specializationSelect.value),
    };
    console.log(caseModel)
    $.ajax({
        url: "/case/update",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify(caseModel)
    }).done(() => {
        window.location.href = "/Home/Cases";
    });
});