import { request } from '@/utils/request'

// 订单 类型定义
export interface Order {
  id?: number

  orderNo?:  string

  userId?: number

  productId?: number

  quota?: number

  totalPrice?:  string

  unitPrice?:  string

  createTime?:  string

  updateTime?:  string

  createBy?: number

  updateBy?: number

  payPhotoUrl?: string

  payStatus?: number

  deleted?: number

}

// 订单 API
export const orderApi = {
  // 分页查询
  page(params: { page: number; pageSize: number; id?: number }) {
    return request({ url: '/biz/order/page', method: 'get', params })
  },

  // 获取详情
  detail(id: number) {
    return request({ url: `/biz/order/${id}`, method: 'get' })
  },

  // 新增
  create(data: Order) {
    return request({ url: '/biz/order', method: 'post', data })
  },

  // 修改
  update(data: Order) {
    return request({ url: '/biz/order', method: 'put', data })
  },

  audit(data: Order) {
    return request({ url: '/biz/order/audit', method: 'put', data })
  },

  // 删除
  delete(ids: number[]) {
    return request({ url: `/biz/order/${ids.join(',')}`, method: 'delete' })
  },

  // 导出
  export(params?: { ids?: number[]; id?: number }) {
    const p: Record<string, any> = {}
    if (params?.ids?.length) p.ids = params.ids.join(',')
    if (params?.id !== undefined && params?.id !== null) p.id = params.id
    return request({ url: `/biz/order/export`, method: 'get', params: p, responseType: 'blob' })
  },

  // 导入
  importData(file: File) {
    const formData = new FormData()
    formData.append('file', file)
    return request<{ success: number; fail: number; errors: string[] }>({
      url: `/biz/order/import`,
      method: 'post',
      data: formData,
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },

  // 下载导入模板
  downloadTemplate() {
    return request({ url: `/biz/order/template`, method: 'get', responseType: 'blob' })
  }
}
