/**
 * Created by Zhong on 2017/12/20.
 */
//定额章节节点说明、计算规则
let explanatoryOprObj = {
    preTreeNode: null,
    currentTreeNode: null,//定额章节树节点
    currentExplanation: null,
    currentRuleText: null,
    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 (exarea, ruarea) {
        let me = explanatoryOprObj;
        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 (exarea, ruarea, explanation, 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();
            }
        });
    }
};