/**
 * 应用管理Mock数据
 * @author pangu
 */
import Mock from 'mockjs'

// 接口授权选项
const apiAuthOptions = [
  { value: 'queryStudent', label: '查询学生信息' },
  { value: 'querySchool', label: '查询学校信息' },
  { value: 'queryGrade', label: '查询年级信息' },
  { value: 'queryClass', label: '查询班级信息' },
  { value: 'queryMember', label: '查询会员信息' },
  { value: 'queryRegion', label: '查询区域树' }
]

// 生成Mock应用数据
const generateAppList = () => {
  const list = []
  const appNames = ['智慧校园', '在线课堂', '家校通', '图书馆管理', '教务系统', '成绩查询', '素质评价', '校园安全']

  for (let i = 1; i <= 20; i++) {
    const randomApis = Mock.Random.shuffle(apiAuthOptions.map(o => o.value)).slice(0, Mock.Random.integer(1, 6))
    list.push({
      id: i,
      appName: appNames[(i - 1) % appNames.length] + (i > 8 ? Math.ceil(i / 8) : ''),
      appCode: 'APP' + Mock.Random.string('upper', 6),
      appSecret: Mock.Random.guid(),
      description: Mock.Random.cparagraph(1, 2),
      contactName: Mock.Random.cname(),
      contactPhone: Mock.mock(/1[3-9]\d{9}/),
      apiAuth: randomApis,
      status: Mock.Random.pick(['0', '1']),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
      createBy: Mock.Random.pick(['admin', '张三', '李四'])
    })
  }
  return list
}

let applicationList = generateAppList()

// 获取应用列表
Mock.mock(/\/api\/application\/list/, 'get', (options) => {
  const url = new URL('http://localhost' + options.url)
  const appName = url.searchParams.get('appName') || ''
  const appCode = url.searchParams.get('appCode') || ''
  const status = url.searchParams.get('status') || ''
  const pageNum = parseInt(url.searchParams.get('pageNum')) || 1
  const pageSize = parseInt(url.searchParams.get('pageSize')) || 10

  let filtered = applicationList.filter(item => {
    let match = true
    if (appName) match = match && item.appName.includes(appName)
    if (appCode) match = match && item.appCode.includes(appCode)
    if (status) match = match && item.status === status
    return match
  })

  const total = filtered.length
  const start = (pageNum - 1) * pageSize
  const rows = filtered.slice(start, start + pageSize)

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

// 获取应用详情
Mock.mock(/\/api\/application\/\d+$/, 'get', (options) => {
  const id = parseInt(options.url.match(/\/api\/application\/(\d+)/)[1])
  const app = applicationList.find(item => item.id === id)

  if (app) {
    return { code: 200, msg: '查询成功', data: app }
  }
  return { code: 500, msg: '应用不存在' }
})

// 新增应用
Mock.mock('/api/application', 'post', (options) => {
  const data = JSON.parse(options.body)
  const newApp = {
    id: applicationList.length + 1,
    ...data,
    appCode: 'APP' + Mock.Random.string('upper', 6),
    appSecret: Mock.Random.guid(),
    createTime: Mock.Random.now('yyyy-MM-dd HH:mm:ss'),
    createBy: 'admin'
  }
  applicationList.unshift(newApp)
  return { code: 200, msg: '新增成功' }
})

// 修改应用
Mock.mock('/api/application', 'put', (options) => {
  const data = JSON.parse(options.body)
  const index = applicationList.findIndex(item => item.id === data.id)

  if (index !== -1) {
    applicationList[index] = { ...applicationList[index], ...data }
    return { code: 200, msg: '修改成功' }
  }
  return { code: 500, msg: '应用不存在' }
})

// 删除应用
Mock.mock(/\/api\/application\/\d+$/, 'delete', (options) => {
  const id = parseInt(options.url.match(/\/api\/application\/(\d+)/)[1])
  const index = applicationList.findIndex(item => item.id === id)

  if (index !== -1) {
    applicationList.splice(index, 1)
    return { code: 200, msg: '删除成功' }
  }
  return { code: 500, msg: '应用不存在' }
})

// 重置应用密钥
Mock.mock(/\/api\/application\/\d+\/resetSecret/, 'post', (options) => {
  const id = parseInt(options.url.match(/\/api\/application\/(\d+)\/resetSecret/)[1])
  const app = applicationList.find(item => item.id === id)

  if (app) {
    const newSecret = Mock.Random.guid()
    app.appSecret = newSecret
    return { code: 200, msg: '重置成功', data: { appSecret: newSecret } }
  }
  return { code: 500, msg: '应用不存在' }
})

// 获取接口授权选项
Mock.mock('/api/application/authOptions', 'get', () => {
  return { code: 200, msg: '查询成功', data: apiAuthOptions }
})
