import request from '@/axios'
import type { upAdmin, verify, mapSearch, searchCity } from './types'
import { REQUEST_BASE, REQUEST_GENERAL } from '@/constants'

/**
 * 获取管理员信息
 *
 * 本函数通过发送GET请求到'/info'端点，异步获取管理员信息
 * 不需要任何参数
 * 返回一个Promise对象，解析为IResponse类型，包含管理员信息
 */
export const getAdminInfo = (params?: any): Promise<IResponse> => {
  return request.get({ url: `${REQUEST_BASE}/info` ,params})
}

/**
 * 更新管理员信息
 *
 * 此函数发送一个POST请求到/update_admin端点，以更新管理员的信息
 * 它携带一个upAdmin类型的data对象作为请求的参数，用于指定更新的详细信息
 *
 * @param data 包含管理员更新信息的对象，类型为upAdmin
 * @returns 返回一个Promise，解析为IResponse类型，包含服务器的响应
 */
export const updateAdmin = (data: upAdmin): Promise<IResponse> => {
  return request.put({ url: `${REQUEST_BASE}/update_admin`, data })
}

export const upImage = (data: { pid: number; type: number; file: File }): Promise<IResponse> => {
  return request.post({
    url: `${REQUEST_BASE}/file/upload/${data.type}`,
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const upload = (data: any): Promise<IResponse> => {
  return request.post({
    url: `${REQUEST_BASE}/file/video_upload`,
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const ossUpload = (url, data): Promise<IResponse> => {
  return request.post({
    url: `${url}`,
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export const getCodeKey = (): Promise<IResponse> => {
  return request.get({ url: `${REQUEST_GENERAL}/verify_code` })
}
export const postVerify = (data: verify): Promise<IResponse> => {
  return request.post({ url: `${REQUEST_GENERAL}/verify`, data })
}
/**
 *
 * @param params pid: number, type: 1 | 2 | 3 //1：省市 2：省市区 0、3：省市区街道
 * @returns
 */
export const getCity = (params: searchCity): Promise<IResponse> => {
  return request.get({ url: `${REQUEST_BASE}/city`, params })
}
export const getMapSearch = (params: mapSearch): Promise<IResponse> => {
  // let url = `${window.location.protocol}//apis.map.qq.com/ws/place/v1/suggestion`
  // if (import.meta.env.VITE_NODE_ENV == 'development') {
  //   url = `/qqmap/place/v1/suggestion`
  // }
  return request.get({ url: `/qqmap/place/v1/suggestion`, params })
}
export const getConfigKey = (key: string): Promise<IResponse> => {
  return request.get({ url: `${REQUEST_BASE}/sys_config`, params: { key } })
}
