var gridBuilder;
var gridBuilderArticles;
$(document).ready(function () {
    gridBuilder = $("#gridDocument").gridBuilder();
    gridBuilderArticles = $("#gridArtikles").gridBuilder();
    gridBuilder.onRowChanged(function (rowIndex) {
        
    });
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "DocumentSerial");
        $("#hfCode").val(code);
        toggleLevel();
        loadGrid("gridArtikles");
    });
    gridBuilderArticles.onRowChanged(function (rowIndex) {
        var nameMoain = gridBuilderArticles.getCellValue(rowIndex, "MoainName");
        var nameTf4 = gridBuilderArticles.getCellValue(rowIndex, "TafsilName");
        var nameMHazineh = gridBuilderArticles.getCellValue(rowIndex, "CostCenterName");
        var nameProject = gridBuilderArticles.getCellValue(rowIndex, "ProjectName");
        $('#lblDetail').text(nameMoain + (nameTf4.length > 0 ? " / " + nameTf4 : "") + (nameMHazineh.length > 0 ? " / " + nameMHazineh : "") + (nameProject.length > 0 ? " / " + nameProject : ""));
    });
    gridBuilder.onExportClick(function () {
        loadGrid("gridDocument", { exportExcel: true });
    });
    gridBuilderArticles.onExportClick(function () {
        loadGrid("gridArtikles", { exportExcel: true });
    });
    $("#cboFund").lookupBuilder().onChanged(async function () {
        loadGrid("gridDocument");
    });
    loadGrid("gridDocument");
    imgToSvg();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    if (gridId === "gridDocument") {
        var isConfirm = $("#ddlState").val() === "-1" ? null : ($("#ddlState").val() === "1" ? true : false);
        ajax({
            loading: false,
            gridBuilder: gridBuilder,
            url: "TreasuryApi/GetFundDocumentList",
            data: {
                fromDate: $("#txtFromDate").val(),
                toDate: $("#txtToDate").val(),
                fromDocumentSerial: $("#txtFromCode").val() || -1,
                toDocumentSerial: $("#txtToCode").val() || -1,
                funderCode: $("#cboFund").lookupBuilder().getSelectedValue() || null,
                isConfirm: isConfirm,
                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);
                }
            }
        });
    }
    else {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            return;
        }
        var documentCode = gridBuilder.getCellValue(rowIndex, "DocumentSerial");
        getFundArticleList(documentCode, isExportExcel);
    }
}
function getFundArticleList(documentCode, isExportExcel) {
    ajax({
        gridBuilder: gridBuilderArticles,
        url: "TreasuryApi/GetFundArticleList",
        data: {
            filter: gridBuilderArticles.getFilter(),
            sort: gridBuilderArticles.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilderArticles.getCurrentPageNumber(),
            code: $("#chkShowAllArticles").prop("checked") ? -1 : documentCode,
            selectionCriteria: gridBuilderArticles.selectionViewMode ? "'" + gridBuilderArticles.selectionList.join("','") + "'" : null
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilderArticles.getColumns(), gridBuilderArticles.getColumnsCaption());
            }
            else {
                data.forEach(function (item) {
                    item.RowKey = item.Code.toString();
                });
                gridBuilderArticles.loadData(data);
                $('#lblDetail').show();
            }
        }
    });
}
function toggleLevel() {
    $("[data-level-toggle]").toggle();
}
function showAllArticles(sender) {
    if ($(sender).prop("checked"))
        loadGrid("gridArtikles");
    else
        gridBuilder.dblClick();
}
async function getEventLookupCombo($combo, search, code) {
    await ajax({
        url: "TreasuryApi/GetFunderList",
        data: { search: search, pageNo: 1, selectedCode: code },
        success: function (result) {
            result.forEach(function (item) {
                item.Name = item.CustomerName;
            });
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function setAccountingInfo() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    var code = gridBuilder.getCellValue(rowIndex, "DocumentSerial");
    var state = gridBuilder.getCellValue(rowIndex, "IsConfirm").toString().toLowerCase() === "true";
    if (!state) {
        showMsgModal(0, window.top.fundNotAccount);
        return;
    }
    var src = "Treasury/FundDocumentSave?code=" + code + "&accounting=1";
    openInTab(src, window.top.fundAccountDefine, 'img/pages/menu/121.svg', true);
}
function insertAccFundDocument() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var code = gridBuilder.getCellValue(rowIndex, "DocumentSerial");
    var accCode = gridBuilder.getCellValue(rowIndex, "AccDocumentCode");
    var state = gridBuilder.getCellValue(rowIndex, "IsConfirm").toString().toLowerCase() === "true";
    if (rowIndex < 0) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    if (!state) {
        showMsgModal(0, window.top.fundNotDocument);
        return;
    }
    if (accCode) {
        showMsgModal(0, window.top.hasFundDocument);
        return;
    }
    ajax({
        loading: true,
        url: "TreasuryApi/SaveFundAccDocument",
        data: { code: code },
        success: function (retValue) {
            showMsgModal(2, retValue.FinalMessage.join("<br />"));
            loadGrid("gridDocument");
        }
    });
}
function deleteAccFundDocument() {
    var selected = getSelectedCodes();
    if (selected.DocumentSerial.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            loading: true,
            url: "TreasuryApi/DeleteAccFundDocument",
            data: { codeList: selected.DocumentSerial },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid("gridDocument");
            }
        });
    }
}
function deleteFundDocument() {
    var selected = getSelectedCodes();
    if (selected.DocumentSerial.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        if (selected.AccDocumentCode.some(x => x)) {
            showMsgModal(0, window.top.fundDocNotDelete);
            return;
        }
        if (selected.IsConfirm.some(x => x.toString() === "true")) {
            showMsgModal(0, window.top.fundNotDelete);
            return;
        }
        ajax({
            loading: true,
            url: "TreasuryApi/DeleteFundDocument",
            data: { codeList: selected.DocumentSerial },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid("gridDocument");
            }
        });
    }
}
function getSelectedCodes() {
    var selectedCodes = gridBuilder.getSelectedRowsCellValue("DocumentSerial");
    var selectedAccCodes = gridBuilder.getSelectedRowsCellValue("AccDocumentCode");
    var selectedState = gridBuilder.getSelectedRowsCellValue("IsConfirm");
    if (selectedCodes.length === 0) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex >= 0) {
            selectedCodes.push(gridBuilder.getCellValue(rowIndex, "DocumentSerial"));
            selectedAccCodes.push(gridBuilder.getCellValue(rowIndex, "AccDocumentCode"));
            selectedState.push(gridBuilder.getCellValue(rowIndex, "IsConfirm"));
        }
    }
    return { DocumentSerial: selectedCodes, AccDocumentCode: selectedAccCodes, IsConfirm: selectedState };
}
function openPrint(reportName, value) {
    var title = window.top.printDocument;
    var src = "Report/Default?" + (value ? "Codesanad=" + value + "&" : "") + "GrpName=" + reportName;
    openInModal('', src, title, "", "", false);
}
function openPrintDetail() {
    openPrint("Trs_Tankhah_Sanad", $('#hfCode').val());
}