/**
 * Created by Tony on 2019/4/10.
 */

let G_OFFSET_X = 0, G_OFFSET_Y = 0;
function loading() {
    if (sessionStorage.multiRptsData) {
        let multiRptData = JSON.parse(sessionStorage.multiRptsData);
        let scaleFactor = parseInt(sessionStorage.scaleFactor);
        for (let idx = 0; idx < multiRptData.length; idx++) {
            let pageData = multiRptData[idx];
            if (idx === 0) {
                $(document).attr("title", pageData[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
            }
            let orgPixelSize = getPixelSize(pageData);
            let actArea = getActualArea(pageData);
            let svgArr = rptPrintHelper.buildSvgArr(pageData, actArea, G_OFFSET_X, G_OFFSET_Y);
            //let orientation = (pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][0] < pageData[JV.NODE_PAGE_INFO][JV.NODE_PAGE_SIZE][1])?"纵向":"横向";
            let orientation = "纵向";
            showPreviewData(svgArr, actArea, scaleFactor, sessionStorage.pageSize, orientation, orgPixelSize);
        }
        window.print();
    } else if (sessionStorage.currentPageData) {
        let pageData = JSON.parse(sessionStorage.currentPageData);
        let scaleFactor = parseInt(sessionStorage.scaleFactor);
        let orgPixelSize = getPixelSize(pageData);
        let actArea = getActualArea(pageData);
        let svgArr = rptPrintHelper.buildSvgArr(pageData, actArea, G_OFFSET_X, G_OFFSET_Y);
        $(document).attr("title", pageData[JV.NODE_PAGE_INFO][JV.NODE_MAIN_INFO_RPT_NAME]);
        //showPreviewData(svgArr, scaleFactor, sessionStorage.pageSize, sessionStorage.orientation);
        showPreviewData(svgArr, actArea, scaleFactor, sessionStorage.pageSize, "纵向", orgPixelSize);
        window.print();
    } else if (sessionStorage.currentPageSvgData) {
        let svgArr = JSON.parse(sessionStorage.currentPageSvgData);
        let scaleFactor = 1;
        //showPreviewData(svgArr, scaleFactor, sessionStorage.pageSize, sessionStorage.orientation);
        showPreviewData(svgArr, null, scaleFactor, sessionStorage.pageSize, "纵向", null);
        window.print();
    } else {
        //alert("没有报表数据！");
    }
}

function showPreviewData(svgArr, actAreaArr, scaleFactor, pageSize, orientation, orgPixelSize) {
    let orgHeight = 793, orgWidth = 1122;
    let DFT_MARGIN = 5;
    if (pageSize === 'A3') {
        orgHeight = 1122;
        orgWidth = 793 * 2;
    } else if (pageSize.indexOf('自定义') >= 0) {
        //自定义
    } else {
        //其他size(LEGAL, 16K etc.)
    }
    let pageHeight = orgHeight * scaleFactor, pageWidth = orgWidth * scaleFactor;
    if (orientation === "纵向") {
        pageHeight = orgWidth * scaleFactor;
        pageWidth = orgHeight * scaleFactor;
    }

    for (let i = 0; i < svgArr.length; i++) {
        let offsetHeight = 0, offsetWidth = 0;
        if (actAreaArr) {
            offsetWidth = actAreaArr[i].Left + (pageWidth - actAreaArr[i].Right) - DFT_MARGIN;
            offsetHeight = actAreaArr[i].Top + (pageHeight - actAreaArr[i].Bottom) - DFT_MARGIN;
            if (orgPixelSize[0] > orgPixelSize[1]) {
                //横向强制改纵向(系统是以纵向为准)，那么计算offset的方式会有所不同
                offsetWidth = actAreaArr[i].Top + (pageWidth - actAreaArr[i].Bottom) - DFT_MARGIN;
                //横向转纵向时，还得考虑左上角转右上角的Left与Top之间的差，否则坐标还是会有偏差
                offsetHeight = actAreaArr[i].Left + (pageHeight - actAreaArr[i].Right) - (actAreaArr[i].Left - actAreaArr[i].Top) - DFT_MARGIN;
            }
        }
        let div = $('<div class="pageBreak"></div>');
        div.append($(svgArr[i].join("")));
        $(div).find("svg").each(function(cIdx,elementSvg){
            elementSvg.setAttribute('height', pageHeight - offsetHeight);
            elementSvg.setAttribute('width', pageWidth - offsetWidth);
        });
        $("body").append(div);
    }
}

function getActualArea(pageData) {
    let rst = [];
    for (let item of pageData.items) {
        let area = {Left: 10000, Right: 0, Top: 10000, Bottom: 0};
        for (let cell of item.cells) {
            if (cell.area.Left < area.Left) {
                area.Left = cell.area.Left;
            }
            if (cell.area.Right > area.Right) {
                area.Right = cell.area.Right;
            }
            if (cell.area.Top < area.Top) {
                area.Top = cell.area.Top;
            }
            if (cell.area.Bottom > area.Bottom) {
                area.Bottom = cell.area.Bottom;
            }
        }
        rst.push(area);
    }
    return rst;
}
function closing() {
    //
}

