import Mock from 'mockjs';

//@XXX相当于在调用 Random.extend里面的方法
const user =  {
  name: 'LUIS',
  avatar: 'https://gw.alipayobjects.com/zos/rmsportal/cnrhVkzwxjPwAaCfPbdc.png',
  address: '长沙',
  position: 'Java工程师 | 蚂蚁金服-计算服务事业群-微信平台部',
};

Mock.mock(`${process.env.VUE_APP_API_BASE_URL}/login`, 'post', ({ body }) => {
  let result = { data: {} };
  const { name, password } = JSON.parse(body);
  let success = false;

  if (name === 'admin' && password === '888888') {
    success = true;
    //mock 页面功能操作权限  页面 -> 操作按钮 
    result.data.permissions = [
      {
        id: 'queryMenus', operation: ['add', 'edit']
      },
    ];
    result.data.roles = [
      {
        id: '1', code: 'systemAdmin', name: '系统管理员',
      },
      {
        id: '2', code: 'systemOperator', name: '系统运维',
      },
    ];
    result.data.routers = [
      {
        id: '1', code: 'systemAdmin', name: '系统管理员',
      },
      {
        id: '2', code: 'systemOperator', name: '系统运维',
      },
    ];
  } else if (name === 'test' && password === '888888') {
    success = true;
    result.data.permissions = [
      {
        id: 'queryMenus', operation: ['query']
      }
    ];
    result.data.roles = [
      {
        id: '3', code: 'systemTest', name: '系统测试员',
      },
    ];

    result.data.routers = [
      {
        router: 'root',
        children: [
          {
            router: 'dashboard',
            children: ['workbench', 'analysis'],
          },
          {
            router: 'workbench',
            name: '工作台',
          },
          {
            router: 'analysis',
            name: '分析页',
          },
          {
            router: 'system',
            children: ['menu', 'organization', 'user', 'role', 'parameter', 'log'],
          },
          {
            router: 'menu',
            name: '菜单管理',
          },
          {
            router: 'organization',
            name: '组织管理',
          },
          {
            router: 'user',
            name: '用户管理',
          },
          {
            router: 'role',
            name: '角色管理',
          },
          {
            router: 'parameter',
            name: '参数管理',
          },
          {
            router: 'log',
            name: '日志管理',
          }
        ]
      }];
  } else {
    success = false;
  }

  if (success) {
    result.code = 0;
    result.message = '中午好，欢迎回来';
    result.data.user = user;
    result.data.token = 'Authorization:' + Math.random();
    result.data.expireAt = new Date(new Date().getTime() + 30 * 60 * 1000);
  } else {
    result.code = -1;
    result.message = '账户名或密码错误';
  }
  return result;
})
