/**
 *  @name:gw_date
 *  用途:   日期/事件处理方法类
 *         主要提供日期时间相关的处理函数
 *  @author:wxcwater
 *  @version:0.1.0
 */

// ---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM/M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
// ---------------------------------------------------
export function dateFormat(dateObj, formatStr) {
  if (!dateObj) {
    // console.log('util_date:传入时间参数为空')
    return '';
  }
  if (!formatStr) {
    // console.log('util_date:传入格式化参数为空')
    return '';
  }
  if (typeof dateObj === 'number') {
    var realDateObj = new Date(dateObj);
    dateObj = realDateObj;
  }
  var str = formatStr;
  var Week = ['日', '一', '二', '三', '四', '五', '六'];

  str = str.replace(/yyyy|YYYY/, dateObj.getFullYear());
  str = str.replace(
    /yy|YY/,
    dateObj.getYear() % 100 > 9
      ? (dateObj.getYear() % 100).toString()
      : '0' + (dateObj.getYear() % 100),
  );

  str = str.replace(
    /MM/,
    dateObj.getMonth() + 1 < 10
      ? '0' + (dateObj.getMonth() + 1)
      : dateObj.getMonth() + 1,
  );
  str = str.replace(/M/g, dateObj.getMonth());

  str = str.replace(/w|W/g, Week[dateObj.getDay()]);

  str = str.replace(
    /dd|DD/,
    dateObj.getDate() > 9
      ? dateObj.getDate().toString()
      : '0' + dateObj.getDate(),
  );
  str = str.replace(/d|D/g, dateObj.getDate());

  str = str.replace(
    /hh|HH/,
    dateObj.getHours() > 9
      ? dateObj.getHours().toString()
      : '0' + dateObj.getHours(),
  );
  str = str.replace(/h|H/g, dateObj.getHours());
  str = str.replace(
    /mm/,
    dateObj.getMinutes() > 9
      ? dateObj.getMinutes().toString()
      : '0' + dateObj.getMinutes(),
  );
  str = str.replace(/m/g, dateObj.getMinutes());

  str = str.replace(
    /ss|SS/,
    dateObj.getSeconds() > 9
      ? dateObj.getSeconds().toString()
      : '0' + dateObj.getSeconds(),
  );
  str = str.replace(/s|S/g, dateObj.getSeconds());

  return str;
}
// 当前日期 分
export function formatDateTime(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  var h = mytime.getHours();
  h = h < 10 ? '0' + h : h;
  var minute = mytime.getMinutes();
  minute = minute < 10 ? '0' + minute : minute;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
}
// 前count天
export function formatDateTime11(count, data) {
  var mytime = data || new Date();
  mytime = new Date(mytime.getTime() - 86400000 * count);
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  var h = mytime.getHours();
  h = h < 10 ? '0' + h : h;
  var minute = mytime.getMinutes();
  minute = minute < 10 ? '0' + minute : minute;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute;
}
// 一周前
export function formatDateWeek(count, data) {
  var mytime = data || new Date();
  mytime = new Date(mytime.getTime() - 86400000 * count);
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  return y + '-' + m + '-' + d;
}
// 当前日期 时
export function formatDateTimeH(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  var h = mytime.getHours();
  h = h < 10 ? '0' + h : h;
  return y + '-' + m + '-' + d + ' ' + h;
}
// 当前日期 天
export function formatDateTimeD(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  return y + '-' + m + '-' + d;
}
export function formatDateTimeD1(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  return y + '年' + m + '月' + d + '日';
}
export function formatDateTimeD2(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  return m + '月' + d + '日';
}

// 当前月
export function formatDateTimeM(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  return y + '-' + m;
}
// 日期时间戳转换
export function formatDateTime2(timeStamp) {
  // timeStamp  12位时间戳码
  if (timeStamp == null) {
    return null;
  } else {
    var date = new Date();
    date.setTime(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    var d = date.getDate();
    d = d < 10 ? '0' + d : d;
    var h = date.getHours();
    h = h < 10 ? '0' + h : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? '0' + minute : minute;
    second = second < 10 ? '0' + second : second;
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
  }
}

// 返回上一个8点
export function formatDateTime8H() {
  var mytime = new Date();
  var h = mytime.getHours();
  if (h < 8) {
    mytime = new Date(mytime.getTime() - 86400000);
  }
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  return y + '-' + m + '-' + d + ' ' + '08:00';
}

// 当前时刻的五分钟后
export function meetRoomStarTime(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  var h = mytime.getHours();
  h = h < 10 ? '0' + h : h;
  var minute = mytime.getMinutes() + 5;
  minute = minute < 10 ? '0' + minute : minute;
  var s = mytime.getSeconds();
  s = s < 10 ? '0' + s : s;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s;
}
// 当前时刻的五分钟后的一年后
export function meetRoomEndTime(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear() + 1;
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate();
  d = d < 10 ? '0' + d : d;
  var h = mytime.getHours();
  h = h < 10 ? '0' + h : h;
  var minute = mytime.getMinutes() + 5;
  minute = minute < 10 ? '0' + minute : minute;
  var s = mytime.getSeconds();
  s = s < 10 ? '0' + s : s;
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + s;
}
// 会议开始时间和结束时间
export function meetingStarTime(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate() + 1;
  d = d < 10 ? '0' + d : d;
  return y + '-' + m + '-' + d + ' ' + '10' + ':' + '00' + ':' + '00';
}
// 会议开始时间和结束时间
export function meetingEndTime(data) {
  var mytime = data || new Date();
  var y = mytime.getFullYear();
  var m = mytime.getMonth() + 1;
  m = m < 10 ? '0' + m : m;
  var d = mytime.getDate() + 1;
  d = d < 10 ? '0' + d : d;
  return y + '-' + m + '-' + d + ' ' + '12' + ':' + '00' + ':' + '00';
}

// 日期格式化年月日
export function formatD(data) {
  if (data != null && data != '') {
    var date = new Date(data);
    return (
      date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
    );
  }
  return '';
}

export default {
  dateFormat,
  formatDateTime,
  formatDateTime2,
  formatDateTimeH,
  formatDateTimeM,
  formatDateTimeD,
  formatDateTimeD1,
  formatDateTimeD2,
  formatDateWeek,
  formatDateTime11,
  formatDateTime8H,
  formatD,
};
