<template>
  <view class="uni-numbox">
    <view class="uni-numbox-minus" @click="_calcValue('subtract')">
      <text class="yticon" :class="canSubtract ? 'uni-numbox-disabled' : ''"> - </text>
    </view>
    <view v-if="disabled" class="uni-numbox-value uni-numbox-txt">{{ value }}</view>
    <input v-else class="uni-numbox-value" type="number" :disabled="disabled" :value="value" />
    <view class="uni-numbox-plus" @click="_calcValue('add')">
      <text class="yticon" :class="canAdd ? '' : 'uni-numbox-disabled'"> + </text>
    </view>
  </view>
</template>
<script>
export default {
  name: "Stepper",
  props: {
    value: {
      type: Number,
      default: 0
    },
    disabled: {
      type: Boolean,
      default: false
    },
    index: {
      type: Number,
      default: 0
    },
    canAdd: Boolean,
    canSubtract: Boolean,
    min: {
      type: Number,
      default: -Infinity
    },
    max: {
      type: Number,
      default: Infinity
    }
  },
  data() {
    return {
      inputValue: this.value
    };
  },
  methods: {
    _calcValue(type) {
      if (type === "subtract") {
        if (this.canSubtract || this.inputValue >= this.min) {
          this.$emit("change", { type, index: this.index });
        }
      } else if (type === "add") {
        if (this.canAdd || this.inputValue <= this.max) {
          this.$emit("change", { type, index: this.index });
        }
      }
    }
  }
};
</script>
<style lang="scss" src="./index.scss"></style>
<style lang="scss" scoped>
.uni-numbox {
  width: 176rpx;
}
.uni-numbox,
.uni-numbox-value,
.uni-numbox-minus,
.uni-numbox-plus {
  height: 48rpx;
  line-height: 48rpx;
}
.uni-numbox-txt {
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
