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

var rationChapterTreeData = require('../models/ration_section_tree').Dao;
var callback = function(req,res,err,message, data){
    res.json({error: err, message: message, data: data});
}
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)
        });
    }
}