/*
 * 全局筛选器
 */

//包含主机
var _inHost = path => {
  var host = 'dreamstech.cn';
  return path && path != '' && path.indexOf(host) > -1;
};

let filter = {};

//图片缩略图 size: 100x100 mode: c
filter.thumb = (value, size, mode) => {
  if (value && _inHost(value)) {
    mode = mode && mode == 'c' ? '_c' : '';
    return value.replace(/(.*)(\.[a-zA-Z]+)$/i, '$1_' + size + mode + '$2');
  }
  return value;
};

// 格式化日期 2019-01-01 TT(WW)
filter.dateFormat = (value, format = 'yyyy-MM-dd') => {
  if (value) {
    var date = typeof value == 'string' ? value.replace(/\-/g, '/') : value;
    var d = new Date(date);

    if (/(T+)/.test(format)) {
      var label = '';
      var cdt = new Date();
      var cur = new Date(
        cdt.getFullYear() + '/' + (cdt.getMonth() + 1) + '/' + cdt.getDate()
      );
      var diffTime = d.getTime() - cur.getTime();
      var oneDayTime = 24 * 60 * 60 * 1000;
      var twoDayTime = 48 * 60 * 60 * 1000;
      var threeDayTime = 72 * 60 * 60 * 1000;
      if (diffTime > 0 && diffTime <= oneDayTime) {
        label = '今天';
      } else if (diffTime > 0 && diffTime <= twoDayTime) {
        label = '明天';
      } else if (diffTime > 0 && diffTime <= threeDayTime) {
        label = '后天';
      } else {
        label =
          d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
      }

      format = format.replace(RegExp.$1, label);
    }

    if (/(W+)/.test(format)) {
      let weeks = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
      let week = weeks[d.getDay()];

      format = format.replace(RegExp.$1, week);
    }

    if (/(y+)/.test(format)) {
      format = format.replace(
        RegExp.$1,
        (d.getFullYear() + '').substr(4 - RegExp.$1.length)
      );
    }

    var o = {
      'M+': d.getMonth() + 1, //month
      'd+': d.getDate(), //day
      'h+': d.getHours(), //hour
      'm+': d.getMinutes(), //minute
      's+': d.getSeconds(), //second
      'q+': Math.floor((d.getMonth() + 3) / 3), //quarter
      S: d.getMilliseconds(), //millisecond
    };

    for (var 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;
  }
  return value;
};

filter.keepTwoDecimalFull = value => {
  if (value) {
    let result = parseFloat(value);
    if (isNaN(result)) {
      return value;
    }
    result = Math.round(value * 100) / 100;
    let s_x = result.toString();
    let pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
      pos_decimal = s_x.length;
      s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
      s_x += '0';
    }
    return s_x;
  }
  return value;
};

/*
 * 参数说明：
 * number：要格式化的数字
 * decimals：保留几位小数
 */
filter.moneyEx = (number, decimals=2) => {
  number = parseFloat(number)
  if(typeof number === 'number' && !isNaN(number)){
    return number.toFixed(decimals)
  }
  return 0;
};

// 格式化金额 50000元 5万元
filter.moneyFormat = (value, format = '万') => {
  if (value) {
    if (value / 10000 > 0) {
      return parseFloat(value) / 10000 + format;
    }
    return parseFloat(value);
  }
  return 0;
};
// 加密手机号 12345674567 => 123****4567
filter.encryPhone = (value)=>{
  if(value){//filter.isPhone(value)
    var reg = /^(\d{3})\d*(\d{4})(.*)/;
    return value.replace(reg,'$1****$2$3')
  }
  return value
}

//默认值，当值为null时显示默认值
filter.defaultValue = (value, defaultValue = '无') => {
  if (value && value != 'null') {
    return value;
  }
  return defaultValue;
};

//公里转换 长度单位换算
filter.kmFormat = value => {
  let num = parseFloat(value);
  if (!isNaN(num)) {
    if (num <= 10) {
      return '<10米';
    } else if (num >= 1000) {
      return (num / 1000).toFixed(3) + '公里';
    } else {
      return num.toFixed(0) + '米';
    }
  }
  return value;
};

//手机号码加星
filter.phoneToStar = value => {
  if (value) {
    return value.replace(/([\d]{3})[\d]{4}([\d]{4})/, '$1****$2');
  }
  return value;
};

export default filter;