import dayjs from 'dayjs'
import { $message } from "@/main"
export function setDate (date) {
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  let d = date.getDate();
  m = m < 10 ? "0" + m : m;
  d = d < 10 ? "0" + d : d;
  return y + "-" + m + "-" + d;
}

export function getCurrentMonthFirst (date) {
  date.setDate(1);
  return date;
}

export function getCurrentMonthLast (date) {
  var currentMonth = date.getMonth();
  var nextMonth = ++currentMonth;
  var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
  var oneDay = 1000 * 60 * 60 * 24;
  return new Date(nextMonthFirstDay - oneDay);
}

export function getNowDateAndMonday (time) {
  let timestamp = new Date(time.replace(/-/g, "/")).getTime();
  let serverDate = new Date(time);
  if (serverDate.getDay() == 0) {
    timestamp -= 7 * 24 * 60 * 60 * 1000;
  }
  let mondayTime =
    timestamp - (serverDate.getDay() - 1) * 24 * 60 * 60 * 1000;

  let mondayData = new Date(mondayTime);
  //年
  let mondayY = mondayData.getFullYear();
  //月
  let mondayM =
    mondayData.getMonth() + 1 < 10
      ? "0" + (mondayData.getMonth() + 1)
      : mondayData.getMonth() + 1;
  //日
  let mondayD =
    mondayData.getDate() < 10
      ? "0" + mondayData.getDate()
      : mondayData.getDate();

  let str = mondayY + "-" + mondayM + "-" + mondayD;
  return str;
}
export function getNowDateAndSunday (time) {
  let timestamp = new Date(time.replace(/-/g, "/")).getTime();
  let serverDate = new Date(time);

  let num = 7 - serverDate.getDay();
  if (num == 7) {
    num = 0;
  }
  let sundayTiem = timestamp + num * 24 * 60 * 60 * 1000;
  let SundayData = new Date(sundayTiem);
  //年
  let tomorrowY = SundayData.getFullYear(); //月
  let tomorrowM =
    SundayData.getMonth() + 1 < 10
      ? "0" + (SundayData.getMonth() + 1)
      : SundayData.getMonth() + 1;
  //日
  let tomorrowD =
    SundayData.getDate() < 10
      ? "0" + SundayData.getDate()
      : SundayData.getDate();
  let str = tomorrowY + "-" + tomorrowM + "-" + tomorrowD;
  return str;
}

export function getWeekDay (str) {
  let week = new Date(str.replace(/-/g, "/")).getDay();
  week === 0 ? week = 7 : week
  return week
}

export function diffTimerFormMinute (classDate, startTime, endTime) {
  return Math.abs(dayjs(classDate + ' ' + startTime).diff(classDate + ' ' + endTime, 'Minute'))
}

export function addTimerFormMinute (classDate, startTime, time) {
  let endTime = dayjs(classDate + ' ' + startTime).add(time, 'minute')
  console.log()
  if (!dayjs(classDate + ' ' + "23:59").isBefore(endTime)) {
    return endTime.format('HH:mm')
  } else {
    $message.error('课程时间无法跨天请重新设置开始时间')
    return ''
  }

}
