import { request } from '@/utils/request'
import type { OrderDetail, OrderSubmitReq, PageResult } from '@/types'

/**
 * 提交订单
 * 后端：POST /api/orders
 * 响应：{ orderNo: string }
 */
export function submitOrderApi(data: OrderSubmitReq) {
  return request<{ orderNo: string }>({
    url: '/api/orders',
    method: 'POST',
    data
  })
}

export function getOrderDetailApi(id: number) {
  return request<OrderDetail>({
    url: `/api/orders/${id}`,
    method: 'GET'
  })
}

export function getMyOrdersApi(params: { current?: number; size?: number; status?: number }) {
  return request<PageResult<OrderDetail>>({
    url: '/api/orders',
    method: 'GET',
    params
  })
}

export function payOrderApi(id: number) {
  return request<void>({
    url: `/api/orders/${id}/pay`,
    method: 'POST'
  })
}

export function cancelOrderApi(id: number) {
  return request<void>({
    url: `/api/orders/${id}/cancel`,
    method: 'POST'
  })
}

export function receiveOrderApi(id: number) {
  return request<void>({
    url: `/api/orders/${id}/receive`,
    method: 'POST'
  })
}

export function refundOrderApi(id: number, reason?: string) {
  return request<void>({
    url: `/api/orders/${id}/refund`,
    method: 'POST',
    params: { reason }
  })
}
