/*
 * @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} data
 * @return {undefined|string}
 */
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} data
 * @return {undefined|string}
 */
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} data timestamp
 * @return {undefined|string}
 */
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} data
 * @return {undefined|string}
 */
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} data
 * @return {undefined|string}
 */
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} date
 * @return {undefined|string}
 */
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}分`;
};

/**
 * 手机号格式化
 * @param {string} phone
 * @return {string}
 */
const formatPhoneNum = function(phone) {
  if (!phone) {
    return '';
  }
  phone = phone.toString();
  return phone.substr(0, 3) + '****' + phone.substr(7, 11);
};

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

/**
 * 毫秒---> *时*分*秒
 * @param {timestamp} msTime
 * @return {string}
 */
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}`;
};

export default {
  dateFormat,
  formatTimeStamp,
  timeStampToYm,
  timeStampToYmd,
  timeStampToHms,
  formatTimeYmdHms,
  formatPhoneNum,
  formatName,
  timeStampSpace,
  formatTime
};
