import { domShowFu, progressDomFu } from '@/utils/domShow'
import http from '@/utils/http'
import axios from 'axios'
import store from '..'

/**
 * 用户登录接口
 */
export const userLoginAPI = (data: any) => {
  return http.post('admin/login', { ...data })
}

/**
 * 修改密码接口
 */
export const passWordEditAPI = (data: any) => {
  return http.post('sys/user/updatePwd', { ...data })
}

/**
 * 获取验证码
 */
export const API_LoginGetCode = () => {
  return http.get('admin/getRandCode', { responseType: 'blob' })
}

// 上传文件自动归类
type FileObjType = {
  [key: string]: string[]
}
export const fileImgArr = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'tiff', 'webp', 'svg', 'psd', 'ai']
export const fileVideoArr = ['mp4', 'mov', 'avi', 'mkv', 'flv', 'wmv', 'mpeg', 'webm']

const fileObj: FileObjType = {
  img: fileImgArr,
  video: fileVideoArr,
  model: ['4dage', 'obj', 'stl', 'fbx', 'gltf', '3ds', 'blend', 'dae', 'step'],
  audio: ['mp3', 'wav', 'flac', 'aac', 'ogg', 'm4a'],
  doc: ['pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'txt', 'csv', 'md', 'html']
  // 'other':[]
}

export const fileTypeRes = (fileName: string) => {
  const txtArr = fileName.split('.')
  const txt = txtArr[txtArr.length - 1]

  let type = 'other'

  for (const k in fileObj) {
    if (fileObj[k].includes(txt)) type = k
  }
  return type
}

const CancelToken = axios.CancelToken
/**
 * 上传封面图和附件
 */
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 + '%')
    },
    // 取消上传
    cancelToken: new CancelToken(function executor(c) {
      store.dispatch({
        type: 'layout/closeUpFile',
        payload: { fu: c, state: true }
      })
    })
  })
}

/**
 * 获取用户权限管理
 */
export const API_getRoleArr = (roleId: number) => {
  return http.get(`sys/resource/getTreeRolePermissions/${roleId}`)
}
