<template>
  <view class="good">
    <uni-popup ref="popup" type="dialog">
      <uni-popup-dialog
        message="成功消息"
        title="修改购买数量"
        :duration="2000"
        :before-close="true"
        type="info"
        @cancel="close"
        @confirm="confirm"
      >
        <uni-number-box
          :value="goodCount"
          :min="1"
          :max="max"
          :common-limit-count="max"
          show-max-toast
          :class="['num']"
          @change="changeGoodNum"
        />
      </uni-popup-dialog>
    </uni-popup>
  </view>
</template>
<script>
import uniNumberBox from "./goodNum.vue";
import uniPopupDialog from "@/components/uni-popup/uni-popup-dialog.vue";
import { mapActions } from "../shopCartModules";
import { throtte } from "@/utils";
export default {
  name: "GoodNum",
  components: {
    uniNumberBox,
    uniPopupDialog
  },
  props: {
    show: {
      type: Boolean,
      default() {
        return false;
      }
    },
    value: {
      type: Object,
      default() {
        return {};
      }
    },
    index: {
      type: Number
    }
  },
  data() {
    return {
      goodCount: this.value?.count
    };
  },
  computed: {
    max() {
      let max = 999999999;
      let v = this.value;
      if (v) {
        if (v.activityCanBuyCount) {
          max = v.activityCanBuyCount;
        } else if (v.commonLimitCount !== undefined) {
          max = v.commonLimitCount;
        } else if (v.limitCount) {
          max = v.limitCount;
        }
      }
      return max;
    }
  },
  watch: {
    show: function(val) {
      if (val) {
        this.goodCount = this?.value?.count;
        this.$refs?.popup?.open();
      } else {
        this.$refs?.popup?.close();
      }
    }
  },
  mounted() {
    this.$refs.popup.duration = 0; //解决输入框上移问题
  },
  methods: {
    ...mapActions({
      changeCartGoodNum: "change_cart_good_num"
    }),
    close() {
      this.$emit("close");
    },
    confirm: throtte(function() {
      this.changeCartGoodNum({
        index: this.index,
        goodInfo: { ...this.value, count: this.goodCount, skuNum: this.goodCount },
        type: "EDIT_NUM"
      });
      this.$emit("close");
    }, 2000),
    changeGoodNum(count) {
      this.goodCount = count;
    }
  }
};
</script>
<style lang="scss"></style>
