// 使用新的请求拦截器，支持Token自动刷新
import { apiClient } from './request'

// 使用配置了自动刷新功能的axios实例
const api = apiClient

// API接口
export const authAPI = {
  login: (data) => api.post('/auth/login', data),
  logout: () => api.post('/auth/logout'),
  getProfile: () => api.get('/users/profile')
}

export const userAPI = {
  getList: (params) => api.get('/users', { params }),
  create: (data) => api.post('/users', data),
  update: (id, data) => api.put(`/users/${id}`, data),
  delete: (id) => api.delete(`/users/${id}`),
  updateProfile: (data) => api.put('/users/profile', data),
  changePassword: (data) => api.put('/users/change-password', data),
  uploadAvatar: (formData) => api.post('/users/avatar', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  }),
  export: (params) => api.get('/users/export', { params, responseType: 'blob' })
};

export const menuAPI = {
  getMenus: async () => {
    const response = await api.get('/menus');
    return response.data; // 返回响应的data部分
  },
  getAllMenus: async () => {
    const response = await api.get('/menus/all');
    return response.data; // 返回响应的data部分
  }
}

export const insuranceTypeAPI = {
  getList: (params) => api.get('/insurance-types', { params }),
  create: (data) => api.post('/insurance-types', data),
  update: (id, data) => api.put(`/insurance-types/${id}`, data),
  delete: (id) => api.delete(`/insurance-types/${id}`),
  updateStatus: (id, data) => api.patch(`/insurance-types/${id}/status`, data),
  export: (params) => api.get('/insurance-types/export', { params, responseType: 'blob' })
}

export const applicationAPI = {
  getList: (params) => api.get('/insurance/applications', { params }),
  getDetail: (id) => api.get(`/insurance/applications/${id}`),
  create: (data) => api.post('/insurance/applications', data),
  update: (id, data) => api.put(`/insurance/applications/${id}`, data),
  review: (id, data) => api.patch(`/insurance/applications/${id}/review`, data),
  updateStatus: (id, data) => api.put(`/insurance/applications/${id}/status`, data),
  delete: (id) => api.delete(`/insurance/applications/${id}`),
  getStats: () => api.get('/insurance/applications-stats'),
  getCategories: () => api.get('/insurance/categories'),
  export: (params) => api.get('/insurance/applications/export', { params, responseType: 'blob' })
}

export const policyAPI = {
  getList: (params) => api.get('/policies', { params }),
  getDetail: (id) => api.get(`/policies/${id}`),
  create: (data) => api.post('/policies', data),
  update: (id, data) => api.put(`/policies/${id}`, data),
  updateStatus: (id, data) => api.put(`/policies/${id}/status`, data),
  delete: (id) => api.delete(`/policies/${id}`),
  export: (params) => api.get('/policies/export', { params, responseType: 'blob' })
}

export const claimAPI = {
  getList: (params) => api.get('/claims', { params }),
  getDetail: (id) => api.get(`/claims/${id}`),
  updateStatus: (id, data) => api.put(`/claims/${id}/status`, data),
  delete: (id) => api.delete(`/claims/${id}`),
  export: (params) => api.get('/claims/export', { params, responseType: 'blob' })
}

export const dashboardAPI = {
  getStats: () => api.get('/dashboard/stats'),
  getRecentActivities: () => api.get('/dashboard/recent-activities'),
  getChartData: (params) => api.get('/dashboard/chart-data', { params })
}

// 设备预警API
export const deviceAlertAPI = {
  getStats: () => api.get('/device-alerts/stats'),
  getList: (params) => api.get('/device-alerts', { params }),
  getDetail: (id) => api.get(`/device-alerts/${id}`),
  markAsRead: (id) => api.patch(`/device-alerts/${id}/read`),
  markAllAsRead: () => api.patch('/device-alerts/read-all'),
  handle: (id, data) => api.patch(`/device-alerts/${id}/handle`, data),
  export: (params) => api.get('/device-alerts/export', { params, responseType: 'blob' })
}

// 数据览仓API
export const dataWarehouseAPI = {
  getOverview: () => api.get('/data-warehouse/overview'),
  getInsuranceTypeDistribution: () => api.get('/data-warehouse/insurance-type-distribution'),
  getApplicationStatusDistribution: () => api.get('/data-warehouse/application-status-distribution'),
  getTrendData: () => api.get('/data-warehouse/trend-data'),
  getClaimStats: () => api.get('/data-warehouse/claim-stats')
}

// 监管任务API
export const supervisionTaskApi = {
  getList: (params) => api.get('/supervision-tasks', { params }),
  create: (data) => api.post('/supervision-tasks', data),
  update: (id, data) => api.put(`/supervision-tasks/${id}`, data),
  delete: (id) => api.delete(`/supervision-tasks/${id}`),
  getDetail: (id) => api.get(`/supervision-tasks/${id}`),
  batchOperate: (data) => api.post('/supervision-tasks/batch/operate', data),
  getStats: () => api.get('/supervision-tasks/stats'),
  export: (params) => api.get('/supervision-tasks/export', { params, responseType: 'blob' }),
  archive: (id) => api.patch(`/supervision-tasks/${id}/archive`),
  downloadReport: (id) => api.get(`/supervision-tasks/${id}/report`, { responseType: 'blob' })
}

