var code = getQuery("code");
$(document).ready(function () {
    $("#cboMoain").lookupBuilder().onChanged(async function () {
        var value = $("#cboMoain").lookupBuilder().getSelectedValue().split("|");
        for (var i = 1; i <= 3; i++) {
            var obj = $("[data-index=" + i + "]>div");
            var state = (!value || Number(value[i]) === 0 ? false : true);
            changeRequired(obj.attr("id"), state);
            obj.lookupBuilder().setEnabled(state);
        }
    });
    initial();
});
async function initial() {
    if (code) {
        await getWorkShop(code);
        $("#txtCode").prop("disabled", true);
    }
    else
        await getMaxWorkShopCode();
    $("#divMain").show();
    closeLoading();
}
async function getWorkShop(code) {
    await ajax({
        url: "SalaryApi/GetWorkShop",
        data: { code: code },
        success: function (result) {
            setJsonValue(result);
        }
    });
}
async function getMaxWorkShopCode() {
    await ajax({
        url: "SalaryApi/GetMaxWorkShopCode",
        data: { code: code },
        success: function (result) {
            $("#txtCode").val(result);
        }
    });
}
function save() {
    var error = checkValidation("");
    if (error === "") {
        var data = getJsonValue("");
        if (code)
            edit(data);
        else
            insert(data);
    }
    else
        showMsgModal(0, error, error);
}
function insert(data) {
    ajax({
        url: "SalaryApi/InsertWorkShop",
        data: data,
        success: function (result) {
            refreshParent('refresh()');
            showMsgModal(2, result.FinalMessage);
            closeTopModal();
        }
    });
}
function edit(data) {
    ajax({
        url: "SalaryApi/UpdateWorkShop",
        data: data,
        success: function (result) {
            refreshParent('refresh()');
            showMsgModal(2, result.FinalMessage);
            closeTopModal();
        }
    });
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    var moainCode = $("#cboMoain").lookupBuilder().getSelectedValue();
    if (moainCode)
        moainCode = moainCode.split("|")[0];
    switch (id) {
        case "cboResponsible":
            url = "GeneralApi/GetCustomerList";
            data = { type: -1, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboAccDlCode":
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: moainCode || null, level: 0, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboMoain":
            url = "AccountingApi/GetMoainList";
            data = { search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboTafsil":
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: moainCode, level: 21, search: search, pageNo: 1, selectedCode: code };
            break;
    }
    if (!url)
        return;
    var result;
    await ajax({
        url: url,
        data: data,
        success: function (resValue) {
            result = resValue;
        }
    });
    if (id === "cboCustomer") {
        $.each(result,
            function () {
                this.Code = this.Code + "|" + this.CurrencyCode + "|" + this.CurrencyDecimalCount + "|" + this.TafsilId + "|" + this.TafsilGroupCode;
            });
    }
    if (id === "cboMoain") {
        $.each(result, function () {
            this.Code = this.Code + "|" + this.ChildCount + "|" + this.ChildCountM + "|" + this.ChildCountP;
        });
    }
    $combo.lookupBuilder().bindData(result);
}