var gridBuilder;
var type = getQuery("type");
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        openSanad();
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter ) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        url: "AccountingApi/GetUserDocumentList",
        gridBuilder: gridBuilder,
        data: {
            fromDocumentCode: $("#txtAzCode").val() ? $("#txtAzCode").val() : null,
            toDocumentCode: $("#txtTaCode").val() ? $("#txtTaCode").val() : null,
            fromDate: $("#txtAzDate").val(),
            toDate: $("#txtTaDate").val(),
            accDocumentTypeCode: type || null,
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                data.forEach(function (item) {
                    if (item.AttcCount > 0) {
                        item.AttcCount = baseUrl + "img/attach1.png";
                    }
                });
                gridBuilder.loadData(data);
            }
        }
    });
}
function newSanad() {
    openInTab("Accounting/InvoiceSave?type=" + (type === "2" ? "start" : ""), window.top.documentDefinition, "img/pages/menu/121.svg", true);
}
function refresh() {
    loadGrid();
}
function setCodeSanad() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var saderKonandeh = gridBuilder.getCellValue(rowIndex, "ExporterUserName");
    var codesanadState = gridBuilder.getCellValue(rowIndex, "DocumentStateCode");
    ajax({
        loading: true,
        url: "GeneralApi/LockDocument",
        data: { systemName: "THS_SanadWeb", code: code },
        success: function (retValue) {
            var url = "Accounting/InvoiceSave?CodeSanad=" + code + "&UserName=" + saderKonandeh + "&CodeSanadState=" + (codesanadState == 1 ? 1 : 2);
            openInTab(url, window.top.accountDocument, "img/pages/menu/121.svg", true);
        }
    });
}
function openSanad() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex === -1) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    setCodeSanad();
}
$(document).click(function () {
    window.top.closeLeftSideMenu();
});
function printSanad() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex == -1)
        return;
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var salMali = gridBuilder.getCellValue(rowIndex, "Year");
    var title = window.top.printDocument;
    var src = "Report/Default?GrpName=SanadGheyasPlus&Inst=" + code + '&Salmali=' + salMali;
    openInModal('', src, title,"","",false);
}
function attachFile() {
    var title = window.top.accountingAttach;
    openInModal("", "Accounting/AttachFile", title, "600", "545");
}