<template>
  <van-popup v-model="showPopup" position="bottom" round>
    <div class="goods-discount-popup">
      <div class="header">
        <div class="title flex-default">
          <div class="empty" />
          <div class="text">优惠</div>
          <m-icon
            icon="close"
            :size="18"
            @click="$refs.discountPopup.close()"
          />
        </div>
        <div class="desc">领券购买更优惠哦~</div>
      </div>
      <div class="coupon-list">
        <coupon-item
          v-for="(item, index) of data.couponList"
          :key="`coupon-item-${index}`"
          :data="item"
          @get="handleGet(item)"
        />
      </div>
    </div>
  </van-popup>
</template>

<script>
import CouponItem from "@/components/coupon-item.vue";

export default {
  components: { CouponItem },
  props: {
    value: {
      type: Boolean,
      default: false,
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  watch: {
    value(val) {
      this.showPopup = val;
    },
    showPopup(val) {
      this.$emit("input", val);
    },
  },
  computed: {},
  data() {
    return {
      showPopup: false,
    };
  },
  methods: {
    handleGet(item) {
      this.$showLoading();
      this.$api({
        apiName: "getCoupon",
        postData: item.couponId,
        success: () => {
          item.uid = true;
        },
        complete: () => {
          this.$hideLoading();
        },
      });
    },
  },
};
</script>

<style lang="less">
.goods-discount-popup {
  background: #ffffff;
  border-radius: 21px 21px 0 0;
  > .header {
    padding: 18px 15px 12px 15px;
    .title {
      align-items: center;
      justify-content: space-between;
      margin-bottom: 18px;
    }
    .title .empty {
      width: 18px;
      height: 18px;
    }
    .title .text {
      font-size: 15px;
      font-weight: bold;
      color: #140603;
    }

    .desc {
      font-size: 13px;
      color: #dd2e41;
      line-height: 21px;
    }
  }
  > .coupon-list {
    height: 450px;
    background: #f5f7fb;
    padding: 10px 15px;
    overflow-y: auto;
  }
}
</style>