<template>
  <view class="uni-numbox">
    <view class="uni-numbox__minus" @click="_calcValue('minus')">
      <text
        class="uni-numbox--text"
        :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }"
        >-</text
      >
    </view>
    <view v-if="disabled || disabledInput" class="uni-numbox__value">{{ inputValue }}</view>
    <input
      v-else
      v-model="inputValue"
      :disabled="disabled || disabledInput"
      class="uni-numbox__value"
      type="number"
      @blur="_onBlur"
      @click="_onClickInputBox"
    />
    <view class="uni-numbox__plus" @click="_calcValue('plus')">
      <text
        class="uni-numbox--text"
        :class="{ 'uni-numbox--disabled': inputValue >= max || disabled || !canAdd }"
        >+</text
      >
    </view>
  </view>
</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
    },
    item: {
      type: Object,
      default: () => ({})
    },
    step: {
      type: Number,
      default: 1
    },
    disabled: {
      type: Boolean,
      default: false
    },
    disabledInput: {
      type: Boolean,
      default: false
    },
    canAdd: {
      type: Boolean,
      default: true
    },
    showMaxToast: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputValue: 0,
      changeType: null
    };
  },
  watch: {
    value(val) {
      this.inputValue = +val;
    },
    inputValue(newVal, oldVal) {
      if (+newVal !== +oldVal) {
        this.$emit("change", +newVal || 1, this.changeType);
      }
    }
  },
  created() {
    this.inputValue = +this.value;
  },
  methods: {
    _calcValue(type) {
      if (this.disabled) {
        return;
      }
      const scale = this._getDecimalScale();
      let value = this.inputValue * scale;
      let step = this.step * scale;
      if (type === "minus") {
        this.changeType = "REDUCE_NUM";
        value -= step;
        if (value < this.min * scale) {
          return;
        }
        if (value > this.max * scale) {
          value = this.max * scale;
        }
      } else if (type === "plus") {
        if (!this.canAdd) {
          return;
        }
        this.changeType = "ADD_NUM";
        value += step;
        if (this.item.commonLimitCount !== undefined && value > this.item.commonLimitCount) {
          let title = "";
          if (this.item.commonLimitCount > 0) {
            title = `您当前购买的商品限购${this.item.commonLimitCount}件`;
          } else {
            title = "今日已用完限购数量";
          }
          uni.showToast({
            title,
            duration: 2000,
            icon: "none"
          });
          return;
        }
        if (value > this.max * scale) {
          return;
        }
        if (value < this.min * scale) {
          value = this.min * 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) {
        if (this.showMaxToast) {
          uni.showToast({ icon: "none", title: `您当前购买的商品限购${this.max}件` });
        }
        value = this.max;
      } else if (value < this.min) {
        value = this.min;
      }
      this.inputValue = value;
    },
    _onClickInputBox() {
      this.$emit("onClickInputBox");
    }
  }
};
</script>
<style lang="scss" scoped>
$box-height: 48px;
/* #ifdef APP-NVUE */
$box-line-height: 48px;
/* #endif */
$box-line-height: 48px;
$box-width: 48px;
$input-width: 89px;
$uni-box: 191px;
.uni-numbox {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  height: $box-height;
  width: $uni-box;
  align-items: center;
  /* #ifdef MP-ALIPAY */
  width: 96px;
  height: 24px;
  /* #endif */
}

.uni-numbox__value {
  background-color: #f8f8f8;
  width: $input-width;
  height: $box-height;
  text-align: center;
  font-size: $uni-font-size-lg;
  border-width: 4rpx;
  border-style: solid;
  border-top-width: 0;
  border-bottom-width: 0;
  border-color: $white;
  line-height: $box-line-height;
  position: relative;
  z-index: 1;
  color: #333;
  /* #ifdef MP-ALIPAY */
  font-size: 14px;
  height: 24px;
  line-height: 24px;
  width: 45px;
  /* #endif */
}

.uni-numbox__minus {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  width: 40px;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: $box-width;
  height: $box-height;
  // line-height: $box-line-height;
  // text-align: center;
  font-size: 20px;
  color: $uni-text-color;
  border-radius: 8px 0 0 8px;
  background-color: $uni-bg-color-grey;
  /* #ifdef MP-ALIPAY */
  height: 24px;
  line-height: 24px;
  width: 24px;
  /* #endif */
}
.uni-numbox__plus {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: row;
  width: 40px;
  align-items: center;
  justify-content: center;
  width: $box-width;
  height: $box-height;
  border-radius: 0px 4px 4px 0px;
  background-color: $uni-bg-color-grey;
  /* #ifdef MP-ALIPAY */
  height: 24px;
  line-height: 24px;
  width: 24px;
  /* #endif */
}

.uni-numbox--text {
  font-size: 40rpx;
  color: $uni-text-color;
}

.uni-numbox--disabled {
  color: $uni-text-color-disable;
}
</style>
