/**
 * Created by Tony on 2018/9/4.
 */

const BG_COLORS = ['#BFEFFF', '#C1FFC1', '#F5F5DC', '#B9D3EE', '#F0FFFF', '#EEE0E5', '#B4EEB4', '#CAE1FF', '#EED2EE', '#AEEEEE', '#76EEC6', '#CD8C95'],
    unitFactor = 1.0 * 96 / 2.54;
let visualCommonOprObj = {
    getActPos: function (textNode, caclStr, typeStr, baseMea) {
        let rst = 0;
        if (caclStr === JV.CAL_TYPE[0]) {
            //percentage
            rst = Math.round(baseMea * textNode[JV.PROP_AREA][typeStr] / 100);
        } else {
            //abstract
            rst = Math.round(textNode[JV.PROP_AREA][typeStr] / 2.54 * 96);
        }
        return rst;
    },
    getActPosEx: function (textNode, caclObj, typeStr, baseMea) {
        let me = this;
        if (typeof caclObj === 'string') {
            return me.getActPos(textNode, caclObj, typeStr, baseMea);
        } else {
            return me.getActPos(textNode, caclObj[typeStr], typeStr, baseMea);
        }
    },
    pushPos: function (textNode, caclObj, typeStr, baseMea, posArr) {
        let me = this, pos = me.getActPosEx(textNode, caclObj, typeStr, baseMea);
        if (posArr.indexOf(pos) < 0) posArr.push(pos);
    },
    getBandEx: function (bandName, rptTpl) {
        let me = this, rst;
        for (let band of rptTpl[JV.NODE_BAND_COLLECTION]) {
            rst = me.getBand(bandName, band);
            if (rst !== null) {
                break;
            }
        }
        return rst;
    },
    getBand: function (bandName, parentBand) {
        let me = this, rst = null;
        if (parentBand[JV.PROP_NAME] === bandName) {
            rst = parentBand;
        } else {
            if (parentBand[JV.BAND_PROP_SUB_BANDS] && parentBand[JV.BAND_PROP_SUB_BANDS].length > 0) {
                for (let subBand of parentBand[JV.BAND_PROP_SUB_BANDS]) {
                    rst = me.getBand(bandName, subBand);
                    if (rst !== null) {
                        break;
                    }
                }
            }
        }
        return rst;
    },
    checkInSpan: function (spans, row, col) {
        let rst = false;
        for (let span of spans) {
            if (span.row <= row && row < (span.row + span.rowCount) && span.col <= col && col < (span.col + span.colCount)) {
                rst = true;
                break;
            }
        }
        return rst;
    },
    setupHeightWidth: function (dest, text, colWidthArr, rowHeightArr) {
        let width = colWidthArr[colWidthArr.length - 1], height = rowHeightArr[rowHeightArr.length - 1];
        dest[JV.PROP_AREA][JV.PROP_RIGHT] = (colWidthArr[text.col + text.colCount - 1] / width * 100).toFixed(2);
        if (text.col > 0) {
            dest[JV.PROP_AREA][JV.PROP_LEFT] = (colWidthArr[text.col - 1] / width * 100).toFixed(2);
        } else {
            dest[JV.PROP_AREA][JV.PROP_LEFT] = 0;
        }
        dest[JV.PROP_AREA][JV.PROP_BOTTOM] = (rowHeightArr[text.row + text.rowCount - 1] / height * 100).toFixed(2);
        if (text.row > 0) {
            dest[JV.PROP_AREA][JV.PROP_TOP] = (rowHeightArr[text.row - 1] / height * 100).toFixed(2);
        } else {
            dest[JV.PROP_AREA][JV.PROP_TOP] = 0;
        }
    },
    createTxtNode: function (text, parentNode, colWidthArr, rowHeightArr) {
        let me = this, rst = dataInfoMapTreeOprObj.getDummyTextNode(parentNode);
        me.setupHeightWidth(rst, text, colWidthArr, rowHeightArr);
        if (stringUtil.isEmptyString(text[`text`])) {
            rst[JV.PROP_NAME] = '';
        } else {
            rst[JV.PROP_NAME] = stringUtil.replaceAll(stringUtil.replaceAll(text[`text`].toString(), '\n', '|'), '\r', '');
        }
        rst[JV.PROP_LABEL] = rst[JV.PROP_NAME];
        return rst;
    },
    collectSheetTxt: function (sheet, texts, reserveRows, colWidthArr, rowHeightArr) {
        let me = this, spans= sheet.getSpans();
        let private_build_pre_text = function (row, col, rowCount, colCount) {
            let cell = sheet.getCell(row, col);
            let textValue = sheet.getValue(row, col);
            if (textValue && cell.cellType().typeName === '7') {
                textValue = `{` + textValue + `}`;
            }
            texts.push({"row": row, "col": col, "rowCount": rowCount, "colCount": colCount, "text": textValue});
        };
        for (let span of spans) {
            private_build_pre_text(span.row, span.col, span.rowCount, span.colCount);
        }
        for (let iRow = 0; iRow < sheet.getRowCount() - reserveRows; iRow++) {
            rowHeightArr.push(sheet.getRowHeight(iRow));
            if (iRow > 0) {
                rowHeightArr[iRow] = rowHeightArr[iRow] + rowHeightArr[iRow - 1];
            }
            for (let jCol = 0; jCol < sheet.getColumnCount(); jCol++) {
                if (iRow === 0) {
                    colWidthArr.push(sheet.getColumnWidth(jCol));
                    if (jCol > 0) {
                        colWidthArr[jCol] = colWidthArr[jCol] + colWidthArr[jCol - 1];
                    }
                }
                if (!me.checkInSpan(spans, iRow, jCol)) {
                    private_build_pre_text(iRow, jCol, 1, 1);
                }
            }
        }
        texts.sort(function(t1, t2){
            if (t1.col === t2.col) {
                return t1.row - t2.row;
            } else {
                return t1.col - t2.col;
            }
        });
    },
    addSpan: function (itemNode, sheet, bandW, bandH, xPos, yPos, sheetAreaType) {
        let me = this;
        let idx1 = xPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_LEFT, bandW));
        let idx2 = xPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_RIGHT, bandW));
        let idy1 = yPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_TOP, bandH));
        let idy2 = yPos.indexOf(me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_BOTTOM, bandH));
        if (idx2 - idx1 > 1 || idy2 - idy1 > 1) {
            sheet.addSpan(idy1, idx1, idy2 - idy1, idx2 - idx1, sheetAreaType);
        }
        let cell = sheet.getCell(idy1, idx1, sheetAreaType);
        if (sheetAreaType === GC.Spread.Sheets.SheetArea.viewport) {
            if (itemNode.FieldID) {
                me.changeCellType(cell, 'field');
            } else {
                me.changeCellType(cell, 'text');
            }
        }
        me.setupCell(cell, itemNode);
    },
    changeBandHeight: function (bandName, newHeight) {
        let nodes = bandTreeOprObj.treeObj.getNodes()[0][JV.BAND_PROP_SUB_BANDS];
        let private_set_band_height = function(bNode) {
            let rst = false;
            if (bNode[JV.PROP_NAME] === bandName) {
                bNode[JV.BAND_PROP_HEIGHT] = newHeight;
                rst = true;
            } else if (bNode[JV.BAND_PROP_SUB_BANDS] && bNode[JV.BAND_PROP_SUB_BANDS].length > 0){
                for (let subNode of bNode[JV.BAND_PROP_SUB_BANDS]) {
                    rst = private_set_band_height(subNode);
                    if (rst) break;
                }
            }
            return rst;
        };
        for (let bandNode of nodes) {
            if (private_set_band_height(bandNode)) break;
        }
    },
    changeCellType: function (cell, newType, newColor) {
        let me = this;
        switch (newType) {
            case `field`:
                let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
                let cellType = new GC.Spread.Sheets.CellTypes.ComboBox();
                // let selectableFields = me.getSelectedFields(rptTpl);
                let selectableFields = me.getAllSelectedFields();
                cellType.items(selectableFields);
                cell.cellType(cellType);
                if (newColor) {
                    cell.backColor(newColor);
                } else {
                    cell.backColor("LightCyan");
                }
                break;
            case `text`:
            default:
                cell.cellType(new GC.Spread.Sheets.CellTypes.Text());
                cell.value(``);
                if (newColor) {
                    cell.backColor(newColor);
                } else {
                    cell.backColor(undefined);
                }
                break;
        }
    },
    setupCell: function (cell, itemNode) {
        let me = this;
        me.private_setCellFont(cell, itemNode);
        me.private_setCellControl(cell, itemNode);
        // let border = new GC.Spread.Sheets.LineBorder;
        // border.color = "Black";
        // border.style = GC.Spread.Sheets.LineStyle.thin;
        // me.private_setCellStyle(cell, itemNode, border);
        let value = itemNode[JV.PROP_NAME];
        if (itemNode[JV.PROP_NAME].indexOf(`|`) >= 0) {
            value = itemNode[JV.PROP_NAME].split('|').join('\n');
        }
        cell.wordWrap(true);
        cell.value(value);
    },
    private_setCellControl: function (cell, textNode) {
        let ctrl = null;
        if (typeof textNode[JV.PROP_CONTROL] === 'string') {
            let idx = rpt_tpl_cfg_helper.reportCfg.controlArr.indexOf(textNode[JV.PROP_CONTROL]);
            ctrl = rpt_tpl_cfg_helper.reportCfg.ctrls[idx];
        } else {
            ctrl = textNode[JV.PROP_CONTROL];
        }
        if (ctrl) {
            switch (ctrl.Horizon) {
                case `center`:
                    cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
                    break;
                case `right`:
                    cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
                    break;
                default:
                    cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
                    break;
            }
            switch (ctrl.Vertical) {
                case `center`:
                    cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
                    break;
                case `bottom`:
                    cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
                    break;
                default:
                    cell.vAlign(GC.Spread.Sheets.VerticalAlign.top);
                    break;
            }
        }
    },
    private_setCellStyle: function (cell, textNode, border) {
        let style;
        if (typeof textNode[JV.PROP_STYLE] === 'string') {
            let idx = rpt_tpl_cfg_helper.reportCfg.borderArr.indexOf(textNode[JV.PROP_STYLE]);
            style = rpt_tpl_cfg_helper.reportCfg.styles[idx];
        } else {
            style = textNode[JV.PROP_STYLE];
        }
        if (style) {
            for (let borderStyle of style[JV.PROP_BORDER_STYLE]) {
                if (parseFloat(borderStyle[JV.PROP_LINE_WEIGHT]) > 0) {
                    switch (borderStyle[JV.PROP_POSITION]) {
                        case JV.PROP_LEFT:
                            cell.borderLeft(border);
                            break;
                        case JV.PROP_RIGHT:
                            cell.borderRight(border);
                            break;
                        case JV.PROP_TOP:
                            cell.borderTop(border);
                            break;
                        case JV.PROP_BOTTOM:
                            cell.borderBottom(border);
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    },
    private_setCellFont: function (cell, textNode) {
        let fontAttr;
        if (typeof textNode[JV.PROP_FONT] === 'string') {
            for (let font of rpt_tpl_cfg_helper.reportCfg.fonts) {
                if (font.ID === textNode[JV.PROP_FONT]) {
                    fontAttr = font;
                    // cell.font(Math.round(font[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]] * 3 / 4) + 'pt ' + font[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]]);
                    break;
                }
            }
        } else {
            fontAttr = textNode[JV.PROP_FONT];
        }
        let fontStr = "";
        if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]])) {
            fontStr = 'bold ' + fontStr;
        }
        if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]])) {
            fontStr = 'italic ' + fontStr;
        }
        fontStr = fontStr + Math.round(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]] * 3 / 4) + 'pt ' + fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]];
        if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]])) {
            cell.textDecoration(GC.Spread.Sheets.TextDecorationType.underline);
        } else {
            cell.textDecoration(GC.Spread.Sheets.TextDecorationType.none);
        }
        cell.font(fontStr);
    },
    getSelectedFields: function (rptTpl) {
        let rst = [];
        if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].length > 0) {
            for (let field of rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]) {
                rst.push(field[JV.PROP_NAME]);
            }
        }
        if (rptTpl[JV.NODE_NO_MAPPING_FIELDS] !== undefined && rptTpl[JV.NODE_NO_MAPPING_FIELDS].length > 0) {
            for (let field of rptTpl[JV.NODE_NO_MAPPING_FIELDS]) {
                rst.push(field[JV.PROP_NAME]);
            }
        }
        return rst;
    },
    getAllSelectedFields: function (rptTpl) {
        let rst = [];
        if (rptTpl) {
            if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].length > 0) {
                for (let field of rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]) {
                    rst.push(field[JV.PROP_NAME]);
                }
            }
            if (rptTpl[JV.NODE_NO_MAPPING_FIELDS] !== undefined && rptTpl[JV.NODE_NO_MAPPING_FIELDS].length > 0) {
                for (let field of rptTpl[JV.NODE_NO_MAPPING_FIELDS]) {
                    rst.push(field[JV.PROP_NAME]);
                }
            }
            if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].length > 0) {
                for (let field of rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS]) {
                    rst.push(field[JV.PROP_NAME]);
                }
            }
            if (rptTpl[JV.NODE_DISCRETE_PARAMS] !== undefined && rptTpl[JV.NODE_DISCRETE_PARAMS].length > 0) {
                for (let field of rptTpl[JV.NODE_DISCRETE_PARAMS]) {
                    rst.push(field[JV.PROP_NAME]);
                }
            }
        } else {
            let private_getNameByTree = function (treeObj, isReverse) {
                let nodes = treeObj.getNodes();
                if (isReverse) nodes.reverse();
                for (let node of nodes) {
                    if (node.items && node.items.length > 0) {
                        for (let subItem of node.items) {
                            rst.push(subItem[JV.PROP_NAME]);
                        }
                    }
                }
                if (isReverse) nodes.reverse();
            };
            if (fieldMapTreeOprObj.treeObj) {
                private_getNameByTree(fieldMapTreeOprObj.treeObj, true);
            }
            if (discreteFieldParamTreeOprObj.treeObj) {
                private_getNameByTree(discreteFieldParamTreeOprObj.treeObj, false);
            }
        }
        return rst;
    },
    setupCellDft: function (cell) {
        cell.font(`9pt 宋体`);
        cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
        cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
        cell.wordWrap(true);
        cell.cellType(new GC.Spread.Sheets.CellTypes.Text());
        cell.value(``);
    },

    addBandPos: function (rptTpl, pageW, pageH, xPos, yPos, bandMappingObj) {
        let left = 0, right = pageW, top = 0, bottom = pageH;
        let orgArea = [left, top, right, bottom];
        let colorIdx = 0;
        bandMappingObj.items = [];
        let private_setup_pos = function (band) {
            left = orgArea[JV.IDX_LEFT];
            top = orgArea[JV.IDX_TOP];
            right = orgArea[JV.IDX_RIGHT];
            bottom = orgArea[JV.IDX_BOTTOM];
            if (xPos.indexOf(left) < 0) xPos.push(left);
            if (xPos.indexOf(right) < 0) xPos.push(right);
            if (yPos.indexOf(top) < 0) yPos.push(top);
            if (yPos.indexOf(bottom) < 0) yPos.push(bottom);
            switch (band[JV.BAND_PROP_ALIGNMENT]) {
                case JV.LAYOUT_TOP:
                case JV.LAYOUT[JV.LAYOUT_TOP]:
                    bottom = Math.round(top + unitFactor * band[JV.BAND_PROP_HEIGHT]);
                    orgArea[JV.IDX_TOP] = bottom;
                    break;
                case JV.LAYOUT_BOTTOM:
                case JV.LAYOUT[JV.LAYOUT_BOTTOM]:
                    top = Math.round(bottom - unitFactor * band[JV.BAND_PROP_HEIGHT]);
                    orgArea[JV.IDX_BOTTOM] = top;
                    break;
                case JV.LAYOUT_LEFT:
                case JV.LAYOUT[JV.LAYOUT_LEFT]:
                    right = Math.round(left + unitFactor * band[JV.BAND_PROP_WIDTH]);
                    orgArea[JV.IDX_LEFT] = right;
                    break;
                case JV.LAYOUT_RIGHT:
                case JV.LAYOUT[JV.LAYOUT_RIGHT]:
                    left = Math.round(right - unitFactor * band[JV.BAND_PROP_WIDTH]);
                    orgArea[JV.IDX_RIGHT] = left;
                    break;
            }
            let item = {};
            bandMappingObj[band[JV.PROP_NAME]] = item;
            item[JV.PROP_BAND_NAME] = band[JV.PROP_NAME];
            item[JV.PROP_AREA] = {};
            item[JV.PROP_AREA][JV.PROP_LEFT] = left;
            item[JV.PROP_AREA][JV.PROP_RIGHT] = right;
            item[JV.PROP_AREA][JV.PROP_TOP] = top;
            item[JV.PROP_AREA][JV.PROP_BOTTOM] = bottom;
            item[JV.PROP_COLOR] = BG_COLORS[colorIdx];
            // item[JV.PROP_COLOR] = 'Blue';
            bandMappingObj.items.push(item);
            colorIdx++;
            if (xPos.indexOf(left) < 0) xPos.push(left);
            if (xPos.indexOf(right) < 0) xPos.push(right);
            if (yPos.indexOf(top) < 0) yPos.push(top);
            if (yPos.indexOf(bottom) < 0) yPos.push(bottom);

            if (colorIdx >= BG_COLORS.length) colorIdx = 0;
            if (band[JV.BAND_PROP_SUB_BANDS] && band[JV.BAND_PROP_SUB_BANDS].length > 0) {
                item[JV.PROP_COLOR] = 'White';
                // item[JV.PROP_COLOR] = null;
                for (let subBand of band[JV.BAND_PROP_SUB_BANDS]) {
                    private_setup_pos(subBand);
                }
            }
        };
        for (let band of rptTpl[JV.NODE_BAND_COLLECTION]) {
            private_setup_pos(band);
        }
    },

    pushAbsPos: function (bandMap, itemNode, xPos, yPos) {
        let me = this;
        if (bandMap) {
            let bandW = bandMap[JV.PROP_AREA][JV.PROP_RIGHT] - bandMap[JV.PROP_AREA][JV.PROP_LEFT],
                bandH = bandMap[JV.PROP_AREA][JV.PROP_BOTTOM] - bandMap[JV.PROP_AREA][JV.PROP_TOP];
            let pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_LEFT, bandW) + bandMap[JV.PROP_AREA][JV.PROP_LEFT];
            if (xPos.indexOf(pos) < 0) xPos.push(pos);
            pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_RIGHT, bandW) + bandMap[JV.PROP_AREA][JV.PROP_LEFT];
            if (xPos.indexOf(pos) < 0) xPos.push(pos);
            pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_TOP, bandH) + bandMap[JV.PROP_AREA][JV.PROP_TOP];
            if (yPos.indexOf(pos) < 0) yPos.push(pos);
            pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_BOTTOM, bandH) + bandMap[JV.PROP_AREA][JV.PROP_TOP];
            if (yPos.indexOf(pos) < 0) yPos.push(pos);
        }
    },
    addTplTxtFldPos: function (rptTpl, tree, xPos, yPos, bandMappingObj) {
        let me = this, nodes = tree.getNodes();
        if (rptTpl[JV.NODE_FLOW_INFO]) {
            //流水式
            let contentBandMap, contentRowH, orgContentTop, lastContentBottom;
            for (let node of nodes) {
                if (node[JV.PROP_NAME].indexOf('_列') >= 0) {
                    //1. 列
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    me.addNormalTxtFldPos(bandMap, node, xPos, yPos);
                    bandMap[JV.PROP_COLOR] = 'LightGray';
                } else if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                    //2. 数据
                    contentBandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    contentRowH = Math.round(unitFactor * rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT]);
                    orgContentTop = contentBandMap[JV.PROP_AREA][JV.PROP_TOP];
                    let bkBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                    contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH;
                    lastContentBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                    if (node.items.length === 0) {
                        if (yPos.indexOf(contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH) < 0) {
                            yPos.push(contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH);
                        }
                    } else {
                        me.addNormalTxtFldPos(contentBandMap, node, xPos, yPos);
                    }
                    contentBandMap[JV.PROP_AREA][JV.PROP_TOP] = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                    contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = bkBottom;
                    contentBandMap[JV.PROP_COLOR] = 'LightCyan';
                } else if (node[JV.PROP_NAME].indexOf('_页统计') >= 0) {
                    //3. 页统计
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    for (let itemNode of node.items) {
                        me.addNormalTxtFldPos(bandMap, itemNode, xPos, yPos);
                    }
                } else if (node[JV.PROP_NAME].indexOf('_段统计') >= 0) {
                    //4. 章统计
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    for (let itemNode of node.items) {
                        me.addNormalTxtFldPos(bandMap, itemNode, xPos, yPos);
                    }
                } else if (node[JV.PROP_NAME].indexOf('_分组') >= 0) {
                    //5. 分组统计
                    if (contentBandMap && contentRowH && node.items && node.items.length > 0) {
                        for (let itemNode of node.items) {
                            if (itemNode[JV.PROP_NAME] === `分组行`) {
                                if (itemNode.items) {
                                    for (let subItemNode of itemNode.items) {
                                        //行
                                        let bkBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                                        contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH;
                                        lastContentBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                                        for (let txtFldNode of subItemNode.items) {
                                            //分组字段集 与 文本集
                                            me.addNormalTxtFldPos(contentBandMap, txtFldNode, xPos, yPos);
                                        }
                                        contentBandMap[JV.PROP_AREA][JV.PROP_TOP] = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                                        contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = bkBottom;
                                    }
                                }
                            }
                        }
                    }
                } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                    //6. 离散信息
                    me.addDiscreteTxtFldPos(node, xPos, yPos, bandMappingObj);
                }
            }
            if (contentBandMap) {
                contentBandMap[JV.PROP_AREA][JV.PROP_TOP] = orgContentTop;
                contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = lastContentBottom;
            }
        } else if (rptTpl[JV.NODE_BILL_INFO]) {
            //账单式
            for (let node of nodes) {
                if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                    //1. 数据
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    me.addNormalTxtFldPos(bandMap, node, xPos, yPos);
                } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                    //2. 离散信息
                    me.addDiscreteTxtFldPos(node, xPos, yPos, bandMappingObj);
                }
            }
        } else if (rptTpl[JV.NODE_CROSS_INFO]) {
            //交叉式
            //目前暂缓
        }
    },
    setupTplTxtFld: function (rptTpl, tree, sheet, xPos, yPos, bandMappingObj) {
        let me = this, nodes = tree.getNodes();
        bandMappingObj.fontAttr = {};
        bandMappingObj.fieldPreSufAttr = {};
        if (rptTpl[JV.NODE_FLOW_INFO]) {
            //流水式
            let contentBandMap, contentRowH;
            for (let node of nodes) {
                if (node[JV.PROP_NAME].indexOf('_列') >= 0) {
                    //1. 列
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    // me.addNormalTxtFldPos(bandMap, node, xPos, yPos);
                    me.addSpanWholeArea(bandMappingObj, bandMap, node, sheet, xPos, yPos);
                } else if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                    //2. 数据
                    contentBandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    contentRowH = Math.round(unitFactor * rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT]);
                    let bkBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                    contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH;
                    // me.addNormalTxtFldPos(contentBandMap, node, xPos, yPos);
                    me.addSpanWholeArea(bandMappingObj, contentBandMap, node, sheet, xPos, yPos);
                    contentBandMap[JV.PROP_AREA][JV.PROP_TOP] = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                    contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = bkBottom;
                } else if (node[JV.PROP_NAME].indexOf('_页统计') >= 0) {
                    //3. 页统计
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    for (let itemNode of node.items) {
                        // me.addNormalTxtFldPos(bandMap, itemNode, xPos, yPos);
                        me.addSpanWholeArea(bandMappingObj, bandMap, itemNode, sheet, xPos, yPos);
                    }
                } else if (node[JV.PROP_NAME].indexOf('_段统计') >= 0) {
                    //4. 章统计
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    for (let itemNode of node.items) {
                        // me.addNormalTxtFldPos(bandMap, itemNode, xPos, yPos);
                        me.addSpanWholeArea(bandMappingObj, bandMap, itemNode, sheet, xPos, yPos);
                    }
                } else if (node[JV.PROP_NAME].indexOf('_分组') >= 0) {
                    //5. 分组统计
                    if (contentBandMap && contentRowH && node.items && node.items.length > 0) {
                        for (let itemNode of node.items) {
                            if (itemNode[JV.PROP_NAME] === `分组行`) {
                                if (itemNode.items) {
                                    for (let subItemNode of itemNode.items) {
                                        //行
                                        let bkBottom = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                                        contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = contentBandMap[JV.PROP_AREA][JV.PROP_TOP] + contentRowH;
                                        for (let txtFldNode of subItemNode.items) {
                                            //分组字段集 与 文本集
                                            // me.addNormalTxtFldPos(contentBandMap, txtFldNode, xPos, yPos);
                                            me.addSpanWholeArea(bandMappingObj, contentBandMap, txtFldNode, sheet, xPos, yPos);
                                        }
                                        contentBandMap[JV.PROP_AREA][JV.PROP_TOP] = contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM];
                                        contentBandMap[JV.PROP_AREA][JV.PROP_BOTTOM] = bkBottom;
                                    }
                                }
                            }
                        }
                    }
                } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                    //6. 离散信息
                    // me.addDiscreteTxtFldPos(node, xPos, yPos, bandMappingObj);
                    me.addDiscreteSpan(node, xPos, yPos, bandMappingObj, sheet);
                }
            }
        } else if (rptTpl[JV.NODE_BILL_INFO]) {
            //账单式
            for (let node of nodes) {
                if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                    //1. 数据
                    let bandMap = bandMappingObj[node[JV.PROP_BAND_NAME]];
                    me.addSpanWholeArea(bandMappingObj, bandMap, node, sheet, xPos, yPos);
                } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                    //2. 离散信息
                    me.addDiscreteSpan(node, xPos, yPos, bandMappingObj, sheet);
                }
            }
        } else if (rptTpl[JV.NODE_CROSS_INFO]) {
            //交叉式
            //目前暂缓
        }
    },
    brushSheet: function (bandMappingObj, sheet, xPos, yPos) {
        for (let bandMap of bandMappingObj.items) {
            let idxTop = yPos.indexOf(bandMap[JV.PROP_AREA][JV.PROP_TOP]);
            let idxBottom = yPos.indexOf(bandMap[JV.PROP_AREA][JV.PROP_BOTTOM]);
            if (idxTop >= 0 && idxBottom > idxTop) {
                sheet.getRange(idxTop, -1, idxBottom - idxTop, -1).backColor(bandMap[JV.PROP_COLOR]);
                let range = sheet.getRange(idxTop, -1, idxBottom - idxTop, -1);
                if (bandMap[JV.PROP_COLOR] === 'White') {
                    range.watermark("[Not Editable]");
                } else {
                    range.watermark(undefined);
                }
            }
        }
    },
    addDiscreteTxtFldPos: function (discreteItemNode, xPos, yPos, bandMappingObj) {
        let me = this;
        if (discreteItemNode.items) {
            for (let itemNode of discreteItemNode.items) {
                //子项
                let bandMap = bandMappingObj[itemNode[JV.PROP_BAND_NAME]];
                if (bandMap) {
                    for (let subItemNode of itemNode.items) {
                        for (let txtFldDtlNode of subItemNode.items) {
                            me.pushAbsPos(bandMap, txtFldDtlNode, xPos, yPos);
                        }
                    }
                }
            }
        }
    },
    addNormalTxtFldPos: function (bandMap, parentNode, xPos, yPos) {
        let me = this;
        if (bandMap && parentNode.items && parentNode.items.length > 0) {
            for (let itemNode of parentNode.items) {
                me.pushAbsPos(bandMap, itemNode, xPos, yPos);
            }
        }
    },
    addSpanWholeArea: function (bandMappingObj, bandMap, parentNode, sheet, xPos, yPos) {
        let me = this;
        if (bandMap && parentNode.items && parentNode.items.length > 0) {
            let bandW = bandMap[JV.PROP_AREA][JV.PROP_RIGHT] - bandMap[JV.PROP_AREA][JV.PROP_LEFT],
                bandH = bandMap[JV.PROP_AREA][JV.PROP_BOTTOM] - bandMap[JV.PROP_AREA][JV.PROP_TOP];
            for (let itemNode of parentNode.items) {
                // me.pushAbsPos(bandMap, itemNode, xPos, yPos);
                let pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_LEFT, bandW) + bandMap[JV.PROP_AREA][JV.PROP_LEFT];
                let idxLeft = xPos.indexOf(pos);
                pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_H_CALCULATION], JV.PROP_RIGHT, bandW) + bandMap[JV.PROP_AREA][JV.PROP_LEFT];
                let idxRight = xPos.indexOf(pos);
                pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_TOP, bandH) + bandMap[JV.PROP_AREA][JV.PROP_TOP];
                let idxTop = yPos.indexOf(pos);
                pos = me.getActPosEx(itemNode, itemNode[JV.PROP_AREA][JV.PROP_V_CALCULATION], JV.PROP_BOTTOM, bandH) + bandMap[JV.PROP_AREA][JV.PROP_TOP];
                let idxBottom = yPos.indexOf(pos);

                if (idxRight - idxLeft > 1 || idxBottom - idxTop > 1) {
                    sheet.addSpan(idxTop, idxLeft, idxBottom - idxTop, idxRight - idxLeft, GC.Spread.Sheets.SheetArea.viewport);
                }
                let cell = sheet.getCell(idxTop, idxLeft, GC.Spread.Sheets.SheetArea.viewport);
                if (itemNode.FieldID || itemNode.ParamID) {
                    me.changeCellType(cell, 'field', bandMap[JV.PROP_COLOR]);
                    let fpsAttr = me.createDftFieldPreSuf();
                    bandMappingObj.fieldPreSufAttr[idxTop + "_" + idxLeft + "_fieldPreSuf"] = fpsAttr;
                    fpsAttr[JV.PROP_PREFIX] = itemNode[JV.PROP_PREFIX];
                    fpsAttr[JV.PROP_SUFFIX] = itemNode[JV.PROP_SUFFIX];
                    fpsAttr[JV.PROP_FORMAT] = itemNode[JV.PROP_FORMAT];
                    fpsAttr[JV.PROP_DFT_VALUE] = itemNode[JV.PROP_DFT_VALUE];
                } else {
                    me.changeCellType(cell, 'text', bandMap[JV.PROP_COLOR]);
                }
                bandMappingObj.fontAttr[idxTop + "_" + idxLeft + "_font"] = itemNode[JV.PROP_FONT];
                me.setupCell(cell, itemNode);
                if ((itemNode.FieldID || itemNode.ParamID) && itemNode[JV.PROP_IS_AUTO_HEIGHT]) {
                    let value = cell.value();
                    cell.value(`{` + value + `}`);
                }
                //*
                let border = new GC.Spread.Sheets.LineBorder;
                border.color = "Black";
                border.style = GC.Spread.Sheets.LineStyle.thin;
                for (let iRow = 0; iRow < idxBottom - idxTop; iRow++) {
                    for (let iCol = 0; iCol < idxRight - idxLeft; iCol++) {
                        let cl = sheet.getCell(iRow + idxTop, iCol + idxLeft, GC.Spread.Sheets.SheetArea.viewport);
                        me.private_setCellStyle(cl, itemNode, border);
                    }
                }
                //*/
            }
        }
    },
    createDftFieldPreSuf: function () {
        let rst = {};
        rst[JV.PROP_PREFIX] = ``;
        rst[JV.PROP_SUFFIX] = ``;
        rst[JV.PROP_FORMAT] = ``;
        rst[JV.PROP_DFT_VALUE] = ``;
        return rst;
    },
    addDiscreteSpan: function (discreteItemNode, xPos, yPos, bandMappingObj, sheet) {
        let me = this;
        if (discreteItemNode.items) {
            for (let itemNode of discreteItemNode.items) {
                //子项
                let bandMap = bandMappingObj[itemNode[JV.PROP_BAND_NAME]];
                if (bandMap) {
                    for (let subItemNode of itemNode.items) {
                        me.addSpanWholeArea(bandMappingObj, bandMap, subItemNode, sheet, xPos, yPos);
                    }
                }
            }
        }
    },
    setupBorder: function (cell, border) {
        cell.borderLeft(border);
        cell.borderRight(border);
        cell.borderTop(border);
        cell.borderBottom(border);
    },
    copyPropertiesForSplittedCell: function (srcCell, destCell) {
        //1. font
        destCell.font(srcCell.font());
        //2. bk color
        destCell.backColor(srcCell.backColor());
        //3. border
        destCell.borderLeft(srcCell.borderLeft());
        destCell.borderRight(srcCell.borderRight());
        destCell.borderTop(srcCell.borderTop());
        destCell.borderBottom(srcCell.borderBottom());
    }
};