$(document).ready(function () {
    $(".pm-accordion-row td:first-child").click();

    $('.select2-jumps-out').on('select2:open', function () {
        $('.select2-container .select2-dropdown').addClass("select2-move-left");
    });
});

function saveCurrentEducationData() {
    $.ajax({
        type: "POST",
        url: $("#SaveCurrentEducationDataUrl").val(),
        data: getEducationData(),
        success: function (data) {
            if (data) {
                window.location.href = data.indexUrl;
            }
        }
    });
}

function getEducationData() {
    return {
        Worker: $("#ModelWorker").val(),
        Occup: $("#Occup").val(),
        FinishedSchool: $("#FinishedSchool").val(),
        SchoolEducation: $("#SchoolEducation").val(),
        ProfEduc: $("#ProfEduc").val(),
        ProfEducLvlName: $("#ProfEducLvlName").val(),
        ProfQual: $("#ProfQual").val(),
        CodeSrv: $("#CodeSrv").val(),
        CodeP: $("#CodeP").val(),
        ProfQualiLvlCode: $("#ProfQualiLvlCode").val(),
        TreineeYr: $("#TreineeYr").val(),
        Unemployed: $("#Unemployed").is(':checked')
    }
}

let selectedEducationId = '';
function openNoteModal(id) {
    selectedEducationId = id;
    $("#item-note").val($("#Note-" + id).val());
    $('#note-modal').modal('show');
}

function saveNote() {
    const noteValue = $("#item-note").val();

    if (noteValue && noteValue.trim()) {
        $("#Note-" + selectedEducationId).val(noteValue);
        $("#Note-button-" + selectedEducationId).addClass('text-primary');
    } else {
        $("#Note-" + selectedEducationId).val("");
        $("#Note-button-" + selectedEducationId).removeClass('text-primary');
    }

    setIsDirty($("#Note-" + selectedEducationId), "education-history-row");
}

function deleteEducationHistoryItem(educationId) {
    $.ajax({
        type: "DELETE",
        url: $("#DeleteEducationHistoryUrl").val(),
        data: {
            worker: $("#ModelWorker").val(),
            educationId: educationId,
        },
        success: function (data) {
            if (data.isDeleted) {
                $(`#educations-history-${educationId}`).remove();
            }
        }
    });
}

function editEducationsHistoryData() {
    const data = [];

    $(".isDirty").each(function () {
        const educId = $(this).attr("id").split("-")[2];
        const educationData = getEducationHistoryData(educId);
        data.push(educationData);
    });

    if (data.length > 0) {
        $.ajax({
            type: "PUT",
            url: $("#EditEducationHistoryUrl").val(),
            contentType: "application/json",
            data: JSON.stringify(data),
            success: function (data) {
                if (!data.status) {
                    showDefaultErrorCustomModal($("#GeneralErrorMessage").val(), $("#custom-modal-confirm").val());
                }
                location.reload();
            },
            error: function () {
                showDefaultErrorCustomModal($("#GeneralErrorMessage").val(), $("#custom-modal-confirm").val());
            }
        });

    }
}

function getEducationHistoryData(educId) {
    return {
        Worker: $("#ModelWorker").val(),
        FinishedSchool: $("#FinishedSchool-" + educId).val(),
        DateFinished: $("#DateFinished-" + educId).val(),
        Performer: $("#Performer-" + educId).val(),
        SchoolEducation: $("#SchoolEducation-" + educId).val(),
        ProfEduc: $("#ProfEduc-" + educId).val(),
        ProfQual: $("#ProfQual-" + educId).val(),
        Note: $("#Note-" + educId).val(),
        Occup: $("#Occup-" + educId).val(),
        CodeP: $("#CodeP-" + educId).val(),
        CodeSrv: $("#CodeSrv-" + educId).val(),
        Id: $("#Id-" + educId).val(),
        Qid: $("#Qid-" + educId).val(),
    };
}


function setSelectedName(element, nameId, position) {
    if ($(element).val()) {
        const id = $(element).attr("id"); 
        const selectedValue = $("#select2-" + id + "-container").attr("title");

        if (selectedValue.indexOf("|") > -1) {
            const splitted = selectedValue.split(" | ");
            $("#" + nameId).val(splitted[position]);
        }
    }
    else {
        $("#" + nameId).val("");
    }
}