import http from '@/utils/http'
import type { EquipmentApprovalListReq, EquipmentApprovalResp } from './type'

/**
 * 设备审批API
 */
export const equipmentApprovalApi = {
  /**
   * 分页查询待审批的设备采购申请
   */
  getPendingApprovals(params: EquipmentApprovalListReq) {
    // 确保分页参数格式正确，与设备采购模块保持一致
    const requestParams = {
      ...params,
      // 直接使用 pageNum，因为后端分页器需要的是 pageNum 参数
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
    }
    
    console.log('🔍 API - equipmentApprovalApi.getPendingApprovals 被调用')
    console.log('🔍 API - 接收到的参数:', params)
    console.log('🔍 API - 最终请求参数:', requestParams)
    
    return http.get<EquipmentApprovalResp[]>('/equipment/approval/pending', requestParams)
  },

  /**
   * 分页查询已审批的设备采购申请
   */
  getApprovedApprovals(params: EquipmentApprovalListReq) {
    // 确保分页参数格式正确，与设备采购模块保持一致
    const requestParams = {
      ...params,
      // 直接使用 pageNum，因为后端分页器需要的是 pageNum 参数
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
    }
    
    console.log('🔍 API - equipmentApprovalApi.getApprovedApprovals 被调用')
    console.log('🔍 API - 接收到的参数:', params)
    console.log('🔍 API - 最终请求参数:', requestParams)
    
    return http.get<EquipmentApprovalResp[]>('/equipment/approval/approved', requestParams)
  },

  /**
   * 审批通过
   */
  approve(approvalId: string, data: any) {
    return http.post(`/equipment/approval/${approvalId}/approve`, data)
  },

  /**
   * 审批拒绝
   */
  reject(approvalId: string, data: any) {
    return http.post(`/equipment/approval/${approvalId}/reject`, data)
  },

  /**
   * 获取审批详情
   */
  getApprovalDetail(approvalId: string) {
    return http.get<EquipmentApprovalResp>(`/equipment/approval/${approvalId}`)
  },

  /**
   * 获取审批统计信息
   */
  getApprovalStats() {
    return http.get('/equipment/approval/stats')
  },

  /**
   * 提交采购申请
   */
  submitProcurementApplication(data: any) {
    return http.post('/equipment/approval/procurement/apply', data)
  },

  /**
   * 获取我的采购申请
   */
  getMyProcurementApplications(params: EquipmentApprovalListReq) {
    return http.get<EquipmentApprovalResp[]>('/equipment/approval/procurement/my-applications', params)
  },

  /**
   * 撤回采购申请
   */
  withdrawProcurementApplication(approvalId: string) {
    return http.post(`/equipment/approval/procurement/${approvalId}/withdraw`)
  }
}
