var gridBuilder;
var hasCurrencySystem = window.top.$("#hfCurrencySystem").val() === "1";
var baseCurrencyName = window.top.$("#hfBaseCurrencyName").val();
var currencyCode = Number(getQuery("currency"));
var currencyDecimalCount = Number(getQuery("currencyDecimalCount"));
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    $("#cboUser").lookupBuilder().onChanged(async function () {
        loadGrid();
    });
    gridBuilder.onSelectedChanged(function (selectedIndex,state) {
        var price = Number(gridBuilder.getCellValue(selectedIndex, "CurrencyPrice"));
        var previousPrice = numberNonCommas($('#lblAmountPeyment').text(), 0);
        var newPrice = (state ? previousPrice + price : previousPrice - price);
        var previousRemained = numberNonCommas($('#lblRemained').text(), 0)
        var newRemained = (state ? previousRemained - price : previousRemained + price).toFixed(currencyDecimalCount);
        $('#lblAmountPeyment').text(numberWithCommas(newPrice));
        $('#lblRemained').text(numberWithCommas(newRemained));
    });
    
    $('#lblRemained').text(getQuery("received"));
    $('#lblAmountReceived').text(getQuery("received"));
    if (!hasCurrencySystem) {
        gridBuilder.removeColumn("CurrencyName");
        gridBuilder.removeColumn("CurrencyPrice");
    }
    else {
        gridBuilder.setColumnCaption("Price", window.top.documentAmount + "(" + baseCurrencyName + ")");
    }
    loadGrid();
});
function choose() {
    var selectedCodes = gridBuilder.getSelectedRowsCellValue("ArticleCode");
    if (selectedCodes.length === 0) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex >= 0) {
            selectedCodes.push(gridBuilder.getCellValue(rowIndex, "ArticleCode"));
        }
    }
    refreshParent('setDataToPonyCheque(' + JSON.stringify(selectedCodes) + ')');
    closeTopModal();
}
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var cusomerCode = Number($("#cboUser").lookupBuilder().getSelectedValue());
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetChequeDetailList",
        data: {
            isChequeIn: true, 
            operationType:[1,6],
            fromDate: $("#txtFromDate").val(),
            toDate: $("#txtToDate").val(),
            customerCode: cusomerCode,
            currencyCode: currencyCode,
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
            selectionCriteria: gridBuilder.selectionViewMode ? "'" + gridBuilder.selectionList.join("','") + "'" : null
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                gridBuilder.loadData(data);
                if (getQuery("chequeCode") !== "") {
                    chequeCode = getQuery("chequeCode").split(",");
                    for (var i = 0; i < chequeCode.length; i++) {
                        var selectedCheque = data.find(function (item) { return item.CodeRiz === Number(chequeCode[i]); });
                        gridBuilder.setSelected(data.indexOf(selectedCheque));
                    }
                }
            }
        }
    });
}
async function getEventLookupCombo($combo, search, code) {
    await ajax({
        url: "GeneralApi/GetUserList",
        data: { isActive: true, pageNo: 1, search: search},
        success: function (result) {
            result.forEach(function (item) {
                item["Code"] = item.CodeTarafH;
                item["Name"] = item.UserName;
            });
            $combo.lookupBuilder().bindData(result);
        }
    });
}