import service from './service'

import config from './config'

import { AxiosResponse } from './types'
import { ElMessage } from 'element-plus'

const { defaultHeaders } = config
const downLoadErrorMsg = {
  "1001":"下载失败,文件路径不存在",
  "1002":"下载失败,未找到文件信息",
  "1003":"下载失败,请联系管理员",
}

const request = (option: AxiosConfig) => {
  const { url, method, params, data, headersType, responseType } = option
  console.log(url,"其余的url");
  return service.request({
    url: url,
    method,
    params,
    data,
    responseType: responseType,
    headers: {
      'Content-Type': headersType || defaultHeaders
    }
  })
}

export default {
  get: <T = any>(option: AxiosConfig) => {
    return request({ method: 'get', ...option }) as Promise<IResponse<T>>
  },
  post: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option }) as Promise<IResponse<T>>
  },
  postJson: <T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option, headersType: 'application/json' }) as Promise<
      IResponse<T>
    >
  },
  delete: <T = any>(option: AxiosConfig) => {
    return request({ method: 'delete', ...option }) as Promise<IResponse<T>>
  },
  put: <T = any>(option: AxiosConfig) => {
    return request({ method: 'put', ...option }) as Promise<IResponse<T>>
  },
  cancelRequest: (url: string | string[]) => {
    return service.cancelRequest(url)
  },
  cancelAllRequest: () => {
    return service.cancelAllRequest()
  },
  doImport: <T = any>(option: AxiosConfig) => {
    return request({
      method: 'post',
      ...option,
      headersType: 'multipart/form-data'
    }) as Promise<IResponse<T>>
  },
  doExport: (option: AxiosConfig) => {
    request({
      method: 'post',
      headersType: 'application/json',
      responseType: 'blob',
      ...option
    }).then((res: AxiosResponse) => {
      const { data, headers } = res;
      if(headers['status'] == 500){// 说明此时下载文件有错误，需要获取WARN和ERROR的值
        const code = headers['error']?headers['error'].substring(headers['error'].length-4):'1003';
        switch(code){
          case "1001":
          case "1002":
            ElMessage.error(downLoadErrorMsg[code]);
            break;
          default:
            ElMessage.error(downLoadErrorMsg['1003']);
            break;
        }
        return;
      }
      const fileName:string = headers['content-disposition']?.replace(/\w+;\s*filename=(.*)/, '$1');
      const blob = new Blob([data], {
        type: headers['content-type']
      })
      const dom = document.createElement('a')
      const _url = window.URL.createObjectURL(blob)
      dom.href = _url
      dom.download = decodeURI(fileName)
      dom.style.display = 'none'
      document.body.appendChild(dom)
      dom.click()
      ;(dom.parentNode as any).removeChild(dom)
      window.URL.revokeObjectURL(_url)
    })
  },
  //预览
  doPreviewFile:<T = any>(option: AxiosConfig) => {
    return request({ method: 'post', ...option,responseType: 'blob', headersType: 'application/json' }) as Promise<IResponse<T>>
  },
}
