let JV = require('./jpc_value_define');
let JE = require('./jpc_rte');
let JpcFieldHelper = require('./helper/jpc_helper_field');
let JpcBandHelper = require('./helper/jpc_helper_band');
let JpcBand = require('./jpc_band');
let JpcCrossTabHelper = require('./helper/jpc_helper_cross_tab');
let JpcCommonHelper = require('./helper/jpc_helper_common');
let JpcDiscreteHelper = require('./helper/jpc_helper_discrete');
let JpcTextHelper = require('./helper/jpc_helper_text');
let JpcCommonOutputHelper = require('./helper/jpc_helper_common_output');
let JpcAreaHelper = require('./helper/jpc_helper_area');

let JpcCrossTabSrv = function(){};
JpcCrossTabSrv.prototype.createNew = function(){
    function private_addTabValue(tabValuedIdxLst, sortedSequence, segIdx, preRec, nextRec, dispSerialIdxLst, sorted_sum_value_Lst, rst_sum_value_Lst) {
        if (tabValuedIdxLst) {
            let serial1stTier = null;
            if (dispSerialIdxLst) serial1stTier = [];
            let pgseg1stTier = [];
            let sumVal = [];
            let sumValL = 1;
            if (sortedSequence) {
                let arrDupVals = sortedSequence[segIdx];
                let arrDupSumVals = null;
                if (sorted_sum_value_Lst !== null) {
                    arrDupSumVals = sorted_sum_value_Lst[segIdx];
                    sumValL = arrDupSumVals[0].length;
                }

                for (let i = 0; i < nextRec; i++) {
                    if (arrDupVals.length <= preRec + i) {
                        pgseg1stTier[i] = JV.BLANK_VALUE_INDEX;
                        sumVal[i] = [];
                        for (let ei = 0; ei < sumValL; ei++) {
                            sumVal[i][ei] = null;
                        }
                        if (serial1stTier !== null) {
                            serial1stTier[i] = JV.BLANK_VALUE_INDEX;
                        }
                        continue;
                    }
                    let duplicateValueArr = arrDupVals[preRec + i];
                    pgseg1stTier[i] = duplicateValueArr[0];
                    if (arrDupSumVals !== null) sumVal[i] = arrDupSumVals[preRec + i];

                    if (serial1stTier !== null) {
                        serial1stTier[i] = preRec + i;
                    }
                }
                tabValuedIdxLst.push(pgseg1stTier);
                if (dispSerialIdxLst !== null) {
                    dispSerialIdxLst.push(serial1stTier);
                }
                if (sorted_sum_value_Lst !== null && rst_sum_value_Lst !== null) {
                    rst_sum_value_Lst.push(sumVal);
                }
            } else {
                //should push blank value index rather than null
                for (let i = 0; i < nextRec; i++) {
                    pgseg1stTier[i] = JV.BLANK_VALUE_INDEX;
                    sumVal[i] = null;
                    if (serial1stTier !== null) {
                        serial1stTier[i] = JV.BLANK_VALUE_INDEX;
                    }
                }
                tabValuedIdxLst.push(pgseg1stTier);
                if (dispSerialIdxLst !== null) {
                    dispSerialIdxLst.push(serial1stTier);
                }
                if (sorted_sum_value_Lst !== null && rst_sum_value_Lst !== null) {
                    rst_sum_value_Lst.push(sumVal);
                }
            }
        }
    }
    function private_addContentValue(dispValueIdxLst_Content, sortedContentSequence, segIdx, counterRowRec, maxRowRec, counterColRec, maxColRec, page_seg_map, pageIdx) {
        if (dispValueIdxLst_Content !== null) {
            page_seg_map.push([pageIdx,segIdx]);
            let arrContents = [];
            if (sortedContentSequence !== null) {
                let arrAllContent = sortedContentSequence[segIdx];
                for (let i = 0; i < maxRowRec; i++) {
                    arrContents.push([]);
                    for (let j = 0; j < maxColRec; j++) {
                        if (arrAllContent.length <= counterRowRec + i || arrAllContent[counterRowRec + i].length <= counterColRec + j) {
                            arrContents[i][j] = JV.BLANK_VALUE_INDEX;
                        } else {
                            arrContents[i][j] = arrAllContent[counterRowRec + i][counterColRec + j];
                        }
                    }
                }
                dispValueIdxLst_Content.push(arrContents);
            } else {
                //should push blank value index rather than null
                for (let i = 0; i < maxRowRec; i++) {
                    arrContents.push([]);
                    for (let j = 0; j < maxColRec; j++) {
                        arrContents[i][j] = JV.BLANK_VALUE_INDEX;
                    }
                }
                dispValueIdxLst_Content.push(arrContents);
            }
        }
    }
    function private_SortAndOptimize(rptTpl, dataObj, dataSeq, sortTab, rstFieldsIdx, $CURRENT_RPT) {
        let result = [];
        let tab = rptTpl[JV.NODE_CROSS_INFO][sortTab];
        if (tab) {
            let sIDX = 0;
            //1. prepare and sort by tab-field
            let fields = [];
            JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, tab[JV.PROP_CROSS_FIELDS], fields, rstFieldsIdx);
            let data_details = dataObj[JV.DATA_DETAIL_DATA];
            JpcCrossTabHelper.sortTabFields(fields, rstFieldsIdx, data_details, dataSeq, $CURRENT_RPT);
            //2. distinguish sort tab fields value
            let b1 = false;
            for (let i = 0; i < dataSeq.length; i++) {
                sIDX = 0;
                let segArr = [];
                if (dataSeq[i].length === 1) {
                    JpcCrossTabHelper.pushToSeg(segArr, dataSeq, i, 0, 1);
                } else {
                    for (let j = 1; j < dataSeq[i].length; j++) {
                        b1 = false;
                        for (let k = 0; k < rstFieldsIdx.length; k++) {
                            if (fields[k].hasOwnProperty(JV.TAB_FIELD_PROP_SORT)) {
                                //只有被选择了作为排序字段的才进入排序及优化
                                if (typeof(rstFieldsIdx[k]) === 'object') {
                                    let map_data_field = JE.F(rstFieldsIdx[k][JV.PROP_ID], $CURRENT_RPT);
                                    if (map_data_field[JV.PROP_AD_HOC_DATA][dataSeq[i][j - 1]] !== map_data_field[JV.PROP_AD_HOC_DATA][dataSeq[i][j]]) {
                                        b1 = true;
                                        break;
                                    }
                                } else {
                                    if (data_details[rstFieldsIdx[k]][dataSeq[i][j - 1]] !== data_details[rstFieldsIdx[k]][dataSeq[i][j]]) {
                                        b1 = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if (b1) {
                            JpcCrossTabHelper.pushToSeg(segArr, dataSeq, i, sIDX, j);
                            sIDX = j;
                            if (j === dataSeq[i].length - 1) {
                                JpcCrossTabHelper.pushToSeg(segArr, dataSeq, i, j, dataSeq[i].length);
                            }
                        } else if (j === dataSeq[i].length - 1) {
                            JpcCrossTabHelper.pushToSeg(segArr, dataSeq, i, sIDX, dataSeq[i].length);
                        }
                    }
                }
                if (segArr.length > 0) result.push(segArr);
            }
        }
        return result;
    }
    function private_SortForDisplayContent(rptTpl, rowSeq, colSeq, rstFieldsIdx){
        let result = [];
        let tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT];
        if (tab) {
            JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, tab[JV.PROP_CROSS_FIELDS], null, rstFieldsIdx);
        }
        for (let i = 0; i < rowSeq.length; i++) {
            let rl = rowSeq[i], cl = colSeq[i];
            let ds = [];
            //1. initialize to blank value index
            for (let j = 0; j < rl.length; j++) {
                ds.push([]);
                for (let k = 0; k < cl.length; k++) {
                    ds[j].push(JV.BLANK_VALUE_INDEX);
                }
            }
            //2. then fill up the right index
            for (let j = 0; j < rl.length; j++) {
                let ra = rl[j];
                for (let k = 0; k < ra.length; k++) {
                    let colIdx = JpcCrossTabHelper.getColIDX(cl, ra[k]);
                    if (colIdx >= 0) {
                        ds[j][colIdx] = ra[k];
                    }
                }
            }
            result.push(ds);
        }
        return result;
    }

    let JpcCrossTabResult = {};
    JpcCrossTabResult.initialize = function() {
        let me = this;
        me.dispValueIdxLst_Row = [];
        me.dispValueIdxLst_Col = [];
        me.dispValueIdxLst_Content = [];
        me.dispSerialIdxLst_Row = [];
        me.col_sum_fields_idx = [];
        me.col_sum_fields_value_total = [];
        me.dispSumValueLst_Col = [];
        me.page_seg_map = [];
        me.row_fields_idx = [];
        me.row_fields_adhoc_idx = [];
        me.col_fields_idx = [];
        me.content_fields_idx = [];
        me.row_extension_fields_idx = [];
        me.row_sum_extension_fields_idx = [];
        me.crsOrient = JV.PAGE_ORIENTATION_V_FIRST;
        me.pageStatusLst = [];
        me.paging_option = JV.PAGING_OPTION_NORMAL;
    };
    JpcCrossTabResult.sorting = function(rptTpl, dataObj, dataSeq, $CURRENT_RPT) {
        let me = this;
        //IMPORTANT: the data should be sorted in SQL/NoSQL level!
        me.sortedRowSequence = private_SortAndOptimize(rptTpl, dataObj, dataSeq, JV.NODE_CROSS_ROW, me.row_fields_idx, $CURRENT_RPT);
        private_SortAndOptimize(rptTpl, dataObj, dataSeq, JV.NODE_CROSS_ROW_AD_HOC, me.row_fields_adhoc_idx, $CURRENT_RPT);
        me.sortedColSequence = private_SortAndOptimize(rptTpl, dataObj, dataSeq, JV.NODE_CROSS_COL, me.col_fields_idx, $CURRENT_RPT);
        me.sortedContentSequence = private_SortForDisplayContent(rptTpl, me.sortedRowSequence, me.sortedColSequence, me.content_fields_idx);
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL_SUM][JV.PROP_CROSS_FIELDS], null, me.col_sum_fields_idx);
        //pre-sum the data(for col sum display)
        let data_details = dataObj[JV.DATA_DETAIL_DATA],
            data_fields = [];
        for (let i = 0; i < me.col_sum_fields_idx.length; i++) {
            let data_field = null;
            if (typeof me.col_sum_fields_idx[i] === 'object') {
                let exField = JE.F(me.col_sum_fields_idx[i][JV.PROP_ID], $CURRENT_RPT);
                if (exField) {
                    data_field = exField[JV.PROP_AD_HOC_DATA];
                }
            } else {
                data_field = data_details[me.col_sum_fields_idx[i]];
            }
            data_fields.push(data_field);
        }
        for (let i = 0; i < me.sortedRowSequence.length; i++) { //seg level
            if (me.sortedRowSequence[i].length > 0) {
                me.col_sum_fields_value_total.push([]);
                for (let j = 0; j < me.sortedRowSequence[i].length; j++) {
                    let rowGrandTotal = [];
                    for (let di = 0; di < data_fields.length; di++) {
                        rowGrandTotal.push(0.0);
                        for (let k = 0; k < me.sortedRowSequence[i][j].length; k++) {
                            //3. start to sum
                            rowGrandTotal[di] = rowGrandTotal[di] + 1.0 * JpcFieldHelper.getValue(data_fields[di], me.sortedRowSequence[i][j][k]);
                        }
                    }
                    me.col_sum_fields_value_total[i].push(rowGrandTotal);
                }
            }

        }
    };
    JpcCrossTabResult.preSetupPages = function(rptTpl, defProperties, option) {
        let me = this, rst = 0;
        me.paging_option = option||JV.PAGING_OPTION_NORMAL;
        //1. original initialize
        let maxRowRec = 1, maxColRec = 1, counterRowRec = 0, counterColRec = 0, pageIdx = 0, segCnt = me.sortedContentSequence.length;
        let pageStatus = [true, true, false, true, false, false, false, false];
        //2. calculate the page info one by one
        let bands = JpcBand.createNew(rptTpl, defProperties);
        function private_resetBandArea() {
            JpcBandHelper.setBandArea(bands, rptTpl, pageStatus);
            maxRowRec = JpcCrossTabHelper.getMaxRowsPerPage(bands, rptTpl);
            maxColRec = JpcCrossTabHelper.getMaxColsPerPage(bands, rptTpl);
        }
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT][JV.PROP_CROSS_FIELDS], null, me.row_extension_fields_idx);
        JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT][JV.PROP_CROSS_FIELDS], null, me.row_sum_extension_fields_idx);
        if (me.paging_option === JV.PAGING_OPTION_INFINITY) {
            /*
             rst = segCnt;
             pageStatus[JV.STATUS_SEGMENT_START] = true;
             pageStatus[JV.STATUS_SEGMENT_END] = true;
             pageStatus[JV.STATUS_CROSS_ROW_END] = true;
             pageStatus[JV.STATUS_CROSS_COL_END] = true;
             for (let segIdx = 0; segIdx < segCnt; segIdx++) {
             if (segIdx === segCnt - 1) {
             pageStatus[JV.STATUS_REPORT_END] = true;
             }
             if (segIdx > 0) {
             pageStatus[JV.STATUS_REPORT_START] = false;
             }
             me.pageStatusLst.push(pageStatus.slice(0));
             pageIdx++;
             private_addTabValue(me.dispValueIdxLst_Row, me.sortedRowSequence, segIdx, 0, me.sortedRowSequence[segIdx].length, me.dispSerialIdxLst_Row, me.col_sum_fields_value_total, me.dispSumValueLst_Col);
             private_addTabValue(me.dispValueIdxLst_Col, me.sortedColSequence, segIdx, 0, me.sortedColSequence[segIdx].length, null, null, null);
             private_addContentValue(me.dispValueIdxLst_Content, me.sortedContentSequence, segIdx, 0, me.sortedRowSequence[segIdx].length, 0, me.sortedColSequence[segIdx].length, me.page_seg_map, pageIdx);
             }
             //*/
        } else {
            for (let segIdx = 0; segIdx < segCnt; segIdx++) {
                //2.1. seg level initialize
                private_resetBandArea();
                let orgMaxRowRec = maxRowRec, orgMaxColRec = maxColRec;
                let rowSplitCnt = Math.ceil(1.0 * me.sortedRowSequence[segIdx].length / maxRowRec);
                let colSplitCnt = Math.ceil(1.0 * me.sortedColSequence[segIdx].length / maxColRec);
                pageStatus[JV.STATUS_CROSS_ROW_END] = true;
                private_resetBandArea();
                let hasAdHocRow = !JpcCrossTabHelper.chkTabEnd(JV.NODE_CROSS_ROW_SUM, rptTpl, bands, me.sortedRowSequence, segIdx, (rowSplitCnt - 1) * orgMaxRowRec, maxRowRec);
                if (hasAdHocRow) {
                    hasAdHocRow = !JpcCrossTabHelper.chkTabEnd(JV.NODE_CROSS_ROW_EXT, rptTpl, bands, me.sortedRowSequence, segIdx, (rowSplitCnt - 1) * orgMaxRowRec, maxRowRec);
                }
                pageStatus[JV.STATUS_CROSS_ROW_END] = false;
                pageStatus[JV.STATUS_CROSS_COL_END] = true;
                private_resetBandArea();
                let hasAdHocCol = !JpcCrossTabHelper.chkTabEnd(JV.NODE_CROSS_COL_SUM, rptTpl, bands, me.sortedColSequence, segIdx, (colSplitCnt - 1) * orgMaxColRec, maxColRec);
                pageStatus[JV.STATUS_CROSS_COL_END] = false;
                private_resetBandArea();
                if (hasAdHocRow) rowSplitCnt++;
                if (hasAdHocCol) colSplitCnt++;
                //2.2
                if (rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_CROSS_DISPLAY_ORDER] === JV.PAGE_ORIENTATION_H_FIRST) {
                    for (let rowIdx = 0; rowIdx < rowSplitCnt; rowIdx++) {
                        pageStatus[JV.STATUS_CROSS_ROW_END] = (rowIdx === (rowSplitCnt - 1));
                        private_resetBandArea();
                        counterRowRec = orgMaxRowRec * rowIdx;
                        let currentSortedRowSequence = me.sortedRowSequence;
                        let currentSortedContentSequence = me.sortedContentSequence;
                        if (hasAdHocRow && rowIdx === (rowSplitCnt - 1)) {
                            currentSortedRowSequence = null;
                            currentSortedContentSequence = null;
                            counterRowRec = 0;
                        }
                        for (let colIdx = 0; colIdx < colSplitCnt; colIdx++) {
                            pageStatus[JV.STATUS_CROSS_COL_END] = (colIdx === (colSplitCnt - 1));
                            private_resetBandArea();
                            counterColRec = orgMaxColRec * colIdx;
                            let currentSortedColSequence = me.sortedColSequence;
                            if (hasAdHocCol && colIdx === (colSplitCnt - 1)) {
                                currentSortedColSequence = null;
                                currentSortedContentSequence = null;
                                counterColRec = 0;
                            }
                            me.pageStatusLst.push(pageStatus.slice(0));
                            pageIdx++;
                            private_addTabValue(me.dispValueIdxLst_Row, currentSortedRowSequence, segIdx, counterRowRec, maxRowRec, me.dispSerialIdxLst_Row, me.col_sum_fields_value_total, me.dispSumValueLst_Col);
                            private_addTabValue(me.dispValueIdxLst_Col, currentSortedColSequence, segIdx, counterColRec, maxColRec, null, null, null);
                            private_addContentValue(me.dispValueIdxLst_Content, currentSortedContentSequence, segIdx, counterRowRec, maxRowRec, counterColRec, maxColRec, me.page_seg_map, pageIdx);
                        }
                    }
                } else {
                    for (let colIdx = 0; colIdx < colSplitCnt; colIdx++) {
                        pageStatus[JV.STATUS_CROSS_COL_END] = (colIdx === (colSplitCnt - 1));
                        private_resetBandArea();
                        counterColRec = orgMaxColRec * colIdx;
                        let currentSortedContentSequence = me.sortedContentSequence;
                        let currentSortedColSequence = me.sortedColSequence;
                        if (hasAdHocCol && colIdx === (colSplitCnt - 1)) {
                            currentSortedColSequence = null;
                            currentSortedContentSequence = null;
                            counterColRec = 0;
                        }
                        for (let rowIdx = 0; rowIdx < rowSplitCnt; rowIdx++) {
                            pageStatus[JV.STATUS_CROSS_ROW_END] = (rowIdx === (rowSplitCnt - 1));
                            private_resetBandArea();
                            me.pageStatusLst.push(pageStatus.slice(0));
                            pageIdx++;
                            counterRowRec = orgMaxRowRec * rowIdx;
                            let currentSortedRowSequence = me.sortedRowSequence;
                            if (hasAdHocRow && rowIdx === (rowSplitCnt - 1)) {
                                currentSortedRowSequence = null;
                                currentSortedContentSequence = null;
                                counterRowRec = 0;
                            }
                            private_addTabValue(me.dispValueIdxLst_Row, currentSortedRowSequence, segIdx, counterRowRec, maxRowRec, me.dispSerialIdxLst_Row, me.col_sum_fields_value_total, me.dispSumValueLst_Col);
                            private_addTabValue(me.dispValueIdxLst_Col, currentSortedColSequence, segIdx, counterColRec, maxColRec, null, null, null);
                            private_addContentValue(me.dispValueIdxLst_Content, currentSortedContentSequence, segIdx, counterRowRec, maxRowRec, counterColRec, maxColRec, me.page_seg_map, pageIdx);
                        }
                    }
                }
                JpcCrossTabHelper.initialPageStatus(pageStatus);
            }
            //3. set pageSeq and return the result
            rst = pageIdx;
        }
        bands = null;
        return rst;
    };
    JpcCrossTabResult.outputAsPreviewPage = function (rptTpl, bands, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let pageStatus = [true, true, true, true, true, true, true, true];
        me.pageStatusLst.push(pageStatus);
        // JpcFieldHelper.findAndPutDataFieldIdx(rptTpl, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_FLOW_FIELDS], null, me.disp_fields_idx, false);
        JpcBandHelper.setBandArea(bands, rptTpl, pageStatus, true, false);
        let maxRowRec = JpcCrossTabHelper.getMaxRowsPerPage(bands, rptTpl);
        let maxColRec = JpcCrossTabHelper.getMaxColsPerPage(bands, rptTpl);
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        //1. 交叉行
        rst = rst.concat(me.outputPreviewRowTab(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, unitFactor));
        //2. 交叉列
        rst = rst.concat(me.outputPreviewColTab(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxColRec, unitFactor));
        //3. 交叉数据
        rst = rst.concat(me.outputPreviewContent(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, maxColRec, unitFactor));
        //4. 交叉行拓展
        rst = rst.concat(me.outputPreviewTabExt(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxColRec, unitFactor));
        //5. 交叉行拓展合计
        rst = rst.concat(me.outputPreviewSumTabExt(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, unitFactor));
        //6. 交叉列合计
        rst = rst.concat(me.outputPreviewTabSum(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, JV.NODE_CROSS_COL_SUM, unitFactor));
        //7. 离散
        rst = rst.concat(JpcDiscreteHelper.outputPreviewDiscreteInfo(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO], bands, unitFactor, pageStatus));
        return rst;
    };
    JpcCrossTabResult.outputPreviewRowTab = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, unitFactor) {
        let rst = this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, 1, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW], unitFactor);
        rst = rst.concat(this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, 1, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_AD_HOC], unitFactor));
        return rst;
    };
    JpcCrossTabResult.outputPreviewColTab = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxColRec, unitFactor) {
        return this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, 1, maxColRec, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL], unitFactor);
    };
    JpcCrossTabResult.outputPreviewContent = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, maxColRec, unitFactor) {
        return this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, maxColRec, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT], unitFactor);
    };
    JpcCrossTabResult.outputPreviewTabSum = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, tabNodeName, unitFactor) {
        return this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, 1, rptTpl[JV.NODE_CROSS_INFO][tabNodeName], unitFactor);
    };
    JpcCrossTabResult.outputPreviewTabExt = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxColRec, unitFactor) {
        //交叉行拓展
        return this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, 1, maxColRec, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT], unitFactor);
    };
    JpcCrossTabResult.outputPreviewSumTabExt = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, unitFactor) {
        //交叉行拓展合计
        return this.private_OutputPreviewCommon(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, 1, 1, rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT], unitFactor);
    };
    JpcCrossTabResult.private_OutputPreviewCommon = function(rptTpl, bands, controls, $CURRENT_RPT, customizeCfg, maxRowRec, maxColRec, tab, unitFactor) {
        let me = this, rst = [];
        let band = (tab)?bands[tab[JV.PROP_BAND_NAME]]:null;
        if (band) {
            let tab_fields = tab[JV.PROP_CROSS_FIELDS];
            for (let rowIdx = 0; rowIdx < maxRowRec; rowIdx++) {
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        for (let colIdx = 0; colIdx < maxColRec; colIdx++) {
                            if (tab_field[JV.PROP_IS_SERIAL]) {
                                rst.push(me.outputTabField(band, tab_field, [rowIdx + 1], 0, -1, maxRowRec, rowIdx, maxColRec, colIdx, unitFactor, false, controls));
                            } else {
                                rst.push(me.outputTabField(band, tab_field, null, -1, -1, maxRowRec, rowIdx, maxColRec, colIdx, unitFactor, false, controls));
                            }
                        }
                    }
                }
                if (tab[JV.PROP_TEXTS]) {
                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                        for (let colIdx = 0; colIdx < maxColRec; colIdx++) {
                            rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, maxRowRec, rowIdx, maxColRec, colIdx, 1, 0));
                        }
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputAsSimpleJSONPage = function(rptTpl, dataObj, page, bands, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], tabRstLst = [];
        let unitFactor = JpcCommonHelper.getUnitFactor(rptTpl);
        if (me.paging_option === JV.PAGING_OPTION_INFINITY) {
            /*
             let segIdx = page - 1;
             //1 calculate the band position
             JpcBandHelper.setBandArea(bands, rptTpl, me.pageStatusLst[page - 1]);
             //2. then reset the band height
             let tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT];
             let crossContentBand = bands[tab[JV.PROP_BAND_NAME]];
             let actH = JpcCrossTabHelper.getActualRowsHeight(bands, rptTpl, me.sortedRowSequence, page);
             let actW = JpcCrossTabHelper.getActualColsWidth(bands, rptTpl, me.sortedColSequence, page);
             let offsetY = actH - (crossContentBand.Bottom - crossContentBand.Top);
             let offsetX = actW - (crossContentBand.Right - crossContentBand.Left);
             JpcBandHelper.resetBandPos(rptTpl[JV.NODE_BAND_COLLECTION], bands, crossContentBand, offsetX, offsetY);

             //2.1 Row-Tab
             tabRstLst.push(me.outputRowTab(rptTpl, dataObj, page, bands, unitFactor, controls));
             //2.2 Col-Tab
             tabRstLst.push(me.outputColTab(rptTpl, dataObj, page, bands, unitFactor, controls));
             //2.3 Content-Tab
             tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls));
             //2.4 Sum-Tab Row
             //2.4 Sum-tab Col
             tabRstLst.push(me.outputTabSum(rptTpl, dataObj, page, bands, unitFactor, JV.NODE_CROSS_COL_SUM, controls));
             //2.x row tab ext
             tabRstLst.push(me.outputTabExt(rptTpl, dataObj, page, bands, unitFactor, controls));
             tabRstLst.push(me.outputSumTabExt(rptTpl, dataObj, page, bands, unitFactor, segIdx, controls));
             //2.5 Discrete
             tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, me.pageStatusLst[page - 1], segIdx, 1, 0, $CURRENT_RPT, customizeCfg));
             //*/
        } else {
            let segIdx = JpcCommonHelper.getSegIdxByPageIdx(page, me.page_seg_map);
            //1 calculate the band position
            JpcBandHelper.setBandArea(bands, rptTpl, me.pageStatusLst[page - 1]);
            //2. start to output detail-part
            //2.1 Row-Tab
            //tabRstLst.push(me.outputRowTab(rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            tabRstLst.push(me.outputRowTabCommon(rptTpl, dataObj, page, bands, JV.NODE_CROSS_ROW, me.row_fields_idx, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            tabRstLst.push(me.outputRowTabCommon(rptTpl, dataObj, page, bands, JV.NODE_CROSS_ROW_AD_HOC, me.row_fields_adhoc_idx, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            //2.2 Col-Tab
            tabRstLst.push(me.outputColTab(rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            //2.3 Content-Tab
            tabRstLst.push(me.outputContent(rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            //2.4 Sum-Tab Row
            //2.4 Sum-tab Col
            tabRstLst.push(me.outputTabSum(rptTpl, dataObj, page, bands, unitFactor, JV.NODE_CROSS_COL_SUM, controls, $CURRENT_RPT, customizeCfg));
            //2.x row tab ext
            tabRstLst.push(me.outputTabExt(rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg));
            tabRstLst.push(me.outputSumTabExt(rptTpl, dataObj, page, bands, unitFactor, segIdx, controls, $CURRENT_RPT, customizeCfg));
            //2.5 Discrete
            tabRstLst.push(JpcDiscreteHelper.outputDiscreteInfo(rptTpl[JV.NODE_CROSS_INFO][JV.NODE_DISCRETE_INFO], bands, dataObj, unitFactor, me.pageStatusLst[page - 1], segIdx, 1, 0, $CURRENT_RPT, customizeCfg));
        }
        for (let i = 0; i < tabRstLst.length; i++) {
            rst = rst.concat(tabRstLst[i]);
            tabRstLst[i] = null;
        }
        return rst;
    };
    JpcCrossTabResult.outputRowTabCommon = function(rptTpl, dataObj, page, bands, tabStr, rowFieldsIdxArr, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let tab = rptTpl[JV.NODE_CROSS_INFO][tabStr];
        let band = (tab)?bands[tab[JV.PROP_BAND_NAME]]:null;
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS];
                let data_details = dataObj[JV.DATA_DETAIL_DATA];
                let valuesIdx = me.dispValueIdxLst_Row[page - 1];
                let serialsIdx = me.dispSerialIdxLst_Row[page - 1];
                let flexiblePrecisionRefObj = null, flexibleRefField = null, precision_ref_data = null;
                for (let i = 0; i < rowFieldsIdxArr.length; i++) {
                    let tab_field = tab_fields[i];
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let data_field = null;
                        let map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                        if (typeof rowFieldsIdxArr[i] !== 'object') {
                            data_field = data_details[rowFieldsIdxArr[i]];
                        } else {
                            if (map_data_field) {
                                data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                            }
                        }
                        let rows = valuesIdx.length;
                        for (let rowIdx = 0; rowIdx < rows; rowIdx++) {
                            if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                                if (flexiblePrecisionRefObj === null) {
                                    flexiblePrecisionRefObj = {};
                                    flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                    precision_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq];
                                    for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                        flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                    }
                                }
                                JpcFieldHelper.resetFlexibleFormat(tab_field, precision_ref_data, flexiblePrecisionRefObj, valuesIdx[rowIdx], customizeCfg);
                            } else {
                                if (rowIdx === 0) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                            }
                            rst.push(me.outputTabField(band, tab_field, data_field, valuesIdx[rowIdx], serialsIdx[rowIdx], rows, rowIdx, 1, 0, unitFactor, true, controls));
                        }
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputColTab = function(rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], firstTextOutput = true;
        let tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_COL];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS];
                let data_details = dataObj[JV.DATA_DETAIL_DATA];
                let valuesIdx = me.dispValueIdxLst_Col[page - 1];
                let flexiblePrecisionRefObj = null, flexibleRefField = null, precision_ref_data = null;
                for (let i = 0; i < me.col_fields_idx.length; i++) {
                    let tab_field = tab_fields[i];
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let mergedRst = [];
                        let data_field = null;
                        let map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                        if (typeof me.col_fields_idx[i] !== 'object') {
                            data_field = data_details[me.col_fields_idx[i]];
                        } else {
                            if (map_data_field) {
                                data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                            }
                        }
                        let cols = valuesIdx.length;
                        for (let colIdx = 0; colIdx < cols; colIdx++) {
                            if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                                if (flexiblePrecisionRefObj === null) {
                                    flexiblePrecisionRefObj = {};
                                    flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                    precision_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq];
                                    for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                        flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                    }
                                }
                                JpcFieldHelper.resetFlexibleFormat(tab_field, precision_ref_data, flexiblePrecisionRefObj, valuesIdx[colIdx], customizeCfg);
                            } else {
                                if (colIdx === 0) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                            }
                            mergedRst.push(me.outputTabField(band, tab_field, data_field, valuesIdx[colIdx], -1, 1, 0, cols, colIdx, unitFactor, false, controls));
                            //rst.push(me.outputTabField(band, tab_field, data_field, valuesIdx[colIdx], -1, 1, 0, cols, colIdx, unitFactor, false, controls));
                            //2. output texts
                            if (firstTextOutput) {
                                if (tab[JV.PROP_TEXT]) {
                                    // mergedRst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                    rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                }
                                if (tab[JV.PROP_TEXTS]) {
                                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                                        // mergedRst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                        rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                    }
                                }
                            }
                        }
                        firstTextOutput = false;
                        //判断是否需要合并
                        if (tab_field[JV.PROP_IS_MERGE] && mergedRst.length > 1) {
                            let lastCell = mergedRst[mergedRst.length - 1];
                            for (let mergeIdx = mergedRst.length - 2; mergeIdx >= 0; mergeIdx--) {
                                if (lastCell[JV.PROP_VALUE] === mergedRst[mergeIdx][JV.PROP_VALUE]) {
                                    mergedRst[mergeIdx][JV.PROP_AREA][JV.PROP_RIGHT] = lastCell[JV.PROP_AREA][JV.PROP_RIGHT];
                                    mergedRst.splice(mergeIdx + 1, 1);
                                }
                                lastCell = mergedRst[mergeIdx];
                            }
                        }
                        rst = rst.concat(mergedRst);
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputContent = function (rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [];
        let tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_CONTENT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS];
                let data_details = dataObj[JV.DATA_DETAIL_DATA];
                let contentValuesIdx = me.dispValueIdxLst_Content[page - 1];
                let flexiblePrecisionRefObj = null, flexibleRefField = null, precision_ref_data = null;
                for (let i = 0; i < tab_fields.length; i++) {
                    let tab_field = tab_fields[i];
                    let data_field = null;
                    let map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (typeof me.content_fields_idx[i] !== 'object') {
                        data_field = data_details[me.content_fields_idx[i]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }
                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let rows = contentValuesIdx.length;
                        for (let rowIdx = 0; rowIdx < rows; rowIdx++) {
                            let cols = contentValuesIdx[rowIdx].length;
                            for (let colIdx = 0; colIdx < cols; colIdx++) {
                                if (map_data_field && map_data_field[JV.PROP_PRECISION] && map_data_field.flexiblePrecisionRefObj) {
                                    if (flexiblePrecisionRefObj === null) {
                                        flexiblePrecisionRefObj = {};
                                        flexibleRefField = JE.F(map_data_field[JV.PROP_PRECISION][JV.PROP_FLEXIBLE_REF_FILED_ID], $CURRENT_RPT);
                                        precision_ref_data = dataObj[map_data_field.DataNodeName][flexibleRefField.DataSeq];
                                        for (let decimalObj of map_data_field.flexiblePrecisionRefObj) {
                                            flexiblePrecisionRefObj["refUnit_" + decimalObj.unit] = decimalObj.decimal;
                                        }
                                    }
                                    JpcFieldHelper.resetFlexibleFormat(tab_field, precision_ref_data, flexiblePrecisionRefObj, contentValuesIdx[rowIdx][colIdx], customizeCfg);
                                } else {
                                    if (colIdx === 0) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                                }
                                rst.push(me.outputTabField(band, tab_field, data_field, contentValuesIdx[rowIdx][colIdx], -1, rows, rowIdx, cols, colIdx, unitFactor, true, controls));
                            }
                        }
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputTabSum = function (rptTpl, dataObj, page, bands, unitFactor, tabNodeName, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [],
            tab = rptTpl[JV.NODE_CROSS_INFO][tabNodeName],
            band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS];
                for (let i = 0; i < me.dispSumValueLst_Col[page - 1].length; i++) {
                    if (i === 0) {
                        for (let tfIdx = 0; tfIdx < tab_fields.length; tfIdx++) {
                            let map_data_field = JE.F(tab_fields[tfIdx][JV.PROP_FIELD_ID], $CURRENT_RPT);
                            JpcFieldHelper.resetFormat(tab_fields[tfIdx], map_data_field, customizeCfg);
                        }
                    }
                    if (me.dispSumValueLst_Col[page - 1][i] !== null) {
                        for (let j = 0; j < me.dispSumValueLst_Col[page - 1][i].length; j++) {
                            let tab_field = tab_fields[j];
                            let val = me.dispSumValueLst_Col[page - 1][i][j];
                            let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, val, controls);
                            cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, me.dispSumValueLst_Col[page - 1].length, i, 1, 0, 1, 0, true, false);
                            rst.push(cellItem);
                        }
                    } else {
                        let sumL = 1;
                        for (let si = 0; si < me.dispSumValueLst_Col.length; si++) {
                            if (me.dispSumValueLst_Col[si][0] !== null) {
                                sumL = me.dispSumValueLst_Col[si][0].length;
                                break;
                            }
                        }
                        for (let j = 0; j < sumL; j++) {
                            let tab_field = tab_fields[j];
                            let val = null;
                            let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, val, controls);
                            cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, me.dispSumValueLst_Col[page - 1].length, i, 1, 0, 1, 0, true, false);
                            rst.push(cellItem);
                        }
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputTabExt = function (rptTpl, dataObj, page, bands, unitFactor, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [], firstTextOutput = true,
            tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_EXT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS],
                    data_details = dataObj[JV.DATA_DETAIL_DATA],
                    valuesIdx = me.dispValueIdxLst_Col[page - 1];
                for (let i = 0; i < me.row_extension_fields_idx.length; i++) {
                    let tab_field = tab_fields[i];
                    let data_field = null;
                    let map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (typeof me.row_extension_fields_idx[i] !== 'object') {
                        data_field = data_details[me.row_extension_fields_idx[i]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }

                    if (!(tab_field[JV.PROP_HIDDEN])) {
                        let cols = valuesIdx.length;
                        for (let colIdx = 0; colIdx < cols; colIdx++) {
                            if (colIdx === 0) JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                            rst.push(me.outputTabField(band, tab_field, data_field, valuesIdx[colIdx], -1, 1, 0, cols, colIdx, unitFactor, false, controls));
                            //2. output texts if has
                            if (firstTextOutput) {
                                if (tab[JV.PROP_TEXT]) {
                                    rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                }
                                if (tab[JV.PROP_TEXTS]) {
                                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                                        rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, cols, colIdx, 1, 0));
                                    }
                                }
                            }
                        }
                        firstTextOutput = false;
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputSumTabExt = function (rptTpl, dataObj, page, bands, unitFactor, segIdx, controls, $CURRENT_RPT, customizeCfg) {
        let me = this, rst = [],
            tab = rptTpl[JV.NODE_CROSS_INFO][JV.NODE_CROSS_ROW_SUM_EXT];
        let band = bands[tab[JV.PROP_BAND_NAME]];
        if (band) {
            let pageStatus = me.pageStatusLst[page - 1];
            if (pageStatus[band[JV.BAND_PROP_DISPLAY_TYPE]] === true && pageStatus[JV.STATUS_CROSS_ROW_END] === true) {
                let tab_fields = tab[JV.PROP_CROSS_FIELDS],
                    data_details = dataObj[JV.DATA_DETAIL_DATA],
                    data_fields = [];
                for (let i = 0; i < me.row_sum_extension_fields_idx.length; i++) {
                    // let data_field = data_details[me.row_sum_extension_fields_idx[i]];
                    let tab_field = tab_fields[i];
                    let data_field = null;
                    let map_data_field = JE.F(tab_field[JV.PROP_FIELD_ID], $CURRENT_RPT);
                    if (typeof me.row_sum_extension_fields_idx[i] !== 'object') {
                        data_field = data_details[me.row_sum_extension_fields_idx[i]];
                    } else {
                        if (map_data_field) {
                            data_field = map_data_field[JV.PROP_AD_HOC_DATA];
                        }
                    }
                    JpcFieldHelper.resetFormat(tab_field, map_data_field, customizeCfg);
                    data_fields.push(data_field);
                }
                //2. initialize grand total value
                let rowGrandTotal = [];
                for (let di = 0; di < data_fields.length; di++) {
                    rowGrandTotal[di] = 0.0;
                    //3. start to sum
                    for (let i = 0; i < me.sortedColSequence[segIdx].length; i++) {
                        //me.sortedColSequence[segIdx][i][0] //this is the data field value index!
                        rowGrandTotal[di] = rowGrandTotal[di] + 1.0 * JpcFieldHelper.getValue(data_fields[di], me.sortedColSequence[segIdx][i][0]);
                    }
                }
                //4. output
                for (let di = 0; di < tab_fields.length; di++) {
                    let tab_field = tab_fields[di];
                    if (!tab_field[JV.PROP_HIDDEN]) {
                        let val = rowGrandTotal[di];
                        let cellItem = JpcCommonOutputHelper.createCommonOutput(tab_field, val, controls);
                        cellItem[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, 1, 0, 1, 0, 1, 0, false, false);
                        rst.push(cellItem);
                    }
                }
                //output texts if has
                if (tab[JV.PROP_TEXT]) {
                    rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXT], band, unitFactor, 1, 0, 1, 0, 1, 0));
                }
                if (tab[JV.PROP_TEXTS]) {
                    for (let j = 0; j < tab[JV.PROP_TEXTS].length; j++) {
                        rst.push(JpcTextHelper.outputText(tab[JV.PROP_TEXTS][j], band, unitFactor, 1, 0, 1, 0, 1, 0));
                    }
                }
            }
        }
        return rst;
    };
    JpcCrossTabResult.outputTabField = function (band, tab_field, data_field, valueIdx, serialIdx, rows, rowIdx, cols, colIdx, unitFactor, isRow, controls) {
        let rst = null;
        if (isRow === true && tab_field[JV.PROP_IS_SERIAL] && tab_field[JV.PROP_IS_SERIAL] === true) {
            if (serialIdx >= 0) {
                rst = JpcCommonOutputHelper.createCommonOutput(tab_field, serialIdx + 1)
            } else rst = JpcCommonOutputHelper.createCommonOutput(tab_field, "", controls);
        } else {
            rst = JpcCommonOutputHelper.createCommonOutput(tab_field, JpcFieldHelper.getValue(data_field, valueIdx), controls);
        }
        //position
        if (isRow === true) {
            rst[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, rows, rowIdx, cols, colIdx, 1, 0, true, false);
        } else {
            rst[JV.PROP_AREA] = JpcAreaHelper.outputArea(tab_field[JV.PROP_AREA], band, unitFactor, rows, rowIdx, cols, colIdx, 1, 0, false, false);
        }
        return rst;
    };

    return JpcCrossTabResult;
};

module.exports = new JpcCrossTabSrv();