/**
 * Created by Zhong on 2017/9/28.
 */

import BaseController from '../../common/base/base_controller';
import OptionsDao from '../models/optionsModel';
import optionSetting from '../models/optionTypes';

let optionsDao = new OptionsDao();
class OptionController extends BaseController {
    //获得所有选项类型的选项
    async getOptions(req, res){
        let resJson = {error: 0, message: '', data: []};
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id;
        try{
            resJson.data = await optionsDao.getOptions(user_id, compilation_id);
            if(!resJson.data){
                resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optionSetting);
            }
        }
        catch (err){
            resJson.error = true;
            resJson.message = '获取失败';
            resJson.data = null;
        }
        res.json(resJson);
    }
    //获得特定选项类型的选项
    async getOptionsByType(req, res){
        let resJson = {error: null, message: '', data: null};
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id,
            optsType = req.body.optsType;
        try{
            resJson.data = await optionsDao.getOptionsByType(user_id, compilation_id, optsType);
        }
        catch (err){
            resJson.error = true;
            resJson.message = '获取失败';
            resJson.data = null;
        }
        res.json(resJson);
    }

    async saveOptions(req, res){
        let resJson = {error: 0, message: '', data: null};
        let data = JSON.parse(req.body.data);
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id,
            optSetting = data.optSetting;
        try{
            resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optSetting);
        }
        catch (err){
            resJson.error = true;
            resJson.message = '保存失败';
            resJson.data = null;
        }
        res.json(resJson);
    }
}




/*class OptionController extends BaseController {
    //获得所有选项类型的选项
    async getOptions(req, res){
        let resJson = {error: 0, message: '', data: []};
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id;
        let defaultOpts = {
            GENERALOPTS: {
                rationQuanACToBillsQuan: true,//自动根据清单工程量填写定额工程量
                rationQuanACToRationUnit: true//自动根据定额单位转换定额工程量
            }
        };
        try{
            resJson.data = await optionsDao.getOptions(user_id, compilation_id);
            if(!resJson.data){
                resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optionsTypes.GENERALOPTS, defaultOpts.GENERALOPTS);
            }
        }
        catch (err){
            resJson.error = true;
            resJson.message = '获取失败';
            resJson.data = null;
        }
        res.json(resJson);
    }
    //获得特定选项类型的选项
    async getOptionsByType(req, res){
        let resJson = {error: null, message: '', data: null};
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id,
            optsType = req.body.optsType;
        try{
            let hasThisOpts = false;
            for(let i in optionsTypes){
                if(optionsTypes[i] === optsType){
                    hasThisOpts = true;
                    break;
                }
            }
            if(!hasThisOpts) throw '不存在此选项类';
            resJson.data = await optionsDao.getOptionsByType(user_id, compilation_id, optsType);
        }
        catch (err){
            resJson.error = true;
            resJson.message = '获取失败';
            resJson.data = null;
        }
        res.json(resJson);
    }

    async saveOptions(req, res){
        let resJson = {error: 0, message: '', data: null};
        let data = JSON.parse(req.body.data);
        let user_id = req.session.sessionUser.id,
            compilation_id = req.session.sessionCompilation._id,
            optsType = data.optsType,
            opts = data.opts;
        try{
            let hasThisOpts = false;
            for(let i in optionsTypes){
                if(optionsTypes[i] == optsType){
                    hasThisOpts = true;
                    break;
                }
            }
            if(!hasThisOpts) throw '不存在此选项类';
            resJson.data = await optionsDao.saveOptions(user_id, compilation_id, optsType, opts);
        }
        catch (err){
            resJson.error = true;
            resJson.message = '保存失败';
            resJson.data = null;
        }
        res.json(resJson);
    }
}*/

export default OptionController;