/**
 * Created by Zhong on 2017/12/20.
 */
//定额章节节点说明、计算规则
let explanatoryOprObj = {
    exEditor: null,
    calcEditor: null,
    erratumEditor: null,
    preTreeNode: null,
    currentTreeNode: null,//定额章节树节点
    currentExplanation: null,
    currentErratum: 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 erratumEditor = CodeMirror.fromTextArea(document.getElementById("erratumRecordShow"), {
            mode: "text/html",
            lineNumbers: true,
            theme: "material",
            readOnly: locked
        });
        erratumEditor.setSize('auto', '500px');
        $('#erratumRecordLink').click(function () {
            setTimeout(function () {
                erratumEditor.refresh();
            }, 100);
        });
        this.erratumEditor = erratumEditor;


        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, erratum, ruleText) {
        let me = explanatoryOprObj;
        me.preTreeNode = preNode;
        me.currentTreeNode = currentNode;
        me.currentExplanation = explanation;
        me.currentErratum = erratum;
        me.currentRuleText = ruleText;
    },
    clickUpdate: function (exarea, errArea, ruarea) {//解决编辑完后在未失去焦点的时候直接定额章节树
        let me = explanatoryOprObj;
        if (exarea.is(':focus')) {
            let explanation = exarea.val();
            if (explanation !== me.currentExplanation) {
                me.preTreeNode.data.explanation = explanation;
                me.unbindEvents(exarea, errArea, ruarea);
                exarea.blur();
                me.updateExplanation(pageOprObj.rationLibId, me.preTreeNode.getID(), explanation, function () {
                    me.bindEvents(exarea, errArea, ruarea);
                });
            }
        }
        if (errArea.is(':focus')) {
            let erratum = errArea.val();
            if (erratum !== me.currentErratum) {
                me.preTreeNode.data.erratumRecord = erratum;
                me.unbindEvents(exarea, errArea, ruarea);
                errArea.blur();
                me.updateErratumRecord(pageOprObj.rationLibId, me.preTreeNode.getID(), erratum, function () {
                    me.bindEvents(exarea, errArea, ruarea);
                });
            }
        }
        if (ruarea.is(':focus')) {
            let ruleText = ruarea.val();
            if (ruleText !== me.currentRuleText) {
                me.preTreeNode.data.ruleText = ruleText;
                me.unbindEvents(exarea, errArea, ruarea);
                ruarea.blur();
                me.updateRuleText(pageOprObj.rationLibId, me.preTreeNode.getID(), ruleText, function () {
                    me.bindEvents(exarea, errArea, ruarea);
                });
            }
        }
    },
    unbindEvents: function (exarea, errArea, ruarea) {
        exarea.unbind();
        errArea.unbind();
        ruarea.unbind();
    },
    bindEvents: function (exEd, errEd, 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;
                });
            }
        });
        errEd.on('change', function () {
            let node = me.currentTreeNode;
            let newData = errEd.getValue();
            if (node && node.data.erratumRecord !== newData) {
                me.updateErratumRecord(pageOprObj.rationLibId, node.getID(), newData, function () {
                    node.data.erratumRecord = 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;
                });
            }
        })
    },
    showText: function (explanation, erratumRecord, ruleText) {
        this.exEditor.setValue(explanation && explanation !== 'undefined' ? explanation : '');
        this.erratumEditor.setValue(erratumRecord && erratumRecord !== 'undefined' ? erratumRecord : '');
        this.calcEditor.setValue(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();
            }
        });
    },
    //更新勘误记录
    updateErratumRecord: function (repId, nodeId, erratumRecord, callback) {
        $.ajax({
            type: 'post',
            url: 'api/updateErratumRecord',
            data: { lastOpr: userAccount, repId: pageOprObj.rationLibId, nodeId: nodeId, erratumRecord: erratumRecord },
            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();
            }
        });
    }
};