/**
 * Created by jimiz on 2017/4/16.
 */
var Project = require('../models/project');
let logger = require('../../../logs/log_helper').logger;
let project_facade = require("../facade/project_facade");

//统一回调函数
var callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};

module.exports = {
    save: function (req, res) {
        if(typeof req.body.data === 'object'){
            req.body.data = JSON.stringify(req.body.data);
        }
        var data = JSON.parse(req.body.data);
        Project.save(data, function (err, message, result) {
            if (err) {
                logger.err(err);
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, result);
            }
        });
    },
    getData: function (req, res) {
        //add
        console.log(`------------------------------------------`);
        console.log(`${req.session.sessionUser.real_name}--id:${req.session.sessionUser.id}--取getdata数据`);
        console.log(`------------------------------------------`);
        //add
        if(typeof req.body.data === 'object'){
            req.body.data = JSON.stringify(req.body.data);
        }
        var data = JSON.parse(req.body.data);
        // 注释代码用于测试getFilterData
        // Project.getFilterData(data.project_id, ['bills', 'projectGLJ'], function (err, result) {
        //     console.log(result);
        // });
        //test
        Project.getData(data.project_id, function (err, message, result) {
            if (!err) {
                callback(req, res, err, message, result);
            } else {
                callback(req, res, err, message, null);
            }
        }, req.session.sessionUser.id);
    },
    markUpdateProject:async function (req,res) {
        let result={
            error:0
        }
        try {
            let data = req.body.data;
            data = JSON.parse(data);
            let resultData= await project_facade.markUpdateProject(data.updateInfo,data.type);
            result.data=resultData;
        }catch (err){
            logger.err(err);
            result.error=1;
            result.message = err.message;
        }
        res.json(result);
    },
    removeProjectMark:async function(req,res){
        let result={
            error:0
        }
        try {
            let data = req.body.data;
            data = JSON.parse(data);
            let resultData= await project_facade.removeProjectMark(data.projectID);
            result.data=resultData;
        }catch (err){
            logger.err(err);
            result.error=1;
            result.message = err.message;
        }
        res.json(result);
    },
    updateNodes:async function (req,res) {
    /*    var data = JSON.parse(req.body.data);
        project_facade.updateNodes(data, function (err, message, result) {
            if (err) {
                logger.err(err);
                callback(req, res, err, message, null);
            } else {
                callback(req, res, err, message, result);
            }
        });*/
        let result={
            error:0
        }
        try {
            let data = req.body.data;
            if(typeof data === 'object'){
                data = JSON.stringify(data);
            }
            data = JSON.parse(data);
            let resultData= await project_facade.updateNodes(data);
            result.data=resultData;
        }catch (err){
            logger.err(err);
            result.error=1;
            result.message = err.message;
        }
        res.json(result);

    },
    calcInstallationFee:async function(req,res){
        let result={
            error:0
        }
        try {
            let data =  JSON.parse(req.body.data);
            let resultData= await project_facade.calcInstallationFee(data);
            result.data=resultData;
        }catch (err){
            logger.err(err);
            result.error=1;
            result.message = err.message;
        }
        res.json(result);
    },
    saveProperty: function(req, res){
        let result = {error: 0, message: '', data: null};
        let data = JSON.parse(req.body.data);
        project_facade.saveProperty(data, function (err, data) {
            if (err == ''){
                result.data = data;
            }else{
                result.error = 1;
                result.message = err;
            }
            res.json(result);
        })
    },
    getDefaultColSetting: async function(req, res){
        let data = JSON.parse(req.body.data);
        let result={
            error: 0,
            data: null
        };
        try{
            result.data = await project_facade.getDefaultColSetting(data.libID);
        }
        catch(err){
            logger.err(err);
            result.error=1;
            result.message = err.message;
        }
        res.json(result);
    }
};