import axios from 'axios'
import { message } from 'ant-design-vue'

// 创建axios实例
const instance = axios.create({
  baseURL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5352/api',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
instance.interceptors.request.use(
  config => {
    // 从localStorage获取token
    const token = localStorage.getItem('token')
    // 如果有token，添加到请求头
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  response => {
    // 处理响应数据
    return response.data
  },
  error => {
    // 处理响应错误
    if (error.response) {
      // 根据不同的状态码处理错误
      switch (error.response.status) {
        case 401:
          // 未授权，清除token并跳转到登录页面
          localStorage.removeItem('token')
          window.location.href = '/login'
          console.error('登录已过期，请重新登录')
          break
        case 403:
          console.error('没有权限执行此操作')
          break
        case 404:
          console.error('请求的资源不存在')
          break
        case 500:
          console.error('服务器内部错误')
          break
        default:
          console.error(error.response.data.message || '请求失败')
      }
    } else if (error.request) {
      // 请求发出但没有收到响应
      console.error('网络错误，请检查网络连接')
    } else {
      // 请求配置出错
      console.error('请求配置错误')
    }
    return Promise.reject(error)
  }
)

// API接口定义
const api = {
  // 认证相关API
  auth: {
    // 登录
    login: (data) => instance.post('/auth/login', data),
    // 获取用户信息
    getUserInfo: () => instance.get('/auth/userinfo'),
    // 退出登录
    logout: () => instance.post('/auth/logout'),
    // 重置密码
    resetPassword: (data) => instance.post('/auth/reset-password', data)
  },
  
  // 用户管理相关API
  user: {
    // 获取用户列表
    getList: (params) => instance.get('/users', { params }),
    // 获取单个用户信息
    getDetail: (id) => instance.get(`/users/${id}`),
    // 创建用户
    create: (data) => instance.post('/users', data),
    // 更新用户
    update: (id, data) => instance.put(`/users/${id}`, data),
    // 删除用户
    delete: (id) => instance.delete(`/users/${id}`),
    // 批量删除用户
    batchDelete: (ids) => instance.post('/users/batch-delete', { ids }),
    // 更新用户状态
    updateStatus: (id, status) => instance.put(`/users/${id}/status`, { status })
  },
  
  // 监管相关API
  supervision: {
    // 获取监管统计数据
    getStats: () => instance.get('/supervision/stats'),
    // 获取监管任务列表
    getTasks: (params) => instance.get('/supervision/tasks', { params }),
    // 获取监管任务详情
    getTaskDetail: (id) => instance.get(`/supervision/tasks/${id}`),
    // 创建监管任务
    createTask: (data) => instance.post('/supervision/tasks', data),
    // 更新监管任务
    updateTask: (id, data) => instance.put(`/supervision/tasks/${id}`, data),
    // 删除监管任务
    deleteTask: (id) => instance.delete(`/supervision/tasks/${id}`)
  },
  
  // 审批相关API
  approval: {
    // 获取审批流程列表
    getList: (params) => instance.get('/approval', { params }),
    // 创建审批流程
    create: (data) => instance.post('/approval', data),
    // 获取审批详情
    getDetail: (id) => instance.get(`/approval/${id}`),
    // 更新审批状态
    updateStatus: (id, status) => instance.put(`/approval/${id}/status`, { status })
  },
  
  // 疫情监控相关API
  epidemic: {
    // 获取疫情统计数据
    getStats: () => instance.get('/epidemic/stats'),
    // 获取疫苗接种数据
    getVaccinationData: (params) => instance.get('/epidemic/vaccination', { params }),
    // 获取检测数据
    getTestData: (params) => instance.get('/epidemic/test', { params }),
    // 防疫机构管理
    agencies: {
      // 获取防疫机构列表
      getList: (params) => instance.get('/epidemic/agencies', { params }),
      // 获取防疫机构详情
      getDetail: (id) => instance.get(`/epidemic/agencies/${id}`),
      // 创建防疫机构
      create: (data) => instance.post('/epidemic/agencies', data),
      // 更新防疫机构
      update: (id, data) => instance.put(`/epidemic/agencies/${id}`, data),
      // 删除防疫机构
      delete: (id) => instance.delete(`/epidemic/agencies/${id}`),
      // 切换防疫机构状态
      toggleStatus: (id) => instance.patch(`/epidemic/agencies/${id}/status`)
    }
  },
  
  // 数据可视化相关API
  visualization: {
    // 获取可视化数据
    getData: (params) => instance.get('/visualization/data', { params })
  },
  
  // 文件管理相关API
  file: {
    // 获取文件列表
    getList: (params) => instance.get('/files', { params }),
    // 上传文件
    upload: (file, onUploadProgress) => {
      const formData = new FormData()
      formData.append('file', file)
      return instance.post('/files/upload', formData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        onUploadProgress
      })
    },
    // 下载文件
    download: (id) => instance.get(`/files/${id}/download`, { responseType: 'blob' }),
    // 删除文件
    delete: (id) => instance.delete(`/files/${id}`)
  },
  
  // 人员管理相关API
  personnel: {
    // 获取人员列表
    getList: (params) => instance.get('/personnel', { params }),
    // 创建人员
    create: (data) => instance.post('/personnel', data),
    // 更新人员
    update: (id, data) => instance.put(`/personnel/${id}`, data),
    // 删除人员
    delete: (id) => instance.delete(`/personnel/${id}`)
  },
  
  // 服务管理相关API
  service: {
    // 获取服务列表
    getList: (params) => instance.get('/service', { params }),
    // 创建服务
    create: (data) => instance.post('/service', data),
    // 更新服务
    update: (id, data) => instance.put(`/service/${id}`, data),
    // 删除服务
    delete: (id) => instance.delete(`/service/${id}`)
  },
  
  // 仓库管理相关API
  warehouse: {
    // 获取物资列表
    getList: (params) => instance.get('/warehouse', { params }),
    // 获取单个物资详情
    getDetail: (id) => instance.get(`/warehouse/${id}`),
    // 创建物资
    create: (data) => instance.post('/warehouse', data),
    // 更新物资
    update: (id, data) => instance.put(`/warehouse/${id}`, data),
    // 删除物资
    delete: (id) => instance.delete(`/warehouse/${id}`),
    // 物资入库
    stockIn: (data) => instance.post('/warehouse/in', data),
    // 物资出库
    stockOut: (data) => instance.post('/warehouse/out', data),
    // 获取库存统计信息
    getStats: () => instance.get('/warehouse/stats')
  },
  
  // 系统设置相关API
  system: {
    // 获取系统设置
    getSettings: () => instance.get('/system/settings'),
    // 更新系统设置
    updateSettings: (data) => instance.put('/system/settings', data),
    // 获取日志列表
    getLogs: (params) => instance.get('/system/logs', { params })
  },
  
  // 政府管理相关API
  government: {
    // 行政人员管理
    adminStaff: {
      // 获取行政人员列表
      getList: (params) => instance.get('/personnel', { params }),
      // 创建行政人员
      create: (data) => instance.post('/personnel', data),
      // 更新行政人员
      update: (id, data) => instance.put(`/personnel/${id}`, data),
      // 删除行政人员
      delete: (id) => instance.delete(`/personnel/${id}`),
      // 重置行政人员密码
      resetPassword: (id) => instance.post(`/personnel/${id}/reset-password`)
    },
    
    // 部门管理
    departments: {
      // 获取部门列表
      getList: (params) => instance.get('/government/departments', { params })
    },
    
    // 岗位管理
    positions: {
      // 获取岗位列表
      getList: (params) => instance.get('/government/positions', { params })
    },
    
    // 养殖户管理
    farmers: {
      // 获取养殖户列表
      getList: (params) => instance.get('/government/farmers', { params }),
      // 新增养殖户
      create: (data) => instance.post('/government/farmers', data),
      // 编辑养殖户
      update: (id, data) => instance.put(`/government/farmers/${id}`, data),
      // 删除养殖户
      delete: (id) => instance.delete(`/government/farmers/${id}`),
      // 重置养殖户密码
      resetPassword: (id) => instance.post(`/government/farmers/${id}/reset-password`)
    },
    
    // 养殖类型相关
    farmTypes: {
      // 获取养殖类型列表
      getList: () => instance.get('/government/farm-types')
    },
    
    // 养殖种类相关
    animalTypes: {
      // 获取养殖种类列表
      getList: () => instance.get('/government/animal-types')
    },
    
    // 智能项圈管理
    collars: {
      // 获取智能项圈列表
      getList: (params) => instance.get('/government/collars', { params }),
      // 新增智能项圈
      create: (data) => instance.post('/government/collars', data),
      // 编辑智能项圈
      update: (id, data) => instance.put(`/government/collars/${id}`, data),
      // 删除智能项圈
      delete: (id) => instance.delete(`/government/collars/${id}`)
    },

    // 智能耳标管理
    earmarks: {
      // 获取智能耳标列表
      getList: (params) => instance.get('/smart-earmark', { params }),
      // 新增智能耳标
      create: (data) => instance.post('/smart-earmark', data),
      // 编辑智能耳标
      update: (id, data) => instance.put(`/smart-earmark/${id}`, data),
      // 删除智能耳标
      delete: (id) => instance.delete(`/smart-earmark/${id}`)
    },
    
    // 智能主机管理
    smartHosts: {
      // 获取智能主机列表
      getList: (params) => instance.get('/smart-host', { params }),
      // 新增智能主机
      create: (data) => instance.post('/smart-host', data),
      // 编辑智能主机
      update: (id, data) => instance.put(`/smart-host/${id}`, data),
      // 删除智能主机
      delete: (id) => instance.delete(`/smart-host/${id}`)
    }
  },
  
  // 屠宰场管理相关API
  slaughter: {
    // 获取屠宰场列表
    getList: (params) => instance.get('/slaughter/slaughterhouses', { params }),
    // 获取单个屠宰场详情
    getDetail: (id) => instance.get(`/slaughter/slaughterhouses/${id}`),
    // 创建屠宰场
    create: (data) => instance.post('/slaughter/slaughterhouses', data),
    // 更新屠宰场
    update: (id, data) => instance.put(`/slaughter/slaughterhouses/${id}`, data),
    // 删除屠宰场
    delete: (id) => instance.delete(`/slaughter/slaughterhouses/${id}`),
    // 切换屠宰场状态
    toggleStatus: (id) => instance.patch(`/slaughter/slaughterhouses/${id}/status`)
  },
  
  // 无害化登记管理相关API
harmlessRegistration: {
  getList: (params) => instance.get('/harmless/list', { params }),
  getDetail: (id) => instance.get(`/harmless/detail/${id}`),
  create: (data) => instance.post('/harmless/create', data),
  update: (id, data) => instance.put(`/harmless/update/${id}`, data),
  delete: (id) => instance.delete(`/harmless/delete/${id}`)
},

// 无害化场所管理相关API
  harmlessPlace: {
    getList: (params) => instance.get('/harmless-place/list', { params }),
    getDetail: (id) => instance.get(`/harmless-place/detail/${id}`),
    create: (data) => instance.post('/harmless-place/create', data),
    update: (id, data) => instance.put(`/harmless-place/update/${id}`, data),
    delete: (id) => instance.delete(`/harmless-place/delete/${id}`)
  },

  // 防疫记录管理相关API
  epidemicRecord: {
    getList: (params) => instance.get('/epidemic-record/list', { params }),
    getDetail: (id) => instance.get(`/epidemic-record/detail/${id}`),
    create: (data) => instance.post('/epidemic-record/create', data),
    update: (id, data) => instance.put(`/epidemic-record/update/${id}`, data),
    delete: (id) => instance.delete(`/epidemic-record/delete/${id}`),
    batchDelete: (ids) => instance.delete('/epidemic-record/batch-delete', { data: { ids } })
  },

  // 疫苗管理相关API
  vaccine: {
    getList: (params) => instance.get('/vaccine/list', { params }),
    getDetail: (id) => instance.get(`/vaccine/detail/${id}`),
    create: (data) => instance.post('/vaccine/create', data),
    update: (id, data) => instance.put(`/vaccine/update/${id}`, data),
    delete: (id) => instance.delete(`/vaccine/delete/${id}`),
    batchDelete: (ids) => instance.delete('/vaccine/batch-delete', { data: { ids } }),
    stockIn: (id, data) => instance.post(`/vaccine/stock-in/${id}`, data),
    stockOut: (id, data) => instance.post(`/vaccine/stock-out/${id}`, data)
  }
}

export default api