import XEUtils from "xe-utils";
import Vue from "vue";

/**
 * @desc   : 日期格式化
 * @author : 周兴
 * @date   : 2022/11/16 10:58
 */
Date.prototype.format = function (fmt) {
  if (!fmt) fmt = 'yyyy-MM-dd hh:mm:ss'
  let o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
  }
  return fmt
}

/**
 * @desc   : 获取当前时间，格式YYYY-MM-DD
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.toDateStr = function () {
  return this.format('yyyy-MM-dd')
}

/**
 * @desc   : 获取当前时间，格式HH:mi:ss
 * @author : 周兴
 * @date   : 2022/11/16 11:02
 */
Date.prototype.toTimeStr = function () {
  return this.format('hh:mm:ss')
}

/**
 * @desc   : 获取当前日期，格式YYYY-MM-DD hh:mm:ss
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.toDateTimeStr = function () {
  return this.format('yyyy-MM-dd hh:mm:ss')
}

/**
 * @desc   : 获取当前年月
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.toYearMonth = function () {
  return this.format('yyyyMM')
}

/**
 * @desc   : 获取当前年月
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.toYear = function () {
  return this.format('yyyy')
}

/**
 * @desc   : 获取当前年月
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.toYearMonth2 = function () {
  return this.format('yyyy-MM')
}

/**
 * @desc   : 获取当前日期
 * @author : 周兴
 * @date   : 2024/3/27 10:59
 */
Date.prototype.toDay = function () {
  return this.format('dd')
}

/**
 * @desc   : 获得星期
 * @author : 周兴
 * @date   : 2022/11/16 17:16
 */
Date.prototype.getWeek = function () {
  let days = this.getDay()
  let weekArray = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  return weekArray[days];
}

/**
 * @desc   : 获得本周一日期
 * @author : 周兴
 * @date   : 2022/11/16 17:16
 */
Date.prototype.getWeekFirstDay = function () {
  let days = this.getDay()
  days = days === 0 ? 6 : days - 1;
  return this.addDays(-1 * days).toDateStr();
}

/**
 * @desc   : 获得本周周末的日期
 * @author : 周兴
 * @date   : 2022/11/16 17:16
 */
Date.prototype.getWeekLastDay = function () {
  let days = this.getDay()
  days = days === 0 ? 1 : 7 - days;
  return this.addDays( days).toDateStr();
}

/**
 * @desc   : 获取当前月份的第一天
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.getFirstDateStr = function () {
  return new Date(this.toYearMonth2() + '-01').toDateStr()
}

/**
 * @desc   : 获取当前月份的最后一天
 * @author : 周兴
 * @date   : 2022/11/16 10:59
 */
Date.prototype.getLastDateStr = function () {
  let year = this.getFullYear();
  let month = this.getMonth() + 1;
  let day = new Date(year, month, 0).getDate();
  return year + '-' + month + '-' + day;
}

/**
 * @desc   : 增加天后日期
 * @author : 周兴
 * @date   : 2022/11/16 14:00
 */
Date.prototype.addDays = function (days) {
  if(!this)return this;
  let value = new Date(this.toDateStr());
  return new Date(value.setDate(value.getDate() + days))
}

/**
 * @desc   : 增加月份后日期
 * @author : 周兴
 * @date   : 2022/11/16 14:00
 */
Date.prototype.addMonths = function (months) {
  let year = this.getFullYear();
  let month = this.getMonth() + months;
  let day = this.getDate();
  return new Date(year, month, day)
}

/**
 * @desc   : 增加月份后日期
 * @author : 周兴
 * @date   : 2022/11/16 14:00
 */
Date.prototype.addYears = function (years) {
  let year = this.getFullYear() + years
  let month = this.getMonth();
  let day = this.getDate();
  return new Date(year, month, day)
}

/**
 * @desc   : 当前日期跟目标日期相差多少天
 * @author : 周兴
 * @date   : 2022/11/16 14:19
 */
Date.prototype.timespan = function (date) {
  if (!this || !date) return 0;
  let startTime = new Date(date.toDateStr()).getTime();
  let endTime = new Date(this.toDateStr()).getTime();
  let timespan = (endTime - startTime) / (1000 * 60 * 60 * 24)
  return timespan
}

/**
 * @desc   : 比较当前日期跟目标日期的大小，如果当前日期大，那么返回true,否则返回false
 * @author : 周兴
 * @date   : 2022/11/16 14:19
 */
Date.prototype.compare = function (date) {
  if (!this || !date) return false;
  let startTime = date.getTime();
  let endTime = this.getTime();
  return endTime > startTime ? true : false
}

/**
 * @desc   : 是工作日还是休息日
 * @author : 周兴
 * @date   : 2022/12/7 10:33
 */
Date.prototype.isWorkDay = function () {
  if (!this) return false;
  let days = this.getDay();
  return days % 6 !== 0;
}


//获取本月时间
Vue.prototype.getThisMonth = function (val) {
  let date = new Date()
  let month = val || date.getMonth() + 1
  let year = date.getFullYear()
  let finalDay = 0
  if (month === 2) {
    if (((year % 100 !== 0) && (year % 4 === 0)) || year % 400 === 0) {
      finalDay = 29
    } else {
      finalDay = 28
    }
  } else {
    if ([1, 3, 5, 7, 8, 10, 12].indexOf(month) !== -1) {
      finalDay = 31
    } else {
      finalDay = 30
    }
  }
  return [new Date(year + '-' + month + '-' + 1), new Date(year + '-' + month + '-' + finalDay)]
}

//获取本季度日期
Vue.prototype.getThisQuarter = function (val) {
  let now = new Date(); //当前日期
  let nowMonth = val ? val * 3 - 2 : now.getMonth(); //当前月
  let nowYear = now.getFullYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0;
  let theMonth = "";
  //获得本季度的开始月份
  if (!theMonth) {
    theMonth = nowMonth;
  }
  let quarterStartMonth = 0;
  if (theMonth < 3) {
    quarterStartMonth = 0;
  }
  if (2 < theMonth && theMonth < 6) {
    quarterStartMonth = 3;
  }
  if (5 < theMonth && theMonth < 9) {
    quarterStartMonth = 6;
  }
  if (theMonth > 8) {
    quarterStartMonth = 9;
  }
  //获得本季度的开始日期
  let quarterStartDate = new Date(nowYear, quarterStartMonth, 1);
  let quarterEndMonth = new Date(nowYear, quarterStartMonth + 2, 1);
  //获取多少天
  let monthStartDate = new Date(nowYear, quarterEndMonth.getMonth(), 1);
  let monthEndDate = new Date(nowYear, quarterEndMonth.getMonth() + 1, 1);
  let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
  // 或的本季度的结束日期
  let quarterEndDate = new Date(nowYear, quarterEndMonth.getMonth(), days);

  return [quarterStartDate, quarterEndDate]
}

Date.prototype.formatDay = function () {
  let days = this.getDay()
  let week;

  switch (days) {
    case 1:
      week = '星期一';
      break;
    case 2:
      week = '星期二';
      break;
    case 3:
      week = '星期三';
      break;
    case 4:
      week = '星期四';
      break;
    case 5:
      week = '星期五';
      break;
    case 6:
      week = '星期六';
      break;
    case 0:
      week = '星期日';
      break;
  }

  return week
}



