'use strict';
const _ = require('lodash');
const colors = require('colors');
class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  async willReady() {
    // await this.initAdmin();
    // await this.initMenu();
  }

  async serverDidReady() {
    // 应用已经启动完毕
    const ctx = await this.app.createAnonymousContext();
    ctx.service.util.install.init();
    // await this.initAdmin(ctx);
    // await this.initMenu(ctx);
    // await this.initRole(ctx);
    // 检查种子
    // await ctx.service.install.index();
    // await ctx.service.util.rabbitMq.mission();
  }


  async initRole(ctx) {
    console.log('开始=>初始化角色'.blue);
    const data = await ctx.model.Role.findOne();
    if (!data) {
      // 没有管理员,初始化一个
      const data = {
        default: true,
        _tenant: 'master',
        name: '管理员角色',
      };
      console.log('正在初始化角色'.blue);
      await ctx.model.Role.create(data);
      console.log('初始化角色=>结束'.green);
    } else console.log('无需再次初始化角色'.yellow);
  }

  async initMenu(ctx) {
    console.log('开始=>初始化总管理员菜单'.blue);
    const data = await ctx.model.Menu.count();
    if (data <= 0) {
      console.log('正在初始化总管理员菜单'.blue);
      console.log(colors.bgBlue.italic('菜单=>初始化 首页'));
      // 首页初始化
      const indexPage = {
        name: '首页',
        icon: 'icon-shouye',
        order_num: 1,
        path: '/admin/homeIndex',
        status: '0',
        type: '1',
        no_delete: true,
        _tenant: 'master',
      };
      await ctx.model.Menu.create(indexPage);
      console.log(colors.bgGreen.italic('菜单=>初始化 首页'));

      console.log(colors.bgBlue.italic('菜单=>初始化 系统菜单'));
      // 系统菜单初始化
      const sys = {
        name: '系统设置',
        icon: 'icon-shouye',
        order_num: 2,
        type: '0',
        status: '0',
        no_delete: true,
        _tenant: 'master',
      };
      const sysData = await ctx.model.Menu.create(sys);
      let sys_id = _.get(sysData, '_id');
      if (sys_id) sys_id = JSON.parse(JSON.stringify(sys_id));
      console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 菜单管理'));
      // 系统-菜单管理
      const sys_menu = {
        name: '菜单设置',
        icon: 'icon-shouye',
        order_num: 1,
        parent_id: sys_id,
        path: '/admin/menu',
        status: '0',
        type: '1',
        no_delete: true,
        _tenant: 'master',
      };
      await ctx.model.Menu.create(sys_menu);
      console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 菜单管理'));
      console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 角色管理'));
      // 系统-角色
      const sys_role = {
        name: '角色管理',
        icon: 'icon-shouye',
        order_num: 2,
        parent_id: sys_id,
        path: '/admin/role',
        status: '0',
        type: '1',
        no_delete: true,
        _tenant: 'master',
      };
      const sr = await ctx.model.Menu.create(sys_role);
      let role_id = _.get(sr, '_id');
      if (role_id) role_id = JSON.parse(JSON.stringify(role_id));
      const sys_role_child = {
        status: '0',
        icon: 'icon-shouye',
        name: '角色信息',
        parent_id: role_id,
        path: '/admin/role/detail',
        type: '2',
        config: {
          api: [],
          table_btn: [],
          btn_area: [],
        },
        no_delete: true,
        _tenant: 'master',
      };
      await ctx.model.Menu.create(sys_role_child);
      console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 角色管理'));

      console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 分站管理'));
      // 系统-分站
      const sys_tenant = {
        status: '0',
        icon: 'icon-shouye',
        no_delete: true,
        _tenant: 'master',
        page_type: 'list',
        name: '分站管理',
        parent_id: sys_id,
        order_num: 3,
        path: '/list/v1/tenant',
        type: '1',
        config: {
          api: [
            {
              is_use: true,
              module: 'tenant',
              func: 'query',
              opera: 'search',
              desc: '查询列表',
            },
            {
              is_use: true,
              module: 'tenant',
              func: 'delete',
              opera: 'delete',
              desc: '删除',
            },
          ],
          table_btn: [
            {
              confirm: false,
              label: '修改',
              type: 'primary',
              api: 'writeBySelf',
              selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service},query:{id:i._id}})',
            },
            {
              confirm: false,
              label: '删除',
              type: 'danger',
              api: 'delete',
            },
          ],
          btn_area: [
            {
              label: '添加',
              type: 'primary',
              api: 'writeBySelf',
              selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service}})',
              confirm: false,
            },
          ],
        },
      };
      const st = await ctx.model.Menu.create(sys_tenant);
      let tenant_id = _.get(st, '_id');
      if (tenant_id) tenant_id = JSON.parse(JSON.stringify(tenant_id));
      const sys_tenant_child = {
        status: '0',
        icon: 'icon-shouye',
        no_delete: true,
        _tenant: 'master',
        page_type: 'detail',
        name: '分站编辑',
        parent_id: tenant_id,
        path: '/detail/v1/tenant',
        type: '2',
        config: {
          api: [
            {
              is_use: true,
              module: 'tenant',
              func: 'fetch',
              opera: 'search',
              desc: '查询',
            },
            {
              is_use: true,
              module: 'tenant',
              func: 'create',
              opera: 'create',
              desc: '创建',
            },
            {
              is_use: true,
              module: 'tenant',
              func: 'update',
              opera: 'update',
              desc: '修改',
            },
          ],
          table_btn: [],
          btn_area: [],
        },
      };
      await ctx.model.Menu.create(sys_tenant_child);
      console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 分站管理'));
      console.log(colors.bgBlue.italic('菜单=>系统菜单=>初始化 管理员管理'));
      // 系统-管理员
      const sys_admin = {
        status: '0',
        icon: 'icon-shouye',
        no_delete: true,
        _tenant: 'master',
        name: '管理员用户',
        parent_id: sys_id,
        order_num: 4,
        path: '/list/v1/admin',
        type: '1',
        __v: 0,
        config: {
          api: [
            {
              is_use: true,
              module: 'admin',
              func: 'query',
              opera: 'search',
              desc: '查询列表',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'create',
              opera: 'create',
              desc: '创建',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'update',
              opera: 'update',
              desc: '修改',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'delete',
              opera: 'toDelete',
              desc: '删除',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'fetch',
              opera: 'toFetch',
              desc: '单项数据查询',
            },
          ],
          table_btn: [
            {
              confirm: false,
              label: '修改',
              type: 'primary',
              api: 'writeBySelf',
              methodZh: '',
              display: '',
              selfFunction: '(i)=>this.$router.push({name:"v1_detail", query:{id:i._id},params:{service:this.service}})',
            },
            {
              confirm: true,
              label: '删除',
              type: 'danger',
              api: 'toDelete',
              display: '(i)=>i.is_super !== true',
              methodZh: '确认是否删除该用户',
            },
            {
              confirm: false,
              label: '查看',
              type: 'primary',
              api: 'writeBySelf',
              selfFunction: '(i)=>this.$router.push({name:"v1_info" , query:{id:i._id},params:{service:this.service}})',
            },
          ],
          btn_area: [
            {
              label: '添加',
              type: 'primary',
              api: 'writeBySelf',
              selfFunction: '(i)=>this.$router.push({name:"v1_detail",params:{service:this.service}})',
              confirm: false,
            },
          ],
        },
        page_type: 'list',
      };
      const sa = await ctx.model.Menu.create(sys_admin);
      let admin_id = _.get(sa, '_id');
      if (admin_id) admin_id = JSON.parse(JSON.stringify(admin_id));
      const sys_admin_child = {
        status: '0',
        icon: 'icon-shouye',
        no_delete: true,
        _tenant: 'master',
        name: '管理员信息',
        parent_id: admin_id,
        path: '/detail/v1/admin',
        type: '2',
        config: {
          api: [
            {
              is_use: true,
              module: 'admin',
              func: 'fetch',
              opera: 'search',
              desc: '单数据查询',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'create',
              opera: 'create',
              desc: '创建',
            },
            {
              is_use: true,
              module: 'admin',
              func: 'update',
              opera: 'update',
              desc: '修改',
            },
          ],
          table_btn: [],
          btn_area: [],
        },
        page_type: 'detail',
      };
      await ctx.model.Menu.create(sys_admin_child);
      console.log(colors.bgGreen.italic('菜单=>系统菜单=>初始化 管理员管理'));
      console.log(colors.bgGreen.italic('菜单=>初始化 系统菜单'));

      console.log('初始化总管理员菜单结束'.green);
    } else console.log('无需初始化总管理员菜单'.yellow);
  }
}
module.exports = AppBootHook;