'use strict';

/**
 * 控制面板
 *
 * @author CaiAoLin
 * @date 2017/11/23
 * @version
 */

const auditConst = require('../const/audit');

module.exports = app => {

    class DashboardController extends app.BaseController {

        /**
         * 控制面板页面
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async index(ctx) {
            const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);
            const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);
            const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);
            const projectAccountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
            const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
            const lastNotice = pa.last_notice ? pa.last_notice : (pa.last_notice === 0 ? new Date() : new Date(pa.last_login * 1000));
            const noticeLedger = await ctx.service.ledgerAudit.getNoticeTender(ctx.session.sessionProject.id, pa.id, lastNotice);
            const noticeStage = await ctx.service.stageAudit.getNoticeStage(ctx.session.sessionProject.id, pa.id, lastNotice);
            const noticeChange = await ctx.service.changeAudit.getNoticeChange(ctx.session.sessionProject.id, pa.id, lastNotice);
            const renderData = {
                auditTenders,
                auditStages,
                auditChanges,
                role: projectAccountInfo.role,
                acLedger: auditConst.ledger,
                acStage: auditConst.stage,
                acChange: auditConst.flow,
                noticeLedger,
                noticeStage,
                noticeChange,
            };
            await this.layout('dashboard/index.ejs', renderData);
            await ctx.service.projectAccount.defaultUpdate({
                id: this.ctx.session.sessionUser.accountId,
                last_notice: new Date(),
            });
        }
    }

    return DashboardController;
};
