/**
 * Created by zhang on 2018/8/22.
 */
import BaseController from "../../common/base/base_controller";
import materialFacade from "../facade/material_replace_facade";
let config = require("../../../config/config.js");
let logger = require('../../../logs/log_helper').logger;

class ReplaceController extends BaseController{
    /**
     * 材料替换库页面
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async main(request, response) {
        let materialLibs = await materialFacade.findByCondition({},null,false);
        let randerData = {
            title:'材料替换库',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            materialLibs:materialLibs,
            layout: 'maintain/common/html/layout'
        };
        response.render("maintain/material_replace_lib/html/main", randerData);
    }
    async edit(request,response){
        //先取出替换库信息：
        let libID = request.params.libID;
        let materialLib = await materialFacade.findByCondition({'ID':libID});
        if(materialLib){
            let billsLibId = materialLib.billsLibId;
            let compilationId = materialLib.compilationId;
            let gljLib = await  materialFacade.findGLJLibByComID(compilationId);
            let billsList = await materialFacade.findBillsByLibID(libID);
            //let templateDatas = await materialFacade.getTemplateDatasByLibID(libID);
            //let stdBills = await  materialFacade.getStdBillsByLib(billsLibId);

            let randerData = {
                title:'材料替换库',
                mainURL:'/materialReplace/main',
                libName:materialLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                billsList:JSON.stringify(billsList),
                billsLibId:billsLibId,
                gljLibID:gljLib.ID,
                libID:libID,
                //stdBills:JSON.stringify(stdBills),
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/material_replace_lib/html/edit", randerData);
        }else {
            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={};
            if(data.compilationID) conditions.compilationId = data.compilationID;
            if(data.billLibID) conditions.billsLibId = data.billLibID;
            if(data.ID) conditions.ID = data.ID;
            let resultData = await materialFacade.findByCondition(conditions);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async addLib(request, response){
        try {
            await materialFacade.addLib(request.body);
        }catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }
    async saveLib(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await materialFacade.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 materialLib ${request.ip}`);
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await materialFacade.deleteLibByID(data.ID);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async saveBills(request,response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await materialFacade.saveBills(data);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async saveMaterial(request,response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await materialFacade.saveMaterial(data);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async findMaterial (request,response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData = await materialFacade.getMaterialByBillsID(data.billsItemID);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
}

export default ReplaceController;