var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    getEventLookupCombo($("#cboAccount"));
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetChequeLiteList",
        data: {
            chequeBookCode: $("#cboChequeNumber").lookupBuilder().getSelectedValue(),
            fromDate: $("#txtAzDate").val(),
            toDate: $("#txtTaDate").val(),
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else
                gridBuilder.loadData(data);
        }
    });
}
function getChequeBookList() {
    $("#cboChequeNumber").lookupBuilder().setSelectedValue("");
    getEventLookupCombo($("#cboChequeNumber"));
}
function openPrint() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var summery = gridBuilder.getCellValue(rowIndex, "Summery");
    var name = gridBuilder.getCellValue(rowIndex, "CustomerName");
    $("#txtSummary").val(summery + " " + name);
    $("#SummaryModal").modal();
}
function print() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var price = gridBuilder.getCellValue(rowIndex, "Price");
    var date = gridBuilder.getCellValue(rowIndex, "ReceiptDateLocal").replaceAll("/", "");
    var printForm = gridBuilder.getCellValue(rowIndex, "PrintForm");
    var title = window.top.printCheck;
    var summary = $("#txtSummary").val();
    var src = "Report/Default?GrpName=PrintChak&DateVosul=" + date + "&Mablagh=" + price + "&Tozihat=" + summary + "&reportId=" + printForm
    openInModal('', src, title, "", "", false);
    $("#SummaryModal").modal("hide");
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    switch (id) {
        case "cboAccount":
            url = "TreasuryApi/GetBankAccountList";
            data = { bank: true, search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboChequeNumber":
            url = "TreasuryApi/GetChequeBookList";
            data = { search: search, bankCode: $("#cboAccount").lookupBuilder().getSelectedValue() || null, pageNo: 1, selectedCode: code };
            break;
    }
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            $combo.lookupBuilder().bindData(result);
            if (!code && result.length > 0)
                $combo.lookupBuilder().setSelectedValue(result[0].Code);
        }
    });
}
