import { formatFloat } from './validate'

const MoneyTest = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;


//去除千分位中的‘，'
export function delcommafy(num) {
  if (!num) return num;
  num = num.toString();
  num = num.replace(/,/gi, '');
  return num;
};
export function valdateFn(rule, val, cb) {
  setTimeout(() => {
    if (val) {
      let inputVal = delcommafy(val);
      if (rule.test(inputVal)) {
        cb()
      } else {
        cb('只能是数字金额,最多两位小数')
      }
    }
    cb()
  })
}
// 验证金额数字可以为负数
export function moneyValid(rule, val, cb) {
  valdateFn(/((^-?[1-9]\d*)|^-?0)(\.\d{0,2}){0,1}$/, val, cb);
};
// 验证金额数字不可以为负数
export function moneyNValid(rule, val, cb) {
  valdateFn(MoneyTest, val, cb);
};
// 获取输入框的值
export function getInputValue(el, length) {
  let inputVal = el.target.value || '';
  return comdify(delcommafy(inputVal), length);
};
export function delInput(val, length) {
  if(!val){
    return ""
  }
  let inputVal = delcommafy(val);
  // var zero = '';
  // for (let i = 0; i < length; i++) {
  //   zero += "0"
  // }
  // if (inputVal.indexOf(`.${zero}`) > 0) {
  //   return inputVal.replace(`.${zero}`, "")
  // } else {
    return inputVal
  // }
}
export function moneyFormatDIS(number, decimals, dec_point, thousands_sep) {
  number = Number(number)
  number=number.toFixed(decimals)
  /*
  * 参数说明：
  * number：要格式化的数字
  * decimals：保留几位小数
  * dec_point：小数点符号
  * thousands_sep：千分位符号
  * */
  number = (number + '').replace(/[^0-9+-Ee.]/g, '');
  var n = !isFinite(+number) ? 0 : +number,
    prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
    sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
    dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
    s = '',
    toFixedFix = function (n, prec) {
      var k = Math.pow(10, prec);
      return '' + Math.ceil(n * k) / k;
    };
  s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
  var 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);
}
//四舍六入五留双
function evenRound(num, decimalPlaces) {
  var d = decimalPlaces || 0;
  var m = Math.pow(10, d);
  var n = +(d ? num * m : num).toFixed(8); // Avoid rounding errors
  var i = Math.floor(n), f = n - i;
  var e = 1e-8; // Allow for rounding errors in f
  var r = (f > 0.5 - e && f < 0.5 + e) ?
    ((i % 2 == 0) ? i : i + 1) : Math.round(n);
  return d ? r / m : r;
}

export function moneyFormat(number, decimals) {
  number = evenRound(Number(number),decimals)
  return comdifCommon(number.toString(),decimals)
}

// 金额添加千分位
export function comdify(number, decimals) {
  number = evenRound(Number(number),decimals)
  return comdifCommon(number.toString(),decimals)
};
// 数量保留两位小数
export function comdifyNum(number, decimals) {
  number = Number(number)
  if(number != undefined && number != null && number != ""){
    return number.toFixed(decimals)
  }else {
    var numZero = 0;
    return numZero.toFixed(decimals)
  }
};
// 数量保留两位小数
export function comdifyNumNotZero(number, decimals) {
  number = Number(number)
  if(number != undefined && number != null && number != ""){
    return number.toFixed(decimals)
  }else {
    return ""
  }
};
// 金额添加千分位
export function comdifCommon(n, length) {
  if (!n) return n;
  let str = n.split('.');
  let re = /\d{1,3}(?=(\d{3})+$)/g;
  let n1 = str[0].replace(re, "$&,");
  var zero = '';
  if(str[1]){
    length -= str[1].length;
  }
  for (let i = 0; i < length; i++) {
    zero += "0"
  }
  return str.length > 1 && str[1] ? `${n1}.${str[1]}${zero}` : `${n1}.${zero}`;
};
/**
 * 乘法
 * @param {} value1
 * @param {*} value2
 */
export function calcMoney(value1,value2){
  return Math.round(parseFloat((value1 * 100) * (value2 * 100)))/10000
}
