import md5 from 'js-md5'
import dayjs from 'dayjs'
import * as qiniu from 'qiniu-js'

/**
 * md5 加密
 * @param val string
 * @returns strng
 */
export const fnMd5 = (val: string) => md5(val)

/**
 * 判断对象是否为空
 * @param obj Object
 * @returns boolean
 */
export const isEmptyObject = (obj: object) => Object.keys(obj).length > 0

/**
 * 时间的处理
 * @param date new Date
 * @returns string
 */
export const dayJs = (date: Date | string) => dayjs(date)

/**
 *
 * @param menuList 菜单列表
 * @param result 输出得结果
 * @param parent 子菜单
 * @returns object
 */
export const getAllBreadcrumbList = (menuList: any, result: { [key: string]: any } = {}, parent = []) => {
  for (const item of menuList) {
    result[item.T_permission.trim()] = [...parent, item]
    if (item.Children) getAllBreadcrumbList(item.Children, result, result[item.T_permission])
  }
  return result
}

const path = {
  home: ['/home'],
  roles: ['/roles'],
  users: ['/users'],
  salary: ['/salary'],
  salarys: ['/salaryCount', '/salaryMy'],
  workAttendance: ['/leave', '/myLeave', '/myOvertime', '/overtime', '/recordsFinance'],
  records: ['/records']
}

const permissionPath = (permission: string): string => {
  if (path.roles.includes(permission)) {
    return '/account/roles'
  } else if (path.users.includes(permission)) {
    return '/account/users'
  } else if (path.salarys.includes(permission)) {
    return '/salary'
  } else if (path.salary.includes(permission)) {
    return '/salary/salary'
  } else if (path.workAttendance.includes(permission)) {
    return '/workAttendance'
  } else if (path.records.includes(permission)) {
    return '/workAttendance/records'
  }
  return '/home'
}

const get_name = (name: string): string => {
  if (/\//.test(name)) {
    return name.split('/')[1]
  }
  return name
}

const functionucFirst = (str: string) => str[0].toUpperCase() + str.slice(1)

/**
 * 将扁平化的数组转换成带有meta component字段的新数组
 * @param menuList []
 */
export const AddRouterMeta = (menuList: any) => {
  let reg = /^[a-zA-Z]+$/
  menuList.forEach((menu: any) => {
    if (menu?.Children) {
      AddRouterMeta(menu?.Children)
    }
    menu.path = menu.T_permission
    menu.name = get_name(menu.T_permission)
    menu.meta = {
      icon: reg.test(menu.T_icon) ? menu.T_icon : menu.T_icon.slice(1),
      title: menu.T_name,
      isKeepAlive: true
    }
    menu.component = permissionPath(menu.T_permission) + '/' + functionucFirst(menu.name)
  })
  return menuList
}

/**
 * tree to flat
 * @param menuList []
 */
export const flatMenuListGet = (menuList: any) => {
  const result = []
  for (let menu of menuList) {
    if (menu.Children) {
      let res: any = flatMenuListGet(menu.Children)
      result.push(...res)
    } else {
      result.push({ ...menu })
    }
  }
  return result
}

/**
 * 分钟转小时
 * @param time
 */
export const getFormatDuration = (time: number) => {
  let hour = 0
  let minute = 0
  let result = ''
  if (time >= 60) {
    hour = time / 60
    minute = time % 60
  } else {
    result = '' + time + '分'
  }
  if (minute > 0) {
    result = '' + parseInt(minute + '') + '分' + result
  }
  if (hour > 0) {
    result = '' + parseInt(hour + '') + '小时' + result
  }
  return result
}

/**
 *
 * @param {*} file 上传的文件
 * @param {*} token 上传需要的token
 */
export const qiniuUpLoadFun = (file: any, token: any) => {
  //七牛上传方法
  return new Promise(function (resolve) {
    let timestamp = new Date().valueOf()
    let params = {
      file: file, //要上传的文件
      key: 'paper_file/' + timestamp + file.name, //自定义文件地址
      token: token,
      config: {
        useCdnDomain: false,
        // region: undefined,
        region: qiniu.region.z2,
        domin: 'https://qiniu.region.z2',
        chunkSize: 1000
      },
      putExtra: {
        fname: file.name,
        params: {},
        mimeType: [] || null
      }
    }
    console.log('domin')
    resolve(params)
  })
}
export function upLoadQiniu(params: any) {
  let observable = qiniu.upload(params.file, params.key, params.token, params.putExtra, params.config)
  let observer = {
    next(res: any) {
      // console.log(res);
      console.log('上传中', res)
    },
    error(err: any) {
      console.log(err)
    },
    complete(res: any) {
      // let url = 'http://xxxxxx.cn/' + res.key
      // layer.close(loading) //关闭loading效果
      console.log(res)
      // resolve(url)
    }
  }
  observable.subscribe(observer)
}

interface iconsType {
  [propName: string]: string | number
}

export const icons: iconsType = {
  ue7d1: '\ue7d1',
  ue63e: '\ue63e',
  ue603: '\ue603',
  ue636: '\ue636',
  ue661: '\ue661',
  ue648: '\ue648',
  ue6a1: '\ue6a1',
  ue645: '\ue645',
  ue619: '\ue619',
  ue627: '\ue627',
  ue64c: '\ue64c',
  ue6aa: '\ue6aa',
  ue690: '\ue690',
  ue663: '\ue663',
  ue7cd: '\ue7cd',
  ue831: '\ue831'
}
