import http from '@/utils/http'
import type { EquipmentListReq, EquipmentReq, EquipmentResp } from './type'

/**
 * 设备采购管理API
 */
export const equipmentProcurementApi = {
  /**
   * 分页查询设备采购记录
   */
  page: (params: EquipmentListReq) => {
    console.log('🔍 API - equipmentProcurementApi.page 被调用')
    console.log('🔍 API - 接收到的参数:', params)
    console.log('🔍 API - 参数类型:', typeof params)
    console.log('🔍 API - 参数的键值对:')
    Object.entries(params).forEach(([key, value]) => {
      console.log(`  ${key}: ${value} (${typeof value})`)
    })
    
    // 确保参数格式正确
    const requestParams = {
      ...params,
      // 直接使用 pageNum，因为后端分页器需要的是 pageNum 参数
      pageNum: params.pageNum || 1,
      pageSize: params.pageSize || 10,
    }
    
    console.log('🔍 API - 最终请求参数:', requestParams)
    console.log('🔍 API - 准备发送GET请求到 /equipment/procurement/page')
    console.log('🔍 API - 请求参数序列化前:', requestParams)
    
    // 手动序列化参数进行调试（使用URLSearchParams）
    const searchParams = new URLSearchParams()
    Object.entries(requestParams).forEach(([key, value]) => {
      if (value !== undefined && value !== null && value !== '') {
        searchParams.append(key, String(value))
      }
    })
    console.log('🔍 API - 手动序列化后的参数:', searchParams.toString())
    
    // 参考设备模块的调用方式，直接将参数作为第二个参数传递
    return http.get<ApiRes<PageRes<EquipmentResp>>>('/equipment/procurement/page', requestParams)
  },

  /**
   * 测试参数传递 - 使用API文档中的参数格式
   */
  testPage: () => {
    console.log('🧪 API - 测试参数传递')
    
    // 使用API文档中的参数格式进行测试
    const testParams = {
      page: 1,
      pageSize: 10,
      equipmentName: '测试设备',
      supplierName: '测试供应商',
      quantity: 10,
      unitPrice: 100.50,
      totalPrice: 1005.00,
      accountNumber: 'TEST001',
      brand: '测试品牌',
      locationStatus: 'spare',
      physicalLocation: '测试位置',
      purchaseOrder: 'PO001',
      inventoryBasis: '测试依据',
      dynamicRecord: '测试记录'
    }
    
    console.log('🧪 API - 测试参数:', testParams)
    
    return http.get<ApiRes<PageRes<EquipmentResp>>>('/equipment/procurement/page', testParams)
  },

  /**
   * 新增设备采购记录
   */
  add: (data: EquipmentReq) => {
    return http.post<ApiRes<null>>('/equipment/procurement', data)
  },

  /**
   * 修改设备采购记录
   */
  update: (equipmentId: string, data: EquipmentReq) => {
    return http.put<ApiRes<null>>(`/equipment/procurement/${equipmentId}`, data)
  },

  /**
   * 删除设备采购记录
   */
  delete: (equipmentId: string) => {
    return http.del<ApiRes<null>>(`/equipment/procurement/${equipmentId}`)
  },

  /**
   * 获取设备采购记录详情
   */
  detail: (equipmentId: string) => {
    return http.get<ApiRes<EquipmentResp>>(`/equipment/procurement/detail/${equipmentId}`)
  },

  /**
   * 确认收货（扩展版，支持完整设备数据）
   */
  receiveGoods: (equipmentId: string, data: ReceiptRequest) => {
    console.log('📦 收货API被调用，设备ID:', equipmentId)
    console.log('📦 收货数据:', data)
    
    return http.post<ApiRes<null>>(`/equipment/procurement/receipt/${equipmentId}`, data)
  },

  /**
   * 获取收货详情
   */
  getReceiptDetail: (equipmentId: string) => {
    return http.get<ApiRes<ReceiptDetail>>(`/equipment/procurement/receipt/${equipmentId}`)
  },

  /**
   * 执行付款
   */
  makePayment: (equipmentId: string, data: PaymentRequest) => {
    return http.post<ApiRes<null>>(`/equipment/procurement/payment/${equipmentId}`, data)
  },

  /**
   * 获取支付详情
   */
  getPaymentDetail: (equipmentId: string) => {
    return http.get<ApiRes<PaymentDetail>>(`/equipment/procurement/payment/${equipmentId}`)
  },

  /**
   * 获取采购统计信息
   */
  getStats: () => {
    return http.get<ApiRes<unknown>>('/equipment/procurement/stats')
  },

  /**
   * 批量删除设备采购记录
   */
  batchDelete: (equipmentIds: string[]) => {
    return http.del<ApiRes<null>>('/equipment/procurement/batch', { data: equipmentIds })
  },

  /**
   * 导出设备采购记录
   */
  export: (params: EquipmentListReq) => {
    return http.get('/equipment/procurement/export', params, { responseType: 'blob' })
  }
} 