<template>
  <div class="uni-numbox">
    <div @click="_calcValue('minus')" class="uni-numbox__minus">
      <span
        class="uni-numbox--text"
        :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }"
        >-</span
      >
    </div>
    <input
      :disabled="inputDisabled"
      @blur="_onBlur"
      :adjust-position="false"
      class="uni-numbox__value"
      type="number"
      v-model="inputValue"
    />
    <div @click="_calcValue('plus')" class="uni-numbox__plus">
      <span
        class="uni-numbox--text"
        :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }"
        >+</span
      >
    </div>
  </div>
</template>
<script>
/**
 * NumberBox 数字输入框
 * @description 带加减按钮的数字输入框
 * @tutorial https://ext.dcloud.net.cn/plugin?id=31
 * @property {Number} value 输入框当前值
 * @property {Number} min 最小值
 * @property {Number} max 最大值
 * @property {Number} step 每次点击改变的间隔大小
 * @property {Boolean} disabled = [true|false] 是否为禁用状态
 * @event {Function} change 输入框值改变时触发的事件，参数为输入框当前的 value
 */

export default {
  name: 'UniNumberBox',
  props: {
    value: {
      type: [Number, String],
      default: 1,
    },
    min: {
      type: Number,
      default: 0,
    },
    max: {
      type: Number,
      default: 100,
    },
    step: {
      type: Number,
      default: 1,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    inputDisabled: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      inputValue: 0,
    };
  },
  watch: {
    value(val) {
      this.inputValue = val;
    },
    inputValue(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.$emit('input', newVal);
      }
    },
  },
  created() {
    this.inputValue = this.value;
  },
  methods: {
    _calcValue(type) {
      if (this.disabled) {
        return;
      }
      console.log(type, 91)
      const scale = this._getDecimalScale();
      let value = this.inputValue * scale;
      let step = this.step * scale;
      if (type === 'minus') {
        value -= step;
        console.log(97, value , this.min * scale)
        if (value < this.min * scale) {
          return;
        }
        console.log(101, value, this.max, scale)
        if (value > this.max * scale) {
          value = this.max * scale;
        }
      } else if (type === 'plus') {
        value += step;
        if (value > this.max * scale) {
          return;
        }
        if (value < this.min * scale) {
          value = this.min * scale;
        }
      }
      console.log(113, value, scale)

      this.inputValue = String(value / scale);
    },
    _getDecimalScale() {
      let scale = 1;
      // 浮点型
      if (~~this.step !== this.step) {
        scale = Math.pow(10, (this.step + '').split('.')[1].length);
      }
      return scale;
    },
    _onBlur(event) {
      let value = event.detail.value;
      if (!value) {
        // this.inputValue = 0;
        return;
      }
      value = +value;
      if (value > this.max) {
        value = this.max;
      } else if (value < this.min) {
        value = this.min;
      }
      this.inputValue = value;
    },
  },
};
</script>
<style scoped>
/* #ifdef APP-NVUE */
/* #endif */

.uni-numbox {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  height: 40px;
  /* line-height: 35px; */
  width: 160px;
  border: 1px solid #e5e5e5;
  border-radius: 6px;
  background-color: #ffffff;
}

.uni-numbox__value {
  flex: 1;
  width: 50px;
  height: 100%;
  text-align: center;
  padding: 0;
  margin: 0;
  line-height: 40px;
  font-size: 32px;
  background-color: transparent;
  border: 0;
}

.uni-numbox__minus {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 35px;
  height: 100%;
  padding: 0 5px;
  font-size: 20px;
  color: #333;
  background-color: #f8f8f8;
}

.uni-numbox__plus {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 35px;
  height: 100%;
  padding: 0 5px;
  background-color: #f8f8f8;
}

.uni-numbox--text {
  font-size: 40px;
  color: #333;
}

.uni-numbox--disabled {
  color: #c0c0c0;
}
</style>
