// 模拟数据服务，用于在开发环境中提供数据

// 模拟的后端延迟
const mockDelay = (ms = 300) => new Promise(resolve => setTimeout(resolve, ms))

// 模拟用户数据
const mockUsers = [
  { id: 1, username: 'admin', real_name: '管理员', phone: '13800138000', email: 'admin@example.com', role: 'admin', status: 1, created_at: '2024-01-01 10:00:00' },
  { id: 2, username: 'user1', real_name: '用户一', phone: '13800138001', email: 'user1@example.com', role: 'user', status: 1, created_at: '2024-01-02 11:00:00' },
  { id: 3, username: 'user2', real_name: '用户二', phone: '13800138002', email: 'user2@example.com', role: 'user', status: 0, created_at: '2024-01-03 12:00:00' },
  { id: 4, username: 'user3', real_name: '用户三', phone: '13800138003', email: 'user3@example.com', role: 'guest', status: 1, created_at: '2024-01-04 13:00:00' },
  { id: 5, username: 'user4', real_name: '用户四', phone: '13800138004', email: 'user4@example.com', role: 'user', status: 1, created_at: '2024-01-05 14:00:00' }
]

// 模拟监管统计数据
const mockSupervisionStats = {
  entityCount: 150,
  inspectionCount: 78
}

// 模拟疫情统计数据
const mockEpidemicStats = {
  vaccinated: 12500,
  tested: 89000
}

// 模拟可视化数据
const mockVisualizationData = {
  charts: [
    {
      id: 1,
      title: '监管趋势图',
      type: 'line',
      data: {
        xAxis: ['1月', '2月', '3月', '4月', '5月', '6月'],
        series: [
          {
            name: '检查次数',
            data: [12, 19, 3, 5, 2, 3]
          }
        ]
      }
    },
    {
      id: 2,
      title: '数据分布图',
      type: 'pie',
      data: {
        series: [
          {
            name: '数据分布',
            data: [
              { value: 30, name: '类型A' },
              { value: 25, name: '类型B' },
              { value: 20, name: '类型C' },
              { value: 15, name: '类型D' },
              { value: 10, name: '其他' }
            ]
          }
        ]
      }
    }
  ],
  indicators: [
    { name: '总实体数', value: 150, unit: '个' },
    { name: '检查次数', value: 78, unit: '次' },
    { name: '疫苗接种数', value: 12500, unit: '人' },
    { name: '检测人数', value: 89000, unit: '人' }
  ]
}

// 模拟审批流程数据
const mockApprovals = [
  { id: 1, name: '企业注册审批', status: 'pending', create_time: '2024-01-10 09:00:00' },
  { id: 2, name: '资质认证申请', status: 'approved', create_time: '2024-01-09 14:30:00' },
  { id: 3, name: '数据变更申请', status: 'rejected', create_time: '2024-01-08 11:20:00' },
  { id: 4, name: '权限申请', status: 'pending', create_time: '2024-01-07 16:40:00' },
  { id: 5, name: '系统配置变更', status: 'approved', create_time: '2024-01-06 10:15:00' }
]

// 模拟登录验证
export const login = async (username, password) => {
  await mockDelay()
  
  // 简单的模拟验证
  if (username === 'admin' && password === 'admin123') {
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: 'mock-jwt-token-' + Date.now(),
        userInfo: {
          id: 1,
          username: 'admin',
          real_name: '管理员',
          phone: '13800138000',
          email: 'admin@example.com',
          role: 'admin'
        },
        permissions: ['admin', 'user', 'guest']
      }
    }
  }
  
  // 模拟普通用户登录
  if (username === 'user' && password === 'user123') {
    return {
      code: 200,
      message: '登录成功',
      data: {
        token: 'mock-jwt-token-' + Date.now(),
        userInfo: {
          id: 2,
          username: 'user',
          real_name: '普通用户',
          phone: '13800138001',
          email: 'user@example.com',
          role: 'user'
        },
        permissions: ['user']
      }
    }
  }
  
  return {
    code: 400,
    message: '用户名或密码错误'
  }
}

// 模拟获取用户信息
export const getUserInfo = async () => {
  await mockDelay()
  return {
    code: 200,
    message: '获取成功',
    data: {
      id: 1,
      username: 'admin',
      real_name: '管理员',
      phone: '13800138000',
      email: 'admin@example.com',
      role: 'admin',
      permissions: ['admin', 'user', 'guest']
    }
  }
}

