'use strict';

/**
 *
 *
 * @author Zhong
 * @date 2018/5/29
 * @version
 */
import BaseController from '../../common/base/base_controller';
import moment from 'moment';
const billsGuidanceFacade = require('../facade/facades');
let callback = function (req, res, err, msg, data) {
    res.json({error: err, message: msg, data: data});
};

class BillsGuideLibController extends BaseController{
    //获取编办及编办清单库信息
    async getComBillsLibInfo(req, res){
        try{
            let comBillsLibInfo = await billsGuidanceFacade.getComBillsLibInfo();
            callback(req, res, 0, '', comBillsLibInfo);
        }
        catch(err) {
            callback(req, res, 1, err, null);
        }
    }

    async getBillsGuideLibs(req, res){
        try{
            let libs = await billsGuidanceFacade.getBillsGuideLibs({deleted: false});
            callback(req, res, 0, '', libs);
        }
        catch(err){
            callback(req, res, 1, '获取清单指引库数据错误', null);
        }
    }

    async updateBillsGuideLib(req, res){
        try{
            let data = JSON.parse(req.body.data);
            if(data.updateType === 'create'){
                data.updateData.createDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
                data.updateData.creator = req.session.managerData.username;
                await billsGuidanceFacade.initBillsGuideLib(data.updateData);
            }
            else{
                await billsGuidanceFacade.updateBillsGuideLib(data);
            }
            callback(req, res, 0, '', data.updateData);
        }
        catch(err){
            callback(req, res, 1, '更新失败', null);
        }
    }
    //获取清单指引库和该库引用的清单
    async getLibWithBills(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let rst = await billsGuidanceFacade.getLibWithBills(data.libID);
            callback(req, res, 0, '', rst);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }

    async getItemsByBills(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let items = await billsGuidanceFacade.getItemsBybills(data.guidanceLibID, data.billsID);
            callback(req, res, 0, '', items);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }

    async updateItems(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let updateDatas = data.updateDatas;
            await billsGuidanceFacade.updateItems(updateDatas);
            callback(req, res, 0, '', null);
        }
        catch(err){
            callback(req, res, 1, err, null);
        }
    }

    async testItems(req, res){
        try{
            let data = JSON.parse(req.body.data);
            let rst = await billsGuidanceFacade.testItems(data.libID);
            res.json({error: 0, data: rst, message: ''});
        }
        catch (error){
            res.json({error: 1, data: null, message: error});

        }
    }
}

export default BillsGuideLibController;