import moment from 'moment';
// 将后端返回的字段转换成树组件的ApiSelectTree的对应字段
import { ImpExcel, ExcelData, jsonToSheetXlsx } from '/@/components/Excel';
export const copyTransFun = (arr: any[]) => {
  arr.forEach((item) => {
    if (item.name) {
      item.label = item.name;
      delete item.name;
    }
    if (item.id) {
      item.value = item.id;
      delete item.id;
    }
    if (item.children) {
      if (item.children.length) {
        copyTransFun(item.children);
      }
    }
  });
};

// 将后端返回的字段转换成树组件的Tree的对应字段
export const copyTransTreeFun = (arr: any[]) => {
  arr.forEach((item) => {
    if (item.name) {
      item.title = item.name;
      delete item.name;
    }
    if (item.id) {
      item.key = item.id;
      delete item.id;
    }
    if (item.children) {
      if (item.children.length) {
        copyTransTreeFun(item.children);
      }
    }
  });
};

// 百度地图url
const ak = '7uOPPyAHn2Y2ZryeQqHtcRqtIY374vKa';

const register_BAI_DU_MAP_URL = (ak: string) => {
  return `https://api.map.baidu.com/getscript?v=3.0&ak=${ak}`;
};

const registerBaiDuMapGlLib = (ak: string) => {
  return `//api.map.baidu.com/getscript?type=webgl&v=1.0&ak=${ak}&services=&t=${Date.now()}`;
};

export const BAI_DU_MAP_TRACK_ANIMATION =
  '//mapopen.bj.bcebos.com/github/BMapGLLib/TrackAnimation/src/TrackAnimation.min.js';

export const BAI_DU_MAP_GL_LIB = registerBaiDuMapGlLib(ak);

export const BAI_DU_MAP_URL = register_BAI_DU_MAP_URL(ak);

// 数字加上每三位加上逗号
export function toThousands(num) {
  return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}

// 获取当前时间和24小时之前
export const getNowTime = (type) => {
  const yy = new Date().getFullYear();
  let MM = '';

  if (new Date(new Date().getTime()).getDate() == 1 && type == 2) {
    MM = new Date().getMonth() < 10 ? '0' + new Date().getMonth() : new Date().getMonth();
  } else {
    MM =
      new Date().getMonth() + 1 < 10
        ? '0' + (new Date().getMonth() + 1)
        : new Date().getMonth() + 1;
  }
  // alert(new Date().getMonth());
  let dd = '';
  if (type == 1) {
    dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate();
  }
  if (type == 2) {
    dd =
      new Date(new Date().getTime() - 86400000).getDate() < 10
        ? '0' + new Date(new Date().getTime() - 86400000).getDate()
        : new Date(new Date().getTime() - 86400000).getDate();
  }
  const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours();
  const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
  const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
  return yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss;
};

// 获取本月和上一月0是上一月，1是本月
export const getMonth = (type) => {
  if (type == 0) {
    function getNowMonth() {
      const date = new Date();
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      month = month > 9 ? month : '0' + month;
      const nowMonth = year + '-' + month;
      return nowMonth;
    }
    const arr = getNowMonth().split('-');
    const year = arr[0]; //获取当前日期的年份
    const month = arr[1]; //获取当前日期的月份

    let year2 = year;
    let month2 = parseInt(month) - 1;
    if (month2 == 0) {
      //1月的上一月是前一年的12月
      year2 = parseInt(year2) - 1;
      month2 = 12;
    }

    if (month2 < 10) {
      //10月之前都需要补0
      month2 = '0' + month2;
    }
    const preMonth = year2 + '-' + month2;
    return preMonth;
  }
  if (type == 1) {
    const date = new Date();
    const year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month > 9 ? month : '0' + month;
    const nowMonth = year + '-' + month;
    return nowMonth;
  }
};
export const getYear = (type) => {
  const timestamp = Date.parse(new Date());
  const date = new Date(timestamp);
  //获取年份
  const thieYear = date.getFullYear();
  if (type == 0) {
    return thieYear - 1 + '-01-01';
  }
  if (type == 1) {
    return thieYear + '-01-01';
  }
};
export const getYearNo = (type) => {
  const timestamp = Date.parse(new Date());
  const date = new Date(timestamp);
  //获取年份
  const thieYear = date.getFullYear();
  if (type == 0) {
    return thieYear - 1 + '';
  }
  if (type == 1) {
    return thieYear + '';
  }
};
// 导出数据
export const exportData = (exportColumns, exportdataSource, nameTitle) => {
  let header = {};
  let data = [];
  exportColumns.forEach((element) => {
    if (element.title != '操作') {
      header['s' + element.key] = element.title ? element.title : 'time';
    }
  });
  console.log(exportdataSource)
  exportdataSource.forEach(element => {
    let obj = {}
    for (const key in element) {
      obj['s' + key] = element[key]
    }
    data.push(obj)
  });
  // data = exportdataSource;
  jsonToSheetXlsx({
    data,
    header: header,
    filename: nameTitle,
  });
}
// 获取年月日
export const currentDay = () => {
  const now = moment();
  // 获取年份
  const year = now.year();
  // 获取月份（注意：月份是从0开始的，所以需要加1）
  const month = (now.month() + 1) < 10 ? '0'+(now.month() + 1) : (now.month() + 1) ;
  // 获取日期
  const day = now.date()<10?'0'+ now.date() : now.date();
  let currentDays = '_'+year+month+day
  return currentDays
}