/**
 * Created by zhang on 2018/9/3.
 */
import BaseController from "../../common/base/base_controller";
import basicInfoFacade from "../facade/basic_info_facade";
let config = require("../../../config/config.js");
let logger = require('../../../logs/log_helper').logger;

class BasicInfoController extends BaseController{
    async main(request, response) {
        let baiscInfoLibs = await basicInfoFacade.findByCondition({},{feature:0},false);
        let randerData = {
            title:'基本信息库',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            basicInfoLibs:baiscInfoLibs,
            layout: 'maintain/common/html/layout'
        };
        response.render("maintain/basic_info_lib/html/main", randerData);
    }
    async addLib(request, response){
        try {
            await basicInfoFacade.addLib(request.body);
        }catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }
    async findLib(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let conditions={'ID' : data.ID};
            let resultData = await basicInfoFacade.findByCondition(conditions);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async saveLib(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await basicInfoFacade.saveLib(data);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async deleteLibByID(request,response){
        logger.info(`delete basicInfoLib ${request.ip}`);
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await basicInfoFacade.deleteLibByID(data.ID);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async edit(request,response){
        //先取出替换库信息：
        let libID = request.params.libID;
        let basicInfoLib = await basicInfoFacade.findByCondition({'ID':libID});
        if(basicInfoLib){
            let randerData = {
                title:'基本信息库',
                mainURL:'/basicInfo/main',
                libName:basicInfoLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                basicInfoList:JSON.stringify(basicInfoLib.info),
                libID:libID,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/basic_info_lib/html/edit", randerData);
        }else {
            response.redirect(request.headers.referer);
        }
    }
}
export default BasicInfoController;