/**
 * Created by CSL on 2017-03-23.
 */

const uuidV1 = require('uuid/v1');
let mongoose = require('mongoose');
let feeRateModel = mongoose.model('fee_rates');
let feeRateFileModel = mongoose.model('fee_rate_file');
let feeRateFacde=require('../facade/fee_rates_facade');
let logger = require("../../../logs/log_helper").logger;
let template = require('./feerate_template');
/*
var stdFeeRateLibs = require('../../std_fee_rate_libs/models/std_fee_rate_libs');
var projectFeeRateFile = require('../models/fee_rates');

module.exports = {
    libNames: function (req, res) {
        var region = req.body.region;
        stdFeeRateLibs.libNames(region, function (doc) {
            res.json({data: doc});
        });
    },

    libFeeRates: function (req, res) {
        var libID = req.body.libID;
        stdFeeRateLibs.rates(libID, function (doc) {
            res.json({data: doc});
        })
    },

    projectFeeRates: function (req, res) {
        var fileID = req.body.fileID;
        projectFeeRateFile.rates(fileID, function (doc) {
            res.json({data: doc});
        })
    },

    projectFeeUpdateRate: function (req, res) {
        var rateObj = {"fileID": req.body.fileID, "rateID": req.body.rateID, "rateValue": req.body.rateValue};
        projectFeeRateFile.updateRate(rateObj, function (doc) {
            res.json({data: doc});
        })
    }
}
*/



module.exports = {
    libNames: libNames,
    getFeeRateStandards:getFeeRateStandards,
    changeFeeRateStandard:changeFeeRateStandard,
    checkFeeRateName:checkFeeRateName,
    getChangeInfo:getChangeInfo,
    changeFeeRateFileFromCurrent:changeFeeRateFileFromCurrent,
    changeFeeRateFileFromOthers:changeFeeRateFileFromOthers,
    setFeeRateToBill:setFeeRateToBill,
    updateFeeRate:updateFeeRate,
    updateFeeRates:updateFeeRates,
    updateRates:updateRates,
    feeRateFileSaveAs:feeRateFileSaveAs
}

function libNames(req, res) {
    // var region = req.body.region;
    let temObject = template.feerate_template1;
    var feeRate = {};
    feeRate.ID = uuidV1();
    feeRate.rates = temObject.rates;
    var feeRateFile = {};
    feeRateFile.ID = uuidV1();
    feeRateFile.rootProjectID =99;
    feeRateFile.name = '费率1';
    feeRateFile.libID = temObject.ID;
    feeRateFile.libName = temObject.libName;
    feeRateFile.feeRateID = feeRate.ID;

    feeRateModel.create(feeRate,function (err,doc) {

    });

    feeRateFileModel.create(feeRateFile,function (err,doc) {

    });
}


async function getFeeRateStandards(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        data = JSON.parse(data);
        let feeRateStandards= await feeRateFacde.getFeeRateStandardsByProjectID(data.projectID);
        result.data= feeRateStandards;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function changeFeeRateStandard(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let newFeeRateFile= await feeRateFacde.changeFeeRateStandard(data);
        result.data= newFeeRateFile;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function checkFeeRateName(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let nameExist= await feeRateFacde.checkFeeRateName(data);
        result.data= nameExist;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function getChangeInfo(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let info= await feeRateFacde.getChangeInfo(data,req.session.sessionCompilation._id);
        result.data= info;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function changeFeeRateFileFromCurrent(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let feeRateFile= await feeRateFacde.changeFeeRateFileFromCurrent(data);
        result.data= feeRateFile;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function changeFeeRateFileFromOthers(req, res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let feeRateFile= await feeRateFacde.changeFeeRateFileFromOthers(data);
        result.data= feeRateFile;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}


async function setFeeRateToBill(req,res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let uresult= await feeRateFacde.setFeeRateToBill(data);
        console.log(uresult);
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function updateFeeRate(req,res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let uresult= await feeRateFacde.updateFeeRate(data);
        result.data=uresult;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

async function updateFeeRates(req,res) {
    let result={
        error:0
    }
    try {
        let data = req.body.data;
        let uresult= await feeRateFacde.updateFeeRates(data);
        result.data=uresult;
    }catch (err){
        console.log(err);
        result.error=1;
        result.message = err.message;
    }
    res.json(result);
}

function updateRates(req,res){
    let result={
        error:0
    }
    let data = req.body.data;
    data=JSON.parse(data);
    feeRateFacde.updateRates(data.user_id,data)(function (err,re) {
        if(err){
            result.error=1;
            result.message = err.message;
        }else {
            result.data = re;
        }
        res.json(result);
    })
}

function feeRateFileSaveAs(req,res){
    let result={
        error:0
    }
    let data = req.body.data;
    data=JSON.parse(data);
    feeRateFacde.feeRateFileSaveAs(data.user_id,data)(function (err,re) {
        if(err){
            result.error=1;
            result.message = err.message;
        }else {
            result.data = re;
        }
        res.json(result);
    })
}