'use strict';

/**
 * 控制器基类
 *
 * @author CaiAoLin
 * @date 2017/10/11
 * @version
 */
const moment = require('moment');
const messageType = require('../const/message_type');
const Controller = require('egg').Controller;
const menuList = require('../../config/menu').menu;
class BaseController extends Controller {

    /**
     * 构造函数
     *
     * @param {Object} ctx - egg全局context
     * @return {void}
     */
    constructor(ctx) {
        super(ctx);
        this.messageType = messageType;
        this.jsValidator = this.app.jsValidator;
        this.menu = this.app.menu;
        // 当前菜单
        ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
        ctx.title = ctx.menu === {} ? '' : ctx.menu.name;
        if (ctx.menu !== {} && ctx.menu.children !== null) {
            for (const index in ctx.menu.children) {
                if (index === ctx.actionName) {
                    ctx.title = ctx.menu.children[index].name;
                }
            }
        }
        // 菜单列表
        ctx.menuList = menuList;
        ctx.showProject = false;
        ctx.showTender = false;
        ctx.showTitle = false;
    }

    /**
     * 渲染layout
     *
     * @param {String} view - 渲染的view
     * @param {Object} data - 渲染的数据
     * @param {String} modal - 渲染的modal
     * @return {void}
     */
    async layout(view, data = {}, modal = '') {
        data.moment = moment;
        // 获取消息提示
        let message = this.ctx.session.message;
        // 取出后删除
        this.ctx.session.message = null;
        // 获取报错信息
        const postError = this.ctx.session.postError;
        // 取出后删除
        this.ctx.session.postError = null;
        if (message === null && postError !== null && postError !== undefined) {
            message = {
                type: 'error',
                icon: 'exclamation-circle',
                message: postError,
            };
        }

        try {
            data.min = this.app.config.min;
            const viewString = await this.ctx.renderView(view, data);
            const modalString = modal === '' ? '' : await this.ctx.renderView(modal, data);
            const renderData = {
                min: this.app.config.min,
                content: viewString,
                message: JSON.stringify(message),
                modal: modalString,
                dropDownMenu: data.dropDownMenu === undefined ? [] : data.dropDownMenu,
                breadCrumb: data.breadCrumb === undefined ? '' : data.breadCrumb,
                tenderList: data.tenderList === undefined ? [] : data.tenderList,
                jsFiles: data.jsFiles ? data.jsFiles : this.app.jsFiles.common,
            };
            await this.ctx.render('layout/layout.ejs', renderData);
        } catch(err) {
            this.log(err);
        }
    }

    /**
     * 设置提示
     *
     * @param {String} message - 提示信息
     * @param {String} type - 提示类型
     * @return {void}
     */
    setMessage(message, type) {
        let icon = '';
        switch (type) {
            case messageType.SUCCESS:
                icon = 'check';
                break;
            case messageType.ERROR:
                icon = 'exclamation-circle';
                break;
            case messageType.INFO:
                icon = 'info-circle';
                break;
            case messageType.WARNING:
                icon = 'warning';
                break;
            default:
                break;
        }
        this.ctx.session.message = {
            type,
            icon,
            message,
        };
    }

    log(error) {
        if (error.stack) {
            this.ctx.logger.error(error);
        } else {
            this.setMessage(error, messageType.ERROR);
            this.ctx.getLogger('fail').info(JSON.stringify({
                error: error,
                project: this.ctx.session.sessionProject,
                user: this.ctx.session.sessionUser,
                body: this.ctx.session.body,
            }));
        }
    }

    async checkMeasureType (mt) {
        if (this.ctx.tender.data.measure_type !== mt) {
            throw '该模式下不可提交此数据';
        }
    }

    ajaxErrorBody(error, msg) {
        if (error.stack) {
            return {err: 2, msg: msg, data: null};
        } else {
            return {err: 1, msg: error.toString(), data: null};
        }
    }
}

module.exports = BaseController;
