/**
 * Created by Tony on 2017/4/21.
 */

var rationChapterTreeData = require('../models/ration_section_tree');
import BaseController from "../../common/base/base_controller";
var callback = function(req,res,err,message, data){
    res.json({error: err, message: message, data: data});
}

class RationChapterTreeController extends BaseController{
    getRationChapterTree(req,res){
        let data = JSON.parse(req.body.data);
        var rationLibId = data.rationLibId;
        var repId = data.rationRepositoryId;
        if (rationLibId) {
            rationChapterTreeData.getRationChapterTree(rationLibId,function(err,data){
                callback(req,res,err, "", data);
            })
        } else if (repId) {
            rationChapterTreeData.getRationChapterTreeByRepId(repId,function(err,data){
                callback(req,res,err,"", data)
            })
        }
    }
    getNewRationTreeID(req, res){
        rationChapterTreeData.getNewRationTreeID(function (err, data) {
            callback(req, res, err, '', data);
        });

    }
    createNewNode(req, res){
        var libId = req.body.rationLibId;
        var lastNodeId = req.body.lastNodeId;
        let lastOpr = req.body.lastOpr;
        var nodeData = JSON.parse(req.body.rawNodeData);
        rationChapterTreeData.createNewNode(lastOpr, libId, lastNodeId, nodeData, function(err,data){
            callback(req,res,err,"", data)
        });
    }
    updateNodes(req, res) {
        let data = JSON.parse(req.body.data);
        let lastOpr = data.lastOpr;
        let updateData = data.updateData;
        rationChapterTreeData.updateNodes(lastOpr, updateData, function(err,results){
            callback(req,res, err, "", results)
        });
    }
    deleteNodes(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        var preNodeId = req.body.preNodeId;
        var preNodeNextId = req.body.preNodeNextId;
        let repId = req.body.repId;
        let lastOpr = req.body.lastOpr;
        rationChapterTreeData.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, "", results)
        });
    }
    updateExplanation(req, res){
        let repId = req.body.repId,
            nodeId = req.body.nodeId,
            explanation = req.body.explanation,
            lastOpr = req.body.lastOpr;
        rationChapterTreeData.updateExplanation(lastOpr, repId, nodeId, explanation, function (err) {
            callback(req, res, err, '', null);
        });

    }
    updateRuleText(req, res){
        let repId = req.body.repId,
            nodeId = req.body.nodeId,
            ruleText = req.body.ruleText,
            lastOpr = req.body.lastOpr;
        rationChapterTreeData.updateRuleText(lastOpr, repId, nodeId, ruleText, function (err) {
            callback(req, res, err, '', null);
        });

    }
    updateSituation(req, res){
        let repId = req.body.repId,
            nodeId = req.body.nodeId,
            situation = req.body.situation,
            lastOpr = req.body.lastOpr;
        rationChapterTreeData.updateSituation(lastOpr, repId, nodeId, situation, function (err) {
            callback(req, res, err, '', null);
        });
    }
    updateAnnoSituation(req, res){
        let repId = req.body.repId,
            nodeId = req.body.nodeId,
            situation = req.body.situation,
            lastOpr = req.body.lastOpr;
        rationChapterTreeData.updateAnnoSituation(lastOpr, repId, nodeId, situation, function (err) {
            callback(req, res, err, '', null);
        });
    }
}

export default RationChapterTreeController;

/*
module.exports ={
    getRationChapterTree: function(req,res){
        var rationLibId = req.body.rationLibId;
        var repId = req.body.rationRepositoryId;
        if (rationLibId) {
            rationChapterTreeData.getRationChapterTree(rationLibId,function(err,data){
                callback(req,res,err, "", data);
            })
        } else if (repId) {
            rationChapterTreeData.getRationChapterTreeByRepId(repId,function(err,data){
                callback(req,res,err,"", data)
            })
        }
    },
    createNewNode: function(req, res){
        var libId = req.body.rationLibId;
        var lastNodeId = req.body.lastNodeId;
        var nodeData = JSON.parse(req.body.rawNodeData);
        rationChapterTreeData.createNewNode(libId, lastNodeId, nodeData, function(err,data){
            callback(req,res,err,"", data)
        });
    },
    updateNodes: function(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        rationChapterTreeData.updateNodes(nodes, function(err,results){
            callback(req,res, err, "", results)
        });
    },
    deleteNodes: function(req, res) {
        var nodes = JSON.parse(req.body.nodes);
        var preNodeId = req.body.preNodeId;
        var preNodeNextId = req.body.preNodeNextId;
        rationChapterTreeData.removeNodes(nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, "", results)
        });
    }
}*/
