import Mock from 'mockjs'
import { resultSuccess } from '../_util'
import { asyncRoutes } from './router.js'

const routes = deepClone([...asyncRoutes])

function deepClone(source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

const menuList = []
const userList = []
// const deptList = []
const count = 100

for (let i = 0; i < count; i++) {
  menuList.push(Mock.mock({
    title: '@name',
    'type|1': ['0', '1'],
    'method|1': ['put', 'post'],
    path: '@name',
    component: '@name',
    permission: '@name',
    'status|1': ['1', '2'],
    sort: '@natural',
    'hide|1': ['0', '1'],
    createTime: '@datetime'
  }))
  userList.push(Mock.mock({
    code: '',
    avatar: '@image',
    username: '@name',
    realname: '@cname',
    roles: '',
    type: '',
    status: '',
    deptName: '',
    createTime: '@datetime',
    updateTime: '@datetime'
  }))
  // deptList.push(Mock.mock({
  //   code: '',
  //   id: 1,
  //   name: '@name',
  //   fullname: '@cname',
  //   'pid|1': [1, 2, 3, 4, 5],
  //   'sort|+1': 0,
  //   type: 1,
  //   'note|1-8': '好',
  //   createTime: '@datetime',
  //   updateTime: '@datetime'
  // }))
}
const deptList = Mock.mock({
  code: 0,
  message: '获取成功',
  type: 'success',
  data: [{
    code: 'XH212313',
    id: 1,
    name: '@name',
    fullname: '@cname',
    pid: 0,
    sort: 0,
    type: 1,
    'note|1-8': '好',
    createTime: '@datetime',
    updateTime: '@datetime'
  },
  {
    code: 'XH212312',
    id: 2,
    name: '@name',
    fullname: '@cname',
    pid: 1,
    sort: 0,
    type: 2,
    'note|1-8': '好',
    createTime: '@datetime',
    updateTime: '@datetime'
  },
  {
    code: 'XH212323',
    id: 3,
    name: '@name',
    fullname: '@cname',
    pid: 2,
    sort: 0,
    type: 2,
    'note|1-8': '好',
    createTime: '@datetime',
    updateTime: '@datetime'
  },
  {
    code: 'XH212319',
    id: 4,
    name: '@name',
    fullname: '@cname',
    pid: 0,
    sort: 1,
    type: 1,
    'note|1-8': '好',
    createTime: '@datetime',
    updateTime: '@datetime'
  },
  {
    code: 'XH212398',
    id: 5,
    name: '@name',
    fullname: '@cname',
    pid: 0,
    sort: 2,
    type: 2,
    'note|1-8': '好',
    createTime: '@datetime',
    updateTime: '@datetime'
  }]
})

export default [
  {
    url: '/api-mock/index/getMenuList',
    timeout: 1000,
    method: 'get',
    response: () => {
      return resultSuccess(routes)
    }
  },
  {
    url: '/api-mock/menu/index',
    timeout: 1000,
    method: 'get',
    response: config => {
      const { page = 1, limit = 10 } = config.query
      const mockList = menuList.filter(item => {
        return true
      })
      const List = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return resultSuccess(List)
    }
  },
  {
    url: '/api-mock/user/apiIndex',
    timeout: 1000,
    method: 'get',
    response: config => {
      const { page = 1, limit = 10 } = config.query
      const mockList = userList.filter(item => {
        return true
      })
      const List = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      const data = {
        list: List,
        page: Number(page),
        limit: Number(limit),
        total: count
      }
      return resultSuccess(data)
    }
  },
  {
    url: '/api-mock/dept/apiIndex',
    timeout: 1000,
    method: 'get',
    response: config => {
      const { page = 1, limit = 10 } = config.query
      const data = {
        list: deptList,
        page: Number(page),
        limit: Number(limit),
        total: 5
      }
      return resultSuccess(data)
    }
  }
]
