/**
 * Created by Tony on 2017/12/8.
 */
let rpt_tpl_cfg_helper = {
    reportCfg: null,
    getReportTplCfg: function() {
        let me = this, params = {};
        params.userId = userID;
        CommonAjax.postEx("report_tpl_api/getUserRptCfg", params, 20000, true, function(result){
                me.reportCfg = result;
                me.refreshRptCfgs();
            }, null, null
        );
    },
    refreshRptCfgs: function () {
        let me = this;
        if (me.reportCfg) {
            me.reportCfg.borderArr = [];
            for (let style of me.reportCfg.styles) {
                me.reportCfg.borderArr.push(style.ID);
                $("#borderStyles").append("<option value='" + style.ID + "'>" + style.CfgDispName + "</option>");
                $("#elementBorders").append("<option value='" + style.ID + "'>" + style.CfgDispName + "</option>");
            }
            me.reportCfg.fontArr = [];
            for (let font of me.reportCfg.fonts) {
                if (font.CfgDispName) {
                    me.reportCfg.fontArr.push(font.ID);
                    $("#elementFonts").append("<option value='" + font.ID + "'>" + font.CfgDispName + "</option>");
                    $("#visElementFonts").append("<option value='" + font.ID + "'>" + font.CfgDispName + "</option>");
                } else {
                    // me.reportCfg.fontArr.push("dummy font");
                }
            }
            me.reportCfg.fontArr.push('自定义');
            $("#elementFonts").append("<option value='自定义'>自定义</option>");
            $("#visElementFonts").append("<option value='自定义'>自定义</option>");
            me.reportCfg.controlArr = [];
            for (let control of me.reportCfg.ctrls) {
                if (control.CfgDispName) {
                    me.reportCfg.controlArr.push(control.ID);
                    $("#elementControls").append("<option value='" + control.ID + "'>" + control.CfgDispName + "</option>");
                } else {
                    me.reportCfg.controlArr.push("dummy control");
                }
            }
        }
        let pf = $("#pageFrequency");
        pf.append("<option value='" + JV.PAGE_STATUS[0] + "'>每页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[1] + "'>首页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[2] + "'>尾页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[3] + "'>章首页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[4] + "'>章尾页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[5] + "'>分组</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[6] + "'>交叉行尾页</option>");
        pf.append("<option value='" + JV.PAGE_STATUS[7] + "'>交叉列尾页</option>");
        let ba = $("#bandAlignment");
        ba.append("<option value='" + JV.LAYOUT[0] + "'>上</option>");
        ba.append("<option value='" + JV.LAYOUT[1] + "'>下</option>");
        ba.append("<option value='" + JV.LAYOUT[2] + "'>左</option>");
        ba.append("<option value='" + JV.LAYOUT[3] + "'>右</option>");
        ba.append("<option value='" + JV.LAYOUT[4] + "'>填充</option>");
        let ha = $("#elementAlignmentHorizon");
        ha.append("<option value='" + JV.OUTPUT_ALIGN.H[0] + "'>左</option>");
        ha.append("<option value='" + JV.OUTPUT_ALIGN.H[1] + "'>中</option>");
        ha.append("<option value='" + JV.OUTPUT_ALIGN.H[2] + "'>右</option>");
        let va = $("#elementAlignmentVertical");
        va.append("<option value='" + JV.OUTPUT_ALIGN.V[0] + "'>上</option>");
        va.append("<option value='" + JV.OUTPUT_ALIGN.V[1] + "'>中</option>");
        va.append("<option value='" + JV.OUTPUT_ALIGN.V[2] + "'>下</option>");
    },
    checkAndSetSelectedNodeCfg: function (treeNode) {
        let me = rpt_tpl_cfg_helper;
        if (treeNode[JV.PROP_AREA]) {
            $("#element_hidden")[0].style.display = "";
            $("#element_font")[0].style.display = "";
            $("#element_border")[0].style.display = "";
            $("#element_control")[0].style.display = "";
            $("#element_area_1")[0].style.display = "";
            $("#element_visual_div1")[0].style.display = "none";
            $("#element_visual_div2")[0].style.display = "none";
            $("#element_visual_div3")[0].style.display = "none";
            // $("#element_area_2")[0].style.display = "";
            if (treeNode[JV.PROP_FIELD_ID] || treeNode[JV.PROP_PARAM_ID]) {
                $("#element_pre_suff")[0].style.display = "";
                if (treeNode[JV.PROP_PREFIX]) {
                    $("#elePrefix").get(0).value = treeNode[JV.PROP_PREFIX];
                } else {
                    $("#elePrefix").get(0).value = "";
                }
                if (treeNode[JV.PROP_SUFFIX]) {
                    $("#eleSuffix").get(0).value = treeNode[JV.PROP_SUFFIX];
                } else {
                    $("#eleSuffix").get(0).value = "";
                }
                if (treeNode[JV.PROP_FORMAT]) {
                    $("#eleFormat").get(0).value = treeNode[JV.PROP_FORMAT];
                } else {
                    $("#eleFormat").get(0).value = "";
                }
                if (treeNode[JV.PROP_DFT_VALUE]) {
                    $("#eleDftValue").get(0).value = treeNode[JV.PROP_DFT_VALUE];
                } else {
                    $("#eleDftValue").get(0).value = "";
                }
            } else {
                $("#element_pre_suff")[0].style.display = "none";
                $("#element_hidden")[0].style.display = "none";
            }
            //setup auto height
            if (treeNode[JV.PROP_IS_AUTO_HEIGHT]) {
                $("#eleAutoHeight").get(0).checked = true;
            } else {
                $("#eleAutoHeight").get(0).checked = false;
            }
            //setup hidden
            if (treeNode[JV.PROP_HIDDEN]) {
                $("#eleFieldHidden").get(0).checked = true;
            } else {
                $("#eleFieldHidden").get(0).checked = false;
            }
            //setup cross-row serial
            if (treeNode[JV.PROP_IS_SERIAL]) {
                $("#eleFieldSerial").get(0).checked = true;
            } else {
                $("#eleFieldSerial").get(0).checked = false;
            }
            if (treeNode[JV.TAB_FIELD_PROP_SORT] !== null && treeNode[JV.TAB_FIELD_PROP_SORT] !== undefined) {
                if (treeNode[JV.TAB_FIELD_PROP_SORT] === JV.TAB_FIELD_PROP_SORT_VAL_ASC) {
                    $("#elementSortType").get(0).selectedIndex = 1;
                } else if (treeNode[JV.TAB_FIELD_PROP_SORT] === JV.TAB_FIELD_PROP_SORT_VAL_DESC) {
                    $("#elementSortType").get(0).selectedIndex = 2;
                } else {
                    $("#elementSortType").get(0).selectedIndex = 0;
                }
            } else {
                $("#elementSortType").get(0).selectedIndex = 0;
            }
            //setup font
            let fontDom = $("#elementFonts").get(0);
            fontDom.removeAttribute("disabled");
            let fontAttr = treeNode[JV.PROP_FONT];
            if (typeof treeNode[JV.PROP_FONT] !== "string") {
                fontAttr = "自定义";
            }
            let idx = me.reportCfg.fontArr.indexOf(fontAttr);
            fontDom.selectedIndex = idx;
            me.fontChange(fontDom);
            //setup border
            let borderDom = $("#elementBorders").get(0);
            let borderAttr = treeNode[JV.PROP_STYLE];
            idx = me.reportCfg.borderArr.indexOf(borderAttr);
            borderDom.selectedIndex = idx;
            me.borderChange(borderDom);
            //setup control
            let ctrlDom = $("#elementControls").get(0);
            let ctrlAttr = treeNode[JV.PROP_CONTROL];
            idx = me.reportCfg.controlArr.indexOf(ctrlAttr);
            ctrlDom.selectedIndex = idx;
            me.controlChange(ctrlDom);
            //setup area
            me.private_setup_area(treeNode);
        } else {
            $("#elementFonts").get(0).disabled = "disabled" ;
            $("#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";
            if (treeNode[JV.PROP_NAME] === JV.NODE_FLOW_COLUMN || treeNode[JV.PROP_NAME] === JV.NODE_FLOW_CONTENT) {
                //一些可视化操作
                $("#element_visual_div1")[0].style.display = "";
                $("#element_visual_div2")[0].style.display = "none";
                $("#element_visual_div3")[0].style.display = "none";
                let columnParentNode = null;
                let contentParentNode = null;
                if (treeNode[JV.PROP_NAME] === JV.NODE_FLOW_CONTENT) {
                    contentParentNode = treeNode;
                    columnParentNode = treeNode.getPreNode();
                } else {
                    columnParentNode = treeNode;
                    contentParentNode = treeNode.getNextNode();
                }
                let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
                fieldLocationOprObj.iniSpreadJs(columnParentNode, contentParentNode);
                fieldLocationOprObj.setupColumn(rptTpl, columnParentNode, contentParentNode);
            } else if (treeNode[JV.PROP_NAME].indexOf("统计信息") > 0) {
                $("#element_visual_div1")[0].style.display = "none";
                $("#element_visual_div2")[0].style.display = "";
                $("#element_visual_div3")[0].style.display = "none";
                let rptTpl = (zTreeOprObj.currentNode)?zTreeOprObj.currentNode.rptTpl:null;
                virtualSummaryOprObj.iniSpreadJs(treeNode);
                virtualSummaryOprObj.setupSummary(rptTpl, treeNode);

            } else if (treeNode[JV.PROP_NAME] === `子项`) {
                $("#element_visual_div1")[0].style.display = "none";
                $("#element_visual_div2")[0].style.display = "none";
                $("#element_visual_div3")[0].style.display = "";
            } else {
                $("#element_visual_div1")[0].style.display = "none";
                $("#element_visual_div2")[0].style.display = "none";
                $("#element_visual_div3")[0].style.display = "none";
            }
        }
        if (!zTreeOprObj.isCrossTpl()) {
            $("#element_hidden")[0].style.display = "none";
        }
    },
    hiddenChange: function (dom) {
        dataInfoMapTreeOprObj.currentNode[JV.PROP_HIDDEN] = dom.checked;
    },
    crossRowIsSerialChange: function (dom) {
        dataInfoMapTreeOprObj.currentNode[JV.PROP_IS_SERIAL] = dom.checked;
    },
    crossSortOrderChange: function (dom) {
        switch (dom.selectedIndex) {
            case 0:
                dataInfoMapTreeOprObj.currentNode[JV.TAB_FIELD_PROP_SORT] = JV.TAB_FIELD_PROP_SORT_VAL_NOSORT;
                break;
            case 1:
                dataInfoMapTreeOprObj.currentNode[JV.TAB_FIELD_PROP_SORT] = JV.TAB_FIELD_PROP_SORT_VAL_ASC;
                break;
            case 2:
                dataInfoMapTreeOprObj.currentNode[JV.TAB_FIELD_PROP_SORT] = JV.TAB_FIELD_PROP_SORT_VAL_DESC;
                break;
            default:
                dataInfoMapTreeOprObj.currentNode[JV.TAB_FIELD_PROP_SORT] = JV.TAB_FIELD_PROP_SORT_VAL_ASC;
                break;
        }
    },
    fontChange: function(dom) {
        let me = rpt_tpl_cfg_helper, fontAttr = dom.value;
        //dataInfoMapTreeOprObj.currentNode
        if (fontAttr === "自定义") {
            fontAttr = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
            if (typeof fontAttr === "string") {
                fontAttr = {};
                fontAttr[JV.FONT_PROPS[0]] = "宋体";
                fontAttr[JV.FONT_PROPS[1]] = 12;
                fontAttr[JV.FONT_PROPS[2]] = "BLACK";
                fontAttr[JV.FONT_PROPS[3]] = "F"; //bold
                fontAttr[JV.FONT_PROPS[4]] = "F"; //italic
                fontAttr[JV.FONT_PROPS[5]] = "F"; //underline
                fontAttr[JV.FONT_PROPS[6]] = "F"; //strikeout
                fontAttr[JV.FONT_PROPS[7]] = 0;   //angle
                dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT] = fontAttr;
            }
        } else {
            dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT] = fontAttr;
        }
        me.private_setup_font(fontAttr);
    },
    private_setup_font: function(fontAttr) {
        let me = rpt_tpl_cfg_helper;
        if (typeof fontAttr === "string" && fontAttr !== "自定义") {
            $("#eleFontName").get(0).disabled = "disabled" ;
            $("#eleFontSize").get(0).disabled = "disabled" ;
            $("#eleFontBold").get(0).disabled = "disabled" ;
            $("#eleFontItalic").get(0).disabled = "disabled" ;
            $("#eleFontUnderline").get(0).disabled = "disabled" ;
            let idx = me.reportCfg.fontArr.indexOf(fontAttr);
            $("#elementFonts").get(0).selectedIndex = idx;
            let font = me.reportCfg.fonts[idx];
            $("#eleFontName").get(0).value = font.Name;
            $("#eleFontSize").get(0).value = parseInt(font.FontHeight);
            $("#eleFontBold").get(0).checked = stringUtil.convertStrToBoolean(font.FontBold);
            $("#eleFontItalic").get(0).checked = stringUtil.convertStrToBoolean(font.FontItalic);
            $("#eleFontUnderline").get(0).checked = stringUtil.convertStrToBoolean(font.FontUnderline);
        } else {
            //$("#elementFonts").get(0).selectedIndex = idx;
            $("#eleFontName").get(0).removeAttribute("disabled");
            $("#eleFontSize").get(0).removeAttribute("disabled");
            $("#eleFontBold").get(0).removeAttribute("disabled");
            $("#eleFontItalic").get(0).removeAttribute("disabled");
            $("#eleFontUnderline").get(0).removeAttribute("disabled");
            $("#eleFontName").get(0).value = fontAttr.Name;
            $("#eleFontSize").get(0).value = parseInt(fontAttr.FontHeight);
            $("#eleFontBold").get(0).checked = stringUtil.convertStrToBoolean(fontAttr.FontBold);
            $("#eleFontItalic").get(0).checked = stringUtil.convertStrToBoolean(fontAttr.FontItalic);
            $("#eleFontUnderline").get(0).checked = stringUtil.convertStrToBoolean(fontAttr.FontUnderline);
        }
    },
    private_setup_area: function (treeNode) {
        $("#elementArea_Left").get(0).value = parseFloat(treeNode[JV.PROP_AREA][JV.PROP_LEFT]);
        $("#elementArea_Right").get(0).value = parseFloat(treeNode[JV.PROP_AREA][JV.PROP_RIGHT]);
        $("#elementArea_Top").get(0).value = parseFloat(treeNode[JV.PROP_AREA][JV.PROP_TOP]);
        $("#elementArea_Bottom").get(0).value = parseFloat(treeNode[JV.PROP_AREA][JV.PROP_BOTTOM]);
        if (typeof treeNode[JV.PROP_AREA][JV.PROP_H_CALCULATION] === "string") {
            $("#isPercentage_Left").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_H_CALCULATION] === "percentage";
            $("#isPercentage_Right").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_H_CALCULATION] === "percentage";
        } else {
            $("#isPercentage_Left").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_H_CALCULATION][JV.PROP_LEFT] === "percentage";
            $("#isPercentage_Right").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_H_CALCULATION][JV.PROP_RIGHT] === "percentage";
        }
        if (typeof treeNode[JV.PROP_AREA][JV.PROP_V_CALCULATION] === "string") {
            $("#isPercentage_Top").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_V_CALCULATION] === "percentage";
            $("#isPercentage_Bottom").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_V_CALCULATION] === "percentage";
        } else {
            $("#isPercentage_Top").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_V_CALCULATION][JV.PROP_TOP] === "percentage";
            $("#isPercentage_Bottom").get(0).checked = treeNode[JV.PROP_AREA][JV.PROP_V_CALCULATION][JV.PROP_BOTTOM] === "percentage";
        }
    },
    fontNameChange: function(dom){
        let font = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
        font[JV.FONT_PROPS[0]] = dom.value;
    },
    fontSizeChange: function(dom) {
        let font = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
        font[JV.FONT_PROPS[1]] = dom.value;
    },
    fontBoldChange: function(dom) {
        let font = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
        font[JV.FONT_PROPS[3]] = dom.checked?'T':'F';
    },
    fontItalicChange: function(dom) {
        let font = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
        font[JV.FONT_PROPS[4]] = dom.checked?'T':'F';
    },
    fontUnderlineChange: function(dom) {
        let font = dataInfoMapTreeOprObj.currentNode[JV.PROP_FONT];
        font[JV.FONT_PROPS[5]] = dom.checked?'T':'F';
    },
    borderChange: function(dom) {
        if (dataInfoMapTreeOprObj.currentNode) {
            let me = this, borderAttr = dom.value;
            let idx = me.reportCfg.borderArr.indexOf(borderAttr);
            let border = me.reportCfg.styles[idx];
            dataInfoMapTreeOprObj.currentNode[JV.PROP_STYLE] = borderAttr;

            for (let borderLine of border[JV.PROP_BORDER_STYLE]) {
                switch (borderLine[JV.PROP_POSITION]) {
                    case JV.PROP_LEFT:
                        $("#eleBorderLeft").get(0).value = borderLine[JV.PROP_LINE_WEIGHT];
                        break;
                    case JV.PROP_RIGHT:
                        $("#eleBorderRight").get(0).value = borderLine[JV.PROP_LINE_WEIGHT];
                        break;
                    case JV.PROP_TOP:
                        $("#eleBorderTop").get(0).value = borderLine[JV.PROP_LINE_WEIGHT];
                        break;
                    case JV.PROP_BOTTOM:
                        $("#eleBorderBottom").get(0).value = borderLine[JV.PROP_LINE_WEIGHT];
                        break;
                    default:
                        break;
                }
            }
        }
    },
    controlChange: function(dom) {
        if (dataInfoMapTreeOprObj.currentNode) {
            let me = this, ctrlAttr = dom.value;
            let idx = me.reportCfg.controlArr.indexOf(ctrlAttr);
            let ctrl = me.reportCfg.ctrls[idx];
            dataInfoMapTreeOprObj.currentNode[JV.PROP_CONTROL] = ctrlAttr;

            $("#elementAlignmentHorizon").get(0).selectedIndex = JV.OUTPUT_ALIGN.H.indexOf(ctrl[JV.CONTROL_PROPS[2]]);
            $("#elementAlignmentVertical").get(0).selectedIndex = JV.OUTPUT_ALIGN.V.indexOf(ctrl[JV.CONTROL_PROPS[3]]);
            $("#eleShrink").get(0).checked = stringUtil.convertStrToBoolean(ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_SHRINK]]);
            $("#eleShowZero").get(0).checked = stringUtil.convertStrToBoolean(ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_SHOW_ZERO]]);
            $("#eleAutoWrap").get(0).checked = stringUtil.convertStrToBoolean(ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_WRAP]]);
            $("#eleIsShrinkFirst").get(0).checked = stringUtil.convertStrToBoolean(ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_SHRINK_FIRST]]);
            $("#eleCloseOutput").get(0).checked = stringUtil.convertStrToBoolean(ctrl[JV.CONTROL_PROPS[JV.CONTROL_PROP_IDX_CLOSE_OUTPUT]]);
        }
    },
    changeAlignment: function(dom, type) {
        let ctrl = dataInfoMapTreeOprObj.currentNode[JV.PROP_CONTROL];
        if (type === "horizon") {
            ctrl[JV.CONTROL_PROPS[2]] = JV.OUTPUT_ALIGN[dom.selectedIndex];
        } else {
            ctrl[JV.CONTROL_PROPS[3]] = JV.OUTPUT_ALIGN[dom.selectedIndex];
        }
    },
    changeArea: function(dom, typeStr) {
        if (dataInfoMapTreeOprObj.currentNode) {
            dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][typeStr] = dom.value;
        }
    },
    changeAreaCalcType: function (dom, typeStr) {
        if (dataInfoMapTreeOprObj.currentNode) {
            let otherSideStr = "", caclStr = "";
            switch (typeStr) {
                case JV.PROP_LEFT:
                    caclStr = JV.PROP_H_CALCULATION;
                    otherSideStr = JV.PROP_RIGHT;
                    break;
                case JV.PROP_RIGHT:
                    caclStr = JV.PROP_H_CALCULATION;
                    otherSideStr = JV.PROP_LEFT;
                    break;
                case JV.PROP_TOP:
                    caclStr = JV.PROP_V_CALCULATION;
                    otherSideStr = JV.PROP_BOTTOM;
                    break;
                case JV.PROP_BOTTOM:
                    caclStr = JV.PROP_V_CALCULATION;
                    otherSideStr = JV.PROP_TOP;
                    break;
            }
            if (typeof dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr] === "string") {
                let bk_type = dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr];
                dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr] = {};
                dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr][typeStr] = dom.checked?JV.CAL_TYPE[0]:JV.CAL_TYPE[1];
                dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr][otherSideStr] = bk_type;
            } else {
                dataInfoMapTreeOprObj.currentNode[JV.PROP_AREA][caclStr][typeStr] = dom.checked?JV.CAL_TYPE[0]:JV.CAL_TYPE[1];
            }
        }
    },
    changePreSuff: function (dom, typeStr) {
        if (dataInfoMapTreeOprObj.currentNode) {
            dataInfoMapTreeOprObj.currentNode[typeStr] = dom.value;
        }
    },
    changeFormat: function (dom) {
        if (dataInfoMapTreeOprObj.currentNode) {
            dataInfoMapTreeOprObj.currentNode[JV.PROP_FORMAT] = dom.value;
        }
    },
    changeDftValue: function (dom) {
        if (dataInfoMapTreeOprObj.currentNode) {
            dataInfoMapTreeOprObj.currentNode[JV.PROP_DFT_VALUE] = dom.value;
        }
    },
    changeAutoHeight: function(dom) {
        if (dataInfoMapTreeOprObj.currentNode) {
            dataInfoMapTreeOprObj.currentNode[JV.PROP_IS_AUTO_HEIGHT] = dom.checked;
        }
    }
};