/**
 * Created by Zhong on 2017/8/22.
 */
import BaseController from "../../common/base/base_controller";
import stdgljutil  from "../../../public/cache/std_glj_type_util";
import GljDao from "../models/gljModel";
import EngineeringLibModel from "../../users/models/engineering_lib_model";

let gljDao = new GljDao();
let callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};

class GljController extends BaseController{
    async redirectGlj(req, res){
        let gljLibId = null, engineeringId, sessionCompilation = req.session.sessionCompilation,
            rationValuation = sessionCompilation.ration_valuation,
            billValuation = sessionCompilation.bill_valuation,
            engineeringLibModel = new EngineeringLibModel();
        if(rationValuation[0]){
            let engineeringList = rationValuation[0].engineering_list;
            engineeringId = engineeringList.length > 0 ? engineeringList[0].engineering_id : null;
            let engineeringInfo = await engineeringLibModel.getEngineering(engineeringId);
            gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
        }
        else if(billValuation[0]){
            let engineeringList = billValuation[0].engineering_list;
            engineeringId = engineeringList.length > 0 ? engineeringList[0].engineering_id : null;
            let engineeringInfo = await engineeringLibModel.getEngineering(engineeringId);
            gljLibId = engineeringInfo.glj_lib.length > 0 && typeof engineeringInfo.glj_lib !== 'undefined' ? engineeringInfo.glj_lib[0].id : null;
        }
        res.render('building_saas/complementary_glj_lib/html/tools-gongliaoji.html',{
            userID: req.session.sessionUser.ssoId,
            gljLibId: gljLibId,
            compilationId: sessionCompilation._id
        });
    }
    getGljDistType (req, res) {
        let gljDistTypeCache = stdgljutil.getStdGljTypeCacheObj().toArray();
        if(gljDistTypeCache.length >0 ){
            callback(req, res, null, '', gljDistTypeCache);
        }
        else {
            callback(req, res, 1, 'Error', null);
        }
    }
    getGljTree(req,res){
        let gljLibId = req.body.gljLibId;
        gljDao.getGljTypes(gljLibId,function(err,data){
            callback(req,res,err, 'Get Tree', data)
        });
    }
   /* updateRationBasePrc(req, res){
        let basePrcArr = JSON.parse(req.body.basePrcArr);
        rationItemDao.updateRationBasePrc(basePrcArr, function (err, message) {
            if(err){
                callback(req, res, err, message, null);
            }
            else{
                callback(req, res, err, message, null);
            }
        });
    }*/

   /* getRationGljIds(req, res){
        let rationLibs = req.body.rationLibs;
        gljDao.getRationGljIds(rationLibs, function (err, msg, data) {
            callback(req, res, err, msg, data);
        })
    }*/
    createNewGljTypeNode(req, res) {
        let repId = req.body.repositoryId;
        let lastNodeId = req.body.lastNodeId;
        let lastOpr = req.body.lastOpr;
        let nodeData = JSON.parse(req.body.rawNodeData);
        gljDao.createNewNode(repId, lastOpr, lastNodeId, nodeData, function(err, msg, data){
            callback(req,res,err,msg, data)
        });
    }
    updateGljNodes(req, res) {
        let nodes = JSON.parse(req.body.nodes);
        let repId = req.body.repId,
            lastOpr = req.body.lastOpr;
        gljDao.updateNodes(repId, lastOpr, nodes, function(err,results){
            callback(req,res, err, results)
        });
    }
    deleteGljNodes(req, res) {
        let nodes = JSON.parse(req.body.nodes);
        let preNodeId = req.body.preNodeId;
        let preNodeNextId = req.body.preNodeNextId;
        let repId = req.body.repId, lastOpr = req.body.lastOpr;
        gljDao.removeNodes(repId, lastOpr, nodes, preNodeId, preNodeNextId, function(err,results){
            callback(req,res, err, results)
        });
    }
    getGljItems(req, res) {
        let stdGljLibId = req.body.stdGljLibId,
            userId = req.body.userId,
            compilationId = req.body.compilationId;
            gljDao.getGljItems(stdGljLibId, userId, compilationId, function(err, data){
                callback(req,res,err,'Get Items',data)
            });
    }
   /* getGljItemsByIds(req, res) {
        let gljIds = JSON.parse(req.body.gljIds);
        gljDao.getGljItems(gljIds, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    }
    getGljItemsByCodes(req, res) {
        let gljCodes = JSON.parse(req.body.gljCodes),
            repId = req.body.repId;
        gljDao.getGljItemsByCode(repId, gljCodes, function(err, data){
            callback(req,res,err,'Get Items',data)
        });
    }*/
    updateComponent(req, res){
        let userId = req.body.userId;
        let updateArr = JSON.parse(req.body.updateArr);
        gljDao.updateComponent(userId, updateArr, function (err, message, rst) {
            callback(req, res, err, message, rst);
        })
    }
    mixUpdateGljItems(req, res){
        let userId = req.body.userId,
            compilationId = req.body.compilationId,
            updateItems = JSON.parse(req.body.updateItems),
            addItems = JSON.parse(req.body.addItems),
            removeIds = JSON.parse(req.body.removeIds);
        gljDao.mixUpdateGljItems(userId, compilationId, updateItems, addItems, removeIds, function(err, message, rst){
            if (err) {
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, rst);
            }
        });
    }
}

export default GljController;