var gridBuilder;
var hasChanged = false;
$(document).ready(function () {
    initial();
});
async function initial() {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onSetMaskEdit(setEventMaskEdit);
    gridBuilder.onExportClick(exportSampleExcel);
    gridBuilder.onImportClick(function (gridId, type, base64) {
        ajax({
            loading: true,
            url: "GeneralApi/ImportExcel",
            data: {
                key: "PriceList",
                base64: base64
            },
            success: function (result) {
                var data = eval(result.result);
                gridBuilder.loadData(data);
            }
        });
    });
    gridBuilder.setColumnVisibility(1, true);
    gridBuilder.setColumnVisibility(5, true);
    $("#cboProductGroup").lookupBuilder().onChanged(async function () {
        await $("#cboProduct").lookupBuilder().setSelectedValue("");
        loadGrid();
    });
    $("#cboProduct").lookupBuilder().onChanged(async function () {
        loadGrid();
    });
    closeLoading();
    $("#divMain").show();
    loadGrid();
}
async function getEventLookupCombo($combo, search, code) {
    var id = $combo.attr('id');
    var url = "";
    var data = null;
    switch (id) {
        case "cboProduct":
            url = "SaleApi/GetPriceList";
            data = { productGroupCode: $("#cboProductGroup").lookupBuilder().getSelectedValue() || -1, search: search, pageNo: 1 };
            break;
        case "cboProductGroup":
            url = "SaleApi/GetProductGroupList";
            data = { search: search, pageNo: 1 };
            break;
    }
    if (!url)
        return;
    await ajax({
        url: url,
        loading:false,
        data: data,
        success: function (result) {
            if (id === "cboProduct") {
                var list = [];
                result.forEach(function (item) {
                    if (list.filter(x => x.Code === item.ProductCode).length === 0) {
                        list.push({ Code: item.ProductCode, Name: item.ProductName });
                    }
                });
                result = list;
            }
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function exportSampleExcel(gridId, type) {
    if (type === "excelSample") {
        exportExcel(null, gridBuilder.getColumns(), gridBuilder.getColumnsCaption(), true);
    } else
        exportExcel(gridBuilder.getDataArray()["data"], gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
}
function loadGrid(gridId, objectParameter ) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading:false,
        gridBuilder: gridBuilder,
        url: "SaleApi/GetPriceList",
        data: {
            productCode: $("#cboProduct").lookupBuilder().getSelectedValue() || -1,
            productGroupCode: $("#cboProductGroup").lookupBuilder().getSelectedValue() || -1,
            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 gridCellChange() {
    hasChanged = true;
}
function openModal(name) {
    if (name === "PriceCopyList") {
        if (hasChanged) {
            alert(window.top.copyListButton);
        }
        else {
            var productCode = $("#cboProduct").lookupBuilder().getSelectedValue();
            var productGroupCode = $("#cboProductGroup").lookupBuilder().getSelectedValue();
            openInModal('', 'General/PriceCopyList?kalaCode' + productCode + '&groupCode' + productGroupCode, window.top.copyPriceList, '600', '400');
        }
    }
    else {
        openInModal('', "General/PriceListEdit", window.top.changeListName, "600", "442");
    }

}
function getNewPriceList(list) {
    var newList = [];
    list.forEach(function (item) {
        newList.push({
            ProductCode: item.ProductCode,
            UnitCode: item.UnitCode,
            Price0: numberNonCommas(item.Price0) || 0,
            Price1: numberNonCommas(item.Price1) || 0,
            Price2: numberNonCommas(item.Price2) || 0,
            Price3: numberNonCommas(item.Price3) || 0,
            Price4: numberNonCommas(item.Price4) || 0,
            Price5: numberNonCommas(item.Price5) || 0,
            Price6: numberNonCommas(item.Price6) || 0,
            CodePartProperty: (item.ProductId.indexOf("-") > 0 ? item.ProductId.split("-")[1] : -1)
        });
    });
    return newList;
}
function saveChange(reload) {
    showLoading();
    var priceList = gridBuilder.getDataArray()["data"];
    var newPriceList = getNewPriceList(priceList);
    ajax({
        loading: true,
        url: "SaleApi/SavePriceList",
        data: { priceList : newPriceList },
        success: function () {
            showMsgModal(2, window.top.informationSaved);
            if (reload)
                location.reload();
        }
    });
}
function onCancel() {
    showLoading();
    if (hasChanged) {
        if (confirm(window.top.cancelChanges)) {
            location.reload();
        }
    }
    else
        location.reload();
}




