import moment from "moment";
// 当前日期
let now = new Date()
// 今天本周的第几天
let nowDayOfWeek = now.getDay()
// 当前日
let nowDay = now.getDate()
// 当前月
let nowMonth = now.getMonth()
// 当前年
let nowYear = now.getYear()
nowYear += nowYear < 2000 ? 1900 : 0

class DateUtils {
  static parseLongDate(dateStr) {
    return Date.parse(dateStr.replace(/-/g, '/'))
  }

  // 格式化日期：yyyy-MM-dd
  static formatLongDate(date) {
    let myyear = date.getFullYear()
    let mymonth = date.getMonth() + 1
    let myweekday = date.getDate()
    let myHour = date.getHours()
    let myMin = date.getMinutes()
    let mySec = date.getSeconds()

    if (mymonth < 10) {
      mymonth = '0' + mymonth
    }
    if (myweekday < 10) {
      myweekday = '0' + myweekday
    }
    if (myHour < 10) {
      myHour = '0' + myHour
    }
    if (myMin < 10) {
      myMin = '0' + myMin
    }
    if (mySec < 10) {
      mySec = '0' + mySec
    }
    return myyear + '-' + mymonth + '-' + myweekday + ' ' + myHour + ':' + myMin + ':' + mySec
  }

  // 获得某月的天数
  static getMonthDays(myMonth) {
    let monthStartDate = new Date(nowYear, myMonth, 1)
    let monthEndDate = new Date(nowYear, myMonth + 1, 1)
    return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
  }

  // 获得本季度的开始月份
  static getQuarterStartMonth() {
    let quarterStartMonth = 0
    if (nowMonth < 3) {
      quarterStartMonth = 0
    }
    if (nowMonth > 2 && nowMonth < 6) {
      quarterStartMonth = 3
    }
    if (nowMonth > 5 && nowMonth < 9) {
      quarterStartMonth = 6
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9
    }
    return quarterStartMonth
  }

  // 获得本周的开始日期
  static getWeekStartDate() {
    let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
    return this.formatDate(weekStartDate)
  }

  // 获得本周的结束日期
  static getWeekEndDate() {
    let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
    return this.formatDate(weekEndDate)
  }

  // 获得本月的开始日期
  static getMonthStartDate() {
    let monthStartDate = new Date(nowYear, nowMonth, 1)
    return this.formatDate(monthStartDate)
  }

  // 获得本月的结束日期
  static getMonthEndDate() {
    let monthEndDate = new Date(nowYear, nowMonth, this.getMonthDays(nowMonth))
    return this.formatDate(monthEndDate)
  }

  // 获得本季度的开始日期
  static getQuarterStartDate() {
    let quarterStartDate = new Date(nowYear, this.getQuarterStartMonth(), 1)
    return this.formatDate(quarterStartDate)
  }

  // 或的本季度的结束日期
  static getQuarterEndDate() {
    let quarterEndMonth = this.getQuarterStartMonth() + 2
    let quarterStartDate = new Date(nowYear, quarterEndMonth, this.getMonthDays(quarterEndMonth))
    return this.formatDate(quarterStartDate)
  }

  static getDateWeek(date) {
    let week = [ '周日', '周一', '周二', '周三', '周四', '周五', '周六' ]
    return week[date.getDay()]
  }

  static utc2beijing(utc_datetime) {
    // 转为正常的时间格式 年-月-日 时:分:秒
    let T_pos = utc_datetime.indexOf('T')
    let Z_pos = utc_datetime.indexOf('Z')
    let year_month_day = utc_datetime.substr(0, T_pos)
    let hour_minute_second = utc_datetime.substr(T_pos + 1, Z_pos - T_pos - 1)
    let new_datetime = year_month_day + ' ' + hour_minute_second // 2017-03-31 08:02:06
    // 处理成为时间戳
    let times_tamp = new Date(Date.parse(new_datetime))
    times_tamp = times_tamp.getTime()
    times_tamp = times_tamp / 1000
    // 增加8个小时，北京时间比utc时间多八个时区
    times_tamp = times_tamp + 8 * 60 * 60
    // 时间戳转为时间
    let date = new Date(parseInt(times_tamp) * 1000)
    // let beijing_datetime =date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let minute = date.getMinutes()
    let second = date.getSeconds()
    minute = minute < 10 ? '0' + minute : minute
    second = second < 10 ? '0' + second : second
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second // 2017-03-31 16:02:06
  }

  static formatDate(date, format) {
    return moment(date).format(format);
  }

  /*日期处理*/
  static dateFormat(row, column) {
    const date = row[column.property];
    if (date === undefined) {
      return ''
    }
    return moment(date).format("YYYY-MM-DD HH:mm:ss")
  }

  /**
   * date 代表指定的日期，格式：2018-09-27
   * day 传-1表始前一天，传1表始后一天
   */
  static getNextDate(day, date) {
    let dd = new Date();
    if (date) {
      dd = new Date(date);
    }
    dd.setDate(dd.getDate() + day);
    const y = dd.getFullYear();
    const m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
    const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
    return y + "-" + m + "-" + d;
  }
}

export default DateUtils
