/**
 * Created by Zhong on 2017/12/20.
 */
//定额章节节点说明、计算规则
let explanatoryOprObj = {
    exEditor: null,
    calcEditor: null,
    preTreeNode: null,
    currentTreeNode: null,//定额章节树节点
    currentExplanation: null,
    currentRuleText: null,
    // 初始化说明、计算规则编辑器
    initEditor: function () {
        const locked = lockUtil.getLocked();
        const exEditor = CodeMirror.fromTextArea(document.getElementById("explanationShow"), {
            mode: "text/html",
            lineNumbers: true,
            theme:"material",
            readOnly: locked
        });
        exEditor.setSize('auto','500px');
        $('#explanationLink').click(function () {
            setTimeout(function () {
                exEditor.refresh();
            }, 100);
        });
        this.exEditor = exEditor;
        const calcEditor = CodeMirror.fromTextArea(document.getElementById("ruleTextShow"), {
            mode: 'text/html',
            lineNumbers: true,
            theme: 'material',
            readOnly: locked
        });
        calcEditor.setSize('auto', '500px');
        $('#ruleTextLink').click(function () {
            setTimeout(function () {
                calcEditor.refresh();
            }, 100);
        });
        this.calcEditor = calcEditor;
    },
    setAttribute: function (preNode, currentNode, explanation, ruleText) {
        let me = explanatoryOprObj;
        me.preTreeNode = preNode;
        me.currentTreeNode = currentNode;
        me.currentExplanation = explanation;
        me.currentRuleText = ruleText;
    },
    clickUpdate: function (exarea, ruarea) {//解决编辑完后在未失去焦点的时候直接定额章节树
        let me = explanatoryOprObj;
        if(exarea.is(':focus')){
            let explanation = exarea.val();
            if(explanation !== me.currentExplanation){
                me.preTreeNode.data.explanation = explanation;
                me.unbindEvents(exarea, ruarea);
                exarea.blur();
                me.updateExplanation(pageOprObj.rationLibId, me.preTreeNode.getID(), explanation, function () {
                    me.bindEvents(exarea, ruarea);
                });
            }
        }
        if(ruarea.is(':focus')){
            let ruleText = ruarea.val();
            if(ruleText !== me.currentRuleText){
                me.preTreeNode.data.ruleText = ruleText;
                me.unbindEvents(exarea, ruarea);
                ruarea.blur();
                me.updateRuleText(pageOprObj.rationLibId, me.preTreeNode.getID(), ruleText, function () {
                    me.bindEvents(exarea, ruarea);
                });
            }
        }
    },
    unbindEvents: function (exarea, ruarea) {
        exarea.unbind();
        ruarea.unbind();
    },
    bindEvents: function (exEd, calcEd) {
        let me = explanatoryOprObj;
        exEd.on('change', function () {
            let node = me.currentTreeNode;
            let newData = exEd.getValue();
            if(node && node.data.explanation !== newData){
                me.updateExplanation(pageOprObj.rationLibId, node.getID(), newData, function () {
                    node.data.explanation = newData;
                });
            }
        });
        calcEd.on('change', function () {
            let node = me.currentTreeNode;
            let newData = calcEd.getValue();
            if(node && node.data.ruleText !== newData){
                me.updateRuleText(pageOprObj.rationLibId, node.getID(), newData, function () {
                    node.data.ruleText = newData;
                });
            }
        })
        /*exarea.bind('change', function () {
            let explanation = exarea.val();
            let node = me.currentTreeNode;
            exarea.attr('disabled', true);
            me.updateExplanation(pageOprObj.rationLibId, node.getID(), explanation, function () {
                node.data.explanation = explanation;
                exarea.attr('disabled', false);
            });
        });*/
        /*ruarea.bind('change', function () {
            let ruleText = ruarea.val();
            let node = me.currentTreeNode;
            ruarea.attr('disabled', true);
            me.updateRuleText(pageOprObj.rationLibId, node.getID(), ruleText, function () {
                node.data.ruleText = ruleText;
                ruarea.attr('disabled', false);
            });
        });*/
    },
    showText: function (exEd, calcEd, explanation, ruleText) {
        exEd.setValue(explanation && explanation !== 'undefined' ? explanation : '');
        calcEd.setValue(ruleText && ruleText !== 'undefined' ? ruleText : '');
        //exarea.val(explanation && explanation !== 'undefined' ? explanation : '');
        //ruarea.val(ruleText && ruleText !== 'undefined' ? ruleText : '');
    },
    //更新说明
    updateExplanation: function (repId, nodeId, explanation, callback) {
        $.ajax({
            type: 'post',
            url: 'api/updateExplanation',
            data: {lastOpr: userAccount, repId: pageOprObj.rationLibId, nodeId: nodeId, explanation: explanation},
            dataType: 'json',
            success: function () {
                callback();
            }
        });
    },
    //更新计算规则
    updateRuleText: function (repId, nodeId, explanation, callback) {
        $.ajax({
            type: 'post',
            url: 'api/updateRuleText',
            data: {lastOpr: userAccount, repId: pageOprObj.rationLibId, nodeId: nodeId, ruleText: explanation},
            dataType: 'json',
            success: function () {
                callback();
            }
        });
    }
};