'use strict';

/**
 * 项目相关控制器
 *
 * @author CaiAoLin
 * @date 2017/11/29
 * @version
 */

const officeList = require('../const/cld_office').list;
module.exports = app => {

    class ProjectController extends app.BaseController {

        /**
         * 构造函数
         *
         * @param {Object} ctx - egg全局context
         * @return {void}
         */
        constructor(ctx) {
            super(ctx);
            ctx.showProject = true;
        }

        /**
         * 项目信息页面
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async info(ctx) {
            try {
                // 获取项目数据
                const projectId = ctx.session.sessionProject.id;
                const projectData = await ctx.service.project.getDataById(projectId);
                if (projectData === null) {
                    throw '没有对应的项目数据';
                }

                // 获取销售人员数据
                const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);

                // 数据规则
                const rule = ctx.service.project.rule('saveInfo');
                const jsValidator = await this.jsValidator.convert(rule).build();

                const renderData = {
                    projectData,
                    salesmanData,
                    officeList,
                    jsValidator,
                };
                await this.layout('project/info.ejs', renderData);
            } catch (error) {
                this.setMessage(error.toString(), this.messageType.ERROR);
                ctx.redirect('/dashboard');
            }
        }

        /**
         * 保存项目信息操作
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async saveInfo(ctx) {
            try {
                // 获取项目数据
                const projectId = ctx.session.sessionUser.projectId;
                const projectData = await ctx.service.project.getDataById(projectId);
                if (projectData === null) {
                    throw '没有对应的项目数据';
                }

                const rule = ctx.service.project.rule('saveInfo');
                ctx.validate(rule);

                const updateData = {
                    name: ctx.request.body.name,
                };
                const result = await ctx.service.project.update(updateData, { id: projectData.id });
                if (!result) {
                    throw '保存项目信息失败';
                }

                this.setMessage('保存成功', this.messageType.SUCCESS);
            } catch (error) {
                this.setMessage(error.toString(), this.messageType.ERROR);
            }

            ctx.redirect(ctx.request.headers.referer);
        }

        /**
         * 切换项目
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async switchProject(ctx) {
            let projectId = ctx.params.projectId;
            try {
                projectId = parseInt(projectId);
                if (isNaN(projectId) || projectId <= 0) {
                    throw '参数错误!';
                }
                const result = await ctx.service.project.switchProject(projectId);
                if (!result) {
                    throw '切换项目失败!';
                }
            } catch (error) {
                this.setMessage(error.toString(), this.messageType.ERROR);
            }
            ctx.redirect(ctx.request.headers.referer);
        }

        /**
         * 查询项目的参与人
         *
         * @param ctx
         * @returns {Promise<void>}
         */
        async searchAccount(ctx) {
            const projectId = ctx.session.sessionProject.id;
            const responseData = {
                err: 0,
                msg: '',
                data: [],
            };
            try {
                if (!projectId) {
                    throw '当前未打开项目';
                }
                const data = JSON.parse(ctx.request.body.data);
                const name = data.keyword;
                if (!name || name === '') {
                    throw '请输入姓名进行检索';
                }

                responseData.data = await ctx.service.projectAccount.getAccountInfoByName(projectId, name);
            } catch (err) {
                responseData.err = 1;
                responseData.msg = err.toString();
            }

            ctx.body = responseData;
        }
    }

    return ProjectController;
};
