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

let visualJumbo = {
    tplWorkBook: null,
    bandMappingObj: null,
    iniSpreadJs: function () {
        let me = this;
        if (me.tplWorkBook !== null) {
            me.tplWorkBook.destroy();
            me.tplWorkBook = null;
        }
        me.tplWorkBook = new GC.Spread.Sheets.Workbook($('#ele_visual_div_main3')[0], {sheetCount: 1});
        me.tplWorkBook.options.tabStripVisible = false;
        me.tplWorkBook.options.allowCopyPasteExcelStyle = false;
        me.tplWorkBook.options.allowUserDragDrop = false;
        me.tplWorkBook.options.allowContextMenu = false;
        let sheet = me.tplWorkBook.getActiveSheet();
        sheet.options.allowCellOverflow = false;
        sheet.options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
        sheet.bind(GC.Spread.Sheets.Events.CellClick, me.onCellEnter);
    },
    setupTpl: function () {
        let me = this;
        let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
        if (rptTpl && me.tplWorkBook) {
            let sheet  = me.tplWorkBook.getActiveSheet();
            let border = new GC.Spread.Sheets.LineBorder;
            border.color = "Black";
            border.style = GC.Spread.Sheets.LineStyle.thin;
            let pageH, pageW;
            let pIdx = JV.PAGES_SIZE_STR.indexOf(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]);
            pageW = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
            pageH = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM])) /2.54*96 );
            if (rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE ||
                rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE_CHN) {
                pageW = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
                pageH = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_TOP]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_BOTTOM])) /2.54*96 );
            }
            sheet.suspendPaint();
            sheet.setRowCount(0);
            sheet.setColumnCount(0);
            if (rptTpl[JV.NODE_FLOW_INFO] || rptTpl[JV.NODE_BILL_INFO]) {
                //流水式/账单式
                me.setupFlowBillTpl(rptTpl, sheet, border, pageW, pageH);
            } else if (rptTpl[JV.NODE_CROSS_INFO]) {
                //交叉式
                //目前暂缓
                me.setupCrossTpl(rptTpl, sheet, border, pageW, pageH);
            }
            sheet.clearSelection();
            sheet.resumePaint();
        }
    },
    setupFlowBillTpl: function (rptTpl, sheet, border, pageW, pageH) {
        let me = this;
        //1. put the pos into array
        let xPos = [0], yPos = [0];
        xPos.push(pageW);
        yPos.push(pageH);
        let bandMappingObj = {};
        me.bandMappingObj = bandMappingObj;
        visualCommonOprObj.addBandPos(rptTpl, pageW, pageH, xPos, yPos, bandMappingObj);
        visualCommonOprObj.addTplTxtFldPos(rptTpl, dataInfoMapTreeOprObj.treeObj, xPos, yPos, bandMappingObj);
        //2. 设置spreadjs
        xPos.sort(function(x1, x2){ return (x1 - x2); });
        yPos.sort(function(y1, y2){ return (y1 - y2); });
        sheet.setRowCount(yPos.length - 1);
        sheet.setColumnCount(xPos.length - 1);
        for (let idx = 1; idx < xPos.length; idx++) {
            sheet.setColumnWidth(idx - 1, xPos[idx] - xPos[idx - 1]);
        }
        for (let idx = 1; idx < yPos.length; idx++) {
            sheet.setRowHeight(idx - 1, yPos[idx] - yPos[idx - 1]);
            sheet.getRange(idx - 1, -1, 1, -1).backColor(undefined);
        }
        visualCommonOprObj.brushSheet(bandMappingObj, sheet, xPos, yPos);
        visualCommonOprObj.setupTplTxtFld(rptTpl, dataInfoMapTreeOprObj.treeObj, sheet, xPos, yPos, bandMappingObj)
        for (let idx = 1; idx < yPos.length; idx++) {
            let bkc = sheet.getCell(idx - 1, 0).backColor();
            if (bkc === 'White') {
                sheet.setRowHeight(idx - 1, 30);
            }
        }
        for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
            for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
                let cell = sheet.getCell(iRow, iCol);
                if (cell.backColor() === 'LightGray' || cell.backColor() === 'LightCyan') {
                    visualCommonOprObj.setupBorder(sheet.getCell(iRow, iCol), border);
                } else if (cell.backColor() === 'White') {
                    sheet.addSpan(iRow, 0, 1, sheet.getColumnCount());
                    break;
                }
            }
        }
    },
    setupCrossTpl: function (rptTpl, sheet, border) {

    },
    onCellEnter: function (sender, args) {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        me.setupCfg(sheet, args.row, args.col);
    },
    setupCfg: function (sheet, row, col) {
        let me = visualJumbo,
            cell = sheet.getCell(row, col);
        if (cell.backColor() !== 'White') {
            //0. 设置UI
            $(`#visElementFonts`)[0].removeAttribute("disabled");
            $(`#visRdIsText`)[0].removeAttribute("disabled");
            $(`#visRdIsField`)[0].removeAttribute("disabled");
            $(`#eleIsAutoHeightVis`)[0].removeAttribute("disabled");
            $(`#font_other_properties_div`)[0].style.display = "none";
            me.private_ShowBandName(cell);
            //1. font
            let fontAttr = me.bandMappingObj.fontAttr[row + "_" + col + "_font"];
            if (fontAttr) {
                me.private_setup_font(fontAttr);
            }
            //2. alignment
            switch (cell.hAlign()) {
                case GC.Spread.Sheets.HorizontalAlign.center:
                    $(`#hAlign_left`)[0].className = "btn btn-sm btn-outline-secondary";
                    $(`#hAlign_center`)[0].className = "btn btn-sm btn-outline-secondary active";
                    $(`#hAlign_right`)[0].className = "btn btn-sm btn-outline-secondary";
                    break;
                case GC.Spread.Sheets.HorizontalAlign.right:
                    $(`#hAlign_left`)[0].className = "btn btn-sm btn-outline-secondary";
                    $(`#hAlign_center`)[0].className = "btn btn-sm btn-outline-secondary";
                    $(`#hAlign_right`)[0].className = "btn btn-sm btn-outline-secondary active";
                    break;
                case GC.Spread.Sheets.HorizontalAlign.left:
                default:
                    $(`#hAlign_left`)[0].className = "btn btn-sm btn-outline-secondary active";
                    $(`#hAlign_center`)[0].className = "btn btn-sm btn-outline-secondary";
                    $(`#hAlign_right`)[0].className = "btn btn-sm btn-outline-secondary";
                    break;
            }
            switch (cell.vAlign()) {
                case GC.Spread.Sheets.VerticalAlign.bottom:
                    $(`#vAlign_center`)[0].className = "btn btn-sm btn-outline-secondary";
                    $(`#vAlign_bottom`)[0].className = "btn btn-sm btn-outline-secondary active";
                    break;
                case GC.Spread.Sheets.VerticalAlign.center:
                default:
                    $(`#vAlign_center`)[0].className = "btn btn-sm btn-outline-secondary active";
                    $(`#vAlign_bottom`)[0].className = "btn btn-sm btn-outline-secondary";
                    break;
            }
            //3. other control
            if (cell.shrinkToFit()) {
                $(`#vis_shrink`)[0].className = "btn btn-sm btn-outline-secondary active";
            } else {
                $(`#vis_shrink`)[0].className = "btn btn-sm btn-outline-secondary";
            }
            if (cell.wordWrap()) {
                $(`#vis_wrap`)[0].className = "btn btn-sm btn-outline-secondary active";
            } else {
                $(`#vis_wrap`)[0].className = "btn btn-sm btn-outline-secondary";
            }
            let border = cell.borderLeft();
            let bottomBorder = cell.borderBottom();
            if (border && border.style === GC.Spread.Sheets.LineStyle.thin) {
                $(`#vis_outter_border`)[0].className = "btn btn-sm btn-outline-secondary active";
                $(`#vis_bottom_border`)[0].className = "btn btn-sm btn-outline-secondary";
                $(`#vis_no_border`)[0].className = "btn btn-sm btn-outline-secondary";
            } else if (bottomBorder && bottomBorder.style === GC.Spread.Sheets.LineStyle.thin){
                $(`#vis_outter_border`)[0].className = "btn btn-sm btn-outline-secondary";
                $(`#vis_bottom_border`)[0].className = "btn btn-sm btn-outline-secondary active";
                $(`#vis_no_border`)[0].className = "btn btn-sm btn-outline-secondary";
            } else {
                $(`#vis_outter_border`)[0].className = "btn btn-sm btn-outline-secondary";
                $(`#vis_bottom_border`)[0].className = "btn btn-sm btn-outline-secondary";
                $(`#vis_no_border`)[0].className = "btn btn-sm btn-outline-secondary active";
            }
            //4. txt/field
            let val = cell.value();
            if (cell.cellType() && cell.cellType().typeName === '7') {
                $(`#visRdIsField`)[0].checked = true;
                $(`#lblIsAutoHeightVis`)[0].style.display = "";
                me.private_enable_pre_suff(cell);
                me.private_set_field_to_pre_suf(cell);
                if (!stringUtil.isEmptyString(val) && val.indexOf(`{`) === 0) {
                    $(`#eleIsAutoHeightVis`)[0].checked = true;
                } else {
                    $(`#eleIsAutoHeightVis`)[0].checked = false;
                }
            } else if (stringUtil.isEmptyString(val) && (!border || border.style === GC.Spread.Sheets.LineStyle.empty)) {
                $(`#visRdIsBlank`)[0].checked = true;
                $(`#lblIsAutoHeightVis`)[0].style.display = "none";
                me.private_disable_pre_suff();

            } else {
                $(`#visRdIsText`)[0].checked = true;
                $(`#lblIsAutoHeightVis`)[0].style.display = "none";
                me.private_disable_pre_suff();
            }
        } else {
            $("#visElementFonts")[0].disabled = "disabled" ;
            $("#visRdIsText")[0].disabled = "disabled" ;
            $("#visRdIsField")[0].disabled = "disabled" ;
            $("#eleIsAutoHeightVis")[0].disabled = "disabled" ;
            $("#id_vis_setup_lbl")[0].innerHTML = '...';
            me.private_disable_pre_suff();
        }
    },
    private_disable_pre_suff: function () {
        let ele1 = $(`#elePrefix_Vis`)[0];
        let ele2 = $(`#eleFormat_Vis`)[0];
        let ele3 = $(`#eleDftValue_Vis`)[0];
        let ele4 =$(`#eleSuffix_Vis`)[0];
        ele1.disabled = "disabled" ;
        ele2.disabled = "disabled" ;
        ele3.disabled = "disabled" ;
        ele4.disabled = "disabled" ;
        ele1.value = ``;
        ele2.value = ``;
        ele3.value = ``;
        ele4.value = ``;
    },
    private_enable_pre_suff: function (cell) {
        $(`#elePrefix_Vis`)[0].removeAttribute("disabled");
        $(`#eleFormat_Vis`)[0].removeAttribute("disabled");
        $(`#eleDftValue_Vis`)[0].removeAttribute("disabled");
        $(`#eleSuffix_Vis`)[0].removeAttribute("disabled");
    },
    private_set_field_to_pre_suf: function (cell) {
        let me = visualJumbo;
        let ele1 = $(`#elePrefix_Vis`)[0];
        let ele2 = $(`#eleFormat_Vis`)[0];
        let ele3 = $(`#eleDftValue_Vis`)[0];
        let ele4 =$(`#eleSuffix_Vis`)[0];
        let fps = me.bandMappingObj.fieldPreSufAttr[cell.row + "_" + cell.col + "_fieldPreSuf"];
        if (fps) {
            ele1.value = (stringUtil.isEmptyString(fps[JV.PROP_PREFIX]))?``:fps[JV.PROP_PREFIX];
            ele2.value = (stringUtil.isEmptyString(fps[JV.PROP_FORMAT]))?``:fps[JV.PROP_FORMAT];
            ele3.value = (stringUtil.isEmptyString(fps[JV.PROP_DFT_VALUE]))?``:fps[JV.PROP_DFT_VALUE];
            ele4.value = (stringUtil.isEmptyString(fps[JV.PROP_SUFFIX]))?``:fps[JV.PROP_SUFFIX];
        } else {
            ele1.value = ``;
            ele2.value = ``;
            ele3.value = ``;
            ele4.value = ``;
        }
    },
    restoreTpl: function () {
        let me = visualJumbo;
        if (me.tplWorkBook) {
            me.setupTpl();
        }
    },
    private_ShowBandName: function (cell) {
        let me = visualJumbo;
        if (me.bandMappingObj) {
            let color = cell.backColor();
            if (color && color !== 'White') {
                for (let bItem of me.bandMappingObj.items) {
                    if (bItem[JV.PROP_COLOR] === color) {
                        $("#id_vis_setup_lbl")[0].innerHTML = bItem[JV.PROP_BAND_NAME];
                        break;
                    }
                }
            } else {
                $("#id_vis_setup_lbl")[0].innerHTML = '...';
            }
        }
    },
    private_AddCols: function (sheet, col) {
        sheet.suspendPaint();
        sheet.addColumns(col, 1);
        sheet.resumePaint();
    },
    insertColumn: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            me.private_AddCols(sheet, selectedRanges[0].col);
        }
    },
    addColumn: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        me.private_AddCols(sheet, sheet.getColumnCount());
    },
    splitColumn: function (newColAmt) {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            if (selectedRanges[0].colCount <= newColAmt) {
                let width = 0;
                for (let iCol = 0; iCol < selectedRanges[0].colCount; iCol++) {
                    width += sheet.getColumnWidth(selectedRanges[0].col + iCol);
                }
                let avgW = Math.round(width / newColAmt),
                    lastW = width - avgW * (newColAmt - 1);
                sheet.suspendPaint();
                sheet.addColumns(selectedRanges[0].col + selectedRanges[0].colCount, newColAmt - selectedRanges[0].colCount);
                sheet.setColumnWidth(selectedRanges[0].col, lastW);
                for (let iCol = 1; iCol < newColAmt; iCol++) {
                    sheet.setColumnWidth(selectedRanges[0].col + iCol, avgW);
                }
                for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
                    let cell = sheet.getCell(iRow, selectedRanges[0].col);
                    for (let iCol = selectedRanges[0].colCount; iCol < newColAmt; iCol++) {
                        visualCommonOprObj.copyPropertiesForSplittedCell(cell, sheet.getCell(iRow, iCol + selectedRanges[0].col));
                    }
                }
                sheet.resumePaint();
            } else {
                alert(`您选择的列数量比要拆分的数量还大！`);
            }
        }
    },
    deleteColumn: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        let cc = sheet.getColumnCount();
        if (cc > 1) {
            if (selectedRanges.length > 0) {
                sheet.deleteColumns(selectedRanges[0].col, 1);
            }
        } else {
            alert('不能删除列！');
        }
    },
    addRow: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            let color = cell.backColor();
            if (color !== 'White') {
                let nextIdx = sheet.getRowCount();
                for (let idx = selectedRanges[0].row + 1; idx < sheet.getRowCount(); idx++) {
                    if (sheet.getCell(idx, selectedRanges[0].col).backColor() !== color) {
                        nextIdx = idx;
                        break;
                    }
                }
                sheet.suspendPaint();
                sheet.addRows(nextIdx, 1);
                sheet.getRange(nextIdx, -1, 1, -1).backColor(color);
                sheet.resumePaint();
            }
        }
    },
    splitRows: function (newRowAmt) {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let firstCell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            let isSameBand = true;
            for (let iRow = 1; iRow < selectedRanges[0].rowCount; iRow++) {
                if (sheet.getCell(iRow + selectedRanges[0].row, 0).backColor() !== firstCell.backColor()) {
                    isSameBand = false;
                    break;
                }
            }
            if (isSameBand) {
                if (firstCell.backColor() !== 'White') {
                    if (selectedRanges[0].rowCount <= newRowAmt) {
                        let height = 0;
                        for (let iRow = 0; iRow < selectedRanges[0].rowCount; iRow++) {
                            height += sheet.getRowHeight(selectedRanges[0].row + iRow);
                        }
                        let avgH = Math.round(height / newRowAmt),
                            lastH = height - avgH * (newRowAmt - 1);
                        sheet.suspendPaint();
                        sheet.addRows(selectedRanges[0].row + selectedRanges[0].rowCount, newRowAmt - selectedRanges[0].rowCount);
                        sheet.setRowHeight(selectedRanges[0].row, lastH);
                        for (let iRow = 1; iRow < newRowAmt; iRow++) {
                            sheet.setRowHeight(selectedRanges[0].row + iRow, avgH);
                        }
                        for (let iRow = selectedRanges[0].rowCount; iRow < newRowAmt; iRow++) {
                            let cell = sheet.getCell(selectedRanges[0].row, 0);
                            for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
                                visualCommonOprObj.copyPropertiesForSplittedCell(cell, sheet.getCell(iRow + selectedRanges[0].row, iCol));
                            }
                        }
                        sheet.resumePaint();
                    } else {
                        alert(`您选择的行数量比要拆分的数量还大！`);
                    }
                }
            } else {
                alert(`不能拆分跨界行！`);
            }
        }
    },
    deleteRow: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            let color = cell.backColor();
            if (color !== 'White') {
                let amt = 0;
                for (let idx = 0; idx < sheet.getRowCount(); idx++) {
                    if (sheet.getCell(idx, selectedRanges[0].col).backColor() === color) {
                        amt++
                    }
                }
                if (amt > 1) {
                    sheet.suspendPaint();
                    sheet.deleteRows(selectedRanges[0].row, 1);
                    sheet.resumePaint();
                }
            }
        }
    },
    fitTheWidth: function (factor) {
        let me = this, rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
        let pIdx = JV.PAGES_SIZE_STR.indexOf(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_PAGE_SIZE]);
        let bandW = 700;
        if (pIdx >= 0) {
            bandW = Math.round(JV.PAGES_SIZE[pIdx][0] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
            if (rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE ||
                rptTpl[JV.NODE_MAIN_INFO][JV.NODE_PAGE_INFO][JV.PROP_ORIENTATION] === JV.ORIENTATION_LANDSCAPE_CHN) {
                bandW = Math.round(JV.PAGES_SIZE[pIdx][1] * 96 - (parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_LEFT]) + parseFloat(rptTpl[JV.NODE_MAIN_INFO][JV.NODE_MARGINS][JV.PROP_RIGHT])) /2.54*96 );
            }
        }
        bandW *= factor;
        let currentWidth = 0;
        let sheet = me.tplWorkBook.getActiveSheet();
        for (let idx = 0; idx < sheet.getColumnCount(); idx++) {
            currentWidth += sheet.getColumnWidth(idx);
        }
        let actFactor = bandW / currentWidth;
        for (let idx = 0; idx < sheet.getColumnCount(); idx++) {
            sheet.setColumnWidth(idx, Math.round(sheet.getColumnWidth(idx) * actFactor));
        }
    },
    mergeCells: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            let color = cell.backColor();
            let isDifferent = false;
            for (let idx = 1; idx < selectedRanges[0].rowCount; idx++) {
                if (sheet.getCell(selectedRanges[0].row + idx, 0).backColor() !== color) {
                    isDifferent = true;
                    break;
                }
            }
            if (!isDifferent) {
                sheet.suspendPaint();
                sheet.addSpan(selectedRanges[0].row, selectedRanges[0].col, selectedRanges[0].rowCount, selectedRanges[0].colCount);
                sheet.resumePaint();
            } else {
                alert(`不能合并跨界单元格！`)
            }
        }
    },
    disMergeCells: function () {
        let me = visualJumbo,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        let spans = sheet.getSpans();
        if (selectedRanges.length > 0 && spans.length > 0) {
            let selectedSpans = [];
            for(let i = 0; i < spans.length; i++)
            {
                for (let j = 0; j < selectedRanges.length; j++) {
                    if (spans[i].row >= selectedRanges[j].row && spans[i].col >= selectedRanges[j].col &&
                        spans[i].row < selectedRanges[j].row + selectedRanges[j].rowCount && spans[i].col < selectedRanges[j].col + selectedRanges[j].colCount) {
                        selectedSpans.push(spans[i]);
                    }
                }
            }
            for (let span of selectedSpans) {
                sheet.removeSpan(span.row, span.col, GC.Spread.Sheets.SheetArea.viewport);
            }
        }
    },
    changeFontSet: function (dom) {
        let me = visualJumbo, fontAttr = dom.value,
            sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            if (fontAttr === "自定义") {
                fontAttr = {};
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = "宋体";
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]] = 12;
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_COLOR]] = "BLACK";
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = "F"; //bold
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = "F"; //italic
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = "F"; //underline
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_STRIKEOUT]] = "F"; //strikeout
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ANGLE]] = 0;   //angle
            }
            me.bandMappingObj.fontAttr[selectedRanges[0].row + "_" + selectedRanges[0].col + "_font"] = fontAttr;
            me.private_setup_font(fontAttr);
            if (typeof fontAttr === 'string') {
                let idx = rpt_tpl_cfg_helper.reportCfg.fontArr.indexOf(fontAttr);
                fontAttr = rpt_tpl_cfg_helper.reportCfg.fonts[idx];
            }
            me.private_setup_cell_font(cell, fontAttr);
        }
    },
    setupCellFont: function () {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let fontAttr = $(`#visElementFonts`)[0].value;
            if (fontAttr === "自定义") {
                fontAttr = {};
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_NAME]] = $(`#visElementFontNames`)[0].value;
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_HEIGHT]] = $(`#visEleFontSize`)[0].value;
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]] = ($("#vis_font_bold")[0].className === "btn btn-sm btn-outline-secondary")?'F':'T'; //bold
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]] = ($("#vis_font_italic")[0].className === "btn btn-sm btn-outline-secondary")?'F':'T'; //italic
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]] = ($("#vis_font_underline")[0].className === "btn btn-sm btn-outline-secondary")?'F':'T'; //underline
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_STRIKEOUT]] = "F"; //strikeout
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_COLOR]] = "BLACK";
                fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ANGLE]] = 0;   //angle
                me.bandMappingObj.fontAttr[selectedRanges[0].row + "_" + selectedRanges[0].col + "_font"] = fontAttr;
            } else {
                let idx = rpt_tpl_cfg_helper.reportCfg.fontArr.indexOf(fontAttr);
                fontAttr = rpt_tpl_cfg_helper.reportCfg.fonts[idx];
                me.bandMappingObj.fontAttr[selectedRanges[0].row + "_" + selectedRanges[0].col + "_font"] = fontAttr;
            }
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            sheet.suspendPaint();
            me.private_setup_cell_font(cell, fontAttr);
            sheet.resumePaint();
        }
    },
    setupCellAlignment: function () {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            // let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            sheet.suspendPaint();
            for (let iRow = 0; iRow < selectedRanges[0].rowCount; iRow++) {
                for (let iCol = 0; iCol < selectedRanges[0].colCount; iCol++) {
                    let cell = sheet.getCell(selectedRanges[0].row + iRow, selectedRanges[0].col + iCol);
                    //1. horizon alignment
                    if ($(`#hAlign_center`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
                    } else if ($(`#hAlign_left`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
                    } else if ($(`#hAlign_right`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
                    }
                    //2. vertical alignment
                    if ($(`#vAlign_center`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.vAlign(GC.Spread.Sheets.VerticalAlign.center);
                    } else if ($(`#vAlign_bottom`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
                    }
                    //3. shrink
                    if ($(`#vis_shrink`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.shrinkToFit(true);
                    } else {
                        cell.shrinkToFit(false);
                    }
                    //4. wrap
                    if ($(`#vis_wrap`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                        cell.wordWrap(true);
                    } else {
                        cell.wordWrap(false);
                    }
                }
            }
            sheet.resumePaint();
        }
    },
    setupCellBorder: function () {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            // let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            sheet.suspendPaint();
            let border = new GC.Spread.Sheets.LineBorder;
            border.color = "Black";
            if ($(`#vis_no_border`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                border.style = GC.Spread.Sheets.LineStyle.empty;
            } else {
                border.style = GC.Spread.Sheets.LineStyle.thin;
            }
            let cellRange = new GC.Spread.Sheets.CellRange(sheet, selectedRanges[0].row, selectedRanges[0].col, selectedRanges[0].rowCount, selectedRanges[0].colCount);
            if ($(`#vis_bottom_border`)[0].className === `btn btn-sm btn-outline-secondary active`) {
                let bottomBorder = new GC.Spread.Sheets.LineBorder;
                bottomBorder.color = "Black";
                bottomBorder.style = GC.Spread.Sheets.LineStyle.empty;
                cellRange.setBorder(bottomBorder, {all: true});
                cellRange.setBorder(border, {bottom: true});
            } else {
                cellRange.setBorder(border, {all: true});
            }
            sheet.resumePaint();
        }
    },
    private_setup_cell_font: function (cell, fontAttr) {
        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);
    },
    private_setup_font: function(fontAttr) {
        if (typeof fontAttr === "string" && fontAttr !== "自定义") {
            $("#visElementFontNames")[0].disabled = "disabled" ;
            $("#visEleFontSize")[0].disabled = "disabled" ;
            let idx = rpt_tpl_cfg_helper.reportCfg.fontArr.indexOf(fontAttr);
            let font = rpt_tpl_cfg_helper.reportCfg.fonts[idx];
            $("#visElementFonts")[0].selectedIndex = idx;
            $("#visElementFontNames")[0].value = font.Name;
            $("#visEleFontSize")[0].value = parseInt(font.FontHeight);

            $("#font_other_properties_div")[0].style.display = "none";
            $("#vis_font_bold")[0].className = "btn btn-sm btn-outline-secondary";
            $("#vis_font_italic")[0].className = "btn btn-sm btn-outline-secondary";
            $("#vis_font_underline")[0].className = "btn btn-sm btn-outline-secondary";
        } else {
            $("#visElementFontNames")[0].removeAttribute("disabled");
            $("#visEleFontSize")[0].removeAttribute("disabled");
            $("#font_other_properties_div")[0].style.display = "";
            $("#visElementFonts")[0].value = "自定义";
            $("#visElementFontNames")[0].value = fontAttr.Name;
            $("#visEleFontSize")[0].value = parseInt(fontAttr.FontHeight);
            if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_BOLD]])) {
                $("#vis_font_bold")[0].className = "btn btn-sm btn-outline-secondary active";
            }
            if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_ITALIC]])) {
                $("#vis_font_italic")[0].className = "btn btn-sm btn-outline-secondary active";
            }
            if (stringUtil.convertStrToBoolean(fontAttr[JV.FONT_PROPS[JV.FONT_PROP_IDX_UNDERLINE]])) {
                $("#vis_font_underline")[0].className = "btn btn-sm btn-outline-secondary active";
            }
        }
    },
    fontBoldChange: function (dom) {
        let me = this;
        me.private_reverse_class(dom);
        me.setupCellFont();
    },
    fontItalicChange: function (dom) {
        let me = this;
        me.private_reverse_class(dom);
        me.setupCellFont();
    },
    fontUnderlineChange: function (dom) {
        let me = this;
        me.private_reverse_class(dom);
        me.setupCellFont();
    },
    changeShrink: function (dom) {
        let me = this;
        me.private_reverse_class(dom);
        me.setupCellAlignment();
    },
    changeWrap: function (dom) {
        let me = this;
        me.private_reverse_class(dom);
        me.setupCellAlignment();
    },
    changeIsAutoHeight: function (dom) {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            if (cell.cellType() && cell.cellType().typeName === '7') {
                sheet.suspendPaint();
                let val = cell.value();
                if (dom.checked) {
                    if (val.indexOf(`{`) < 0) {
                        cell.value(`{` + val + `}`);
                    }
                } else {
                    if (val.indexOf(`{`) === 0) {
                        cell.value(val.replace('{', '').replace('}',''));
                    }
                }
                sheet.resumePaint();
            }
        }
    },
    private_reverse_class: function (dom) {
        if (dom.className === "btn btn-sm btn-outline-secondary") {
            dom.className = "btn btn-sm btn-outline-secondary active";
        } else {
            dom.className = "btn btn-sm btn-outline-secondary";
        }
    },
    changeHAlign: function (dom) {
        let me = this;
        if (dom.className === "btn btn-sm btn-outline-secondary") {
            dom.className = "btn btn-sm btn-outline-secondary active"
            let hKeys = [`hAlign_left`, `hAlign_center`, `hAlign_right`];
            let domIdx = hKeys.indexOf(dom.id);
            if (domIdx >= 0) {
                for (let idx = 0; idx < hKeys.length; idx++) {
                    if (idx !== domIdx) {
                        $(`#` + hKeys[idx])[0].className = "btn btn-sm btn-outline-secondary";
                    }
                }
            }
            me.setupCellAlignment();
        }
    },
    changeVAlign: function (dom) {
        let me = this;
        if (dom.className === "btn btn-sm btn-outline-secondary") {
            dom.className = "btn btn-sm btn-outline-secondary active"
            let hKeys = [`vAlign_top`, `vAlign_center`, `vAlign_bottom`];
            let domIdx = hKeys.indexOf(dom.id);
            if (domIdx >= 0) {
                for (let idx = 0; idx < hKeys.length; idx++) {
                    if (idx !== domIdx) {
                        $(`#` + hKeys[idx])[0].className = "btn btn-sm btn-outline-secondary";
                    }
                }
            }
            me.setupCellAlignment();
        }
    },
    changeBorder: function (dom) {
        let me = this;
        if (dom.className === "btn btn-sm btn-outline-secondary") {
            dom.className = "btn btn-sm btn-outline-secondary active"
            let hKeys = [`vis_outter_border`, `vis_no_border`, `vis_bottom_border`];
            let domIdx = hKeys.indexOf(dom.id);
            if (domIdx >= 0) {
                for (let idx = 0; idx < hKeys.length; idx++) {
                    if (idx !== domIdx) {
                        $(`#` + hKeys[idx])[0].className = "btn btn-sm btn-outline-secondary";
                    }
                }
            }
            me.setupCellBorder();
        }
    },
    changeCellType: function (newType) {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            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 = visualCommonOprObj.getAllSelectedFields();
                    cellType.items(selectableFields);
                    cell.cellType(cellType);
                    break;
                case `text`:
                default:
                    cell.cellType(new GC.Spread.Sheets.CellTypes.Text());
                    cell.value(``);
                    break;
            }
        }
    },
    changePreSuf: function (typeStr, dom) {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let selectedRanges = sheet.getSelections();
        if (selectedRanges.length > 0) {
            let cell = sheet.getCell(selectedRanges[0].row, selectedRanges[0].col);
            let fps = me.bandMappingObj.fieldPreSufAttr[cell.row + "_" + cell.col + "_fieldPreSuf"];
            if (!fps) {
                fps = visualCommonOprObj.createDftFieldPreSuf();
                me.bandMappingObj.fieldPreSufAttr[cell.row + "_" + cell.col + "_fieldPreSuf"] = fps;
            }
            fps[typeStr] = dom.value;
        }
    },
    applyBack: function () {
        let me = visualJumbo, sheet = me.tplWorkBook.getActiveSheet();
        let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
        let xPos = [0], yPos = [0];
        for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
            xPos.push(sheet.getRowHeight(iRow) + xPos[xPos.length - 1]);
            for (let iCol = 0; iCol < sheet.getColumnCount(); iCol++) {
                yPos.push(sheet.getColumnWidth(iCol) + yPos[yPos.length - 1]);
            }
        }

        if (rptTpl[JV.NODE_FLOW_INFO]) {
            //流水式
            me.applyBack_Flow(rptTpl, sheet, xPos, yPos);
        } else if (rptTpl[JV.NODE_BILL_INFO]) {
            //账单式
            me.applyBack_Bill(rptTpl, sheet, xPos, yPos);
        } else if (rptTpl[JV.NODE_CROSS_INFO]) {
            //交叉式
            //目前暂缓
            me.applyBack_Cross(rptTpl, sheet, xPos, yPos);
        }
    },
    applyBack_Flow: function (rptTpl, sheet, xPos, yPos) {
        let me = this;
        let startRow = -1, handledBands = [];
        let discreteNodesArr = [];
        //先清除所有相关text/field节点，然后再重新生成
        me.private_clear_flow_txt_fld_nodes();
        let textFldArr = [], colWidthArr = [], rowHeightArr = [];
        //1. 锚定column / content(& grouping)
        let columnBand = visualCommonOprObj.getBandEx(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_COLUMN][JV.PROP_BAND_NAME], rptTpl);
        handledBands.push(columnBand[JV.PROP_NAME]);
        me.private_setup_bandHeight(columnBand[JV.PROP_NAME], columnBand, sheet);
        startRow = me.collectSheetTxtFldByArea(sheet, me.bandMappingObj[columnBand[JV.PROP_NAME]][JV.PROP_COLOR] , textFldArr, colWidthArr, rowHeightArr);
        let columnNodes = [];
        let columnDisFieldArr = null;
        for (let txtFld of textFldArr) {
            if (txtFld.isField) {
                if (columnDisFieldArr === null) {
                    columnDisFieldArr = {Name: "子项", BandName: columnBand[JV.PROP_NAME], items: [[],[]], isParent: true};
                    discreteNodesArr.push(columnDisFieldArr);
                }
                let fldNode = me.private_create_field_param_node(sheet, txtFld, startRow, colWidthArr, rowHeightArr, rptTpl);
                columnDisFieldArr.items[0].push(fldNode);
            } else {
                let node = me.createTxtNode(txtFld, sheet, startRow, colWidthArr, rowHeightArr);
                columnNodes.push(node);
            }
        }

        let cotentBand = visualCommonOprObj.getBandEx(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_BAND_NAME], rptTpl);
        handledBands.push(cotentBand[JV.PROP_NAME]);
        me.private_setup_flow_content_height(rptTpl, cotentBand[JV.PROP_NAME], sheet);
        textFldArr = [];
        colWidthArr = [];
        rowHeightArr = [];
        startRow = me.collectSheetTxtFldByArea(sheet, me.bandMappingObj[cotentBand[JV.PROP_NAME]][JV.PROP_COLOR] , textFldArr, colWidthArr, rowHeightArr);
        let contentNodes = [];
        for (let field of textFldArr) {
            if (field.isField) {
                let node = me.private_create_field_param_node(sheet, field, startRow, colWidthArr, rowHeightArr, null);
                contentNodes.push(node);
            }
        }

        //2. 锚定 统计类型
        let pageTotalBand = visualCommonOprObj.getBandEx(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM][JV.PROP_BAND_NAME], rptTpl);
        let pageSummaryNodes = [[], []];
        if (pageTotalBand) {
            handledBands.push(pageTotalBand[JV.PROP_NAME]);
            me.private_setup_bandHeight(pageTotalBand[JV.PROP_NAME], pageTotalBand, sheet);
            me.private_build_txt_fld_nodes(pageTotalBand, sheet, rptTpl, pageSummaryNodes);
        }

        let segTotalBand = visualCommonOprObj.getBandEx(rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM][JV.PROP_BAND_NAME], rptTpl);
        let segSummaryNodes = [[], []];
        if (segTotalBand) {
            handledBands.push(segTotalBand[JV.PROP_NAME]);
            me.private_setup_bandHeight(segTotalBand[JV.PROP_NAME], segTotalBand, sheet);
            me.private_build_txt_fld_nodes(segTotalBand, sheet, rptTpl, segSummaryNodes);
        }
        //3. 其他就归类到离散去了
        if (me.bandMappingObj && me.bandMappingObj.items && me.bandMappingObj.items.length > 0) {
            for (let sBandMap of me.bandMappingObj.items) {
                if (handledBands.indexOf(sBandMap[JV.PROP_BAND_NAME]) < 0 && sBandMap[JV.PROP_COLOR] !== 'White') {
                    let sArr = {Name: "子项", BandName: sBandMap[JV.PROP_BAND_NAME], items: [[],[]], isParent: true};
                    let sBand = visualCommonOprObj.getBandEx(sBandMap[JV.PROP_BAND_NAME], rptTpl);
                    if (sBand) {
                        me.private_setup_bandHeight(sBand[JV.PROP_NAME], sBand, sheet);
                        me.private_build_txt_fld_nodes(sBand, sheet, rptTpl, sArr.items);
                    }
                    if (sArr.items[0].length > 0 || sArr.items[1].length > 0) {
                        discreteNodesArr.push(sArr);
                    }
                }
            }
        }
        //4. 最后生成节点
        me.private_create_tpl_flow_nodes(columnNodes, contentNodes, pageSummaryNodes, segSummaryNodes, null, discreteNodesArr);
        displayMessage("应用提交成功！", "green", 5000, "id_vis_setup_lbl");
    },
    private_build_txt_fld_nodes: function (band, sheet, rptTpl, destNodes) {
        let me = this;
        me.private_setup_bandHeight(band[JV.PROP_NAME], band, sheet);
        let textFldArr = [], colWidthArr = [], rowHeightArr = [];
        let startRow = me.collectSheetTxtFldByArea(sheet, me.bandMappingObj[band[JV.PROP_NAME]][JV.PROP_COLOR] , textFldArr, colWidthArr, rowHeightArr);
        for (let txtFld of textFldArr) {
            if (txtFld.isField) {
                let fldNode = me.private_create_field_param_node(sheet, txtFld, startRow, colWidthArr, rowHeightArr, rptTpl);
                destNodes[0].push(fldNode);
            } else {
                let txtNode = me.createTxtNode(txtFld, sheet, startRow, colWidthArr, rowHeightArr);
                destNodes[1].push(txtNode);
            }
        }
    },
    private_create_tpl_flow_nodes: function (columnNodes, contentNodes, pageSummaryNodes, segSummaryNodes, groupSummaryNodes, discreteNodesArr) {
        let nodes = dataInfoMapTreeOprObj.treeObj.getNodes();
        for (let node of nodes) {
            if (node[JV.PROP_NAME].indexOf('_列') >= 0) {
                //列
                dataInfoMapTreeOprObj.treeObj.addNodes(node, -1, columnNodes, true);
            } else if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                //数据
                dataInfoMapTreeOprObj.treeObj.addNodes(node, -1, contentNodes, true);
            } else if (node[JV.PROP_NAME].indexOf('_页统计') >= 0) {
                //页统计
                if (pageSummaryNodes[0].length > 0) {
                    dataInfoMapTreeOprObj.treeObj.addNodes(node.items[0], -1, pageSummaryNodes[0], true);
                }
                if (pageSummaryNodes[1].length > 0) {
                    dataInfoMapTreeOprObj.treeObj.addNodes(node.items[1], -1, pageSummaryNodes[1], true);
                }
            } else if (node[JV.PROP_NAME].indexOf('_段统计') >= 0) {
                //章统计
                if (segSummaryNodes[0].length > 0) {
                    dataInfoMapTreeOprObj.treeObj.addNodes(node.items[0], -1, segSummaryNodes[0], true);
                }
                if (segSummaryNodes[1].length > 0) {
                    dataInfoMapTreeOprObj.treeObj.addNodes(node.items[1], -1, segSummaryNodes[1], true);
                }
            } else if (node[JV.PROP_NAME].indexOf('_分组') >= 0) {
                //分组统计
                // for (let subNode of node.items) {
                //     dataInfoMapTreeOprObj.treeObj.removeChildNodes(subNode);
                // }
            } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                //离散信息
                if (discreteNodesArr) {
                    for (let dNode of discreteNodesArr) {
                        let discreteNode = {"Name": "子项", "BandName": dNode[JV.PROP_BAND_NAME], "items": [{"Name": "离散字段集", "items": [], "isParent": true},{"Name": "离散文本集", "items": [], "isParent": true}], "isParent": true};
                        if (dNode.items[0].length > 0) {
                            // dataInfoMapTreeOprObj.treeObj.addNodes(node.items[0], -1, segSummaryNodes[0], true);
                            discreteNode.items[0].items = dNode.items[0];
                        }
                        if (dNode.items[1].length > 0) {
                            // dataInfoMapTreeOprObj.treeObj.addNodes(node.items[1], -1, segSummaryNodes[1], true);
                            discreteNode.items[1].items = dNode.items[1];
                        }
                        dataInfoMapTreeOprObj.treeObj.addNodes(node, -1, discreteNode, true);
                    }
                }
            }
        }

    },
    private_create_field_param_node: function (sheet, field, startRow, colWidthArr, rowHeightArr, rptTpl) {
        let me = this;
        let isAutoHeight = false;
        if (field.text && field.text.indexOf(`{`) === 0) {
            isAutoHeight = true;
            field.text = field.text.replace('{', '').replace('}','');
        }
        let rst = {"Name": field.text, "Title": '', "FieldID": -1, "font": "Content", "control": "Column", "style" : "Default_Normal", "isAutoHeight" : false,
            "area" : {"Left" : 0, "Right" : 100, "Top" : 0, "Bottom" : 100, "H_CalculationType" : "percentage", "V_CalculationType" : "percentage"}
        };
        //1. 设置FieldID
        let hasChkField = false;
        let native_chk_field_param = function (fpArr) {
            for (let rptField of fpArr) {
                if (rptField[JV.PROP_NAME] === field.text) {
                    rst.FieldID = rptField[JV.PROP_ID];
                    rst["Title"] = "ID: " + rptField[JV.PROP_ID];
                    hasChkField = true;
                    break;
                }
            }
        };
        if (rptTpl) {
            if (rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS].length > 0) {
                native_chk_field_param(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]);
            }
            if (!hasChkField && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS].length > 0) {
                native_chk_field_param(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS]);
            }
            if (!hasChkField && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS] !== undefined && rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS].length > 0) {
                native_chk_field_param(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS]);
            }
            if (!hasChkField && rptTpl[JV.NODE_NO_MAPPING_FIELDS] !== undefined && rptTpl[JV.NODE_NO_MAPPING_FIELDS].length > 0) {
                native_chk_field_param(rptTpl[JV.NODE_NO_MAPPING_FIELDS]);
            }
            if (!hasChkField && rptTpl[JV.NODE_DISCRETE_PARAMS] !== undefined && rptTpl[JV.NODE_DISCRETE_PARAMS].length > 0) {
                native_chk_field_param(rptTpl[JV.NODE_DISCRETE_PARAMS]);
                if (hasChkField) {
                    rst.ParamID = rst.FieldID;
                    delete rst.FieldID;
                }
            }
        } else {
            let nodes1 = fieldMapTreeOprObj.treeObj.getNodes();
            for (let node of nodes1) {
                if (node.items && node.items.length > 0) {
                    native_chk_field_param(node.items);
                }
                if (hasChkField) break;
            }
            if (!hasChkField) {
                let nodes2 = discreteFieldParamTreeOprObj.treeObj.getNodes();
                for (let node of nodes2) {
                    if (node.items && node.items.length > 0) {
                        native_chk_field_param(node.items);
                    }
                    if (hasChkField) break;
                }
            }
        }

        let cell = sheet.getCell(field.row, field.col);
        //2. 字体
        rst[JV.PROP_FONT] = me.private_getFontByCellProperties(cell);
        rst[JV.PROP_STYLE] = me.private_getBorderByCellProperties(cell);
        rst[JV.PROP_CONTROL] = me.private_getCtrlByCellProperties(cell);
        //3. 自动行高
        rst[JV.PROP_IS_AUTO_HEIGHT] = isAutoHeight;

        //4. 左右位置%
        me.setupHeightWidth(rst, field, startRow, colWidthArr, rowHeightArr);

        //5. 其他(前后缀、Format、默认值)
        let preSufCfg = me.bandMappingObj.fieldPreSufAttr[field.row + "_" + field.col + "_fieldPreSuf"];
        if (preSufCfg) {
            if (preSufCfg[JV.PROP_PREFIX]) {
                rst[JV.PROP_PREFIX] = preSufCfg[JV.PROP_PREFIX];
            }
            if (preSufCfg[JV.PROP_SUFFIX]) {
                rst[JV.PROP_SUFFIX] = preSufCfg[JV.PROP_SUFFIX];
            }
            if (preSufCfg[JV.PROP_FORMAT]) {
                rst[JV.PROP_FORMAT] = preSufCfg[JV.PROP_FORMAT];
            }
            if (preSufCfg[JV.PROP_DFT_VALUE]) {
                rst[JV.PROP_DFT_VALUE] = preSufCfg[JV.PROP_DFT_VALUE];
            }
        }

        return rst;
    },
    private_clear_flow_txt_fld_nodes: function () {
        let nodes = dataInfoMapTreeOprObj.treeObj.getNodes();
        for (let node of nodes) {
            if (node[JV.PROP_NAME].indexOf('_列') >= 0 || node[JV.PROP_NAME].indexOf('数据') >= 0) {
                //1. 列 / 数据
                dataInfoMapTreeOprObj.treeObj.removeChildNodes(node);
            } else if (node[JV.PROP_NAME].indexOf('_页统计') >= 0 || node[JV.PROP_NAME].indexOf('_段统计') >= 0) {
                //3. 页统计 / 章统计
                for (let subNode of node.items) {
                    dataInfoMapTreeOprObj.treeObj.removeChildNodes(subNode);
                }
            } else if (node[JV.PROP_NAME].indexOf('_分组') >= 0) {
                //5. 分组统计
                for (let subNode of node.items) {
                    dataInfoMapTreeOprObj.treeObj.removeChildNodes(subNode);
                }
            } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                //6. 离散信息
                dataInfoMapTreeOprObj.treeObj.removeChildNodes(node);
            }
        }
    },
    applyBack_Bill: function (rptTpl, sheet, xPos, yPos) {
        let me = this;
        // let pageWith = xPos[xPos.length - 1];
        //先清除所有相关text/field节点，然后再重新生成
        me.private_clear_bill_txt_fld_nodes();
        //1. 锚定content
        let cotentBand = visualCommonOprObj.getBandEx(rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT][JV.PROP_BAND_NAME], rptTpl);
        let textFldArr = [], colWidthArr = [], rowHeightArr = [];
        let startRow = me.collectSheetTxtFldByArea(sheet, me.bandMappingObj[cotentBand[JV.PROP_NAME]][JV.PROP_COLOR] , textFldArr, colWidthArr, rowHeightArr);
        let contentNodes = [];
        for (let field of textFldArr) {
            if (field.isField) {
                let node = me.private_create_field_param_node(sheet, field, startRow, colWidthArr, rowHeightArr, rptTpl);
                contentNodes.push(node);
            }
        }
        //2. 其他就归类到离散去了
        let discreteNodesArr = [];
        if (me.bandMappingObj && me.bandMappingObj.items && me.bandMappingObj.items.length > 0) {
            for (let sBandMap of me.bandMappingObj.items) {
                let sArr = {Name: "子项", BandName: sBandMap[JV.PROP_BAND_NAME], items: [[],[]], isParent: true};
                let sBand = visualCommonOprObj.getBandEx(sBandMap[JV.PROP_BAND_NAME], rptTpl);
                if (sBand) {
                    me.private_setup_bandHeight(sBand[JV.PROP_NAME], sBand, sheet);
                    me.private_build_txt_fld_nodes(sBand, sheet, rptTpl, sArr.items);
                    if (sBand[JV.PROP_NAME] === cotentBand[JV.PROP_NAME]) {
                        sArr.items[0] = []; //清除指标集合(因与步骤1的结果有重叠了)
                    }
                }
                if (sArr.items[0].length > 0 || sArr.items[1].length > 0) {
                    discreteNodesArr.push(sArr);
                }
            }
        }
        //3. 最后生成节点
        me.private_create_tpl_bill_nodes(contentNodes, discreteNodesArr);
        displayMessage("应用提交成功！", "green", 5000, "id_vis_setup_lbl");
    },
    private_clear_bill_txt_fld_nodes: function () {
        let nodes = dataInfoMapTreeOprObj.treeObj.getNodes();
        for (let node of nodes) {
            dataInfoMapTreeOprObj.treeObj.removeChildNodes(node);
            //bill类型可以直接remove child nodes
        }
    },
    private_create_tpl_bill_nodes: function (contentNodes, discreteNodesArr) {
        let nodes = dataInfoMapTreeOprObj.treeObj.getNodes();
        for (let node of nodes) {
            if (node[JV.PROP_NAME].indexOf('数据') >= 0) {
                //数据
                dataInfoMapTreeOprObj.treeObj.addNodes(node, -1, contentNodes, true);
            } else if (node[JV.PROP_NAME].indexOf('离散') >= 0) {
                //离散信息
                if (discreteNodesArr) {
                    for (let dNode of discreteNodesArr) {
                        let discreteNode = {"Name": "子项", "BandName": dNode[JV.PROP_BAND_NAME], "items": [{"Name": "离散字段集", "items": [], "isParent": true},{"Name": "离散文本集", "items": [], "isParent": true}], "isParent": true};
                        if (dNode.items[0].length > 0) {
                            // dataInfoMapTreeOprObj.treeObj.addNodes(node.items[0], -1, segSummaryNodes[0], true);
                            discreteNode.items[0].items = dNode.items[0];
                        }
                        if (dNode.items[1].length > 0) {
                            // dataInfoMapTreeOprObj.treeObj.addNodes(node.items[1], -1, segSummaryNodes[1], true);
                            discreteNode.items[1].items = dNode.items[1];
                        }
                        dataInfoMapTreeOprObj.treeObj.addNodes(node, -1, discreteNode, true);
                    }
                }
            }
        }
    },
    applyBack_Cross: function (rptTpl, sheet, xPos, yPos) {
        let me = this;
        let pageWith = xPos[xPos.length - 1];
        //先清除所有相关text/field节点，然后再重新生成
        me.private_clear_cross_txt_fld_nodes();
        //1. 锚定...
    },
    private_clear_cross_txt_fld_nodes: function () {
        let nodes = dataInfoMapTreeOprObj.treeObj.getNodes();
        for (let node of nodes) {
            //
        }
    },
    private_setup_flow_content_height: function (rptTpl, bandName, sheet) {
        let me = visualJumbo;
        if (me.bandMappingObj[bandName]) {
            let bandColor = me.bandMappingObj[bandName][JV.PROP_COLOR];
            for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
                if (sheet.getCell(iRow, 0).backColor() === bandColor) {
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT] = (sheet.getRowHeight(iRow) / unitFactor).toFixed(2);
                    $(`#element_content_height`)[0].value = (sheet.getRowHeight(iRow) / unitFactor).toFixed(2);
                }
            }
        }
    },
    private_setup_bandHeight: function (bandName, band, sheet) {
        let me = visualJumbo;
        if (band && me.bandMappingObj[bandName]) {
            let bandColor = me.bandMappingObj[bandName][JV.PROP_COLOR];
            let isFirst = true, firstRowIdx = -1, lastRowIdx = -1;
            for (let iRow = 0; iRow < sheet.getRowCount(); iRow++) {
                if (sheet.getCell(iRow, 0).backColor() === bandColor) {
                    lastRowIdx = iRow;
                    if (isFirst) {
                        firstRowIdx = iRow;
                        isFirst = false;
                    }
                }
            }
            if (firstRowIdx >= 0) {
                let bH = 0;
                for (let idx = firstRowIdx; idx <= lastRowIdx; idx++) {
                    bH += sheet.getRowHeight(idx);
                }
                let bandH = (bH / unitFactor).toFixed(2);
                band[JV.BAND_PROP_HEIGHT] = bandH;
                bandTreeOprObj.changeBandHeight(bandName, bandH);
            }
        }
    },
    collectSheetTxtFldByArea: function (sheet, backColor, textArr, colWidthArr, rowHeightArr) {
        let me = this, allSpans = sheet.getSpans();
        let startRow = -1, endRow = -1;
        let isFirst = true;
        for (let idx = 0; idx < sheet.getRowCount(); idx++) {
            if (sheet.getCell(idx, 0).backColor() === backColor) {
                if (isFirst) {
                    startRow = idx;
                    isFirst = false;
                }
                endRow = idx;
            }
        }
        let spans = [];
        for (let span of allSpans) {
            if (span.row >= startRow && span.row <= endRow) {
                spans.push(span);
            }
        }
        for (let span of spans) {
            me.private_build_pre_text(sheet, textArr, span.row, span.col, span.rowCount, span.colCount);
        }
        for (let iRow = startRow; iRow <= endRow; iRow++) {
            rowHeightArr.push(sheet.getRowHeight(iRow));
            if (iRow > startRow) {
                rowHeightArr[iRow - startRow] = rowHeightArr[iRow - startRow] + rowHeightArr[iRow - startRow - 1];
            }
            for (let jCol = 0; jCol < sheet.getColumnCount(); jCol++) {
                if (iRow === startRow) {
                    colWidthArr.push(sheet.getColumnWidth(jCol));
                    if (jCol > 0) {
                        colWidthArr[jCol] = colWidthArr[jCol] + colWidthArr[jCol - 1];
                    }
                }
                if (!visualCommonOprObj.checkInSpan(spans, iRow, jCol)) {
                    me.private_build_pre_text(sheet, textArr, iRow, jCol, 1, 1);
                }
            }
        }
        return startRow;
    },
    private_build_pre_text: function (sheet, textArr, row, col, rowCount, colCount) {
        let cell = sheet.getCell(row, col);
        let textValue = sheet.getValue(row, col);
        let isField = false, isBlank = false;
        if (textValue && cell.cellType() && cell.cellType().typeName === '7') {
            isField = true;
        } else {
            isBlank = stringUtil.isEmptyString(textValue);
            if (isBlank) {
                let border = cell.borderBottom();
                if (border && border.style === GC.Spread.Sheets.LineStyle.thin) {
                    isBlank = false;
                }
            }
        }
        if (!isBlank) {
            textArr.push({"row": row, "col": col, "rowCount": rowCount, "colCount": colCount, "text": textValue, "isField": isField});
        }
    },
    createTxtNode: function (text, sheet, startRow, colWidthArr, rowHeightArr) {
        let me = this, rst = dataInfoMapTreeOprObj.getDummyTextNode(null);
        me.setupHeightWidth(rst, text, startRow, 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];
        //then setup font/border/control
        let cell = sheet.getCell(text.row, text.col);
        rst[JV.PROP_FONT] = me.private_getFontByCellProperties(cell);
        rst[JV.PROP_STYLE] = me.private_getBorderByCellProperties(cell);
        rst[JV.PROP_CONTROL] = me.private_getCtrlByCellProperties(cell);
        return rst;
    },
    setupHeightWidth: function (dest, text, startRow, 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 - startRow + text.rowCount - 1] / height * 100).toFixed(2);
        if (text.row > startRow) {
            dest[JV.PROP_AREA][JV.PROP_TOP] = (rowHeightArr[text.row - startRow - 1] / height * 100).toFixed(2);
        } else {
            dest[JV.PROP_AREA][JV.PROP_TOP] = 0;
        }
    },
    private_getFontByCellProperties: function (cell) {
        let me = this, rst = 'Content';
        if (me.bandMappingObj.fontAttr[cell.row + "_" + cell.col + "_font"]) {
            rst = me.bandMappingObj.fontAttr[cell.row + "_" + cell.col + "_font"];
        }
        return rst;
    },
    private_getBorderByCellProperties: function (cell) {
        let rst = 'Default_None';
        let border = cell.borderLeft();
        if (border && border.style === GC.Spread.Sheets.LineStyle.thin) {
            rst = 'Default_Normal'
        } else {
            border = cell.borderBottom();
            if (border && border.style === GC.Spread.Sheets.LineStyle.thin) {
                rst = 'Label_Underline'
            }
        }
        return rst;
    },
    private_getCtrlByCellProperties: function (cell) {
        let rst = 'Default';
        let border = cell.borderLeft();
        if (cell.vAlign() === GC.Spread.Sheets.VerticalAlign.center) {
            if (border && border.style === GC.Spread.Sheets.LineStyle.thin) {
                switch (cell.hAlign()) {
                    case GC.Spread.Sheets.HorizontalAlign.center:
                        rst = `Column`;
                        break;
                    case GC.Spread.Sheets.HorizontalAlign.right:
                        rst = `Column_Right`;
                        break;
                    case GC.Spread.Sheets.HorizontalAlign.left:
                    default:
                        rst = `Column_Left`;
                        break;
                }
            } else {
                switch (cell.hAlign()) {
                    case GC.Spread.Sheets.HorizontalAlign.center:
                        rst = `Title`;
                        break;
                    case GC.Spread.Sheets.HorizontalAlign.right:
                        rst = `Header_Right`;
                        break;
                    case GC.Spread.Sheets.HorizontalAlign.left:
                    default:
                        rst = `Header`;
                        break;
                }
            }
        } else {
            switch (cell.hAlign()) {
                case GC.Spread.Sheets.HorizontalAlign.center:
                    rst = `Content_Center`;
                    break;
                case GC.Spread.Sheets.HorizontalAlign.right:
                    rst = `Content_Right`;
                    break;
                case GC.Spread.Sheets.HorizontalAlign.left:
                default:
                    rst = `Content_Left`;
                    break;
            }
        }
        return rst;
    }
};