'use strict';

/**
 * 标段管理控制器
 *
 * @author CaiAoLin
 * @date 2018/2/5
 * @version
 */

const tenderConst = require('../const/tender');
module.exports = app => {

    class TenderController extends app.BaseController {

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

        /**
         * 标段列表页
         *
         * @param {object} ctx - egg全局变量
         * @return {void}
         */
        async index(ctx) {
            // 获取新增标段的规则
            const rule = ctx.service.tender.rule('add');
            const jsValidator = await this.jsValidator.convert(rule).build();

            let tenderType = ctx.query.type;

            // 根据项目id获取标段数据
            const tenderList = await ctx.service.tender.getList(tenderType);
            const renderData = {
                tenderList,
                tenderConst,
                jsValidator,
                tenderType
            };
            await this.layout('tender/index.ejs', renderData, 'tender/modal.ejs');
        }

        /**
         * 添加标段操作
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async add(ctx) {
            try {
                const rule = ctx.service.tender.rule('add');
                ctx.helper.validate(rule);

                const result = ctx.service.tender.add(ctx.request.body);
                if (!result) {
                    throw '新增标段失败';
                }
            } catch (error) {
                console.log(error);
                this.setMessage(error.toString(), this.messageType.ERROR);
            }

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

        /**
         * 切换标段 --》 暂时废弃，不存在此功能
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async switchTender(ctx) {
            let tenderId = ctx.params.tenderId;
            tenderId = parseInt(tenderId);
            try{
                if(isNaN(tenderId) || tenderId <= 0) {
                    throw '参数错误';
                }
                const result = await ctx.service.tender.switchTender(tenderId);
                if (!result) {
                    throw '切换标段失败!';
                }
            } catch (error) {
                this.setMessage(error.toString(), this.messageType.ERROR);
            }
            ctx.redirect(ctx.request.headers.referer);
        }

        /**
         * 保存标段操作
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        async save(ctx) {
            let id = ctx.request.body.tenderId;
            id = parseInt(id);

            try {
                if (isNaN(id) || id < 0) {
                    throw '参数错误';
                }
                // 获取数据规则
                const rule = ctx.service.tender.rule('save');
                ctx.validate(rule);

                const result = await ctx.service.tender.save(ctx.request.body, 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 delete(ctx) {
            let id = ctx.request.body.tenderId;
            id = parseInt(id);

            try {
                if (isNaN(id) || id <= 0) {
                    throw '参数错误';
                }
                const result = ctx.service.tender.deleteTenderById(id);

                if (!result) {
                    throw '删除标段失败';
                }

                this.setMessage('删除标段成功', this.messageType.SUCCESS);
            } catch (error) {
                this.setMessage(error.toString(), this.messageType.ERROR);
            }

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

    return TenderController;
};
