/**
 * Created by yaobo on 2018/3/2.
 * 存放一些公用的全局方法
 */
import Vue from 'vue';

Vue.prototype.resetFormX = (formName) => {
  this.$refs[formName].resetFields();
}

// 浮点运算
Vue.prototype.floatComputed = (arg1, arg2, sign) => {
  if (!arg1) {
    arg1 = 0;
  }
  if (!arg2) {
    arg2 = 0;
  }
  let val1 = arg1 + '';
  let val2 = arg2 + '';
  let rate = 1;
  let rate1 = 1;
  let rate2 = 1;
  if (val1.indexOf('.') < 0) {
    rate1 = 0;
  } else {
    rate1 = val1.length - val1.indexOf('.') - 1;
  }
  if (val2.indexOf('.') < 0) {
    rate2 = 0;
  } else {
    rate2 = val2.length - val2.indexOf('.') - 1;
  }
  rate = rate2;
  val2 = val2.replace('.', '');
  val1 = val1.replace('.', '');
  if (rate1 > rate2) {
    rate = rate1;
    for (let i = 0; i < rate1 - rate2; i++) {
      val2 = val2 + '0'
    }
  } else {
    for (let i = 0; i < rate2 - rate1; i++) {
      val1 = val1 + '0';
    }
  }
  let rateNum = Math.pow(10, rate);
  if (sign === '-') {
    return (Number(val1) - Number(val2)) / rateNum
  }
  if (sign === '+') {
    return (Number(val1) + Number(val2)) / rateNum
  }
  if (sign === '*') {
    return (Number(val1) * Number(val2)) / (rateNum * rateNum)
  }
  if (sign === '/') { // 默认保留4位
    let num = (Number(val1) / Number(val2)) + '';
    if (num.split('.').length > 0) {
      if (num.split('.')[1].length > 4) {
        num = Number(num).toFixed(4);
      }
    }
    return num;
  }
  return null;
}
