$(document).ready(function () {
    $(".main-title").click(function () {
        $('.main-title').removeClass("selected-main-title");
        $(this).addClass("selected-main-title");
        if ($(this).attr('data-child') === "div-printaccounting") {
            $('#div-printaccounting').show();
            $("#sub").children().removeClass("sub-title").addClass("sub no-sub-title");
            $("#sub").removeClass("sub-backGround ").addClass("no-sub-backGround");
            $('#div-assigningdefaultaccount').hide();
        }
        else if ($(this).attr('data-child') === "div-assigningdefaultaccount") {
            $('#div-printaccounting').hide();
            $("#sub").children().removeClass("no-sub-title").addClass("sub sub-title");
            $("#sub").removeClass("no-sub-backGround").addClass("sub-backGround");
            $('#div-assigningdefaultaccount').show();
            $('#SubOpeningDiv').show();
            $('#SubSigningAccountDiv').hide();
            $('#SubSigningServiceDiv').hide();
            $('[data-child="SubOpeningDiv"]').addClass("selected-sub-title");
        }
        $(".sub").click(function () {
            var destinationId = $(this).attr('data-child');
            $(".page-div").hide();
            $("#" + destinationId).show();
            $(".sub-title").removeClass('selected-sub-title');
            $(this).addClass("selected-sub-title");
        });
    });
    
    $("#cboMoainService").lookupBuilder().onChanged(function () {
        var selectedMoain = $("#cboMoainService").lookupBuilder().getSelectedValue();
        if (selectedMoain) {
            var data = selectedMoain.split("|");
            $("#cboTafsilService").lookupBuilder().setEnabled(Number(data[1]) > 0);
            $("#cboCostCenterService").lookupBuilder().setEnabled(Number(data[2]) > 0);
            $("#cboProjectService").lookupBuilder().setEnabled(Number(data[3]) > 0);
        }
        $("#cboTafsilService").lookupBuilder().setSelectedValue("");
        $("#cboCostCenterService").lookupBuilder().setSelectedValue("");
        $("#cboProjectService").lookupBuilder().setSelectedValue("");
    });
    $('#SubSigningAccount').height(window.screen.height + 150);

    $('.check').click(function () {
        var id = $(this).attr("data-id");
        if ($(this).is(':checked')) {
            $('#span' + id).addClass('span-checked');
            $('#span' + id).text(window.top.print);
        }
        else {
            $('#span' + id).removeClass('span-checked');
            $('#span' + id).text(window.top.notPrint);
        }
    });
    initial();
});
function saveChange() {
    var model = [];
    $('.check').each(function () {
        model.push({ Param: $(this).attr("data-id"), Value: $(this).is(':checked') ? "1" : "0", Tozihat: 'Public' });
    });
    $('.system-lookup-control').each(function () {
        var selectedValue = $(this).lookupBuilder().getSelectedValue();
        if ($(this).attr("id") === "cboMoainService" && selectedValue)
            selectedValue = selectedValue.split("|")[0];
        model.push({ Param: "LinkHesab", Value: selectedValue || -1, Tozihat: $(this).attr("data-id") });
    });
    $('.text-control[data-id]').each(function () {
        if (!$(this).parent().hasClass("dropdown-menu"))
            model.push({ Param: $(this).attr("data-id"), Value: $(this).val(), Tozihat: 'Public' });
    });
    ajax({
        loading: true,
        url: "GeneralApi/SetConfigArray",
        data: { configList: model },
        success: function (result) {
            showMsgModal(2, window.top.informationSaved);
            location.reload();
        }
    });
}
async function initial() {
    await getConfig("hs");
    await getConfig("hst");
    $("#divMain").show();
    closeLoading();
}
async function getConfig(systemName) {
    var data;
    await ajax({
        url: "GeneralApi/GetConfigArrayBySystem",
        data: { systemName: systemName },
        success: function (result) {
            data = result;
        }
    });
    await loadData(data, systemName);
}
async function loadData(result, systemName) {
    for (var i = 0; i < result.length; i++) {
        var dataId = systemName === "hs" ? result[i].Param : result[i].Tozihat;
        var $element = $("[data-id='" + dataId + "']");
        if ($element.length === 1) {
            if ($element.hasClass("system-lookup-control"))
                await $element.lookupBuilder().setSelectedValue(result[i].Value);
            else if ($element.hasClass("check")) {
                if (result[i].Value === "1") {
                    $element.prop("checked", true);
                    $element.parent().find("span").addClass("span-checked").innerText = window.top.print;
                }
                else {
                    $element.prop("checked", false);
                    $element.parent().find("span").removeClass("span-checked").innerText = window.top.notPrint;
                }
            }
            else
                $element.val(result[i].Value);
        }
    }
}
async function getEventLookupCombo($combo, search, code) {
    var level = $combo.parent().attr("data-level");
    var url = "";
    var id = $combo.attr("id");
    var data = null;
    switch (level) {
        case "tafsilGroup":
            url = "AccountingApi/GetTafsilGroupLinkedMoain";
            data = { moainCode: -1, search: search, selectedCode: code, pageNo: 1 };
            break;
        case "moain":
            url = "AccountingApi/GetMoainList";
            data = { checkActive: false, search: search, selectedCode: code, pageNo: 1 };
            break;
        case "tafsilOpening":
            if (!$("#cboEftetahiyehMoein").lookupBuilder().getSelectedValue()) {
                $combo.lookupBuilder().clearItems();
                return;
            }
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: $("#cboEftetahiyehMoein").lookupBuilder().getSelectedValue() || -1, level: 21, search: search, selectedCode: code, pageNo: 1 };
            break;
        case "tafsilClosing":
            if (!$("#cboEkhtetamiyehMoein").lookupBuilder().getSelectedValue()) {
                $combo.lookupBuilder().clearItems();
                return;
            }
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: $("#cboEkhtetamiyehMoein").lookupBuilder().getSelectedValue() || -1, level: 21, search: search, selectedCode: code, pageNo: 1 };
            break;
        case "tafsil":
            var selectedMoain = $("#cboMoainService").lookupBuilder().getSelectedValue();
            if (!selectedMoain)
                return;
            var moainCode = selectedMoain.split("|")[0];
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: moainCode, level: $combo.attr("data-tafsil"), search: search, selectedCode: code, pageNo: 1 };
            break;
    }
    if (url === "")
        return;
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            if (id === "cboMoainService") {
                $.each(result,
                    function () {
                        this.Code = this.Code + "|" + this.ChildCount + "|" + this.ChildCountM + "|" + this.ChildCountP
                    });
            }
            if (level === "tafsilGroup") {
                result = result.filter(function (item) { return Number(item.Level) === 21; });
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}