import { domShowFu, fileDomInitialFu, progressDomFu } from '@/utils/domShow'
import http from '@/utils/http'
import { MessageFu } from '@/utils/message'

/**
 * 获取当前日期的配置信息（发送当天日期拿不可预约日期）
 */
export const A3_APIgetInfoByDay = (day: string) => {
  return http.get(`wx/book/config?date=${day}`)
}

/**
 * 选择日期-上午-下午列表
 */
export const A3_APIgetList = () => {
  return http.get('wx/subject/config')
}

/**
 * 获取课堂列表（选择课程）
 */
export const A4_APIgetList = () => {
  const obj = { pageNum: 1, pageSize: 99999 }

  return http.post('wx/subject/pageList', obj)
}

/**
 * 获取课堂详情-通过id
 */
export const A5_APIgetInfo = (id: number) => {
  return http.get(`wx/subject/detail/${id}`)
}

/**
 * 课堂预约 获取 模板下载地址
 */
export const A5_APIgetTemplateUrl = (id: number) => {
  return http.get(`wx/subject/template/${id}`)
}

/**
 * 课堂预约 提交
 */
export const A5_APIsave = (data: any) => {
  return http.post('wx/subject/apply', data)
}

/**
 * 团体认证 - 检查
 */
export const A4_APIcheck = () => {
  return http.get('wx/auth/check')
}

/**
 * 团体认证 - 提交
 */
export const A8_APIsave = (data: any) => {
  return http.post('wx/auth', data)
}

/**
 * 团体认证 - 邀请码认证
 */
export const A8_APIcodeSave = (data: any) => {
  return http.post('wx/invitation/auth', data)
}

/**
 * 我的申请-预约申请
 */
export const A6_APIgetListYuYue = () => {
  return http.get('wx/my/apply')
}

/**
 * 我的申请-认证申请
 */
export const A6_APIgetListRenZheng = () => {
  return http.get('wx/my/auth')
}

/**
 * 我的申请-取消认证
 */
export const A6_APIcancelAuth = (id: number, type: 'apply' | 'auth') => {
  return http.get(`wx/my/${type}/cancel/${id}`)
}

// ------------------展馆------------------------

/**
 * 获取当前日期的配置信息（发送当天日期拿不可预约日期）
 */
export const B1_APIgetInfoByDay = (day: string) => {
  return http.get(`exhibition/book/config?date=${day}`)
}

/**
 * 获取预约日期列表
 */
export const B3_APIgetList = () => {
  return http.get('exhibition/subject/config')
}

/**
 * 提交
 */
export const B4_APIsave = (data: any) => {
  return http.post('exhibition/apply', data)
}

/**
 * 我的预约列表
 */
export const B2_APIgetList = () => {
  return http.get('exhibition/my/apply')
}

/**
 * 撤回预约
 */
export const B2_APIcancel = (id: number) => {
  return http.get(`exhibition/my/apply/cancel/${id}`)
}

/**
 * 用code获取token
 */
export const API_login = (code: string) => {
  return http.get(`show/wx/mpLogin?code=${code}`)
}

/**
 * 通过id获取配置
 */
export const API_getConfigById = (id: number) => {
  return http.get(`wx/dict/detail/${id}`)
}

// --------------------------------------------

/**
 * 上传封面图和附件
 */
export const API_upFile = (data: any, url: string) => {
  domShowFu('#UpAsyncLoding', true)

  return http.post(url, data, {
    timeout: 0,
    // 显示进度条
    onUploadProgress: (e: any) => {
      const complete = (e.loaded / e.total) * 100 || 0
      progressDomFu(complete + '%')
    }
  })
}

export const upFileFu = async (
  e: React.ChangeEvent<HTMLInputElement>,
  url: string, //接口地址
  size: number, //文件大小限制
  type: string[], //文件类型限制
  errTit: string, //文件类型校验不通过的提示
  callFu: (data: any) => void, //回调函数
  formObj: any //给后端的其他信息
) => {
  if (e.target.files) {
    // 拿到files信息
    const filesInfo = e.target.files[0]
    // 校验格式
    if (!type.includes(filesInfo.type)) {
      e.target.value = ''
      return MessageFu.warning(`只支持${errTit}格式!`)
    }
    // 校验大小
    if (filesInfo.size > size * 1024 * 1024) {
      e.target.value = ''
      return MessageFu.warning(`最大支持${size}M!`)
    }
    // 创建FormData对象
    const fd = new FormData()
    if (formObj) {
      for (const k in formObj) {
        fd.append(k, formObj[k])
      }
    }
    fd.append('file', filesInfo)

    e.target.value = ''

    // for (var [a, b] of fd.entries()) {
    //   console.log(a, b)
    // }

    try {
      const res = await API_upFile(fd, url)
      if (res.code === 0) {
        MessageFu.success('上传成功!')
        callFu(res.data)
      }
      fileDomInitialFu()
    } catch (error) {
      fileDomInitialFu()
    }
  }
}

// --------------------------新模块预约-----------------
/**
 * 课程预约-获取预约列表
 */
export const APIN_getList = (num?: number) => {
  let url = 'wx/v2/book/config'

  if (num) url += `?limit=${num}`

  return http.get(url)
}
