'use strict';

/**
 *
 *
 * @author Mai
 * @date
 * @version
 */

const libConst = require('../const/lib');
module.exports = app => {
    class UnitCompareController extends app.BaseController {
        /**
         * 指标对比页面
         *
         * @param {object} ctx - egg全局context
         * @return {void}
         */
        async index (ctx) {
            try {
                const libList = await ctx.service.quotaLib.getList(libConst.status.enter);
                const dyNames = await ctx.service.templateNode.getDanYuanNames();
                const xMatchDyNames = await ctx.service.templateNode.getXMatchDanYuanNames();
                for (const dn of dyNames) {
                    dn.indexClass = this.app.nodeConst.indexClass.dy;
                    const x = xMatchDyNames.find(function (a) {
                        return a.name === dn.name;
                    });
                    dn.xMatch = false || (x !== null && x !== undefined);
                }
                const renderData = {
                    libList,
                    nodeConst: this.app.nodeConst,
                    dyNames,
                };
                await this.layout('unit_compare/index.ejs', renderData, 'unit_compare/modal.ejs');
            } catch(err) {
                console.log(err);
            }
        }

        async getParent(ctx) {
            try {
                const data = JSON.parse(ctx.request.body.data);

                if (!data.name) throw '选择单元指标错误';
                if (!data.xMatch) throw '该单元指标无可对比项，请刷新页面重新';
                if (!data.lib_id && data.lib_id < 0) throw '选择造价文件错误';

                const parent = await this.ctx.service.tenderNode.searchParent(data.lib_id, data.name);

                ctx.body = {err: 0, msg: '', data: parent}
            } catch (err) {
                console.log(err);
                if (err.stack) {
                    ctx.body = {err: 1, msg: '获取对比项失败，请重试', data: []};
                } else {
                    ctx.body = {err: 1, msg: err, data: []};
                }
            }
        }

        async search(ctx) {
            try {
                const data = JSON.parse(ctx.request.body.data);
                console.log(data);

                if (!data.lib_id) {
                    throw '查询造价文件数据错误';
                }
                if (!data.name) {
                    throw '查询的指标分类错误';
                }
                const condition = {lib_id: data.lib_id, class_name: data.name};
                if (data.xMatch) {
                    if (!data.selects || data.selects.length === 0) {
                        throw '查询的对比项错误';
                    }
                    condition.bills_xid = [];
                    for (const s of data.selects) {
                        condition.bills_xid.push(s.id);
                    }
                }

                const nodes = await ctx.service.tenderNode.getAllDataByCondition({ where: condition });
                for (const n of nodes) {
                    const condition = {
                        lib_id: n.lib_id,
                        node_id: n.node_id,
                    };
                    n.children = await ctx.service.tenderIndex.getAllDataByCondition({where: condition});
                }
                ctx.body = {err: 0, msg: '', data: nodes};
            } catch (err) {
                ctx.body = {err: 1, msg: err.toString(), data: []};
                console.log(err);
            }
        }
    }

    return UnitCompareController;
};