/**
 * 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{
    //某费用定额下补充定额库章节树模板数据条数
    async sectionTemplateCount(req, res) {
        try {
            let count = await rationChapterTreeData.sectionTemplateCount(req.params.compilationId);
            callback(req, res, 0, 'success', {count});
        } catch (err) {
            callback(req, res, 1, err, {count: 0});
        }
    }
    //将该标准定额库的章节树设置为该费用定额下补充定额章节树的模板
    async initSectionTemplate(req, res) {
        try {
            let data = JSON.parse(req.body.data);
            await rationChapterTreeData.initSectionTemplate(data.rationLibId, data.compilationId);
            callback(req, res, 0, 'success', null);
        } catch (err) {
            callback(req, res, 1, err, null)
        }
    }
    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;
