/**
 * 报名相关API接口
 */

import request from '@/utils/request.js'

export default {
  /**
   * 提交报名订单
   * @param {Object} data 报名数据 { competitionId, projectIds, athleteIds, contactPhone, totalAmount }
   * @returns {Promise}
   */
  submitRegistration(data) {
    // 处理数组参数：将数组转换为逗号分隔的字符串
    const formattedData = {
      orderNo: data.orderNo,
      competitionId: data.competitionId,
      projectIds: Array.isArray(data.projectIds) ? data.projectIds.join(',') : data.projectIds,
      athleteIds: Array.isArray(data.athleteIds) ? data.athleteIds.join(',') : data.athleteIds,
      contactPhone: data.contactPhone,
      totalAmount: data.totalAmount
    }

    console.log('=== API层转换后的数据 ===')
    console.log('订单号:', formattedData.orderNo)
    console.log('转换前 projectIds:', data.projectIds)
    console.log('转换后 projectIds:', formattedData.projectIds)
    console.log('转换前 athleteIds:', data.athleteIds)
    console.log('转换后 athleteIds:', formattedData.athleteIds)
    console.log('最终发送到后端的完整数据:', formattedData)

    return request.post('/martial/registrationOrder/submit', formattedData)
  },

  /**
   * 获取报名订单列表
   * @param {Object} params 查询参数 { current, size, status }
   * @returns {Promise}
   */
  getRegistrationList(params = {}) {
    return request.get('/martial/registrationOrder/list', {
      current: params.current || 1,
      size: params.size || 10,
      ...params
    })
  },

  /**
   * 获取报名订单详情
   * @param {String|Number} id 订单ID
   * @returns {Promise}
   */
  getRegistrationDetail(id) {
    return request.get('/martial/registrationOrder/detail', { id })
  },

  /**
   * 取消报名
   * @param {String|Array} ids 订单ID或ID数组
   * @returns {Promise}
   */
  cancelRegistration(ids) {
    return request.post('/martial/registrationOrder/remove', {
      ids: Array.isArray(ids) ? ids.join(',') : ids
    })
  }
}
