/*
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-09-11 18:18:32
 * @LastEditors: 无尘
 * @LastEditTime: 2020-07-08 14:46:27
 */
/* 全局过滤器 */

const dateFormat = function(timeSpan, format) {
  if (!timeSpan) return;
  timeSpan = timeSpan.toString().length === 10 ? timeSpan * 1000 : timeSpan;
  let date = new Date(timeSpan);
  let o = {
    'M+': date.getMonth() + 1,
    'D+': date.getDate(),
    W: '日一二三四五六'.charAt(date.getDay()),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    S: date.getMilliseconds()
  };
  if (/(Y+)/.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
    }
  }
  return format;
};

/**
 * 时间戳---> 年-月-日 时:分:秒
 * @param timestamp
 */
const formatTimeStamp = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${date.getFullYear()}-${newMonth}-${day} ${hours}:${minutes}:${seconds}`;
};

/**
 * 时间戳---> 年.月.日 时:分:秒
 * @param timestamp
 */
const formatTimeYmdHms = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${date.getFullYear()}.${newMonth}.${day} ${hours}:${minutes}:${seconds}`;
};

/**
 * 时间戳---> 年-月
 * @param timestamp
 */
const timeStampToYm = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let newMonth = month < 10 ? '0' + month : month;
  return `${date.getFullYear()}-${newMonth}`;
};

/**
 * 时间戳---> 年-月-日
 * @param timestamp
 */
const timeStampToYmd = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  return `${date.getFullYear()}-${newMonth}-${day}`;
};
/**
 * 时间戳---> 年-月-日
 * @param timestamp
 */
const timeStampToYmd3 = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  return `${date.getFullYear()}年${newMonth}月${day}日`;
};
/**
 * 时间戳---> 年-月-日
 * @param timestamp
 */
const timeStampToYmd2 = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data - 1000 * 60 * 60 * 24);
  let month = date.getMonth() + 1;
  let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  let newMonth = month < 10 ? '0' + month : month;
  return `${date.getFullYear()}-${newMonth}-${day}`;
};

/**
 * 时间戳---> 时:分:秒
 * @param timestamp
 */
const timeStampToHms = function(data) {
  if (!data) {
    return;
  }
  let date = new Date(data);
  let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
  let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  let seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return `${hours}:${minutes}:${seconds}`;
};

/**
 * 时间戳---> *天*小时*分
 * @param timestamp
 */
const timeStampSpace = function(date) {
  if (!date) {
    return;
  }
  let date2 = new Date();
  let date3 = new Date(Number(date)).getTime() - date2.getTime(); //时间差的毫秒数
  if (date3 < 0) {
    return '';
  }
  //计算出相差天数
  let days = Math.floor(date3 / (24 * 3600 * 1000));
  //计算出小时数
  let leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
  let hours = Math.floor(leave1 / (3600 * 1000));
  //计算相差分钟数
  let leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
  let minutes = Math.floor(leave2 / (60 * 1000));
  //计算相差秒数
  // let leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
  // let seconds = Math.round(leave3 / 1000);
  return `${days}天${hours}小时${minutes}分`;
};
// 时间戳转化为明后天系列
const timeStampMode = function(time) {
  if (!isNaN(time)) time = Number(time);
  let now = new Date(); // 当前时间
  console.log(now);
  let today = new Date(`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} 23:59:59`).getTime(); // 今天
  let yesterday = new Date(`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate() - 1} 23:59:59`).getTime(); // 昨天
  let tomorrow = new Date(`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate() + 1} 23:59:59`).getTime(); // 明天
  let dayAfterTomorrow = new Date(`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate() + 2} 23:59:59`).getTime(); // 后天
  console.log((time - today) / (24 * 60 * 60 * 1000));
  if (time <= today && time > yesterday) {
    // 今天
    return 0;
  } else if (time > today && time <= tomorrow) {
    // 明天
    return 1;
  } else if (time > tomorrow && time <= dayAfterTomorrow) {
    // 后天
    return 2;
  } else if (time > dayAfterTomorrow) {
    // 未来时间
    let sub = parseInt((time - today) / (24 * 60 * 60 * 1000)) + 1;
    return sub;
  } else if (time <= yesterday) {
    // 昨天以前
    return -1;
  }
};
/**
 * 手机号格式化
 * @param {String} phone
 */
const formatPhoneNum = function(phone) {
  if (!phone) {
    return '';
  }
  phone = phone.toString();
  return phone.substr(0, 3) + '****' + phone.substr(7, 11);
};

/**
 * 姓名格式化
 * @param {String} phone
 */
const formatName = function(name) {
  if (!name) {
    return '';
  }
  name = name.toString();
  return '**' + name.substr(name.length - 1, name.length);
};

/**
 * 毫秒---> *时*分*秒
 * @param timestamp
 */
const formatTime = function(msTime) {
  if (!msTime) {
    return '00:00:00';
  }
  let time = msTime / 1000;

  let day = Math.floor(time / 60 / 60 / 24);

  let hour = day * 24 + (Math.floor(time / 60 / 60) % 24) < 10 ? '0' + (day * 24 + (Math.floor(time / 60 / 60) % 24)) : day * 24 + (Math.floor(time / 60 / 60) % 24);

  let minute = Math.floor(time / 60) % 60 < 10 ? '0' + (Math.floor(time / 60) % 60) : Math.floor(time / 60) % 60;

  let second = Math.floor(time) % 60 < 10 ? '0' + (Math.floor(time) % 60) : Math.floor(time) % 60;

  return `${hour}:${minute}:${second}`;
};
const formatInvalidStatus = function(data) {
  let str = '--';
  if (data == 1) {
    str = '未失效';
  } else if (data == 2) {
    str = '已失效';
  }
  return str;
};
export default {
  dateFormat,
  formatTimeStamp,
  timeStampToYm,
  timeStampToYmd,
  timeStampToHms,
  formatTimeYmdHms,
  formatPhoneNum,
  timeStampToYmd2,
  timeStampToYmd3,
  timeStampMode,
  formatName,
  timeStampSpace,
  formatTime,
  formatInvalidStatus
};
