import Vue from 'vue'
import moment from 'moment'
function _number_format(number, decimals) {
  /*
     * 参数说明：
     * number：要格式化的数字
     * decimals：保留几位小数
     * dec_point：小数点符号
     * thousands_sep：千分位e5a48de588b6e799bee5baa631333366306464符号
     * */
  const dec_point = '.';
  const thousands_sep = ',';
  number = (number + '').replace(/[^0-9+-Ee.]/g, '');
  let n = !isFinite(+number) ? 0 : +number;
  let prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
  let sep = typeof thousands_sep === 'undefined' ? ',' : thousands_sep;
  let dec = typeof dec_point === 'undefined' ? '.' : dec_point;
  let toFixedFix = function (n, prec) {
    let k = Math.pow(10, prec);
    return '' + Math.ceil(n * k) / k
  };
  let s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  let re = /(-?\d+)(\d{3})/;
  while (re.test(s[0])) {
    s[0] = s[0].replace(re, '$1' + sep + '$2')
  }
  if ((s[1] || '').length < prec) {
    s[1] = s[1] || '';
    s[1] += new Array(prec - s[1].length + 1).join('0')
  }
  return s.join(dec)
}
/**
 * 转大写
 */
Vue.filter('capitalize', function (value) {
  if (!value) return '';
  value = value.toString();
  return value.charAt(0).toUpperCase() + value.slice(1)
});

/**
 * 格式化日期
 */
Vue.filter('dateFormat', function (value, format = 'yyyy-MM-dd') {
  if (!value) return '';
  return new moment(value).format(format)
});

/**
 * 字符串截取
 */
Vue.filter('strIntercept', function (value, length) {
  if (value && value.length > length) {
    return value.substring(0, length) + "...";
  }
  return value;
});
/**
 * 字符串截取
 */
Vue.filter('amount', function (value, length) {
  if (value && value.length > length) {
    value = _number_format(value, 2, '.', ',') // 1,234,567.09
  }
  return value;
});


//时间显示问题（几天前、几分钟前）
Vue.filter('formatTime', function (valueTime) {
  if (valueTime) {
    let newData = Date.parse(new Date());
    let diffTime = Math.abs(newData - valueTime);
    if (diffTime > 7 * 24 * 3600 * 1000) {
      let date = new Date(valueTime);
      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 ? ('1' + minute) : minute;
      second = second < 10 ? ('0' + second) : second;
      return m + '-' + d + ' ' + h + ':' + minute;
    } else if (diffTime < 7 * 24 * 3600 * 1000 && diffTime > 24 * 3600 * 1000) {
      // //注释("一周之内");
      // let time = newData - diffTime;
      let dayNum = Math.floor(diffTime / (24 * 60 * 60 * 1000));
      return dayNum + "天之前";
    } else if (diffTime < 24 * 3600 * 1000 && diffTime > 3600 * 1000) {
      // //注释("一天之内");
      // let time = newData - diffTime;
      let dayNum = Math.floor(diffTime / (60 * 60 * 1000));
      return dayNum + "小时之前";
    } else if (diffTime < 3600 * 1000 && diffTime > 60 * 1000) {
      // //注释("一小时之内");
      // let time = newData - diffTime;
      let dayNum = Math.floor(diffTime / (60 * 1000));
      return dayNum + "分钟之前";
    } else if (diffTime <= 60 * 1000) {
      // //注释("一分钟之内");
      // let time = newData - diffTime;
      let dayNum = Math.floor(diffTime / 1000);
      return dayNum + "秒之前";
    }
  }
});

/**
 * 去除字符串两边的空格
 */
Vue.filter('stringTrim', function (value, trim = 1) {
  switch (trim) {
    case 1:
      return value.replace(/\s+/g, "");
    case 2:
      return value.replace(/(^\s*)|(\s*$)/g, "");
    case 3:
      return value.replace(/(^\s*)/g, "");
    case 4:
      return value.replace(/(\s*$)/g, "");
    default:
      return value;
  }
});


/**
 * 字母大小写切换
 * type
 * 1:首字母大写
 * 2：首页母小写
 * 3：大小写转换
 * 4：全部大写
 * 5：全部小写
 */
Vue.filter('changeCase', function (str, type = 1) {
  function ToggleCase(str) {
    let itemText = ""
    str.split("").forEach(
      function (item) {
        if (/^([a-z]+)/.test(item)) {
          itemText += item.toUpperCase();
        } else if (/^([A-Z]+)/.test(item)) {
          itemText += item.toLowerCase();
        } else {
          itemText += item;
        }
      });
    return itemText;
  }

  switch (type) {
    case 1:
      return str.replace(/\b\w+\b/g, function (word) {
        return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
      });
    case 2:
      return str.replace(/\b\w+\b/g, function (word) {
        return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
      });
    case 3:
      return ToggleCase(str);
    case 4:
      return str.toUpperCase();
    case 5:
      return str.toLowerCase();
    default:
      return str;
  }
});

/**
 * 字符替换*，隐藏手机号或者身份证号等
 * replaceStr(字符串,字符格式, 替换方式,替换的字符（默认*）)
 * ecDo.replaceStr('18819322663',[3,5,3],0)
 * result：188*****663
 * ecDo.replaceStr('asdasdasdaa',[3,5,3],1)
 * result：***asdas***
 * ecDo.replaceStr('1asd88465asdwqe3',[5],0)
 * result：*****8465asdwqe3
 * ecDo.replaceStr('1asd88465asdwqe3',[5],1,'+')
 * result："1asd88465as+++++"
 */
Vue.filter('replaceStr', function (str, regArr, type, ARepText) {
  let regtext = '',
    Reg = null,
    replaceText = ARepText || '*';
  //repeatStr是在上面定义过的（字符串循环复制），大家注意哦
  if (regArr.length === 3 && type === 0) {
    regtext = '(\\w{' + regArr[0] + '})\\w{' + regArr[1] + '}(\\w{' + regArr[2] + '})'
    Reg = new RegExp(regtext);
    let replaceCount = this.repeatStr(replaceText, regArr[1]);
    return str.replace(Reg, '$1' + replaceCount + '$2')
  } else if (regArr.length === 3 && type === 1) {
    regtext = '\\w{' + regArr[0] + '}(\\w{' + regArr[1] + '})\\w{' + regArr[2] + '}'
    Reg = new RegExp(regtext);
    let replaceCount1 = this.repeatStr(replaceText, regArr[0]);
    let replaceCount2 = this.repeatStr(replaceText, regArr[2]);
    return str.replace(Reg, replaceCount1 + '$1' + replaceCount2)
  } else if (regArr.length === 1 && type === 0) {
    regtext = '(^\\w{' + regArr[0] + '})'
    Reg = new RegExp(regtext);
    let replaceCount = this.repeatStr(replaceText, regArr[0]);
    return str.replace(Reg, replaceCount)
  } else if (regArr.length === 1 && type === 1) {
    regtext = '(\\w{' + regArr[0] + '}$)'
    Reg = new RegExp(regtext);
    let replaceCount = this.repeatStr(replaceText, regArr[0]);
    return str.replace(Reg, replaceCount)
  }
})
