import {RcFile} from "antd/es/upload/interface";
import {message} from "antd";

const file_type_list = [
  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', //xlsx
  'application/vnd.ms-excel', //xls
  'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
  'text/plain', //text
  'application/pdf', //pdf
  'application/json', //json
  'application/msword', //doc
]
export const parseIds = (data: string | undefined)=>{
  console.log(555, data)
  if (data === undefined || data === '') {
    console.log(88888, data)
    return []
  } else {
    return data.split(",").map((item)=>{return parseInt(item)})
  }
}

export const handleOneFile: any = (values: any, name: string)=>{
  if (values[name] && values[name].length) {
    // 有文件
    if(values[name][0].status === 'error'){
      // 第一个文件 上传失败
      delete values[name]
    } else {
      if (values[name][0]?.response && values[name][0]?.response?.data?.path) {
        values[name] = values[name][0]?.response?.data?.path
      } else {
        delete values[name]
      }
    }
  } else {
    delete values[name]
  }
  return values
}

export const beforeUploadImage = (file: RcFile) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  console.log(file.type)
  if (!isJpgOrPng) {
    message.error('不是图片类型');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('大小超过2M');
  }
  return isJpgOrPng && isLt2M;
};

export const beforeUploadFile = (file: RcFile) => {
  const isLt50M = file.size / 1024 / 1024 < 50;
  if (!isLt50M) {
    message.error('大小超过50M');
  }
  return isLt50M;
};

export const imageInit = (host: string, data: any)=>{
  if (data) {
    return [{uid: '-1', name: 'image.png', status: 'done', url: host+data,}]
  } else {
    return []
  }
}

export const getEnum = (intl: any, data: Array<string>)=>{
  let temp = {}
  data.forEach((v)=>{
    let name = v.split('.enum.')[1]
    // @ts-ignore
    temp[name] = intl.formatMessage({id: `${v}`, defaultMessage: '$$$',})
  })
  return temp
}

export function addObjectByCode(obj: any, code: any, data:any) {
  if (obj.code === code) {
    if (!obj.children) {
      obj.children = [];
    }
    obj.children.push(data);
    return true;
  } else if (obj.children) {
    for (let i = 0; i < obj.children.length; i++) {
      const added = addObjectByCode(obj.children[i], code, data);
      if (added) {
        return true;
      }
    }
  }
  return false;
}

export function floatFormat(num: number, precision = 12) {
  return +parseFloat(num.toPrecision(precision));
}

export function getAllRouteNameTile(routes: any[], parent_name: string) {
  let temp: any[] = []
  routes.forEach((v)=>{
    temp.push({name: `${parent_name}.${v.name}`, title: v.title})
    let new_parent_name = `${parent_name}.${v.name}`
    if (v.routes.length) {
      temp = temp.concat(getAllRouteNameTile(v.routes, new_parent_name))
    }
  })
  return temp
}

export function dateFormat(date: string) : string{


  const datetime = new Date(date);
  const year = datetime.getFullYear();
  const month = (datetime.getMonth() + 1).toString().padStart(2, '0');
  const day = datetime.getDate().toString().padStart(2, '0');
  return `${year}-${month}-${day}`
}
