/**
 * Created by Tony on 2017/10/5.
 * 各种类型(flow/bill/cross)数据类型 + 离散
 */
'use strict'

let dataInfoMapTreeOprObj = {
    treeObj : null,
    selectedTreeObj: null,
    currentNode: null,
    dataTypeDef: ['string', 'int32', 'int64', 'double', 'currency', 'date', 'image'],
    iniTree: function(rptTpl) {
        let me = this;
        let fieldMapList = me.buildTreeData(rptTpl);
        me.treeObj = $.fn.zTree.init($("#tpl_data_info_reversed"), rptDataInfoSetting, fieldMapList);
        let nodes = me.treeObj.getNodes();
        for (let node of nodes) {
            if (node[JV.PROP_NAME].indexOf("_列") < 0 && node[JV.PROP_NAME].indexOf("_数据") < 0) {
                me.treeObj.expandNode(node, true);
            }
        }
        // me.treeObj.expandAll(true);
        $("#element_hidden")[0].style.display = "none";
        $("#element_font")[0].style.display = "none";
        $("#element_border")[0].style.display = "none";
        $("#element_control")[0].style.display = "none";
        $("#element_area_1")[0].style.display = "none";
        // $("#element_area_2")[0].style.display = "none";
        $("#element_pre_suff")[0].style.display = "none";
        $("#element_visual_div1")[0].style.display = "none";
        $("#element_visual_div2")[0].style.display = "none";
        $("#element_visual_div3")[0].style.display = "none";
    },
    iniDataMap: function () {
        let me = this, bandList = bandTreeOprObj.copyBandList(false);
        if (bandList) {
            let selectedFieldList = fieldMapTreeOprObj.copySelectedFieldList();
            if (me.selectedTreeObj) {
                let nodes = me.selectedTreeObj.getNodes();
                for (let node of nodes) {
                    me.selectedTreeObj.removeNode(node);
                }
                me.selectedTreeObj.refresh();
            }
            me.selectedTreeObj = $.fn.zTree.init($("#tpl_data_selected_field_map_reversed"), selectedFieldMapSetting, selectedFieldList);
            me.selectedTreeObj.expandAll(true);
            me.refreshBandSelection();
        }
    },
    refreshBandSelection: function () {
        let me = dataInfoMapTreeOprObj;
        let tplNodes = me.treeObj.getNodes();
        let bandList = bandTreeOprObj.copyBandList(false);
        let currentIdx = 0;

        let private_addBandOption = function(prefix, bandObj, selector, nodeBandName) {
            selector.append("<option value='" + bandObj[JV.PROP_NAME] + "'>" + prefix + bandObj[JV.PROP_NAME] + "</option>");
            currentIdx++;
            if (nodeBandName === bandObj[JV.PROP_NAME]) {
                selector[0].selectedIndex = currentIdx;
            }
            //selector.selectedIndex
            if (bandObj.items && bandObj.items.length > 0) {
                for (let subItem of bandObj.items) {
                    private_addBandOption(prefix + "&nbsp&nbsp", subItem, selector, nodeBandName);
                }
            }
        };
        let private_refresh_node_selection = function (treeNode) {
            if (me.checkIfCanAddBand(treeNode)) {
                //准备加selection
                let selectObj = $("#diySelect_" +treeNode.tId);
                selectObj.empty();
                selectObj.append("<option value='none'>请选择</option>");
                currentIdx = 0;
                for (let band of bandList) {
                    private_addBandOption("", band, selectObj, treeNode[JV.PROP_BAND_NAME]);
                }
            }
            if (treeNode.items && treeNode.items.length > 0) {
                for (let subItem of treeNode.items) {
                    private_refresh_node_selection(subItem);
                }
            }
        };
        for (let i = 0; i < tplNodes.length; i++) {
            private_refresh_node_selection(tplNodes[i]);
        }
    },
    buildTreeData: function (rptTpl) {
        let me = this, rst = null;

        $("#element_content_height_width")[0].style.display = "";
        if (rptTpl[JV.NODE_FLOW_INFO]) {
            rst = me.buildFlowData(rptTpl);
            $("#element_content_height_width")[0].style.display = "";
            if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT]) {
                $("#element_content_height")[0].value = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT][JV.PROP_CMN_HEIGHT];
            } else {
                $("#element_content_height")[0].value = 0.6;
            }
            $("#element_content_height").get(0).removeAttribute("disabled");
            $("#element_content_width").get(0).disabled = "disabled" ;
        } else if (rptTpl[JV.NODE_CROSS_INFO]) {
            //
        } else if (rptTpl[JV.NODE_BILL_INFO]) {
            rst = me.buildBillData(rptTpl);
            $("#element_content_height_width")[0].style.display = "none";
        }

        return rst;
    },
    buildFlowData: function(rptTpl) {
        let me = this, rst = [];
        let discreteNode = {Name: JV.NODE_DISCRETE_INFO, type: "discrete_data", isParent: true, Title: ""};
        let rootFieldNode = {Name: JV.NODE_FLOW_CONTENT, type: "flow_field_data", isParent: true, Title: ""};
        let rootColumnNode = {Name: JV.NODE_FLOW_COLUMN, type: "flow_column_data", isParent: true, Title: ""};
        let rootPageSumNode = {Name: JV.NODE_FLOW_PAGE_SUM, type: "flow_page_sum_data", isParent: true, Title: ""};
        let rootChapterSumNode = {Name: JV.NODE_FLOW_SEG_SUM, type: "flow_chapter_sum_data", isParent: true, Title: ""};
        let rootGroupSumNode = {Name: JV.NODE_FLOW_GROUP, type: "flow_group_sum_data", isParent: true, Title: ""};
        rst.push(rootColumnNode);
        rst.push(rootFieldNode);
        if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_COLUMN].hasOwnProperty(JV.PROP_BAND_NAME)) {
            rootColumnNode[JV.PROP_BAND_NAME] = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_COLUMN][JV.PROP_BAND_NAME];
        }
        rst.push(rootPageSumNode);
        if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM].hasOwnProperty(JV.PROP_BAND_NAME)) {
            rootPageSumNode[JV.PROP_BAND_NAME] = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM][JV.PROP_BAND_NAME];
        }
        rst.push(rootChapterSumNode);
        if (rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM].hasOwnProperty(JV.PROP_BAND_NAME)) {
            rootChapterSumNode[JV.PROP_BAND_NAME] = rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM][JV.PROP_BAND_NAME];
        }
        rst.push(rootGroupSumNode);
        rst.push(discreteNode);
        me.private_build_content_field_data(rootFieldNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT], JV.PROP_FLOW_FIELDS, rptTpl);
        me.private_build_discrete_data(discreteNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO], rptTpl);
        me.private_build_column_data(rootColumnNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_COLUMN]);
        me.private_build_sum_data(rootChapterSumNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM], rptTpl);
        me.private_build_sum_data(rootPageSumNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM], rptTpl);
        me.private_build_grp_data(rootGroupSumNode, rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP], rptTpl);
        return rst;
    },
    buildBillData: function(rptTpl) {
        let me = this, rst = [];
        let rootFieldNode = {Name: JV.NODE_BILL_CONTENT, type: "bill_field_data", isParent: true, Title: ""};
        let discreteNode = {Name: JV.NODE_DISCRETE_INFO, type: "discrete_data", isParent: true, Title: ""};
        rst.push(rootFieldNode);
        rst.push(discreteNode);
        me.private_build_content_field_data(rootFieldNode, rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT], JV.PROP_BILL_FIELDS, rptTpl);
        me.private_build_discrete_data(discreteNode, rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO], rptTpl);

        return rst;
    },
    private_build_grp_data: function (grpNode, grpData, rptTpl) {
        let me = this;
        grpNode.items = [];
        grpNode.sum_field_s = [];
        if (grpData.sum_field_s && grpData.sum_field_s.length > 0) {
            for (let sumF of grpData.sum_field_s) {
                let sf = {};
                sf[JV.PROP_FIELD_ID] = sumF[JV.PROP_FIELD_ID];
                sf[JV.PROP_SUM_KEY] = sumF[JV.PROP_SUM_KEY];
                grpNode.sum_field_s.push(sf);
            }
        }
        let grp_fields = {Name: "分组判断指标集", items: [], Title: "", isParent: true};
        let grp_lines = {Name: "分组行", items: [], Title: "", isParent: true};
        grpNode.items.push(grp_fields);
        grpNode.items.push(grp_lines);
        if (grpData[JV.PROP_GROUP_FIELDS]) {
            for (let grpItem of grpData[JV.PROP_GROUP_FIELDS]) {
                grp_fields.items.push(me.private_build_field_node(grpItem, rptTpl));
            }
        }
        if (grpData[JV.PROP_GROUP_LINES] && grpData[JV.PROP_GROUP_LINES].length > 0) {
            for (let grpline of grpData[JV.PROP_GROUP_LINES]) {
                let gl = {Name: "行", items:[], isParent: true}, sumKeys = {Name:"分组字段集", items:[], Title: "", isParent: true}, texts = {Name:"文本集", items:[], Title: "", isParent: true};
                gl.items.push(sumKeys);
                gl.items.push(texts);
                grp_lines.items.push(gl);
                if (grpline[JV.PROP_GROUP_SUM_KEYS]) {
                    for (let fieldItem of grpline[JV.PROP_GROUP_SUM_KEYS]) {
                        let item = me.private_build_field_node(fieldItem, rptTpl);
                        item[JV.PROP_SUM_KEY] = fieldItem[JV.PROP_SUM_KEY];
                        item[JV.PROP_NAME] = fieldItem[JV.PROP_SUM_KEY];
                        for (let gsi of grpNode.sum_field_s) {
                            if (gsi[JV.PROP_SUM_KEY] === item[JV.PROP_SUM_KEY]) {
                                item[JV.PROP_FIELD_ID] = gsi[JV.PROP_FIELD_ID];
                                break;
                            }
                        }
                        sumKeys.items.push(item);
                    }
                }
                if (grpline[JV.PROP_TEXTS]) {
                    for (let txt of grpline[JV.PROP_TEXTS]) {
                        texts.items.push(me.private_build_text_node(txt));
                    }
                }
            }
        }
        //then group lines
    },
    private_build_sum_data: function(sumNode, sumData, rptTpl) {
        let me = this;
        sumNode.items = [];
        let sum_fields = {Name: "统计指标集", items: [], isParent: true};
        let texts = {Name: "文本集", items: [], isParent: true};
        sumNode.items.push(sum_fields);
        sumNode.items.push(texts);
        if (sumData[JV.PROP_SUM_FIELDS]) {
            for (let fieldItem of sumData[JV.PROP_SUM_FIELDS]) {
                sum_fields.items.push(me.private_build_field_node(fieldItem, rptTpl))
            }
        }
        if (sumData[JV.PROP_TEXTS]) {
            for (let txt of sumData[JV.PROP_TEXTS]) {
                texts.items.push(me.private_build_text_node(txt));
            }
        }
    },
    private_build_column_data: function(columnNode, columnData) {
        let me = this;
        if (columnData[JV.PROP_TEXTS] && columnData[JV.PROP_TEXTS].length > 0) {
            columnNode.items = [];
            // columnNode.Title = "区域： " + columnNode[JV.PROP_BAND_NAME];
            columnNode.Title = "";
            for (let txt of columnData[JV.PROP_TEXTS]) {
                columnNode.items.push(me.private_build_text_node(txt));
            }
        }
    },
    private_build_content_field_data: function(rootFieldNode, contentData, fieldsStr, rptTpl) {
        let me = this, bandName = contentData[JV.PROP_BAND_NAME];
        if (rootFieldNode) {
            rootFieldNode[JV.PROP_BAND_NAME] = bandName;
            rootFieldNode.items = [];
            for (let fieldItem of contentData[fieldsStr]) {
                if (fieldItem[JV.PROP_FIELD_ID]) {
                    rootFieldNode.items.push(me.private_build_field_node(fieldItem, rptTpl))
                } else if (fieldItem[JV.PROP_PARAM_ID]) {
                    rootFieldNode.items.push(me.private_build_param_node(fieldItem, rptTpl));
                }
            }
        }
    },
    private_build_discrete_data: function (discreteNode, discreteData, rptTpl) {
        let me = this;
        discreteNode.isParent = true;
        discreteNode.items = [];
        for (let i = 0; i < discreteData.length; i++) {
            let dtl = discreteData[i];
            // let item = {"Name": "子项(区域：" + dtl[JV.PROP_BAND_NAME] + ")", "items":[], Title: ""};
            let item = {"Name": "子项", "items":[], Title: ""};
            item[JV.PROP_BAND_NAME] = dtl[JV.PROP_BAND_NAME];
            let discreteFieldsNode = {"Name": "离散字段集", "items": [], "isParent": true, Title: ""};
            let discreteTextsNode = {"Name": "离散文本集", "items": [], "isParent": true, Title: ""};
            item.items.push(discreteFieldsNode);
            item.items.push(discreteTextsNode);

            discreteNode.items.push(item);
            if (dtl[JV.PROP_DISCRETE_FIELDS]) {
                for (let field of dtl[JV.PROP_DISCRETE_FIELDS]) {
                    if (field[JV.PROP_FIELD_ID]) {
                        discreteFieldsNode.items.push(me.private_build_field_node(field, rptTpl))
                    } else if (field[JV.PROP_PARAM_ID]) {
                        discreteFieldsNode.items.push(me.private_build_param_node(field, rptTpl));
                    }
                }
            }
            if (dtl[JV.PROP_TEXTS]) {
                for (let txt of dtl[JV.PROP_TEXTS]) {
                    discreteTextsNode.items.push(me.private_build_text_node(txt));
                }
            }
        }
    },
    private_get_field_by_id: function (fieldId, rptTpl) {
        let rst = null;
        function getField(collection) {
            if (!rst && collection) {
                for (let item of collection) {
                    if (item[JV.PROP_ID] === fieldId) {
                        rst = item;
                        break;
                    }
                }
            }
        }
        getField(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DISCRETE_FIELDS]);
        getField(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS]);
        getField(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_MASTER_FIELDS_EX]);
        getField(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS]);
        getField(rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS_EX]);
        if (!rst) getField(rptTpl[JV.NODE_NO_MAPPING_FIELDS]);
        return rst;
    },
    private_get_param_by_id: function (paramId, rptTpl) {
        let rst = null;
        function getParam(collection) {
            if (!rst && collection) {
                for (let item of collection) {
                    if (item[JV.PROP_ID] === paramId) {
                        rst = item;
                        break;
                    }
                }
            }
        }
        getParam(rptTpl[JV.NODE_DISCRETE_PARAMS]);
        return rst;
    },
    private_build_field_node: function (fieldData, rptTpl) {
        let me = this, rst = null;
        if (fieldData) {
            rst = {};
            let f = me.private_get_field_by_id(fieldData[JV.PROP_FIELD_ID], rptTpl);
            if (f) {
                rst[JV.PROP_NAME] = f[JV.PROP_NAME];
            } else {
                rst[JV.PROP_NAME] = fieldData[JV.PROP_FIELD_ID];
            }
            me.private_copy_field_properties(fieldData, rst);
            rst.Title = "ID: " + fieldData[JV.PROP_FIELD_ID];
        }
        return rst;
    },
    private_copy_field_properties: function (source, destination) {
        let me = this;
        destination[JV.PROP_FIELD_ID] = source[JV.PROP_FIELD_ID];
        if (source[JV.TAB_FIELD_PROP_SORT]) destination[JV.TAB_FIELD_PROP_SORT] = source[JV.TAB_FIELD_PROP_SORT];
        destination[JV.PROP_FONT] = source[JV.PROP_FONT];
        destination[JV.PROP_CONTROL] = source[JV.PROP_CONTROL];
        destination[JV.PROP_STYLE] = source[JV.PROP_STYLE];
        destination[JV.PROP_PREFIX] = source[JV.PROP_PREFIX];
        destination[JV.PROP_SUFFIX] = source[JV.PROP_SUFFIX];
        if (source[JV.PROP_FORMAT]) destination[JV.PROP_FORMAT] = source[JV.PROP_FORMAT];
        if (source[JV.PROP_IS_AUTO_HEIGHT]) destination[JV.PROP_IS_AUTO_HEIGHT] = true
        else destination[JV.PROP_IS_AUTO_HEIGHT] = false;
        if (source[JV.PROP_HIDDEN]) destination[JV.PROP_HIDDEN] = true;
        if (source[JV.PROP_IS_SERIAL]) destination[JV.PROP_IS_SERIAL] = true;
        if (source[JV.TAB_FIELD_PROP_SORT]) destination[JV.TAB_FIELD_PROP_SORT] = source[JV.TAB_FIELD_PROP_SORT];
        if (source[JV.PROP_COMBINE_TYPE]) destination[JV.PROP_COMBINE_TYPE] = source[JV.PROP_COMBINE_TYPE];
        if (source.hasOwnProperty(JV.PROP_IS_MERGE)) destination[JV.PROP_IS_MERGE] = source[JV.PROP_IS_MERGE];
        if (source[JV.PROP_AREA]) {
            destination[JV.PROP_AREA] = {};
            me.private_copy_area(source[JV.PROP_AREA], destination[JV.PROP_AREA]);
        }
    },
    private_copy_area: function (orgArea, destArea) {
        if (orgArea) {
            destArea[JV.PROP_LEFT] = orgArea[[JV.PROP_LEFT]];
            destArea[JV.PROP_RIGHT] = orgArea[[JV.PROP_RIGHT]];
            destArea[JV.PROP_TOP] = orgArea[[JV.PROP_TOP]];
            destArea[JV.PROP_BOTTOM] = orgArea[[JV.PROP_BOTTOM]];
            destArea[JV.PROP_H_CALCULATION] = orgArea[[JV.PROP_H_CALCULATION]];
            destArea[JV.PROP_V_CALCULATION] = orgArea[[JV.PROP_V_CALCULATION]];
        }
    },
    private_build_param_node: function (param, rptTpl) {
        let me = this, rst = null;
        if (param) {
            rst = {};
            let p = me.private_get_param_by_id(param[JV.PROP_PARAM_ID], rptTpl);
            if (p) {
                rst[JV.PROP_NAME] = p[JV.PROP_NAME];
                if (param[JV.PROP_DFT_VALUE]) {
                    p[JV.PROP_DFT_VALUE] = param[JV.PROP_DFT_VALUE];
                } else if (p[JV.PROP_DFT_VALUE]) {
                    param[JV.PROP_DFT_VALUE] = p[JV.PROP_DFT_VALUE];
                }
                rst[JV.PROP_DFT_VALUE] = p[JV.PROP_DFT_VALUE];
            } else {
                rst[JV.PROP_NAME] = param[JV.PROP_PARAM_ID];
            }
            me.private_copy_param_properties(param, rst);
            rst.Title = "ID: " + param[JV.PROP_PARAM_ID];
        }
        return rst;
    },
    private_copy_param_properties: function (source, destination) {
        let me = this;
        destination[JV.PROP_PARAM_ID] = source[JV.PROP_PARAM_ID];
        destination[JV.PROP_FONT] = source[JV.PROP_FONT];
        destination[JV.PROP_CONTROL] = source[JV.PROP_CONTROL];
        destination[JV.PROP_STYLE] = source[JV.PROP_STYLE];
        destination[JV.PROP_PREFIX] = source[JV.PROP_PREFIX];
        destination[JV.PROP_SUFFIX] = source[JV.PROP_SUFFIX];
        if (source[JV.PROP_FORMAT]) destination[JV.PROP_FORMAT] = source[JV.PROP_FORMAT];
        if (source[JV.PROP_DFT_VALUE]) {
            destination[JV.PROP_DFT_VALUE] = source[JV.PROP_DFT_VALUE];
        } else {
            destination[JV.PROP_DFT_VALUE] = "";
        }
        destination[JV.PROP_AREA] = {};
        me.private_copy_area(source[JV.PROP_AREA], destination[JV.PROP_AREA]);
    },
    private_build_text_node: function (text) {
        let me = this, rst = null;
        if (text) {
            rst = {};
            rst[JV.PROP_NAME] = text[JV.PROP_LABEL];
            me.private_copy_text_properties(text, rst);
        }
        return rst;
    },
    private_copy_text_properties: function (source, destination) {
        let me = this;
        destination[JV.PROP_LABEL] = source[JV.PROP_LABEL];
        destination[JV.PROP_FONT] = source[JV.PROP_FONT];
        destination[JV.PROP_CONTROL] = source[JV.PROP_CONTROL];
        destination[JV.PROP_STYLE] = source[JV.PROP_STYLE];
        destination.Title = "";
        destination[JV.PROP_AREA] = {};
        me.private_copy_area(source[JV.PROP_AREA], destination[JV.PROP_AREA]);
    },

    getDummyTextNode: function(treeNode) {
        let me = this, rst = {};
        rst[JV.PROP_NAME] = "文本";
        rst[JV.PROP_LABEL] = "文本";
        rst.Title = "";
        rst[JV.PROP_FONT] = "Content";
        rst[JV.PROP_CONTROL] = "Default";
        rst[JV.PROP_STYLE] = "Default_None";
        rst[JV.PROP_AREA] = {};
        rst[JV.PROP_AREA][JV.PROP_LEFT] = 0;
        rst[JV.PROP_AREA][JV.PROP_RIGHT] = 100;
        rst[JV.PROP_AREA][JV.PROP_TOP] = 0;
        rst[JV.PROP_AREA][JV.PROP_BOTTOM] = 100;
        rst[JV.PROP_AREA][JV.PROP_H_CALCULATION] = JV.CAL_TYPE[0];
        rst[JV.PROP_AREA][JV.PROP_V_CALCULATION] = JV.CAL_TYPE[0];
        if (treeNode) {
            if (treeNode[JV.PROP_NAME].indexOf("_列") >= 0) {
                rst[JV.PROP_FONT] = "HeaderColumn";
                rst[JV.PROP_CONTROL] = "Column";
                rst[JV.PROP_STYLE] = "Default_Normal";
            }
        }
        return rst;
    },

    private_setup_dummy_field_node: function(node, targetNode) {
        if (!node[JV.PROP_PARAM_ID]) {
            node[JV.PROP_FIELD_ID] = node[JV.PROP_ID];
        }
        node[JV.PROP_FONT] = "Content";
        node[JV.PROP_CONTROL] = "Default";
        node[JV.PROP_STYLE] = "Default_None";
        node[JV.PROP_AREA] = {};
        node[JV.PROP_AREA][JV.PROP_LEFT] = 0;
        node[JV.PROP_AREA][JV.PROP_RIGHT] = 100;
        node[JV.PROP_AREA][JV.PROP_TOP] = 0;
        node[JV.PROP_AREA][JV.PROP_BOTTOM] = 100;
        node[JV.PROP_AREA][JV.PROP_H_CALCULATION] = JV.CAL_TYPE[0];
        node[JV.PROP_AREA][JV.PROP_V_CALCULATION] = JV.CAL_TYPE[0];
        if (targetNode[JV.PROP_NAME].indexOf("_数据") >= 0) {
            node[JV.PROP_FONT] = "Content";
            node[JV.PROP_CONTROL] = "Content_Left";
            node[JV.PROP_STYLE] = "Default_Normal";
        }
    },

    onTabDataClick: function (event,treeId,treeNode) {
        let me = dataInfoMapTreeOprObj;
        me.currentNode = treeNode;
        //检测是否为field/param/text对象
        rpt_tpl_cfg_helper.checkAndSetSelectedNodeCfg(treeNode);
    },
    onBeforeRemove: function(treeId, treeNode){
        let rst = true;
        if (treeNode.isParent) {
            if (treeNode.Name === `子项` && treeNode.getParentNode() !== null && treeNode.getParentNode().Name === `离散信息`) {
                rst = confirm(`确认要删除此离散子项？`);
            } else if (treeNode.Name === `行`) {
                rst = confirm(`确认要删除此分组行？`);
            }
        } else {
            //
        }
        return rst;
    },
    beforeRename: function(treeId, treeNode, newName, isCancel) {
        let rst = false;
        return rst;
    },
    beforeEditName: function(treeId, treeNode) {
        let me = dataInfoMapTreeOprObj, rst = false;
        let pn = treeNode.getParentNode();
        if (pn && me.checkIfCanAddTxt(pn)) {
            setTimeout(function(){
                let ip = $("#" + treeNode.tId + IDMark_Input);
                if (ip.length > 0) {
                    ip[0].className = "individualRename";
                } else {
                    setTimeout(function(){
                        let ip = $("#" + treeNode.tId + IDMark_Input);
                        if (ip.length > 0) {
                            ip[0].className = "individualRename";
                        } else {
                            console.log("sorry!");
                        }
                    }, 50);
                }
            }, 50);
            rst = true;
        }
        return rst;
    },
    onBeforeDrag: function (treeId, treeNodes) {
        let rst = true;
        for (let node of treeNodes) {
            if (node.level === 0) {
                rst = false;
                break;
            }
        }
        return rst;
    },
    onBeforeDrop: function(treeId, treeNodes, targetNode, moveType){
        let rst = false;
        if (targetNode.Name === JV.NODE_FLOW_CONTENT || targetNode.Name === JV.NODE_BILL_CONTENT || targetNode.Name === JV.NODE_CROSS_CONTENT
            || targetNode.Name === JV.NODE_CROSS_ROW || targetNode.Name === JV.NODE_CROSS_COL
            || targetNode.Name === "离散字段集" || targetNode.Name === "统计指标集" || targetNode.Name === "分组判断指标集"
            || targetNode.Name === "分组字段集") {
            rst = true;
        }
        return rst;
    },
    onDrop: function(event, treeId, treeNodes, targetNode, moveType) {
        let me = dataInfoMapTreeOprObj;
        for (let node of treeNodes) {
            me.private_setup_dummy_field_node(node, targetNode);
            if (targetNode[JV.PROP_NAME] === "分组判断指标集") {
                delete node[JV.PROP_AREA];
            }
        }
    },
    onRemove: function (e, treeId, treeNode) {
        //
    },
    onRename: function (e, treeId, treeNode, isCancel) {
        if (!isCancel) {
            treeNode[JV.PROP_LABEL] = treeNode[JV.PROP_NAME];
        }
    },
    checkIfCanAddTxt: function(treeNode) {
        let rst = false;
        if (treeNode[JV.PROP_NAME] === "离散文本集" || treeNode[JV.PROP_NAME] === "文本集" || treeNode[JV.PROP_NAME] === JV.NODE_FLOW_COLUMN) {
            rst = true;
        }
        return rst;
    },
    checkIfCanAddBand: function (treeNode) {
        let rst = false;
        if (typeof(treeNode[JV.PROP_NAME]) === "string") {
            if (treeNode[JV.PROP_NAME].indexOf(JV.NODE_FLOW_CONTENT) === 0 ||
                treeNode[JV.PROP_NAME].indexOf(JV.NODE_FLOW_COLUMN) === 0 ||
                treeNode[JV.PROP_NAME].indexOf(JV.NODE_FLOW_PAGE_SUM) === 0 ||
                treeNode[JV.PROP_NAME].indexOf(JV.NODE_FLOW_SEG_SUM) === 0 ||
                treeNode[JV.PROP_NAME].indexOf(JV.NODE_BILL_CONTENT) === 0 ||
                treeNode[JV.PROP_NAME].indexOf("子项") === 0) {
                rst = true;
            }
        }
        return rst;
    },
    checkIfCanAddFieldCombine: function (treeNode) {
        let rst = (treeNode.getParentNode() !== null && treeNode.getParentNode().Name === `流水式表_数据`);
        return rst;
    },
    checkIfCanAddColFieldCombine: function (treeNode) {
        let rst = (treeNode.getParentNode() !== null && treeNode.getParentNode().getParentNode() !== null && treeNode.getParentNode().getParentNode().Name === `交叉列`);
        return rst;
    },
    bandSelectChange: function(dom, treeNodeId) {
        let me = dataInfoMapTreeOprObj, node = me.treeObj.getNodeByTId(treeNodeId);
        if (dom.selectedIndex > 0) {
            node[JV.PROP_BAND_NAME] = dom.value;
        } else {
            node[JV.PROP_BAND_NAME] = "";
        }
    },
    combineSelectChange: function(dom, treeNodeId) {
        let me = dataInfoMapTreeOprObj, node = me.treeObj.getNodeByTId(treeNodeId);
        if (dom.selectedIndex > 0) {
            node[JV.PROP_COMBINE_TYPE] = dom.value;
        } else {
            node[JV.PROP_COMBINE_TYPE] = null;
        }
    },
    combineColSelectChange: function(dom, treeNodeId) {
        let me = dataInfoMapTreeOprObj, node = me.treeObj.getNodeByTId(treeNodeId);
        if (dom.selectedIndex > 0) {
            node[JV.PROP_IS_MERGE] = true;
        } else {
            node[JV.PROP_IS_MERGE] = false;
        }
    },
    addDiyDom: function (treeId, treeNode) {
        let me = dataInfoMapTreeOprObj, aObj = $("#" + treeNode.tId + IDMark_A);
        //加band selection
        if (me.checkIfCanAddBand(treeNode)) {
            //准备加selection
            let addBandStr = "<select class='selDemo' id='diySelect_" +treeNode.tId+ "' onchange='dataInfoMapTreeOprObj.bandSelectChange(this, \"" + treeNode.tId + "\")'></select>";
            aObj.after(addBandStr);
        } else if (me.checkIfCanAddFieldCombine(treeNode)) {
            let addCombineFieldStr = "<select class='selDemo' id='diyCombineSelect_" +treeNode.tId+ "' onchange='dataInfoMapTreeOprObj.combineSelectChange(this, \"" + treeNode.tId + "\")'><option value='none'>请选择</option><option value='vertical'>纵向合并</option><option value='horizon'>横向合并</option></select>";
            aObj.after(addCombineFieldStr);
            let selectCombineObj = $("#diyCombineSelect_" +treeNode.tId);
            if (treeNode.hasOwnProperty(JV.PROP_COMBINE_TYPE)) {
                if (treeNode[JV.PROP_COMBINE_TYPE] === 'vertical') {
                    selectCombineObj[0].selectedIndex = 1;
                } else if (treeNode[JV.PROP_COMBINE_TYPE] === 'horizon') {
                    selectCombineObj[0].selectedIndex = 2;
                } else {
                    selectCombineObj[0].selectedIndex = 0;
                }
            } else {
                selectCombineObj[0].selectedIndex = 0;
            }
        } else if (me.checkIfCanAddColFieldCombine(treeNode)) {
            let addCombineColFieldStr = "<select class='selDemo' id='diyCombineColFieldSelect_" +treeNode.tId+ "' onchange='dataInfoMapTreeOprObj.combineColSelectChange(this, \"" + treeNode.tId + "\")'><option>请选择</option><option>数据合并</option>";
            aObj.after(addCombineColFieldStr);
            let selectCombineObj = $("#diyCombineColFieldSelect_" +treeNode.tId);
            if (treeNode.hasOwnProperty(JV.PROP_IS_MERGE)) {
                if (treeNode[JV.PROP_IS_MERGE]) {
                    selectCombineObj[0].selectedIndex = 1;
                } else {
                    selectCombineObj[0].selectedIndex = 0;
                }
            } else {
                selectCombineObj[0].selectedIndex = 0;
            }
        }
    },
    addHoverDom: function(treeId, treeNode) {
        let me = dataInfoMapTreeOprObj, sObj = $("#" + treeNode.tId + IDMark_Span);
        if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length > 0 || $("#selInput_"+treeNode.tId).length > 0) {
            return;
        } else if (treeNode[JV.PROP_NAME] === JV.NODE_DISCRETE_INFO || treeNode[JV.PROP_NAME] === "分组行" || me.checkIfCanAddTxt(treeNode)) {
            let addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            let btn = $("#addBtn_"+treeNode.tId);
            if (treeNode[JV.PROP_NAME] === JV.NODE_DISCRETE_INFO) {
                btn.bind("click", function(){
                    let node = {};
                    node[JV.PROP_NAME] = "子项";
                    node[JV.PROP_BAND_NAME] = "";
                        node.items = [];
                    let fields = {"Name": "离散字段集", "items": [], "isParent": true};
                    let texts = {"Name": "离散文本集", "items": [], "isParent": true};
                    node.items.push(fields);
                    node.items.push(texts);
                    let newNodes = [];
                    newNodes.push(node);
                    me.treeObj.addNodes(treeNode, -1, newNodes, true);
                    me.refreshBandSelection();
                });
            } else if (treeNode[JV.PROP_NAME] === "分组行") {
                btn.bind("click", function(){
                    let node = {};
                    node[JV.PROP_NAME] = "行";
                    node.items = [];
                    let fields = {"Name": "分组字段集", "items": [], "isParent": true};
                    let texts = {"Name": "文本集", "items": [], "isParent": true};
                    node.items.push(fields);
                    node.items.push(texts);
                    let newNodes = [];
                    newNodes.push(node);
                    me.treeObj.addNodes(treeNode, -1, newNodes, true);
                    me.refreshBandSelection();
                });
            } else {
                //text
                btn.bind("click", function(){
                    let node = me.getDummyTextNode(treeNode);
                    let newNodes = [];
                    newNodes.push(node);
                    me.treeObj.addNodes(treeNode, -1, newNodes, true);
                });
            }
        }
    },
    removeHoverDom: function(treeId, treeNode) {
        $("#addBtn_"+treeNode.tId).unbind().remove();
    },
    extractTabFields: function (rptTpl) {
        let me = dataInfoMapTreeOprObj;
        if (rptTpl[JV.NODE_FLOW_INFO]) {
            //1. 流水式数据整理收集
            let nodes = me.treeObj.getNodes();
            for (let node of nodes) {
                if (node[JV.PROP_NAME] === JV.NODE_DISCRETE_INFO) {
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_DISCRETE_INFO] = me.private_extract_discrete_info(node, rptTpl);
                } else if (node[JV.PROP_NAME] === JV.NODE_FLOW_CONTENT) {
                    let newContent = {};
                    newContent[JV.PROP_BAND_NAME] = node[JV.PROP_BAND_NAME];
                    newContent[JV.PROP_CMN_HEIGHT] = $("#element_content_height")[0].value;
                    newContent[JV.PROP_FLOW_FIELDS] = [];
                    for (let subNode of node.items) {
                        let f = {};
                        me.private_copy_field_properties(subNode, f);
                        newContent[JV.PROP_FLOW_FIELDS].push(f);
                    }
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_CONTENT] = newContent;
                } else if (node[JV.PROP_NAME] === JV.NODE_FLOW_COLUMN) {
                    let newColumn = {};
                    newColumn[JV.PROP_BAND_NAME] = node[JV.PROP_BAND_NAME];
                    if (node.items && node.items.length > 0) {
                        newColumn[JV.PROP_TEXTS] = [];
                        for (let txt of node.items) {
                            let item = {};
                            newColumn[JV.PROP_TEXTS].push(item);
                            me.private_copy_text_properties(txt, item);
                        }
                    }
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_COLUMN] = newColumn;
                } else if (node[JV.PROP_NAME] === JV.NODE_FLOW_SEG_SUM) {
                    let newSegSum = me.private_extract_sum_info(node);
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_SEG_SUM] = newSegSum;
                } else if (node[JV.PROP_NAME] === JV.NODE_FLOW_PAGE_SUM) {
                    let newSegSum = me.private_extract_sum_info(node);
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_PAGE_SUM] = newSegSum;
                } else if (node[JV.PROP_NAME] === JV.NODE_FLOW_GROUP) {
                    let grpInfo = me.private_extract_grp_info(node);
                    rptTpl[JV.NODE_FLOW_INFO][JV.NODE_FLOW_GROUP] = grpInfo;
                }
            }
        }
        if (rptTpl[JV.NODE_CROSS_INFO]) {
            //2. 交叉表数据整理收集
        }
        if (rptTpl[JV.NODE_BILL_INFO]) {
            //3. 账单式数据整理收集
            let nodes = me.treeObj.getNodes();
            for (let node of nodes) {
                if (node[JV.PROP_NAME] === JV.NODE_BILL_CONTENT) {
                    let newContent = {};
                    newContent[JV.PROP_BAND_NAME] = node[JV.PROP_BAND_NAME];
                    newContent[JV.PROP_BILL_FIELDS] = [];
                    for (let subNode of node.items) {
                        let f = {};
                        me.private_copy_field_properties(subNode, f);
                        newContent[JV.PROP_BILL_FIELDS].push(f);
                    }
                    rptTpl[JV.NODE_BILL_INFO][JV.NODE_BILL_CONTENT] = newContent;
                } else if (node[JV.PROP_NAME] === JV.NODE_DISCRETE_INFO) {
                    rptTpl[JV.NODE_BILL_INFO][JV.NODE_DISCRETE_INFO] = me.private_extract_discrete_info(node, rptTpl);
                }
            }
        }
    },
    private_extract_grp_info: function(grpNode) {
        let me = dataInfoMapTreeOprObj, rst = {};
        rst[JV.PROP_GROUP_FIELDS] = [];
        rst[JV.PROP_SUM_FIELDS] = [];
        rst[JV.PROP_GROUP_LINES] = [];
        let sumFieldObj = {};
        for (let subNode of grpNode.items) {
            if (subNode[JV.PROP_NAME] === "分组判断指标集") {
                if (subNode.items && subNode.items.length > 0) {
                    for (let sumField of subNode.items) {
                        let item = {FieldID: sumField[JV.PROP_FIELD_ID]};
                        rst[JV.PROP_GROUP_FIELDS].push(item);
                    }
                }
            } else if (subNode[JV.PROP_NAME] === "分组行") {
                if (subNode.items && subNode.items.length > 0) {
                    for (let line of subNode.items) {
                        let grpLine = {"SumKey_S": [], "text_s":[]};
                        rst[JV.PROP_GROUP_LINES].push(grpLine);
                        for (let dtl of line.items) {
                            if (dtl[JV.PROP_NAME] === "分组字段集") {
                                for (let gf of dtl.items) {
                                    if (!sumFieldObj[gf[JV.PROP_NAME]]) {
                                        sumFieldObj[gf[JV.PROP_NAME]] = "Y";
                                        rst[JV.PROP_SUM_FIELDS].push({"FieldID": gf[JV.PROP_FIELD_ID], "SumKey": gf[JV.PROP_NAME]});
                                    }
                                    let item = {};
                                    // rst[JV.PROP_SUM_FIELDS].push(item);
                                    me.private_copy_field_properties(gf, item);
                                    item[JV.PROP_SUM_KEY] = gf[JV.PROP_NAME];
                                    grpLine[JV.PROP_GROUP_SUM_KEYS].push(item);
                                }
                            } else if (dtl[JV.PROP_NAME] === "文本集") {
                                for (let txt of dtl.items) {
                                    let item = {};
                                    grpLine[JV.PROP_TEXTS].push(item);
                                    me.private_copy_text_properties(txt, item);
                                }
                            }
                        }
                    }
                }
            }
        }
        return rst;
    },
    private_extract_sum_info: function(sumNode) {
        let me = dataInfoMapTreeOprObj, rst = {};
        rst[JV.PROP_BAND_NAME] = sumNode[JV.PROP_BAND_NAME];
        rst[JV.PROP_SUM_FIELDS] = [];
        rst[JV.PROP_TEXTS] = [];
        for (let subNode of sumNode.items) {
            if (subNode[JV.PROP_NAME] === "统计指标集") {
                if (subNode.items && subNode.items.length > 0) {
                    for (let sumField of subNode.items) {
                        let item = {};
                        rst[JV.PROP_SUM_FIELDS].push(item);
                        me.private_copy_field_properties(sumField, item);
                    }
                }
            } else if (subNode[JV.PROP_NAME] === "文本集") {
                if (subNode.items && subNode.items.length > 0) {
                    for (let txt of subNode.items) {
                        let item = {};
                        rst[JV.PROP_TEXTS].push(item);
                        me.private_copy_text_properties(txt, item);
                    }
                }
            }
        }
        return rst;
    },
    private_extract_discrete_info: function(discreteNode, rptTpl) {
        let me = this, rst = [];
        for (let dtlItemNode of discreteNode.items) {
            let newContent = {};
            rst.push(newContent);
            newContent[JV.PROP_BAND_NAME] = dtlItemNode[JV.PROP_BAND_NAME];
            for (let node of dtlItemNode.items) {
                // if (node[JV.PROP_DISCRETE_FIELDS] && node[JV.PROP_DISCRETE_FIELDS].length > 0) {
                if (node[JV.PROP_NAME] === "离散字段集" && node.items.length > 0) {
                    newContent[JV.PROP_DISCRETE_FIELDS] = [];
                    for (let field of node.items) {
                        let item = {};
                        newContent[JV.PROP_DISCRETE_FIELDS].push(item);
                        if (field[JV.PROP_FIELD_ID]) {
                            me.private_copy_field_properties(field, item);
                        } else if (field[JV.PROP_PARAM_ID]) {
                            let p = me.private_get_param_by_id(field[JV.PROP_PARAM_ID], rptTpl);
                            if (p) {
                                if (field[JV.PROP_DFT_VALUE]) {
                                    p[JV.PROP_DFT_VALUE] = field[JV.PROP_DFT_VALUE];
                                }
                            }
                            me.private_copy_param_properties(field, item);
                        }
                    }
                }
                // if (node[JV.PROP_TEXTS] && node[JV.PROP_TEXTS].length > 0) {
                if (node[JV.PROP_NAME] === "离散文本集" && node.items.length > 0) {
                    newContent[JV.PROP_TEXTS] = [];
                    for (let txt of node.items) {
                        let item = {};
                        newContent[JV.PROP_TEXTS].push(item);
                        me.private_copy_text_properties(txt, item);
                    }
                }
            }
        }
        return rst;
    },
    extractDiscreteFieldParam: function(rptTpl) {
        let me = dataInfoMapTreeOprObj;
        //待完善中...
    }
};



