
/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone (source) {
  if (!source && typeof source !== 'object') {
    throw new Error('error arguments', 'deepClone')
  }
  const targetObj = source.constructor === Array ? [] : {}
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === 'object') {
      targetObj[keys] = deepClone(source[keys])
    } else {
      targetObj[keys] = source[keys]
    }
  })
  return targetObj
}

/**
 * @description:封装方法：获取今天、昨天、明天
 * @param {*} dates  0 今天   -1   -2
 * @returns
 */
export function getDay (dates) {
  var dd = new Date()
  var n = dates || 0
  dd.setDate(dd.getDate() + n)
  var y = dd.getFullYear()
  var m = dd.getMonth() + 1
  var d = dd.getDate()
  m = m < 10 ? '0' + m : m
  d = d < 10 ? '0' + d : d
  var day = y + '-' + m + '-' + d
  return day
}

/**
 * @description: 获取某月的起始、结束日期
 * @param {*} val 年月 2023-01
 * @returns
 */
export function getMonday (val) {
  let date = new Date(val)
  let new_year = date.getFullYear() // 取当前的年份
  let month = date.getMonth()
  let new_month = month + 1 // 取当前的月份
  let mon = ''
  let day = ''
  if (month > 12) {
    new_month -= 12 // 月份减
    new_year++ // 年份增
  }
  let firstDay = new Date(new_year, new_month, 1) // 取当年当月中的第一天
  let lastDay = new Date(firstDay.getTime() - 1000 * 60 * 60 * 24).getDate() // 获取当月最后一天日期
  if (firstDay.getMonth() < 10) {
    mon = '0' + firstDay.getMonth()
  } else {
    mon = firstDay.getMonth()
  }
  if (lastDay < 10) {
    day = '0' + lastDay
  } else {
    day = lastDay
  }
  let startDate =
    firstDay.getFullYear() + '-' + mon + '-' + '0' + firstDay.getDate()
  let endDate = firstDay.getFullYear() + '-' + mon + '-' + day
  let dateRange = [startDate, endDate]
  return dateRange
}

/**
 * @description: 获取本周起始日期
 * @param {*} type s 周开始  e周结束
 * @param {*} dates  0 本周
 * @returns
 */
export function getDate (type, dates = 0) {
  var now = new Date()
  var nowTime = now.getTime()
  var day = now.getDay()
  var longTime = 24 * 60 * 60 * 1000
  var n = longTime * 7 * (dates || 0)
  if (type == 's') {
    var dd = nowTime - (day - 1) * longTime + n
  }
  if (type == 'e') {
    var dd = nowTime + (7 - day) * longTime + n
  }
  dd = new Date(dd)
  var y = dd.getFullYear()
  var m = dd.getMonth() + 1
  var d = dd.getDate()
  m = m < 10 ? '0' + m : m
  d = d < 10 ? '0' + d : d
  var day = y + '-' + m + '-' + d
  return day
}

/**
 * @description 获取最近 7天日期
 * @returns days
 */
export function getRecent7Days () {
  let days = []
  for (let i = 0; i <= 24 * 6; i += 24) {
    // 使用当天时间戳减去以前的时间毫秒（小时 * 分 * 秒 * 毫秒）
    let dateItem = new Date(Date.now() - i * 60 * 60 * 1000)
    let y = dateItem.getFullYear()
    let m = dateItem.getMonth() + 1
    let d = dateItem.getDate()
    m = addDate0(m)
    d = addDate0(d)
    let valueItem = y + '-' + m + '-' + d
    days.push(valueItem)
  }
  return days
}
// 给日期拼接 0
function addDate0 (time) {
  if (time.toString().length === 1) {
    time = '0' + time.toString()
  }
  return time
}

//补零 000
export function pushZero (num) {
  let count = 3 - String(num).length
  if (count > 0) {
    return new Array(count).fill('0').join('') + num
  }
  return num
}

//表格分页 序号连续
export function tableIndex (index, page, size) {
  return index + 1 + size * (page - 1)
}
