var gridBuilder;
var gridArtiklesBuilder;
var level = 1;
$(document).ready(function () {
    gridBuilder = $("#grdSanadList").gridBuilder();
    gridArtiklesBuilder = $("#grdRizSanad").gridBuilder();

    $("#ddlState").on("change", function () {
        loadGrid("grdSanadList");
    });
    gridBuilder.onRowChanged(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex === -1) {
            return;
        }
        var isConfirm = gridBuilder.getCellValue(rowIndex, "IsConfirm")==="true";
        $("#btnState").val(isConfirm ? window.top.returnConfirmation : window.top.confirmation)
    });
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            return;
        }
        $('#txtAzDate').prop("disabled", true);
        $('#txtTaDate').prop("disabled", true);
        $('#ddlState').prop("disabled", true);
        $("#level1").hide();
        $("#level2").show();
        loadGrid("grdRizSanad");
    });
    gridBuilder.onExportClick(function () {
        loadGrid("grdSanadList", { exportExcel: true });
    });
    gridArtiklesBuilder.onRowChanged(function () {
        var rowIndex = gridArtiklesBuilder.getActiveRowIndex();
        if (rowIndex === -1)
            return;
        
        var nameMoain = gridArtiklesBuilder.getCellValue(rowIndex, "MoainName");
        var nemaeTafsil = gridArtiklesBuilder.getCellValue(rowIndex, "TafsilName");
        var mhname = gridArtiklesBuilder.getCellValue(rowIndex, "CostCenterName");
        var projectName = gridArtiklesBuilder.getCellValue(rowIndex, "ProjectName");
        $('#lblDetail').text(nameMoain + (nemaeTafsil.length > 0 ? " / " + nemaeTafsil : "") + (mhname.length > 0 ? " / " + mhname : "") + (projectName.length > 0 ? " / " + projectName : ""));

    });
    gridArtiklesBuilder.onExportClick(function () {
        loadGrid("grdRizSanad", { exportExcel: true });
    });
    loadGrid("grdSanadList");
    imgToSvg();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    if (gridId === "grdSanadList") {
        ajax({
            url: "AccountingApi/GetReferenceDocumentList",
            gridBuilder: gridBuilder,
            data: {
                fromDate: $("#txtAzDate").val(),
                toDate: $("#txtTaDate").val(),
                confirmState: Number($("#ddlState").val()) === -1 ? null : Number($("#ddlState").val()),
                filter: gridBuilder.getFilter(),
                sort: gridBuilder.getSortFields(),
                pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
                selectionCriteria: gridBuilder.selectionViewMode ? "'" + gridBuilder.selectionList.join("','") + "'" : null
            },
            success: function (data) {
                if (isExportExcel) {
                    exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
                }
                else {
                    gridBuilder.loadData(data);
                }
            }
        });
    }
    else {
        var rowIndex = gridBuilder.getActiveRowIndex();
        var documentCode = gridBuilder.getCellValue(rowIndex, "Code");
        ajax({
            loading: true,
            url: "AccountingApi/GetReferenceDocumentDetailList",
            data: {
                code: documentCode,
                filter: gridArtiklesBuilder.getFilter(),
                sort: gridArtiklesBuilder.getSortFields(),
                pageNo: isExportExcel ? -1 : gridArtiklesBuilder.getCurrentPageNumber()
            },
            success: function (data) {
                if (isExportExcel) {
                    exportExcel(data, gridArtiklesBuilder.getColumns(), gridArtiklesBuilder.getColumnsCaption());
                }
                else {
                    gridArtiklesBuilder.loadData(data);
                }
            }
        });
    }
}
function showDocumentList() {
    $('#txtAzDate').prop("disabled", false);
    $('#txtTaDate').prop("disabled", false);
    $('#ddlState').prop("disabled", false);
    $("#level1").show();
    $("#level2").hide();

}

function deleteDocument() {
    var selected = getSelectedCodes();
    if (selected.codes.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        if (selected.isConfirms.some(x => x.toString().toLowerCase() === "true")) {
            showMsgModal(0, window.top.referenceNotDelete);
            return;
        }
        ajax({
            loading: true,
            url: "AccountingApi/DeleteReferenceDocument",
            data: { codes: selected.codes },
            success: function (retValue) {
                showMsgModal(2, retValue.FinalMessage);
                gridBuilder.changeSelection(-1, false);
                loadGrid("grdSanadList");
            }
        });
    }
}
function newDocument() {
    var title = window.top.referenceDocument;
    openInTab('Accounting/ReferenceDocumentSave', title, '', true);
}
function confirmDocument() {
    var selected = getSelectedCodes();
    if (selected.codes.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (!checkAllRecordType(selected.isConfirms)) {
        showMsgModal(0, window.top.sameTypeDocument);
        return;
    }
    if (confirm(window.top.changeStatus)) {
        ajax({
            loading: true,
            url: "AccountingApi/ConfirmReferenceDocument",
            data: { codes: selected.codes, state: !(selected.isConfirms[0].toLowerCase() === "true") },
            success: function (retValue) {
                showMsgModal(2, retValue.FinalMessage);
                gridBuilder.changeSelection(-1, false);
                loadGrid("grdSanadList");
            }
        });
    }
}
function printDocument() {
    var selected = getSelectedCodes();
    if (selected.codes.length === 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var title = window.top.printDocument;
    var src = "Report/Default?GrpName=SanadMarjaGheyasPlus&Codesanad=" + selected.codes.join(",");
    openInModal('', src, title,"","",false);
}
function getSelectedCodes() {
    var selectedCodes = gridBuilder.getSelectedRowsCellValue("Code");
    var selectedIsConfirm = gridBuilder.getSelectedRowsCellValue("IsConfirm");
    if (selectedCodes.length === 0) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex >= 0) {
            selectedCodes.push(gridBuilder.getCellValue(rowIndex, "Code"));
            selectedIsConfirm.push(gridBuilder.getCellValue(rowIndex, "IsConfirm"));
        }
    }
    return { codes: selectedCodes, isConfirms: selectedIsConfirm };
}
function checkAllRecordType(states) {
    var type = states[0];
    var result = true;
    for (var i = 0; i < states.length; i++) {
        if (states[i] !== type) {
            result = false;
        }
    }
    return result;
}
