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

// 年级预置数据
const gradeData = [
  { id: 1, name: '一年级', code: 'G01', sort: 1, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 2, name: '二年级', code: 'G02', sort: 2, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 3, name: '三年级', code: 'G03', sort: 3, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 4, name: '四年级', code: 'G04', sort: 4, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 5, name: '五年级', code: 'G05', sort: 5, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 6, name: '六年级', code: 'G06', sort: 6, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 7, name: '七年级', code: 'G07', sort: 7, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 8, name: '八年级', code: 'G08', sort: 8, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 9, name: '九年级', code: 'G09', sort: 9, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 10, name: '高一', code: 'G10', sort: 10, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 11, name: '高二', code: 'G11', sort: 11, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 12, name: '高三', code: 'G12', sort: 12, status: '0', createTime: '2026-01-01 10:00:00' }
]

// 获取年级列表
Mock.mock(/\/api\/grade\/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 = gradeData.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\/grade\/options/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: gradeData.filter(item => item.status === '0').map(item => ({
      id: item.id,
      name: item.name,
      code: item.code
    }))
  }
})

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

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

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

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