/**
 * 区域管理Mock数据
 * @author pangu
 */
import Mock from 'mockjs'

// 区域树形数据
const regionTree = [
  {
    id: 1,
    name: '湖北省',
    code: 'HB',
    level: 1,
    parentId: 0,
    status: '0',
    createTime: '2026-01-01 10:00:00',
    children: [
      {
        id: 11,
        name: '武汉市',
        code: 'WH',
        level: 2,
        parentId: 1,
        status: '0',
        createTime: '2026-01-01 10:00:00',
        children: [
          { id: 111, name: '武昌区', code: 'WC', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 112, name: '洪山区', code: 'HS', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 113, name: '江夏区', code: 'JX', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 114, name: '汉阳区', code: 'HY', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 115, name: '江岸区', code: 'JA', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 116, name: '江汉区', code: 'JH', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 117, name: '硚口区', code: 'QK', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 118, name: '青山区', code: 'QS', level: 3, parentId: 11, status: '0', createTime: '2026-01-01 10:00:00' }
        ]
      },
      {
        id: 12,
        name: '黄冈市',
        code: 'HG',
        level: 2,
        parentId: 1,
        status: '0',
        createTime: '2026-01-01 10:00:00',
        children: [
          { id: 121, name: '黄州区', code: 'HZ', level: 3, parentId: 12, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 122, name: '红安县', code: 'HA', level: 3, parentId: 12, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 123, name: '麻城市', code: 'MC', level: 3, parentId: 12, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 124, name: '武穴市', code: 'WX', level: 3, parentId: 12, status: '0', createTime: '2026-01-01 10:00:00' }
        ]
      },
      {
        id: 13,
        name: '宜昌市',
        code: 'YC',
        level: 2,
        parentId: 1,
        status: '0',
        createTime: '2026-01-01 10:00:00',
        children: [
          { id: 131, name: '西陵区', code: 'XL', level: 3, parentId: 13, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 132, name: '伍家岗区', code: 'WJG', level: 3, parentId: 13, status: '0', createTime: '2026-01-01 10:00:00' }
        ]
      },
      {
        id: 14,
        name: '襄阳市',
        code: 'XY',
        level: 2,
        parentId: 1,
        status: '0',
        createTime: '2026-01-01 10:00:00',
        children: [
          { id: 141, name: '襄城区', code: 'XCQ', level: 3, parentId: 14, status: '0', createTime: '2026-01-01 10:00:00' },
          { id: 142, name: '樊城区', code: 'FC', level: 3, parentId: 14, status: '0', createTime: '2026-01-01 10:00:00' }
        ]
      }
    ]
  }
]

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

// 获取区域列表（扁平化）
Mock.mock(/\/api\/region\/list/, 'get', () => {
  return {
    code: 200,
    msg: '查询成功',
    data: regionTree
  }
})

// 获取区域详情
Mock.mock(/\/api\/region\/\d+/, 'get', (options) => {
  const id = parseInt(options.url.match(/\/api\/region\/(\d+)/)[1])

  // 递归查找
  const findById = (list, targetId) => {
    for (const item of list) {
      if (item.id === targetId) return item
      if (item.children) {
        const found = findById(item.children, targetId)
        if (found) return found
      }
    }
    return null
  }

  return {
    code: 200,
    msg: '查询成功',
    data: findById(regionTree, id)
  }
})

// 新增区域
Mock.mock('/api/region', 'post', () => {
  return { code: 200, msg: '新增成功' }
})

// 修改区域
Mock.mock('/api/region', 'put', () => {
  return { code: 200, msg: '修改成功' }
})

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