// 待安装任务API
export const installationTaskApi = {
  getList: (params) => api.get('/installation-tasks', { params }),
  create: (data) => api.post('/installation-tasks', data),
  update: (id, data) => api.put(`/installation-tasks/${id}`, data),
  delete: (id) => api.delete(`/installation-tasks/${id}`),
  getById: (id) => api.get(`/installation-tasks/${id}`),
  updateStatus: (id, data) => api.patch(`/installation-tasks/${id}/status`, data),
  getStats: () => api.get('/installation-tasks/stats'),
  assign: (id, data) => api.post(`/installation-tasks/${id}/assign`, data),
  complete: (id, data) => api.post(`/installation-tasks/${id}/complete`, data),
  getHistory: (id) => api.get(`/installation-tasks/${id}/history`),
  export: (params) => api.get('/installation-tasks/export', { params, responseType: 'blob' }),
  batchDelete: (ids) => api.post('/installation-tasks/batch-delete', { ids }),
  batchUpdateStatus: (data) => api.post('/installation-tasks/batch-update-status', data),
  getDetail: (id) => api.get(`/installation-tasks/${id}`)
}

// 生资保险相关API
export const livestockTypeApi = {
  getList: (params) => api.get('/livestock-types', { params }),
  create: (data) => api.post('/livestock-types', data),
  update: (id, data) => api.put(`/livestock-types/${id}`, data),
  delete: (id, data) => api.delete(`/livestock-types/${id}`, data),
  getById: (id) => api.get(`/livestock-types/${id}`),
  updateStatus: (id, data) => api.patch(`/livestock-types/${id}/status`, data),
  batchUpdateStatus: (data) => api.patch('/livestock-types/batch-status', data)
}

export const livestockPolicyApi = {
  getList: (params) => api.get('/livestock-policies', { params }),
  create: (data) => api.post('/livestock-policies', data),
  update: (id, data) => api.put(`/livestock-policies/${id}`, data),
  delete: (id) => api.delete(`/livestock-policies/${id}`),
  getById: (id) => api.get(`/livestock-policies/${id}`),
  updateStatus: (id, data) => api.patch(`/livestock-policies/${id}/status`, data),
  getStats: () => api.get('/livestock-policies/stats'),
  getLivestockTypes: () => api.get('/livestock-types/active'),
  export: (params) => api.get('/livestock-policies/export', { params, responseType: 'blob' })
}

export const livestockClaimApi = {
  getList: (params) => api.get('/livestock-claims', { params }),
  create: (data) => api.post('/livestock-claims', data),
  update: (id, data) => api.put(`/livestock-claims/${id}`, data),
  delete: (id) => api.delete(`/livestock-claims/${id}`),
  getById: (id) => api.get(`/livestock-claims/${id}`),
  approve: (id, data) => api.post(`/livestock-claims/${id}/approve`, data),
  reject: (id, data) => api.post(`/livestock-claims/${id}/reject`, data),
  updatePaymentStatus: (id, data) => api.patch(`/livestock-claims/${id}/payment`, data),
  getStats: () => api.get('/livestock-claims/stats'),
  export: (params) => api.get('/livestock-claims/export', { params, responseType: 'blob' })
}

// 操作日志API
export const operationLogAPI = {
  getList: (params) => api.get('/operation-logs', { params }),
  getStats: () => api.get('/operation-logs/stats'),
  getById: (id) => api.get(`/operation-logs/${id}`),
  export: (params) => api.get('/operation-logs/export', { params })
}

// 权限管理API
export const permissionAPI = {
  getList: (params) => api.get('/permissions', { params }),
  create: (data) => api.post('/permissions', data),
  update: (id, data) => api.put(`/permissions/${id}`, data),
  delete: (id) => api.delete(`/permissions/${id}`),
  getTree: () => api.get('/permissions/tree'),
  getRolePermissions: (roleId) => api.get(`/permissions/roles/${roleId}`)
}

// 角色权限管理API - 使用统一的请求方法
export const rolePermissionAPI = {
  // 获取所有角色及其权限
  getAllRolesWithPermissions: () => api.get('/role-permissions/roles'),
  
  // 获取所有权限
  getAllPermissions: () => api.get('/role-permissions/permissions'),
  
  // 获取指定角色的详细权限信息
  getRolePermissions: (roleId) => api.get(`/role-permissions/${roleId}`),
  
  // 批量分配角色权限
  assignRolePermissions: (roleId, requestData) => api.post(`/role-permissions/${roleId}/assign`, requestData),
  
  // 复制角色权限
  copyRolePermissions: (copyData) => api.post('/role-permissions/copy', copyData),
  
  // 检查用户权限
  checkUserPermission: (userId, permissionCode) => api.get(`/role-permissions/check/${userId}/${permissionCode}`),
  
  // 获取权限统计
  getPermissionStats: () => api.get('/role-permissions/stats')
}

export default api