/**
 * 学科管理Mock数据
 * @author pangu
 */
import Mock from 'mockjs'

// 学科预置数据
const subjectData = [
  { id: 1, name: '语文', code: 'S01', sort: 1, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 2, name: '数学', code: 'S02', sort: 2, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 3, name: '英语', code: 'S03', sort: 3, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 4, name: '物理', code: 'S04', sort: 4, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 5, name: '化学', code: 'S05', sort: 5, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 6, name: '生物', code: 'S06', sort: 6, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 7, name: '历史', code: 'S07', sort: 7, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 8, name: '地理', code: 'S08', sort: 8, status: '0', createTime: '2026-01-01 10:00:00' },
  { id: 9, name: '政治', code: 'S09', sort: 9, status: '0', createTime: '2026-01-01 10:00:00' }
]

// 获取学科列表
Mock.mock(/\/api\/subject\/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 = subjectData.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\/subject\/\d+/, 'get', (options) => {
  const id = parseInt(options.url.match(/\/api\/subject\/(\d+)/)[1])
  const item = subjectData.find(s => s.id === id)
  return {
    code: 200,
    msg: '查询成功',
    data: item || null
  }
})

// 新增学科
Mock.mock('/api/subject', 'post', () => {
  return { code: 200, msg: '新增成功' }
})

// 修改学科
Mock.mock('/api/subject', 'put', () => {
  return { code: 200, msg: '修改成功' }
})

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