var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    var caption = getQuery("type") == "out" ? window.top.exitList : window.top.interList;
    gridBuilder.setCaption(caption);
    gridBuilder.onDblClick(function() {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "FactorSerial");
        var factorTypeParentCode = gridBuilder.getCellValue(rowIndex, "FactorTypeParentCode");
        if (Number(factorTypeParentCode) === 15)
            alert(window.top.couldntEditTransferDocuments);
        else
        openModal(code);
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    closeLoading();
    loadGrid();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "SaleApi/GetStoreFactorList",
        data: {
            documentMode: getQuery("type") === "out" ? 6 : 5,
            mySelf:true,
            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 refresh() {
    loadGrid();
}

function openModal(code,isCopy) {
    showLoading();
    var title = '', icon = '';
    switch (getQuery('type')) {
        case 'out':
            title = window.top.exitRequest;
            icon = '622';
            break;
        default:
            title = window.top.interRequest;
            icon = '621';
            break;
    }

    if (code === -1) {
        var url = "Store/InOutSave?type=" + getQuery('type');
        openInTab(url, title, 'img/pages/menu/' + icon + '.svg', false);
        return;
    }
    ajax({
        url: "SaleApi/CheckLock",
        data: { code: code },
        success: function (retValue) {
            if (retValue != '') {
                eval(retValue);
            }
            else {
                var rowIndex = gridBuilder.getActiveRowIndex();
                var baseFactorSerial = gridBuilder.getCellValue(rowIndex, "BaseDocumentCode");
                var baseRequire = gridBuilder.getCellValue(rowIndex, "BaseRequire") === "true";
                if (baseRequire && isCopy) {
                    showMsgModal(0, window.top.cantCopyFactor);
                    return;
                }


                var url = "Store/InOutSave?Code=" + code + "&type=" + getQuery('type');
                openInTab(url, title, 'img/pages/menu/' + icon + '.svg', false);
            }
        }
    });
}

function printSanad() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex == -1)
        return;
    var fC_Code = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var salMali = gridBuilder.getCellValue(rowIndex, "Year");
    var title = window.top.printDocument;
    var url = "Report/Default?GrpName=An_Sanad_Tedadi&codesanad=" + fC_Code + "&SalMali=" + salMali;
    openInModal('', url, title,"","",false);
}

$(document).click(function () {
    window.top.closeLeftSideMenu();
});

function edit() {
    gridBuilder.dblClick();
}
function copyFactor() {
    var data = getData();
    if (data)
        checkLock(data, true);
}
function getData() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factorSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var factorState = gridBuilder.getCellValue(rowIndex, "FactorState");
    var saleSystemParentCode = gridBuilder.getCellValue(rowIndex, "SaleSystemParentCode");
    var saleSystemCode = gridBuilder.getCellValue(rowIndex, "SaleSystemCode");
    return {
        factorSerial: factorSerial,
        factorState: factorState,
        saleSystemParentCode: saleSystemParentCode,
        saleSystemCode: saleSystemCode
    };
}