/**
 * 会员管理Mock数据
 * @author pangu
 */
import Mock from 'mockjs'

// 会员列表数据
const memberList = []
for (let i = 1; i <= 50; i++) {
  memberList.push({
    id: i,
    memberNo: Mock.mock('@natural(100000000000, 999999999999)').toString(),
    phone: Mock.mock(/^1[3-9]\d{9}$/),
    nickname: Mock.mock('@cname'),
    gender: Mock.mock('@pick(["0", "1", "2"])'),
    birthday: Mock.mock('@date("yyyy-MM-dd")'),
    identityType: Mock.mock('@pick(["1", "2"])'), // 1-家长 2-教师
    registerTime: Mock.mock('@datetime("yyyy-MM-dd HH:mm:ss")'),
    registerSource: Mock.mock('@pick(["APP", "H5", "PC"])'),
    status: Mock.mock('@pick(["0", "1"])'), // 0-正常 1-停用
    regionId: Mock.mock('@pick([111, 112, 113, 114])'),
    regionName: Mock.mock('@pick(["武昌区", "汉口区", "汉阳区", "江夏区"])'),
    schoolId: i,
    schoolName: Mock.mock('@ctitle(4, 8)') + '学校',
    gradeId: Mock.mock('@natural(1, 12)'),
    gradeName: Mock.mock('@pick(["一年级", "二年级", "三年级", "四年级", "五年级", "六年级"])'),
    classId: Mock.mock('@natural(1, 10)'),
    className: Mock.mock('@natural(1, 10)') + '班',
    // 绑定的学生
    students: Mock.mock({
      'list|0-3': [{
        'id|+1': 1,
        name: '@cname',
        studentNo: /^S\d{10}$/,
        schoolName: '@ctitle(4, 8)学校',
        gradeName: '@pick(["一年级", "二年级", "三年级"])',
        className: '@natural(1, 10)班'
      }]
    }).list
  })
}

// 区域树数据
const regionTree = [
  {
    id: 1,
    label: '湖北省',
    children: [
      {
        id: 11,
        label: '武汉市',
        children: [
          { id: 111, label: '武昌区' },
          { id: 112, label: '汉口区' },
          { id: 113, label: '汉阳区' },
          { id: 114, label: '江夏区' }
        ]
      },
      {
        id: 12,
        label: '黄冈市',
        children: [
          { id: 121, label: '黄州区' },
          { id: 122, label: '红安县' }
        ]
      }
    ]
  }
]

// 学校列表
const schoolList = [
  { id: 1, name: '武昌实验小学' },
  { id: 2, name: '武汉小学' },
  { id: 3, name: '华师附小' },
  { id: 4, name: '水果湖第一小学' },
  { id: 5, name: '武珞路小学' }
]

// 年级列表
const gradeList = [
  { id: 1, name: '一年级' },
  { id: 2, name: '二年级' },
  { id: 3, name: '三年级' },
  { id: 4, name: '四年级' },
  { id: 5, name: '五年级' },
  { id: 6, name: '六年级' }
]

// 班级列表
const classList = [
  { id: 1, name: '1班' },
  { id: 2, name: '2班' },
  { id: 3, name: '3班' },
  { id: 4, name: '4班' },
  { id: 5, name: '5班' }
]

// 查询会员列表
Mock.mock(/\/api\/member\/list/, 'get', (options) => {
  const params = new URLSearchParams(options.url.split('?')[1])
  const pageNum = parseInt(params.get('pageNum')) || 1
  const pageSize = parseInt(params.get('pageSize')) || 10
  const phone = params.get('phone')
  const nickname = params.get('nickname')
  const identityType = params.get('identityType')
  const status = params.get('status')

  let filteredList = [...memberList]

  // 手机号筛选
  if (phone) {
    filteredList = filteredList.filter(item => item.phone.includes(phone))
  }
  // 昵称筛选
  if (nickname) {
    filteredList = filteredList.filter(item => item.nickname.includes(nickname))
  }
  // 身份类型筛选
  if (identityType) {
    filteredList = filteredList.filter(item => item.identityType === identityType)
  }
  // 状态筛选
  if (status) {
    filteredList = filteredList.filter(item => item.status === status)
  }

  const start = (pageNum - 1) * pageSize
  const end = start + pageSize
  const rows = filteredList.slice(start, end)

  return {
    code: 200,
    msg: '查询成功',
    total: filteredList.length,
    rows
  }
})

// 获取会员详情
Mock.mock(/\/api\/member\/\d+$/, 'get', (options) => {
  const id = parseInt(options.url.match(/\/api\/member\/(\d+)$/)[1])
  const member = memberList.find(item => item.id === id)
  return {
    code: 200,
    msg: '查询成功',
    data: member || null
  }
})

// 新增会员
Mock.mock('/api/member', 'post', () => {
  return {
    code: 200,
    msg: '新增成功'
  }
})

// 修改会员
Mock.mock('/api/member', 'put', () => {
  return {
    code: 200,
    msg: '修改成功'
  }
})

// 删除会员
Mock.mock(/\/api\/member\/\d+$/, 'delete', () => {
  return {
    code: 200,
    msg: '删除成功'
  }
})

// 重置密码
Mock.mock(/\/api\/member\/resetPwd\/\d+$/, 'put', () => {
  return {
    code: 200,
    msg: '重置成功',
    data: {
      newPassword: Mock.mock(/^[A-Za-z0-9]{8}$/)
    }
  }
})

// 获取区域树
Mock.mock(/\/api\/region\/tree/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: regionTree
  }
})

// 获取学校列表
Mock.mock(/\/api\/school\/list/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: schoolList
  }
})

// 获取年级列表
Mock.mock(/\/api\/grade\/list/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: gradeList
  }
})

// 获取班级列表
Mock.mock(/\/api\/class\/list/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: classList
  }
})
