var salaryFactorList = JSON.parse($("#hfSalaryFactorList").val());
$(document).ready(function () {
    $("#hfSalaryFactorList").remove();
    setElements();
    $("#cboFunctionType").lookupBuilder().onChanged(function (sender) {
        var code = Number(sender.getSelectedValue());
        var item = salaryFactorList.find(x => x.Code === code);
        if (!item)
            return;
        $("[data-name]").each(function () {
            if (item[$(this).attr("data-name")])
                $(this).removeAttr("hidden");
            else
                $(this).attr("hidden", "hidden");
        });
    });
});
function savePersonnelFunction(reloadPage) {
    var data = getJsonValue('');
    var month = $("#cboMonth").lookupBuilder().getSelectedValue().padStart(2, 0);
    var year = $("#cboYear").lookupBuilder().getSelectedValue();
    data["YearMonth"] = year + month;
    var exceprionText = checkValidation('');
    if (exceprionText == '') {
        var code = getQuery("code");
        ajax({
            url: "SalaryApi/" + (code ? "UpdateRevenue" : "InsertRevenue"),
            data: data,
            success: function (result) {
                if (result.Result) {
                    showMsgModal(2, window.top.savedSuccessfully);
                    refreshParent('loadGrid()');
                    if (!reloadPage) {
                        closeTopModal();
                    }
                    else {
                        location.reload();
                    }
                }
                else showMsgModal(0, result.ErrorMessage, result.ErrorMessage, '0');
            }
        });
    }
    else {
        showMsgModal(0, exceprionText, exceprionText, '0');
    }
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    var result;
    switch (id) {
        case "cboPersonnel":
            url = "SalaryApi/GetPersonnels";
            data = { search: search, pageNo: 1, selectedCode: code };
            break;
    }
    if (url) {
        await ajax({
            url: url,
            data: data,
            success: function (retValue) {
                result = retValue;
            }
        });
        if (id === "cboPersonnel") {
            $.each(result, function () {
                this.Code = this.CorrespondenceCode;
                this.Name = this.FullName;
            });
        }
        $combo.lookupBuilder().bindData(result);
    }
}
function setElements() {
    var code = Number($("#cboFunctionType").lookupBuilder().getSelectedValue());
    var item = salaryFactorList.find(x => x.Code === code);
    if (!item)
        return;
    $("[data-name]").each(function () {
        if (item[$(this).attr("data-name")])
            $(this).removeAttr("hidden");
        else
            $(this).attr("hidden", "hidden");
    });
}