var code = getQuery("code");
var code = getQuery("code");
$(document).ready(function () {
    $("#cboMoain").lookupBuilder().onChanged(async function () {
        await $("#cboTafsil").lookupBuilder().setSelectedValue("");
    });
    $("#cboProvince").lookupBuilder().onChanged(async function () {
        await $("#cboCity").lookupBuilder().setSelectedValue("");
    });
    initial();
});
async function initial() {
    if (code) {
        await getCostCenter(code);
        $("#txtCode").prop("disabled", true);
    }
    else
        await getMaxCostCenterCode();
    $("#divMain").show();
    closeLoading();
}
async function getCostCenter(code) {
    await ajax({
        url: "SalaryApi/GetCostCenter",
        data: { code: code },
        success: function (result) {
            setJsonValue(result);
        }
    });
}
async function getMaxCostCenterCode() {
    await ajax({
        url: "SalaryApi/GetMaxCostCenterCode",
        data: { code: code },
        success: function (result) {
            $("#txtCode").val(result);
            $("#txtId").val(result);
        }
    });
}
function save() {
    if (code)
        edit();
    else
        insert();
}
function insert() {
    var data = getJsonValue("");
    var error = checkValidation("");
    if (error === "") {
        ajax({
            url: "SalaryApi/InsertCostCenter",
            data: data,
            success: function (result) {
                refreshParent('refresh()');
                showMsgModal(2, result.FinalMessage);
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, error, error);
    }
}
function edit() {
    var data = getJsonValue("");
    var error = checkValidation("");
    data["Code"] = data["Id"]= code;
    if (error === "") {
        ajax({
            url: "SalaryApi/UpdateCostCenter",
            data: data,
            success: function (result) {
                refreshParent('refresh()');
                showMsgModal(2, result.FinalMessage);
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, error, error);
    }
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr("id");
    var url = "";
    var data = null;
    var provinceCode = $("#cboProvince").lookupBuilder().getSelectedValue();
    switch (id) {
        case "cboMoain":
            url = "AccountingApi/GetTafsilGroupList";
            data = { level: 20, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboTafzil":
            url = "AccountingApi/GetTafsilList";
            data = { tafsilGroupCode: $("#cboMoain").lookupBuilder().getSelectedValue(), level: -1, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboProvince":
            url = "GeneralApi/GetProvinceList";
            data = {};
            break;
        case "cboCity":
            url = "GeneralApi/GetCityList";
            data = { provinceCode: provinceCode ? provinceCode : -1, 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);
}

