/**
 * Created by Tony on 2017/5/2.
 */

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

class RationController extends BaseController{
    async getRationItemsByLib(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let rationItems = await rationItem.getRationItemsByLib(data.rationLibId, data.showHint, data.returnFields);
            callback(req, res, 0, '', rationItems);
        }
        catch(err){
            console.log(err);
            callback(req, res, 1, err, null);
        }
    }
    getRationItemsBySection(req, res){
        let sectionId = req.body.sectionID;
        let rationRepId = req.body.rationRepId;
        rationItem.getRationItemsBySection(rationRepId, sectionId, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    }
    mixUpdateRationItems(req, res){
        var sectionId = req.body.sectionID,
            rationLibId = req.body.rationLibId,
            lastOpr = req.body.lastOpr,
            updateItems = JSON.parse(req.body.updateItems),
            addItems = JSON.parse(req.body.addItems),
            removeIds = JSON.parse(req.body.removeIds);
        rationItem.mixUpdateRationItems(rationLibId, lastOpr, sectionId, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    }
    removeRationItems(req, res){
        var rIds = JSON.parse(req.body.updateItems);
        if (rIds && rIds.length > 0) {
            rationItem.removeRationItems(rIds, function(err, message, rst){
                if (err) {
                    callback(req, res, err, message, null);
                } else {
                    callback(req, res, err, message, rst);
                }
            });
        }
    }
    updateRationBasePrc (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.updateRationBasePrc(data, function (err, message) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, null);
            }
        })
    }
    getRationGljIds (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.getRationGljIds(data, function (err, message, ids) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, ids);
            }
        })
    }
    getRationsCodes (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.getRationsCodes(data, function (err, message, codes) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, codes);
            }
        })
    }

    updateJobContent(req, res){
        let repId = req.body.repId,
            updateArr = JSON.parse(req.body.updateArr),
            lastOpr = req.body.lastOpr;
        rationItem.updateJobContent(lastOpr, repId, updateArr, function (err) {
            callback(req, res, err, '', null);
        });
    }
    updateAnnotation(req, res){
        let repId = req.body.repId,
            updateArr = JSON.parse(req.body.updateArr),
            lastOpr = req.body.lastOpr;
        rationItem.updateAnnotation(lastOpr, repId, updateArr, function (err) {
            callback(req, res, err, '', null);
        });
    }
    async updateRationTemplate(req, res){
        let data = JSON.parse(req.body.data);
        try {
            let newTemplate = await rationItem.updateRationTemplate(data.rationRepId, data.rationID, data.templateData);
            callback(req, res, 0, 'success', newTemplate);
        } catch (err) {
            callback(req, res, 1, err, null);
        }
    }
}

export default RationController;

/*
module.exports = {
    getRationItemsBySection: function(req, res){
        var sectionId = req.body.sectionID;
        rationItem.getRationItemsBySection(sectionId, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    },
    mixUpdateRationItems: function(req, res){
        var sectionId = req.body.sectionID,
            rationLibId = req.body.rationLibId,
            updateItems = JSON.parse(req.body.updateItems),
            addItems = JSON.parse(req.body.addItems),
            removeIds = JSON.parse(req.body.removeIds);
        rationItem.mixUpdateRationItems(rationLibId, sectionId, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    },
    removeRationItems: function(req, res){
        var rIds = JSON.parse(req.body.updateItems);
        if (rIds && rIds.length > 0) {
            rationItem.removeRationItems(rIds, function(err, message, rst){
                if (err) {
                    callback(req, res, err, message, null);
                } else {
                    callback(req, res, err, message, rst);
                }
            });
        }
    },
    updateRationBasePrc: function (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.updateRationBasePrc(data, function (err, message) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, null);
            }
        })
    },
    getRationGljIds: function (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.getRationGljIds(data, function (err, message, ids) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, ids);
            }
        })
    },
    getRationsCodes: function (req, res) {
        let data = JSON.parse(req.body.data);
        rationItem.getRationsCodes(data, function (err, message, codes) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, codes);
            }
        })
    }
}*/
