var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        openModal(gridBuilder.getCellValue(rowIndex, "Code"));
    });
    loadGrid();
});

function loadGrid(gridId, objectParameter) {
    ajax({
        url: "SaleApi/GetDiscountList",
        gridBuilder: gridBuilder,
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: gridBuilder.getCurrentPageNumber(),
            priceListCode: -1
        },
        success: function (data) {
            gridBuilder.loadData(data);
        }
    });
}

function edit() {
    gridBuilder.dblClick();
}

function openModal(code) {
    var title = code === -1 ? window.top.discount : window.top.editDiscount;
    var width = "960";
    var src = "General/DiscountSave" + (code > 0 ? "?code=" + code : "");
    openInModal('', src, title, width,"",false);
}

function deleteRow() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            loading: true,
            url: "SaleApi/DeleteDiscountList",
            data: { code: gridBuilder.getCellValue(rowIndex, "Code") },
            success: function (result) {
                showMsgModal(2, result.FinalMessage);
                loadGrid();
            }
        });
    }
}

