/**
 * Created by Tony on 2017/7/7.
 */

let calculationTreeOprObj = {
    treeObj : null,
    extCodeTreeObj: null,
    currentNode: null,
    currentExtCodeNode: null,
    iniTree: function(rptTpl) {
        var me = this;
        let fieldMapList = me.buildTreeData(rptTpl);
        me.treeObj = $.fn.zTree.init($("#rpt_tpl_formulas"), caculationSetting, fieldMapList);
        me.treeObj.expandAll(true);
        $("#exprDetail")[0].style.display = "none";
    },
    iniExtCodeTpl: function () {
        let me = this, params = {};
        CommonAjax.postEx("report_tpl_api/getExtCodeTpl", params, 10000, true, function(result){
            me.extCodeTreeObj = $.fn.zTree.init($("#rpt_ext_code_tpl"), extCodeSetting, result);
            let nodes = me.extCodeTreeObj.getNodes();
            for (let node of nodes) {
                me.extCodeTreeObj.expandNode(node, true, false);
            }
            // me.extCodeTreeObj.expandAll(true);
            }, null, null
        );
    },
    buildRunType: function() {
        let et = $("#exprRunType");
        et.append("<option value='" + JV.RUN_TYPE_BEFORE_PAGING + "'>预运行</option>");
        et.append("<option value='" + JV.RUN_TYPE_BEFORE_OUTPUT + "'>实时运行</option>");
        et.append("<option value='" + JV.RUN_TYPE_BEFORE_ANALYZING + "'>数据分析前运行</option>");
        et.append("<option value='" + JV.RUN_TYPE_BEFORE_COMBINE + "'>合并单元格前事件</option>");
        et.append("<option value='" + JV.RUN_TYPE_AFTER_COMBINE + "'>合并单元格后事件</option>");
        et.append("<option value='" + JV.RUN_TYPE_BEFORE_GROUP_TEXT_OUT + "'>分组text输出前事件</option>");
    },
    buildTreeData: function (rptTpl) {
        let me = this, rst = {"Name": "计算式", items: []};
        if (rptTpl[JV.NODE_FORMULAS]) {
            for (let cItem of rptTpl[JV.NODE_FORMULAS]) {
                let node = {};
                me.copyContent(cItem, node);
                node[JV.PROP_NAME] = cItem[JV.PROP_NAME];
                node[JV.PROP_RUN_TYPE] = cItem[JV.PROP_RUN_TYPE];
                node[JV.PROP_EXPRESSION] = cItem[JV.PROP_EXPRESSION];
                node["format"] = (cItem["format"])?cItem["format"]:"";
                rst.items.push(node);
            }
        }
        return rst;
    },
    copyContent: function(source, target) {
        target[JV.PROP_NAME] = source[JV.PROP_NAME];
        target[JV.PROP_RUN_TYPE] = source[JV.PROP_RUN_TYPE];
        target[JV.PROP_EXPRESSION] = source[JV.PROP_EXPRESSION];
        target["format"] = (source["format"])?source["format"]:"";
    },
    changeRunType: function (dom) {
        let me = calculationTreeOprObj;
        if (me.currentNode) {
            me.currentNode[JV.PROP_RUN_TYPE] = dom.value;
        }
    },
    setExtCodeTemplate: function (dom) {
        let me = calculationTreeOprObj;
        if (me.currentExtCodeNode && me.currentExtCodeNode["Template"] !== '') {
            $("#exprContent")[0].value = me.currentExtCodeNode["Template"];
        }
    },
    addExtCodeTemplate: function (dom) {
        let me = calculationTreeOprObj;
        if (me.currentExtCodeNode && me.currentExtCodeNode["Template"] !== '') {
            $("#exprContent")[0].value += me.currentExtCodeNode["Template"];
        }
    },
    changeFormat: function (dom) {
        let me = calculationTreeOprObj;
        if (me.currentNode) {
            me.currentNode["format"] = dom.value;
        }
    },
    changeExpression: function (dom) {
        let me = calculationTreeOprObj;
        if (me.currentNode) {
            me.currentNode[JV.PROP_EXPRESSION] = dom.value;
        }
    },
    onClick: function (event,treeId,treeNode) {
        let me = calculationTreeOprObj;
        me.currentNode = treeNode;
        if (treeNode.level === 0) {
            $("#exprDetail")[0].style.display = "none";
        } else {
            $("#exprDetail")[0].style.display = "";
            if (treeNode[JV.PROP_RUN_TYPE] === JV.RUN_TYPE_BEFORE_PAGING) {
                $("#exprRunType")[0].selectedIndex = 0;
            } else if (treeNode[JV.PROP_RUN_TYPE] === JV.RUN_TYPE_BEFORE_OUTPUT) {
                $("#exprRunType")[0].selectedIndex = 1;
            } else if (treeNode[JV.PROP_RUN_TYPE] === JV.RUN_TYPE_BEFORE_ANALYZING) {
                $("#exprRunType")[0].selectedIndex = 2;
            } else if (treeNode[JV.PROP_RUN_TYPE] === JV.RUN_TYPE_BEFORE_COMBINE) {
                $("#exprRunType")[0].selectedIndex = 3;
            } else {
                $("#exprRunType")[0].selectedIndex = 4;
            }
            $("#exprFormat")[0].value = (treeNode["format"])?treeNode["format"]:"";
            $("#exprContent")[0].value = treeNode[JV.PROP_EXPRESSION];
            //
        }
    },
    onExtCodeTreeClick: function (event, treeId, treeNode) {
        let me = calculationTreeOprObj;
        me.currentExtCodeNode = treeNode;
    },
    onBeforeRemove: function(treeId, treeNode){
        let rst = false;
        if (treeNode.level > 0) {
            rst = confirm('请确认是否真的删除?');
        }
        return rst;
    },
    beforeRename: function(treeId, treeNode, newName, isCancel) {
        if (isCancel) {
            return true;
        }
        if (treeNode.level === 0) {
            return false;
        }
        return true;
    },
    onBeforeDrop: function (treeId, treeNodes, targetNode, moveType) {
        let rst = false;
        if (targetNode.level === 1 && (moveType === "next" || moveType === "prev")) {
            return true;
        }
        return rst;
    },
    addHoverDom: function(treeId, treeNode) {
        let me = calculationTreeOprObj, sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_"+treeNode.tId).length > 0 || treeNode.level > 0) {
            return;
        } else {
            let addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='新增' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            let btn = $("#addBtn_"+treeNode.tId);
            btn.bind("click", function(){
                let newNodes = [], node = {};
                node[JV.PROP_NAME] = "新计算式";
                node[JV.PROP_RUN_TYPE] = JV.RUN_TYPE_BEFORE_PAGING;
                node[JV.PROP_EXPRESSION] = "";
                node["format"] = "";
                newNodes.push(node);
                me.treeObj.addNodes(treeNode, -1, newNodes, true);
            });
        }
    },
    removeHoverDom: function(treeId, treeNode) {
        $("#addBtn_"+treeNode.tId).unbind().remove();
    },
    extractCalculation: function (rptTpl) {
        let me = this, newCaclList = [];
        let topNode = me.treeObj.getNodes()[0];
        for (let node of topNode.items) {
            let item = {};
            me.copyContent(node, item);
            newCaclList.push(item);
        }
        rptTpl[JV.NODE_FORMULAS] = newCaclList;
    }
}