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

// 获取场地信息
export const informationQueryService = (data) =>
  request.post('/information/query', data)

// 添加场地信息
export const informationCreateService = (data) =>
  request.post('/information/create', data)

// 修改场地信息
export const informationUpdateService = (data) =>
  request.post('/information/update', data)

// 修改场地价格
export const moneyUpdateService = (data) =>
  request.post('/information/money', data)

// 获取场地的分类列表
export const typeQueryService = () => request.get('/information/classify')

// 获取场地二维码信息
export const codeQueryService = () => request.get('/information/code')

// 上传场地照片
export const informationImgUploadService = (formData) =>
  request.post('/information/img', formData)

// 获取场地的所有分类
export const typeListService = () => request.get('/information/types')

// 移除场地信息
export const informationRemoveService = (id) =>
  request.get('/information/remove?id=' + id)

// 下载二维码图片
export const QRDownload = async (id, name) => {
  try {
    const response = await axios.get(baseURL + '/information/qr?id=' + id, {
      responseType: 'blob'
    })
    // 创建下载链接
    const url = window.URL.createObjectURL(response.data)
    const a = document.createElement('a')
    a.href = url
    a.download = name + '.png'
    a.click()

    // 释放 URL 对象
    window.URL.revokeObjectURL(url)
  } catch (error) {
    console.error('下载出错:', error)
    alert('文件下载失败，请重试')
  }
}

// 获取某个场馆下的所有场地信息
export const informationListService = (id) =>
  request.get('/information/detail?type_id=' + id)

// 获取某个场地的可用时间集合
export const timeUseService = (data) =>
  request.post('/information/condition', data)
