var code = getQuery("code");
$(document).ready(function () {
    initial();
});
async function initial() {
    $("#cboMoain").lookupBuilder().onChanged(function () {
        cboMoainChange();
    });
    await getBranchUser();
    if (code !== "") {
        await getFunder();
    }
    else {
        closeLoading();
        $("#divMain").show();
    }
}
async function getFunder() {
    await ajax({
        url: "TreasuryApi/GetFunder",
        loading: false,
        data: { code: code },
        success: function (result) {
            result.Users = result.UsersStr.split(",");
            loadData(result);
        }
    });
}
async function loadData(result) {
    await setJsonValue(result);
    closeLoading();
    $("#divMain").show();
}
async function getBranchUser() {
    await ajax({
        url: "GeneralApi/GetShobehUserList",
        loading: false,
        success: function (result) {
            result.forEach(function (item) {
                $("#drpUser").append("<option value='" + item.Code + "'>" + item.Name + "</option>");
            });
            $("#drpUser").selectize({
                plugins: ['remove_button'],
                sortField: 'text'
            });
        }
    });
}
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 "cboTarafH":
            url = "GeneralApi/GetCustomerList";
            data = { type: 2, search: search, sort: 'name', selectedCode: code, pageNo: 1 };
            break;
        case "cboFundType":
            url = "TreasuryApi/GetFunderTypeList";
            data = { search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboMoain":
            url = "AccountingApi/GetMoainList";
            data = { checkActive: true, search: search, selectedCode: code, pageNo: 1 };
            break;
        case "cboTafsil5":
            if (!moainCode)
                return;
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: moainCode, level: 20, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboTafsil6":
            if (!moainCode)
                return;
            url = "AccountingApi/GetTafsilList";
            data = { moainCode: moainCode, level: 22, search: search, pageNo: 1, selectedCode: code };
            break;
    }
    if (url === "")
        return;
    await ajax({
        url: url,
        loading: false,
        data: data,
        success: function (result) {
            if (id === "cboMoain") {
                $.each(result,
                    function () {
                        this.Code = this.Code +
                            "|" +
                            this.ChildCount +
                            "|" +
                            this.ChildCountM +
                            "|" +
                            this.ChildCountP;
                    });
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function cboMoainChange() {
    var selectedItem = $("#cboMoain").lookupBuilder().getSelectedValue().split("|");
    for (var i = 2; i <= 3; i++) {
        var $obj;
        switch (i) {
            case 2:
                $obj = $("#cboTafsil5");
                break;
            case 3:
                $obj = $("#cboTafsil6");
                break;
        }
        if (Number(selectedItem[i]) === 0) {
            $obj.lookupBuilder().setEnabled(false);
            $obj.lookupBuilder().setSelectedValue("");
            $obj.removeClass("validation").attr("data-required", "");
        }
        else {
            $obj.lookupBuilder().setEnabled(true);
            $obj.addClass("validation").attr("data-required", "true");
        }
    }
}
function save() {
    var exceptionText = checkValidation("");
    if (exceptionText === "") {
        var data = getJsonValue();
        data['Code'] = data["Code"] || -1;
        if (!checkLength(data["Code"]))
            showMsgModal(0, window.top.codeCharacter);
        else {
            data["Users"] = $("#drpUser")[0].selectize.getValue();
            if (data["Users"].length > 0) {
                ajax({
                    loading:true,
                    url: "TreasuryApi/" + (code ? "UpdateFunder" : "InsertFunder"),
                    data: data ,
                    success: function (result) {
                        showMsgModal(2, result.FinalMessage.join("<br />"));
                        refreshParent("refresh()");
                        closeTopModal();
                    }
                });
            }
            else {
                showMsgModal(0, window.top.selectAuthorizedUsers);
            }
        }
    }
    else {
        showMsgModal(0, exceptionText);
    }
}
function checkLength(value) {
    return !(!isNaN(value) && value.length > 12);
}
