/**
 * Created by zhang on 2018/7/14.
 */

/**
 * 列设置控制器
 *
 */
import BaseController from "../../common/base/base_controller";
import mainColFacade from "../facade/main_col_facade";
let config = require("../../../config/config.js");

class MainColController extends BaseController {

    async main(request, response) {
        let mainColLibs = await mainColFacade.getAllLibs();
        let randerData = {
            title:'列设置',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            mainColLibs:mainColLibs,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
            layout: 'maintain/common/html/layout'
        };

        response.render("maintain/main_col_lib/html/main", randerData);
    }

    async addLib(request, response){
        try {
            await mainColFacade.addLib(request.body);
        }catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }

    async deleteLibByID(request,response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await mainColFacade.deleteLibByID(data.ID);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }

    async getLibByID(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await mainColFacade.getLibByID(data.libID);
            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 mainColFacade.saveLib(data);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }

    async edit(request,response){
        let result = {
            error:0
        }
        //先取出替换库信息：
        let libID = request.params.libID;
        let colLib = await mainColFacade.getLibByID(libID);
        if(colLib){
            let randerData = {
                title:'列设置库',
                mainURL:'/mainTreeCol/main',
                libName:colLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                main_tree_col:JSON.stringify(colLib.main_tree_col),
                libID:libID,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/main_col_lib/html/edit", randerData);
        }else {
            response.redirect(request.headers.referer);
        }

    }

    /*async updateBillsTemplateItem(request, response) {
        let libID = request.params.libID;
        let result = {error: 1, message: '更新数据错误', data: null};
        try {
            let data = JSON.parse(request.body.data);
            if(libID){
                result = await await billsTemplateFacade.updateTemplateItem(libID,data);
                if (result) {
                    result = {error: 0, message: '', data: data};
                }
            }

        }catch (err){
            console.log(err);
        }
        response.json(result);
    }

    async editTemplate(request, response){

        //先取出清单库信息：
        let libID = request.params.libID;
        let templateLib = await billsTemplateFacade.getLibByID(libID);
        if(templateLib){

            let templateDatas = await billsTemplateFacade.getTemplateDatasByLibID(libID);

            let randerData = {
                title:'清单模板',
                mainURL:'/billsTemplate/main',
                libName:templateLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                billsTemplateData:JSON.stringify(templateDatas),
                billsFixedFlagList: JSON.stringify(BillsFixedFlagList),
                billsTypeFlagList: JSON.stringify(BillsTypeFlagList),
                libID:libID,
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/bill_template_lib/html/edit", randerData);
        }else {
            response.redirect(request.headers.referer);
        }
    }






   */
}

export default MainColController;