'use strict';

/**
 * 计量相关控制器
 *
 * @author olym
 * @date 2018/2/11
 * @version
 */

module.exports = app => {

    class MeasureController extends app.BaseController {

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

        /**
         * 中间计量页面
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async middle(ctx) {
            const renderData = {};
            await this.layout('measure/middle.ejs', renderData);
        }

        /**
         * 期计量页面
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async stage(ctx) {
            if (ctx.request.query.tenderId) {
                const tenderId = parseInt(ctx.request.query.tenderId);
                const tender = await ctx.service.tender.getDataById(tenderId);
                ctx.session.sessionUser.tenderId = tender.id;
                ctx.session.sessionUser.tenderName = tender.name;

                ctx.redirect('/measure/stage');
            } else {
                let tenderId = ctx.session.sessionUser.tenderId;
                const tenderInfo = await ctx.service.tender.getDataById(tenderId);
                const tenderList = await ctx.service.tender.getList();
                for (const tender of tenderList) {
                    tender.url = ctx.menuList.measure.children.stage.url + '?tenderId=' + tender.id;
                }
                const renderData = {
                    tenderInfo,
                    tenderList
                };

                await this.layout('measure/stage.ejs', renderData, 'measure/stage-modal.ejs');
            }

        }

    }

    return MeasureController;
};
