var gridBuilderNoSelect;
var gridBuilderSelect;
$(document).ready(function () {
    gridBuilderNoSelect = $("#gridProductNoSelect").gridBuilder({ multiSelect: true });
    gridBuilderSelect = $("#gridProductSelect").gridBuilder({ multiSelect: true });
    gridBuilderNoSelect.setCaption(window.top._noSelectProduct);
    gridBuilderSelect.setCaption(window.top._selectProduct);
    gridBuilderNoSelect.onExportClick(function () {
        loadGrid("gridProductNoSelect", { exportExcel: true });
    });
    gridBuilderSelect.onExportClick(function () {
        loadGrid("gridProductSelect", { exportExcel: true });
    });
});
async function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var valid = gridId === "gridProductSelect";
    var gridBuilder = valid ? gridBuilderSelect : gridBuilderNoSelect;
    await ajax({
        loading:false,
        gridBuilder: gridBuilder,
        url: "SaleApi/GetProductsValidInStore",
        data: {
            storeCode: $("#cboStore").lookupBuilder().getSelectedValue(),
            valid: valid,
            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);
        }
    });
}
async function storeChange() {
    await loadGrid("gridProductNoSelect");
    await loadGrid("gridProductSelect");
}
function onDoAction(value, isAdd) {
    var storeCode = $("#cboStore").lookupBuilder().getSelectedValue();
    if (!storeCode)
        showMsgModal(0, window.top.storeSelect);
    else {
        ajax({
            loading : true,
            url: "SaleApi/" + (isAdd ? "InsertProductsValidInStore" : "DeleteProductsValidInStore"),
            data: { storeCode: storeCode, productCodes: value },
            success: function (result) {
                showMsgModal(2, result.FinalMessage);
                storeChange();
            }
        });
    }
}
function addRemoveSingle(isAdd) {
    var codes = isAdd ? gridProductNoSelectBuilder.getSelectedRowsCellValue("Code") : gridProductSelectBuilder.getSelectedRowsCellValue("Code");
    if (codes.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    onDoAction(codes, isAdd);
}
function addRemoveAll(isAdd) {
    ajax({
        url: "SaleApi/GetProductsValidInStore",
        data: {
            storeCode: $("#cboStore").lookupBuilder().getSelectedValue(),
            valid: !isAdd,
            filter: !isAdd ? gridBuilderSelect.getFilter() : gridBuilderNoSelect.getFilter(),
            sort: !isAdd ? gridBuilderSelect.getSortFields() : gridBuilderNoSelect.getSortFields(),
            pageNo: -1
        },
        success: function (data) {
            if (data.length > 0) {
                var codes = [];
                data.forEach(function (item) {
                    codes.push(item.Code);
                });
                onDoAction(codes, isAdd);
            }
        }
    });
}
async function getEventLookupCombo($combo, search, code) {
    await ajax({
        url: "SaleApi/GetStoreList",
        loading: false,
        data: { creditType: 1, search: search, filter: "", sort: "", pageNo: 1, selectedCode: code },
        success: function (result) {
            $combo.lookupBuilder().bindData(result);
            if (result.length > 0)
                setTimeout(function () { $combo.lookupBuilder().setSelectedValue(result[0].Code); }, 200);
        }
    });
}