var gridBuilder;
var saleSystemCode = getQuery("systemcode");
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var factoreNumber = gridBuilder.getCellValue(rowIndex, "FactorNumber");
        var factorTypeName = gridBuilder.getCellValue(rowIndex, "DocumentTypeName");
        var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
        var usernameInserted = gridBuilder.getCellValue(rowIndex, "UserNameInsertDocument");
        window.parent.$('.iframe-list').hide();
        var url = 'ShopPrintList?code=' + factoreSerial + '&factorNumber=' + factoreNumber + '&factorTypeName=' + factorTypeName + '&dateAz=' + $('#txtAzDate').val() + '&dateTa=' + $('#txtTaDate').val() + '&insertUser=' + usernameInserted + "&systemcode=" + saleSystemCode
        //window.parent.$('.iframe-detail').attr('src', 'ShopPrintList?code=' + factoreSerial + '&factorNumber=' + factoreNumber + '&factorTypeName=' + factorTypeName + '&dateAz=' + $('#txtAzDate').val() + '&dateTa=' + $('#txtTaDate').val() + '&insertUser=' +usernameInserted + "&systemcode=" + saleSystemCode);
        window.parent.redirectUrl(url,"iframeDetail");
        window.parent.$('.iframe-detail').show();
    });
    gridBuilder.onRowChanged(function (rowIndex) {
        $("#btnSaleReturn").prop("disabled", gridBuilder.getCellValue(rowIndex, "FactorTypeParentCode") === "10");
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading: true,
        url: "SaleApi/GetSaleFactorList",
        data: {
            saleSystemCode: saleSystemCode,
            shopCashDeskCode: -1,
            fromDate: $("#txtAzDate").val(),
            toDate: $("#txtTaDate").val(),
            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 printDocument() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var year = gridBuilder.getCellValue(rowIndex, "Year");
    var title = window.top.printDocument;
    var url = "Report/Default?GrpName=An_SanadN&Inst=" + factoreSerial + "&SalMali=" + year;
    openInModal("", url, title,"","",false);
}

function returned() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var factorTypeParentCode = gridBuilder.getCellValue(rowIndex, "FactorTypeParentCode");
    openInTab('Sale/Shop?code=' + factoreSerial + '&ret=1&p=' + factorTypeParentCode + "&systemcode=" + saleSystemCode, window.top.returnSale, 'img/pages/menu/241.svg', true);
}



