import {
  getAddress
} from "@/api/getAddress.js";
import {
  getUserArea
} from "@/api/base.js";


// 毫秒转时长 
export function formatDuring(mss) {
  var days = parseInt(mss / (1000 * 60 * 60 * 24));
  var hours = parseInt((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = parseInt((mss % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = (mss % (1000 * 60)) / 1000;
  if (days) {
    return days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
  } else if (hours) {
    return hours + "小时" + minutes + "分钟" + seconds + "秒";
  } else if (minutes) {
    return minutes + "分钟" + seconds + "秒";
  } else {
    return seconds + "秒";
  }
}

// 转换时间格式
export function timeValidate(date, type) {
  const time = date ? new Date(date) : new Date()
  const yyyy = time.getFullYear()
  const MM = (time.getMonth() + 1).toString().padStart(2, 0)
  const dd = time.getDate().toString().padStart(2, '0')
  const hh = time.getHours().toString().padStart(2, '0')
  const mm = time.getMinutes().toString().padStart(2, '0')
  const ss = time.getSeconds().toString().padStart(2, '0')
  if (type == 'ymd') {
    return `${yyyy}-${MM}-${dd}`;
  }
  if (type == 'md') {
    return `${MM}.${dd}`
  }
  return `${yyyy}-${MM}-${dd} ${hh}:${mm}:${ss}`
}


// 获取当前近多少天  7后7天  -7 前五天 
export function getRecentDay(n) {
  var currentDate = new Date();
  var preDate = new Date(currentDate.getTime() + n * 24 * 3600 * 1000)
  let year = preDate.getFullYear()
  let mon = preDate.getMonth() + 1
  let day = preDate.getDate()
  let s = year + '-' + (mon < 10 ? ('0' + mon) : mon) + '-' + (day < 10 ? ('0' + day) : day)
  return s
}

// 获取时间的第一天 和最后一天
export function getThistWeekDare(type) {
  var dateArr = []
  var date = new Date()
  let year = date.getFullYear() //当年
  let m = date.getMonth() + 1 //当月
  let mon = m < 10 ? ("0" + m) : m;
  if (type == 'week') { //周
    date.setDate(date.getDate() - date.getDay() + 1); // 本周一的日期
    let first = year + '-' + mon + '-' + date.getDate()
    date.setDate(date.getDate() - date.getDay() + 7); // 本周日的日期
    let last = year + '-' + mon + '-' + date.getDate()
    dateArr = [first,last]
  } else if (type == 'month') { //月
    let d = new Date(year, m, 0).getDate() //获取当月最后一天
    let day = d < 10 ? "0" + d : d
    dateArr = [[year, mon, '01'].join('-'), [year, mon, day].join('-')]
  } else if (type == 'quarter') { //季度
    let quarter = Math.floor(mon / 3)//从1开始到4
    let quarterArr = []
    if(quarter == 1) quarterArr = [new Date(year,0,1),new Date(year,2,31)];
    if(quarter == 2) quarterArr = [new Date(year,3,1),new Date(year,5,30)];
    if(quarter == 3) quarterArr = [new Date(year,6,1),new Date(year,8,30)];
    if(quarter == 4) quarterArr = [new Date(year,9,1),new Date(year,11,31)];
    dateArr = [timeValidate(quarterArr[0],'ymd'),timeValidate(quarterArr[1],'ymd')]
  }
  return dateArr
}

// 获取n近7月  7后7 -7 前 
export function getnRencebtMonth(n) {
  let date = new Date();
  date.setMonth(date.getMonth() - n)
  date.toLocaleDateString()
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m + ''
  return y + m
}
/**
 * 数据去重 相同数据值累加
 * @param {Object} array 数据
 */
export function setArray(array) {
  let newArr = []
  array.forEach(item => {
    const res = newArr.findIndex(ol => {
      //组织机构代码相同 并且报警类别相同
      return item.ssbmdm == ol.ssbmdm && item.bjlb == ol.bjlb
    })
    if (res !== -1) {
      newArr[res].sl = newArr[res].sl + item.sl
    } else {
      newArr.push(item)
    }
  })

  return newArr
}

/**
 * 合并数据 
 * @param {Object} array 数据
 */
export function hbArray(array, item1, item2, item3) {
  let newArr = []
  array.forEach(item => {
    const res = newArr.findIndex(ol => {
      //组织机构代码相同 并且报警类别相同
      return item.product == ol.product
    })
    if (res !== -1) {
      newArr[res][item1] = newArr[res][item1] + item[item1]
      newArr[res][item2] = newArr[res][item2] + item[item2]
      newArr[res][item3] = newArr[res][item3] + item[item3]
    } else {
      newArr.push(item)
    }
  })

  return newArr
}
//时间格式
export function dateFormat(type, time) {
  let date
  if (time) {
    date = new Date(time);
  } else {
    date = new Date();
  }
  let year = date.getFullYear();
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  let day

  if (type == 'z') {
    //前一天日期
    day = date.getDate() - 1;
    day = day < 10 ? "0" + day : day;
    return `${year}-${month}-${day}`;
  } else if (type == 'all') {
    //格式化日期时间
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  } else {
    //当天日期
    day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    return `${year}-${month}-${day}`;
  }
  return day
}
//地址解析
export function getAddressApi(coords, fun) {
  let {
    jd,
    wd
  } = coords
  getAddress({
    jd,
    wd
  }).then(res => {
    fun(res)
  })
}
//数字超长处理
export function handleNum(num) {
  var data = 0
  if (num) {
    try {
      if (num * 1 > 100000) {
        data = (num / 10000).toFixed(0) + '万'
      } else {
        data = (num * 1).toFixed(0)
      }
    } catch (error) {
      data = 0
    }
  }

  return data
}
/**
 * 文件是否是图片
 * @param {*} val 
 */
export function IS_PNG(val) {
  return ['bmp', 'jpg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif', 'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo',
    'eps', 'ai', 'raw', 'wmf', 'webp', 'avif', 'apng'
  ].indexOf(val.toLowerCase()) !== -1
}
/**
 * 文件是否是音频
 * @param {*} val 
 */
export function IS_MP3(val) {
  return ['mp3', 'wav', 'wma', 'mp2', 'flac', 'midi', 'ra', 'ape', 'aac', 'cda', 'mov'].indexOf(val.toLowerCase()) !==
    -1
}

/**
 * 文件是否是视频
 * @param {*} val 
 */
export function IS_MP4(val) {
  return ['avi', 'wmv', 'mpeg', 'mp4', 'm4v', 'mov', 'asf', 'fiv', 'f4v', 'mvb', 'rm', '3gp', 'vob'].indexOf(val
    .toLowerCase()) !== -1
}
/**
 * 获取用户所属区域数据
 * @param {*} fun 回调方法
 */
export function getUserAreaData(fun) {
  getUserArea().then(res => {
    if (res && res.jd && res.wd) {
      let centerCoord = [res.jd, res.wd]
      let leavel = res.level.slice(0, 1)
      let coorList = [...handelArr(res.dtm), ...handelArr(res.dtm1), ...handelArr(res.dtm2)]
      fun(coorList, centerCoord, leavel)
    }
  })
}

function handelArr(arr) {
  let brr = []
  if (arr && arr.length > 0) {
    let obj = {}
    let coords = "";
    for (let i = 0; i < arr.length; i++) {
      coords += arr[i] + ","
    }
    obj.coords = coords
    brr.push(obj)
  }
  return brr
}


/**
 * 时间  天数
 * @param {*} val 
 */
export function setEchartTime(val) {
  let date = new Date();
  let arrTime = [];
  if (val == 0) {
    for (let i = 0; i < 24; i++) {
      arrTime.push(i)
    }
  } else {
    for (let i = 0; i < val; i++) {
      let date1 = new Date(date.getTime() - i * 24 * 60 * 60 * 1000)
      arrTime.push(_setTime(date1))
    }
    arrTime.reverse()
  }
  return arrTime
}
//设置时间
function _setTime(date) {
  let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
  let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
  return `${month}-${day}`;
}
