<template>
  <view class="count">
    <div class="count-btn" @click="reduce" :class="{ 'count-btn-disabled': num <= 1 }">-</div>
    <div class="count-text">{{ num }}</div>
    <div class="count-btn" :class="{ 'count-btn-disabled': num >= max }" @click="add">+</div>
  </view>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    },
    max: {
      type: Number,
      default: 99
    }
  },
  watch: {
    value: {
      handler(val) {
        this.num = val
      },
      immediate: true
    },
    num(val) {
      this.$emit('input', val)
    }
  },
  data() {
    return {
      num: 1
    }
  },
  methods: {
    add() {
      if (this.num < this.max) {
        this.num++
      }
    },
    reduce() {
      if (this.num > 1) {
        this.num--
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.count {
  margin: 0;
  padding: 0;
  display: flex;
  width: 130upx;
  height: 40upx;
  box-sizing: border-box !important;
  border-radius: 8upx;
  overflow: hidden;
  border: 2upx solid #2373ea;
  background-color: #045fe7;

  .count-btn {
    margin: 0;
    padding: 0;
    display: flex;
    width: 28%;
    color: #fff;
    font-size: 24upx;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
  }
  .count-text {
    margin: 0;
    padding: 0;
    display: flex;
    flex: 1;
    font-size: 24upx;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    background-color: #fff;
  }
  .count-btn-disabled {
    background-color: #2373ea;
  }
}
</style>