// 模拟获取用户列表
export const getUsers = async (page = 1, pageSize = 10, keyword = '') => {
  await mockDelay()
  
  // 模拟搜索
  let filteredUsers = [...mockUsers]
  if (keyword) {
    filteredUsers = mockUsers.filter(user => 
      user.username.includes(keyword) || 
      user.real_name.includes(keyword) || 
      user.id.toString().includes(keyword)
    )
  }
  
  // 模拟分页
  const start = (page - 1) * pageSize
  const end = start + pageSize
  const paginatedUsers = filteredUsers.slice(start, end)
  
  return {
    code: 200,
    message: '获取成功',
    data: {
      list: paginatedUsers,
      total: filteredUsers.length,
      page,
      pageSize
    }
  }
}

// 模拟添加用户
export const createUser = async (userData) => {
  await mockDelay()
  
  const newUser = {
    id: mockUsers.length + 1,
    ...userData,
    created_at: new Date().toLocaleString('zh-CN')
  }
  
  mockUsers.push(newUser)
  
  return {
    code: 200,
    message: '用户创建成功',
    data: newUser
  }
}

// 模拟更新用户
export const updateUser = async (id, userData) => {
  await mockDelay()
  
  const index = mockUsers.findIndex(user => user.id === id)
  if (index === -1) {
    return {
      code: 404,
      message: '用户不存在'
    }
  }
  
  mockUsers[index] = { ...mockUsers[index], ...userData }
  
  return {
    code: 200,
    message: '用户更新成功',
    data: mockUsers[index]
  }
}

// 模拟删除用户
export const deleteUser = async (id) => {
  await mockDelay()
  
  const index = mockUsers.findIndex(user => user.id === id)
  if (index === -1) {
    return {
      code: 404,
      message: '用户不存在'
    }
  }
  
  mockUsers.splice(index, 1)
  
  return {
    code: 200,
    message: '用户删除成功'
  }
}

// 模拟获取监管统计数据
export const getSupervisionStats = async () => {
  await mockDelay()
  return {
    code: 200,
    message: '获取成功',
    data: mockSupervisionStats
  }
}

// 模拟获取疫情统计数据
export const getEpidemicStats = async () => {
  await mockDelay()
  return {
    code: 200,
    message: '获取成功',
    data: mockEpidemicStats
  }
}

// 模拟获取可视化数据
export const getVisualizationData = async () => {
  await mockDelay()
  return {
    code: 200,
    message: '获取成功',
    data: mockVisualizationData
  }
}

// 模拟获取审批列表
export const getApprovals = async (page = 1, pageSize = 10, keyword = '') => {
  await mockDelay()
  
  // 模拟搜索
  let filteredApprovals = [...mockApprovals]
  if (keyword) {
    filteredApprovals = mockApprovals.filter(approval => 
      approval.name.includes(keyword) || 
      approval.status.includes(keyword) || 
      approval.id.toString().includes(keyword)
    )
  }
  
  // 模拟分页
  const start = (page - 1) * pageSize
  const end = start + pageSize
  const paginatedApprovals = filteredApprovals.slice(start, end)
  
  return {
    code: 200,
    message: '获取成功',
    data: {
      list: paginatedApprovals,
      total: filteredApprovals.length,
      page,
      pageSize
    }
  }
}

// 模拟创建审批流程
export const createApproval = async (approvalData) => {
  await mockDelay()
  
  const newApproval = {
    id: mockApprovals.length + 1,
    ...approvalData,
    status: 'pending',
    create_time: new Date().toLocaleString('zh-CN')
  }
  
  mockApprovals.push(newApproval)
  
  return {
    code: 200,
    message: '审批流程创建成功',
    data: newApproval
  }
}

// 模拟更新审批状态
export const updateApprovalStatus = async (id, status) => {
  await mockDelay()
  
  const index = mockApprovals.findIndex(approval => approval.id === id)
  if (index === -1) {
    return {
      code: 404,
      message: '审批流程不存在'
    }
  }
  
  mockApprovals[index].status = status
  
  return {
    code: 200,
    message: '审批状态更新成功',
    data: mockApprovals[index]
  }
}