import axios from 'axios';

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

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

// 合同相关API
export const contractApi = {
  // 获取合同列表
  getContractList: (params: {
    page?: number;
    pageSize?: number;
    name?: string;
    submitter?: string;
    department?: string;
    querySource?: string;
    riskLevel?: string;
    uploadTime?: string;
    finishTime?: string;
  }) => {
    return apiClient.get('/contracts', { params });
  },

  // 获取合同详情
  getContractById: (id: number) => {
    return apiClient.get(`/contracts/${id}`);
  },

  // 获取合同风险详情
  getContractRisks: (id: number) => {
    return apiClient.get(`/contracts/${id}/risks`);
  },

  // 提交新合同审查
  addContract: (contract: any) => {
    return apiClient.post('/contracts', contract);
  },

  // 更新合同
  updateContract: (id: number, contract: any) => {
    return apiClient.put(`/contracts/${id}`, contract);
  },

  // 删除合同
  deleteContract: (id: number) => {
    return apiClient.delete(`/contracts/${id}`);
  },

  // 获取合同源文件内容
  getContractContent: (id: number) => {
    return apiClient.get(`/contracts/${id}/content`);
  },

  // 获取合同预览地址
  getContractPreviewUrl: (id: number) => {
    return apiClient.get(`/contracts/${id}/preview-url`);
  }
};

// 统计相关API
export const statisticsApi = {
  // 获取统计数据
  getStatistics: () => {
    return apiClient.get('/statistics');
  }
};

export default apiClient;