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

// 区域树数据
const regionTree = [
  {
    regionId: 1,
    regionName: '湖北省',
    children: [
      {
        regionId: 11,
        regionName: '武汉市',
        children: [
          { regionId: 111, regionName: '武昌区' },
          { regionId: 112, regionName: '汉口区' },
          { regionId: 113, regionName: '汉阳区' },
          { regionId: 114, regionName: '江夏区' },
          { regionId: 115, regionName: '洪山区' }
        ]
      },
      {
        regionId: 12,
        regionName: '黄冈市',
        children: [
          { regionId: 121, regionName: '黄州区' },
          { regionId: 122, regionName: '红安县' },
          { regionId: 123, regionName: '麻城市' }
        ]
      },
      {
        regionId: 13,
        regionName: '宜昌市',
        children: [
          { regionId: 131, regionName: '西陵区' },
          { regionId: 132, regionName: '伍家岗区' }
        ]
      }
    ]
  }
]

// 学校类型选项
const schoolTypes = ['小学', '初中', '高中', '九年一贯制', '完全中学']

// 生成学校列表Mock数据
const generateSchoolList = () => {
  const list = []
  const regions = [
    { id: 111, name: '湖北省/武汉市/武昌区' },
    { id: 112, name: '湖北省/武汉市/汉口区' },
    { id: 113, name: '湖北省/武汉市/汉阳区' },
    { id: 114, name: '湖北省/武汉市/江夏区' },
    { id: 121, name: '湖北省/黄冈市/黄州区' },
    { id: 122, name: '湖北省/黄冈市/红安县' }
  ]
  
  const schoolNames = [
    '武昌实验小学', '华师一附中', '武汉外国语学校', '水果湖第一小学',
    '武汉第二中学', '武汉六中', '江夏区第一中学', '武汉市第三中学',
    '红安县第一中学', '黄州中学', '武汉小学', '光谷第一小学',
    '华中科技大学附属小学', '武汉市育才小学', '武汉市常青第一学校'
  ]
  
  for (let i = 0; i < schoolNames.length; i++) {
    const region = regions[i % regions.length]
    list.push({
      id: i + 1,
      schoolName: schoolNames[i],
      schoolType: schoolTypes[i % schoolTypes.length],
      regionId: region.id,
      regionName: region.name,
      status: Mock.Random.pick(['0', '1']),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss'),
      createBy: Mock.Random.pick(['admin', '张三', '李四'])
    })
  }
  return list
}

const schoolList = generateSchoolList()

// 年级选项
const gradeOptions = [
  { value: '1', label: '一年级' },
  { value: '2', label: '二年级' },
  { value: '3', label: '三年级' },
  { value: '4', label: '四年级' },
  { value: '5', label: '五年级' },
  { value: '6', label: '六年级' },
  { value: '7', label: '七年级' },
  { value: '8', label: '八年级' },
  { value: '9', label: '九年级' },
  { value: '10', label: '高一' },
  { value: '11', label: '高二' },
  { value: '12', label: '高三' }
]

// 班级选项
const classOptions = [
  { value: '1', label: '1班' },
  { value: '2', label: '2班' },
  { value: '3', label: '3班' },
  { value: '4', label: '4班' },
  { value: '5', label: '5班' },
  { value: '6', label: '6班' },
  { value: '7', label: '7班' },
  { value: '8', label: '8班' },
  { value: '9', label: '9班' },
  { value: '10', label: '10班' }
]

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

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

  let filteredList = [...schoolList]

  // 按学校名称筛选
  if (schoolName) {
    filteredList = filteredList.filter(item => 
      item.schoolName.includes(schoolName)
    )
  }

  // 按状态筛选
  if (status !== null && status !== '') {
    filteredList = filteredList.filter(item => item.status === status)
  }

  // 按区域筛选（包含子区域）
  if (regionId) {
    const rid = parseInt(regionId)
    filteredList = filteredList.filter(item => {
      // 匹配当前区域或其子区域
      const itemRegionId = item.regionId
      if (rid < 10) {
        // 省级
        return Math.floor(itemRegionId / 100) === rid
      } else if (rid < 100) {
        // 市级
        return Math.floor(itemRegionId / 10) === rid
      } else {
        // 区级
        return itemRegionId === rid
      }
    })
  }

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

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

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

// 新增学校
Mock.mock('/api/school', 'post', (options) => {
  const data = JSON.parse(options.body)
  const newSchool = {
    id: schoolList.length + 1,
    ...data,
    createTime: Mock.Random.now('yyyy-MM-dd HH:mm:ss'),
    createBy: 'admin'
  }
  schoolList.unshift(newSchool)
  return {
    code: 200,
    msg: '新增成功'
  }
})

// 修改学校
Mock.mock('/api/school', 'put', (options) => {
  const data = JSON.parse(options.body)
  const index = schoolList.findIndex(item => item.id === data.id)
  if (index !== -1) {
    schoolList[index] = { ...schoolList[index], ...data }
  }
  return {
    code: 200,
    msg: '修改成功'
  }
})

// 删除学校
Mock.mock(/\/api\/school\/\d+$/, 'delete', (options) => {
  const id = parseInt(options.url.match(/\/api\/school\/(\d+)/)[1])
  const index = schoolList.findIndex(item => item.id === id)
  if (index !== -1) {
    schoolList.splice(index, 1)
  }
  return {
    code: 200,
    msg: '删除成功'
  }
})

// 获取年级选项
Mock.mock(/\/api\/grade\/options/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: gradeOptions
  }
})

// 获取班级选项
Mock.mock(/\/api\/class\/options/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: classOptions
  }
})

// 为学校添加年级
Mock.mock('/api/school/grade', 'post', () => {
  return {
    code: 200,
    msg: '添加年级成功'
  }
})

// 为年级添加班级
Mock.mock('/api/school/class', 'post', () => {
  return {
    code: 200,
    msg: '添加班级成功'
  }
})
