<template>
  <div class="uni-numbox">
    <div class="uni-numbox-minus" @click="_calcValue('subtract')">
      <span class="yticon" :class="canSubtract ? 'uni-numbox-disabled' : ''"> - </span>
    </div>
    <input class="uni-numbox-value" type="number" :disabled="disabled" :value="value" />
    <div class="uni-numbox-plus" @click="_calcValue('add')">
      <span class="yticon" :class="canAdd ? 'uni-numbox-disabled' : ''"> + </span>
    </div>
  </div>
</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
    };
  },
  watch: {
    value(val) {
      this.inputValue = val;
    }
  },
  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 });
        } else {
          this.$toast(`库存不足`);
        }
      }
    }
  }
};
</script>
<style lang="less" scoped>
.uni-numbox {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  height: 24px;
  background: #fff;
}

.uni-numbox-minus,
.uni-numbox-plus {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
  background-color: #f2f3f5;
  width: 24px;
  height: 100%;
  text-align: center;
  position: relative;
}
.uni-numbox-minus .yticon,
.uni-numbox-plus .yticon {
  .text-size(14);
  color: #555;
}

.uni-numbox-minus {
  border-right: none;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;
}

.uni-numbox-plus {
  border-left: none;
  border-top-right-radius: 3px;
  border-bottom-right-radius: 3px;
}

.uni-numbox-value {
  position: relative;
  background-color: #f2f3f5;
  width: 38px;
  height: 24px;
  text-align: center;
  padding: 0;
  .text-size(14);
  margin: 0 2px;
}

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