import axios from 'axios'

const API_BASE_URL = `${import.meta.env.VITE_API_BASE_URL || 'http://localhost:5352/api'}/production-material-certification`

// 创建axios实例
const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  config => {
    // 可以在这里添加token等认证信息
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    console.error('API请求错误:', error)
    return Promise.reject(error)
  }
)

// 生资认证管理API
export default {
  // 获取生资认证列表
  getCertifications: (params) => api.get('/', { params }),
  
  // 获取生资认证详情
  getCertificationById: (id) => api.get(`/${id}`),
  
  // 创建生资认证
  createCertification: (data) => api.post('/', data),
  
  // 更新生资认证
  updateCertification: (id, data) => api.put(`/${id}`, data),
  
  // 删除生资认证
  deleteCertification: (id) => api.delete(`/${id}`),
  
  // 切换认证状态
  toggleCertificationStatus: (id, status) => api.patch(`/${id}/status`, { status })
}
