var baseCurrencyCode= window.top.$("#hfBaseCurrencyCode").val();
$(document).ready(function () {
    $("#cboTafsilGroup").lookupBuilder().onChanged(async function () {
        await $("#cboTafsil").lookupBuilder().setSelectedValue("");
    });
    $("#cboTafsil").lookupBuilder().onChanged(async function () {
        if ($("#cboCurrency").length === 0)
            return;
        var selectedItem = $("#cboTafsil").lookupBuilder().getSelectedValue();
        if (selectedItem.length > 0) {
            selectedItem = selectedItem.split("|");
            await $("#cboCurrency").lookupBuilder().setSelectedValue(selectedItem[1]);
            $("#cboCurrency").lookupBuilder().setEnabled(Number(selectedItem[0]) === -1 ? true : false);
        }
        else {
            await $("#cboCurrency").lookupBuilder().setSelectedValue(baseCurrencyCode);
        }
    });
});
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;

    switch (id) {
        case "cboTafsilGroup":
            url = "AccountingApi/GetTafsilGroupList";
            data = { level: -1, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboTafsil":
            url = "AccountingApi/GetTafsilList";
            data = { tafsilGroupCode: $("#cboTafsilGroup").lookupBuilder().getSelectedValue(), level: -1, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboCurrency":
            url = "GeneralApi/GetCurrencyList";
            data = { search: search, filter: "", sort: "", pageNo: 1 };
            break;
        case "drpMoainCash": case "drpMoainPayment": case "drpMoainCollection":
            url = "AccountingApi/GetMoainLinkedTafsilGroup";
            data = { tafsilGroupCode: $("#cboTafsilGroup").lookupBuilder().getSelectedValue(), search: search, sort: "", pageNo: 1 };
            break;
        case "drpGuaranteed":
            url = "AccountingApi/GetMoainLinkedTafsilGroup";
            data = { search: search, sort: "", pageNo: 1 };
            break;
    }
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            if (id === "cboTafsil") {
                var newResult = [{ Name: window.top.newAccount, Code: "-1|" + baseCurrencyCode }];
                $.each(result, function () {
                    this.Code = this.Code + "|" + this.CodeArz;
                });
                result = newResult.concat(result);
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function save() {
    var data = getJsonValue();
    var exceptionText = checkValidation('');
    var code = getQuery("code");
    if (exceptionText === '') {
        ajax({
            loading: true,
            url: "TreasuryApi/" + (code === "" ? "InsertBankAccount" : "UpdateBankAccount"),
            data: data,
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                refreshParent('loadGrid()');
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, exceptionText);
    }
}