/**
 * 班级管理Mock数据
 * @author pangu
 */
import Mock from 'mockjs'

// 班级预置数据
const classData = [
  { id: 1, name: '1班', code: 'C01', sort: 1, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 2, name: '2班', code: 'C02', sort: 2, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 3, name: '3班', code: 'C03', sort: 3, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 4, name: '4班', code: 'C04', sort: 4, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 5, name: '5班', code: 'C05', sort: 5, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 6, name: '6班', code: 'C06', sort: 6, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 7, name: '7班', code: 'C07', sort: 7, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 8, name: '8班', code: 'C08', sort: 8, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 9, name: '9班', code: 'C09', sort: 9, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 10, name: '10班', code: 'C10', sort: 10, status: '0', createTime: '2026-01-01 10:00:00' }
]

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

  let list = classData.filter(item => {
    let match = true
    if (name) {
      match = match && item.name.includes(name)
    }
    if (status !== null && status !== '') {
      match = match && item.status === status
    }
    return match
  })

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

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

// 获取班级选项
Mock.mock(/\/api\/class\/options/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: classData.filter(item => item.status === '0').map(item => ({
      id: item.id,
      name: item.name,
      code: item.code
    }))
  }
})

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

// 新增班级
Mock.mock('/api/class', 'post', () => {
  return { code: 200, msg: '新增成功' }
})

// 修改班级
Mock.mock('/api/class', 'put', () => {
  return { code: 200, msg: '修改成功' }
})

// 删除班级
Mock.mock(/\/api\/class\/\d+/, 'delete', () => {
  return { code: 200, msg: '删除成功' }
})
