var MoainLevel = "3";
var TafsilLevel = 4;
var TafsilLevelType = 21;
var isInsert;
var level = Number(getQuery("depth"));
var levelType = Number(getQuery("parentLevel"));
var currencySystem = window.top.$("#hfCurrencySystem").val() === "1";
var baseCurrencyCode = window.top.$("#hfBaseCurrencyCode").val();
var baseCurrencyName = window.top.$("#hfBaseCurrencyName").val();
var showCurrencyPanel = true;
$(document).ready(function () {
    isInsert = getQuery("id") === "";
    if (!currencySystem || level !== TafsilLevel || levelType !== TafsilLevelType) {
        showCurrencyPanel = false;
        $("#pnlCurrencyType").hide();
    }
    if (isInsert)
        getNewHeadingCode();
    else
        setForm(getQuery("code"));
});

function getNewHeadingCode() {
    ajax({
        loading: true,
        url: "AccountingApi/GetNewHeadingCode",
        data: {
            level: level,
            code: getQuery("code")
        },
        success: function (result) {
            setForm(result);
        }
    });
}

function getNewTafsilId() {
    ajax({
        loading: true,
        url: "AccountingApi/GetNewTafsilId",
        data: { tafsilGroupCode: getQuery("code") },
        success: function (result) {
            $("input[data-id='Id']").val(result);
        }
    });
}

async function getEventLookupCombo($combo, filtera, code) {
    await ajax({
        url: "GeneralApi/GetCurrencyList",
        async: false,
        data: {
            search: filtera,
            filter: "",
            pageNo: 1,
            selectedCode: code
        },
        success: function (result) {
            $combo.lookupBuilder().bindData(result);
        }
    });
}

async function setForm(code) {
    $("input[data-id='Code']").val(code);
    if (level === 3) {
        $("#txtId").attr("data-length", "3").val(code.toString().substr(3, 3));
        if (getQuery("id")) {
            $("#txtName").val(getQuery("text").trim().substr($("#txtId").val().length));
            $("#txtId").prop("disabled", true);
        }
    }
    else {
        if (isInsert) {
            if (showCurrencyPanel) {
                $("#cboCurrency").lookupBuilder().setSelectedValue(baseCurrencyCode);
            }
            getNewTafsilId();
        }
        else {
            var id = getQuery("id");
            $("input[data-id='Id']").val(id);
            $("input[data-id='MainId']").val(id);
            $("input[data-id='Name']").val(getQuery("text").substr(id.length));
            if (showCurrencyPanel) {
                await $("#cboCurrency").lookupBuilder().setSelectedValue(getQuery("currencyTypeCode"));
            }
        }
    }
    $("#divMain").show();
    closeLoading();
}

function save() {
    var methodName = "";
    var data;
    if ($("#txtId").attr('data-length'))
        $("#txtId").inputmask("999");

    var exceptionText = checkValidation('');
    if (exceptionText === "") {
        var item = getJsonValue();
        var currencyCode = showCurrencyPanel ? $("#cboCurrency").lookupBuilder().getSelectedValue() : baseCurrencyCode;
        var currencyName = showCurrencyPanel ? $("#cboCurrency").lookupBuilder().getSelectedText() : baseCurrencyName;
        if (level === 3) {
            methodName = isInsert ? "InsertMoain" : "UpdateMoain";
            data = item;
        }
        else {
            methodName = isInsert ? "InsertTafsil" : "UpdateTafsil";
            data = item;
        }

        ajax({
            loading: true,
            url: "AccountingApi/" + methodName,
            data: data,
            success: function (result) {
                result = result.ReturnValue.split("|");
                var parentMethod = "";
                var code = result[0];
                var name = result[1];
                var id = result[2];
                if ($.trim(getQuery("comebacktotarafhesab")) === "1") {
                    showMsgModal(2, window.top.savedSuccessfully);
                    parentMethod = "fechDataListItem()";
                }
                else if (isInsert)
                    parentMethod = "addNodeToTree(" + code.substr(0, getQuery("depth") === MoainLevel ? 3 : 6) + "," + code + ",'" + name + "','" + id + "','" + currencyCode + "','" + currencyName + "')";
                else
                    parentMethod = "editNodeToTree('" + name + "'," + code + ",'" + id + "','" + currencyCode + "','" + currencyName + "')";

                refreshParent(parentMethod);
                closeTopModal();

            }
        });
    }
    else {
        showMsgModal(0, exceptionText);
    }
    
}

