/**  http 方法封装 */
import type { axiosApiType } from "@/libs/axios"
import LoadingBar from "@/plugins/loadingBar"
import { ElNotification, ElMessageBox, ElMessage } from "element-plus"
import { downloadFile } from "@/libs/tools"

/** ajax 请求 */
export const httpAjax = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    axiosApi(...nargs)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        resolve(err)
      })
  })
}
/**错误会提示 ajax 请求 */
export const httpAjaxErrMsg = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    axiosApi(...nargs)
      .then(res => {
        const data = res.data as apiResDataType
        if (data.code !== 200) {
          ElMessage({
            showClose: true,
            message: data.message,
            type: "error"
          })
        }
        resolve(res.data)
      })
      .catch(err => {
        ElMessage({
          showClose: true,
          message: err.message,
          type: "error"
        })
        resolve(err)
      })
  })
}
/** 带loadingBar ajax 请求 */
export const httpAjaxLoading = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    LoadingBar.loading(true)
    axiosApi(...nargs)
      .then(res => {
        LoadingBar.loading(false)
        resolve(res.data)
      })
      .catch(err => {
        LoadingBar.loading(false)
        resolve(err)
      })
  })
}
/**
 * 带loadingBar并且错误会提示 ajax 请求
 */
export const httpAjaxLoadingErrMsg = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    LoadingBar.loading(true)
    axiosApi(...nargs)
      .then(res => {
        LoadingBar.loading(false)
        const data = res.data as apiResDataType
        if (data.code !== 200) {
          ElMessage({
            showClose: true,
            message: data.message,
            type: "error"
          })
        }
        resolve(res.data)
      })
      .catch(err => {
        LoadingBar.loading(false)
        ElMessage({
          showClose: true,
          message: err.message,
          type: "error"
        })
        resolve(err)
      })
  })
}
/** 右上角弹窗提醒 ajax请求 */
export const httpAjaxCrud = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    LoadingBar.loading(true)
    axiosApi(...nargs)
      .then(res => {
        LoadingBar.loading(false)
        const data = res.data as apiResDataType
        if (data.code === 200) {
          ElNotification({
            type: "success",
            title: "成功",
            message: data.message,
            position: "top-right",
            duration: 3000
          })
        } else {
          ElNotification({
            type: "error",
            title: "失败",
            message: data.message,
            position: "top-right",
            duration: 3000
          })
        }
        resolve(data)
      })
      .catch(err => {
        LoadingBar.loading(false)
        ElNotification({
          type: "error",
          title: "失败",
          message: err.message,
          position: "top-right",
          duration: 3000
        })
        resolve(err)
      })
  })
}

/** 中间弹窗提醒 ajax请求 */
export const httpAjaxAlert = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P): Promise<apiResDataType> {
  return new Promise(resolve => {
    LoadingBar.loading(true)
    axiosApi(...nargs)
      .then(res => {
        LoadingBar.loading(false)
        const data = res.data as apiResDataType
        if (data.code === 200) {
          ElMessageBox.alert(data.message, "提示", {
            confirmButtonText: "关闭",
            type: "success"
          })
        } else {
          ElMessageBox.alert(data.message, "提示", {
            confirmButtonText: "关闭",
            type: "error"
          })
        }
        resolve(data)
      })
      .catch(err => {
        LoadingBar.loading(false)
        ElMessageBox.alert(err.message, "提示", {
          confirmButtonText: "关闭",
          type: "error"
        })
        resolve(err)
      })
  })
}

/** 下载 接口 文件名字从content-disposition拿*/
export const httpAjaxDownload = function <T extends axiosApiType, P extends Parameters<T>>(axiosApi: T, ...nargs: P) {
  LoadingBar.loading(true)
  axiosApi(...nargs)
    .then(res => {
      LoadingBar.loading(false)
      const filename: undefined | string = res.headers["content-disposition"]?.split("=")[1]
      if (filename) {
        downloadFile(res.data, decodeURIComponent(filename))
      } else {
        ElMessage({
          showClose: true,
          message: "未获取到文件名",
          type: "error"
        })
      }
    })
    .catch(err => {
      LoadingBar.loading(false)
      ElMessage({
        showClose: true,
        message: err.message,
        type: "error"
      })
    })
}
