var code = getQuery("code");
var type = getQuery("type");
$(document).ready(function () {
    initial();
});
async function initial() {
    if (code)
        await GetFactorFunctional(code);
    closeLoading();
    $("#divMain").show();
}
async function GetFactorFunctional(code) {
    await ajax({
        url: "SalaryApi/GetSalaryFactor",
        data: { code: code },
        success: function (result) {
            setJsonValue(result);
            $("#cboBasePercentage").prop("Checked", result.IsBasePercentage);
            $("#cboIsInReciptPrint").prop("Checked", result.IsInReciptPrint);
            $("#cboIsInReciptPrint1").prop("Checked", result.IsInReciptPrint);
            $("#cboIsActive").prop("Checked", result.IsActive);
            if (result.LatinName && result.LatinName.includes("EtcExtAmount")) {
                $("#lblName").hide();
                $("#txtName").show();
                $("#txtName").val(result.Name);
            }
            else {
                $("#lblName").show();
                $("#txtName").hide();
                $("#lblName").text(result.Name);
            }
            switch (type) {
                case "Rule":
                    $("#cboHasExtWork").prop("Checked", result.HasExtWork);
                    $("#cboHasImpactOfDays").prop("Checked", result.HasImpactOfDays);
                    $("#cboHasMonthRate").prop("Checked", result.HasMonthRate);
                    $("#cboHasNewYearReward").prop("Checked", result.HasNewYearReward);
                    $("#divRuling").show();
                    $("#divLegal").show();
                    $("#divFunctional").hide();
                    $("#divPrintToRecipt").hide();
                    break;
                case "Legal":
                    $("#divLegal").show();
                    $("#divRuling").hide();
                    $("#divFunctional").hide();
                    $("#divPrintToRecipt").hide();
                    break;
                case "Functional":
                case "FunctionalAmount":
                    $("#cboIsMinute").prop("Checked", result.IsMinute);
                    $("#cboIsHour").prop("Checked", result.IsHour);
                    $("#cboIsDay").prop("Checked", result.IsDay);
                    $("#cboIsMinute").prop("disabled", result.IsAmount);
                    $("#cboIsHour").prop("disabled", result.IsAmount);
                    $("#cboIsDay").prop("disabled", result.IsAmount);
                    $("#radNotIsAmount").prop("Checked", !result.IsAmount);
                    $("#radIsAmount").prop("Checked", result.IsAmount);
                    $("#radIsDec").prop("Checked", result.IsAmount && result.IsDec);
                    $("#radNotIsDec").prop("Checked", result.IsAmount && !result.IsDec);
                    $("#radIsDec").prop("disabled", !result.IsAmount);
                    $("#radNotIsDec").prop("disabled", !result.IsAmount);
                    var chkInsur = result.IsAmount && !result.IsDec ? true : false;
                    $("#chkInsur").prop("Checked", chkInsur ? result.HasInsur : false);
                    $("#chkTax").prop("Checked", chkInsur ? result.HasTax : false);
                    $("#chkInsur").prop("disabled", !chkInsur);
                    $("#chkTax").prop("disabled", !chkInsur);
                    $("#divFunctional").show();
                    $("#divPrintToRecipt").show();
                    $("#divRuling").hide();
                    $("#divLegal").hide();
                    break;
            }
        }
    });
}
function save() {
    var data = getJsonValue("");
    var error = checkValidation("");
    if (error === "") {
        if (code)
            edit(data);
    }
    else {
        showMsgModal(0, error, error);
    }
}
function edit(data) {
    ajax({
        url: "SalaryApi/UpdateSalaryFactor",
        data: data,
        success: function (result) {
            refreshParent("refresh('" + type + "')");
            showMsgModal(2, result.FinalMessage);
            closeTopModal();
        }
    });
}
function stateAmount() {
    if ($('#radIsAmount').prop("checked")) {
        $('#cboIsMinute').prop("disabled", true);
        $('#cboIsHour').prop("disabled", true);
        $('#cboIsDay').prop("disabled", true);
        $('#radIsDec').prop("disabled", false);
        $('#radNotIsDec').prop("disabled", false);
        $('#cboIsHour').prop("checked", false);
        $('#cboIsDay').prop("checked", false);
        $('#cboIsMinute').prop("checked", false);
    }
    else {
        $('#radIsDec').prop("checked", false);
        $('#radNotIsDec').prop("checked", false);
        $('#radIsDec').prop("disabled", true);
        $('#radNotIsDec').prop("disabled", true);
        $('#cboIsMinute').prop("disabled", false);
        $('#cboIsHour').prop("disabled", false);
        $('#cboIsDay').prop("disabled", false);
        $('#chkTax').prop("checked", false);
        $('#chkInsur').prop("checked", false);
        $('#chkInsur').prop("disabled", true);
        $('#chkTax').prop("disabled", true);
    }
}
function stateDec() {
    if ($("#radIsDec").prop("checked")) {
        $("#chkInsur").prop("disabled", true);
        $("#chkTax").prop("disabled", true);
        $("#chkTax").prop("checked", false);
        $("#chkInsur").prop("checked", false);
    }
    else {
        $("#chkInsur").prop("disabled", false);
        $("#chkTax").prop("disabled", false);
    }
}
function IsDec() {
    $("#radIsDec").prop("checked") ? $("#lblIsDec").text(window.top.decrease) : $("#lblIsDec").text(window.top.increase);
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr("id");
    var url = "";
    var data = null;
    switch (id) {
        case "cboMoain":
            url = "AccountingApi/GetMoainList";
            data = { checkActive : true, search: search, pageNo: 1, selectedCode: code };
            break;
    }
    if (!url)
        return;
    var result;
    await ajax({
        url: url,
        data: data,
        success: function (resValue) {
            result = resValue;
        }
    });
    $combo.lookupBuilder().bindData(result);
}