var gridNoRialBuilder;
var gridRialBuilder;
$(document).ready(function () {
    gridNoRialBuilder = $("#gridNoRial").gridBuilder();
    gridRialBuilder = $("#gridRial").gridBuilder();
    gridNoRialBuilder.onExportClick(function () {
        loadGrid("gridNoRial", { exportExcel: true });
    });
    gridRialBuilder.onExportClick(function () {
        loadGrid("gridRial", { exportExcel: true });
    });
    $("#cboProductGroup").lookupBuilder().onChanged(async function () {
        $("#cboProduct").lookupBuilder().setSelectedValue("");
        refresh();
    });
    $("#cboProduct").lookupBuilder().onChanged(async function () {
        refresh();
    });
    $("#cboProduct").lookupBuilder().setSelectedValue("0");
});
function setRialRemove(state) {
    var title = state === 1 ? window.top.delProductRial : window.top.calcProductRial;
    var width = "950";
    var src = "Store/DepotRialRemove?state=" + state;
    openInModal('', src, title, width, '',false);
}
function docControled(state) {
    var title = state === 1 ? window.top.confirmNumberControl : window.top.retConfirmNumberControl;
    var width = "800";
    var src = "Store/RialControls?&state=" + (state === 0);
    openInModal('', src, title, width,540);
}
async function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var data = getJsonValue();
    gridBuilder = gridId === "gridRial" ? gridRialBuilder : gridNoRialBuilder;
    await ajax({
        url: "SaleApi/GetRialProductTurnover",
        gridBuilder: gridBuilder,
        data: {
            rialState: gridId === "gridRial",
            productGroupCode: data["ProductGroupCode"],
            productCode: Number(data["ProductCode"]) || null,
            controlState: Number(data["ControlState"]) || null,
            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);
            }
        }
    });
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    if (!id)
        return;
    var url = "";
    var data = null;
    switch (id) {
        case "cboProductGroup":
            url = "SaleApi/GetProductGroupList";
            data = { search: search, pageNo: 1, selectedCode: code };
            break;
        case "cboProduct":
            url = "SaleApi/GetProductLiteList";
            data = { productGroupCode: $("#cboProductGroup").lookupBuilder().getSelectedValue(), search: search, pageNo: 1, selectedCode: code };
            break;
    }
    await ajax({
        url: url,
        data: data,
        success: function (result) {
            var newResult = [{ Name: window.top.allItems, Code: "0" }];
            result = newResult.concat(result);
            $combo.lookupBuilder().bindData(result);
        }
    });
}
async function refresh()
{
    await loadGrid("gridNoRial");
    await loadGrid("gridRial");
}
