<!-- 优惠券弹框 -->
<template>
  <div class="popup-coupon">
    <cr-popup v-model="tempShow" closeable close-icon-position="top-right" @close="close">
      <div>
        <div class="content">
          <div class="content-wrapper">
            <!-- itm.publishCountFinished发行数量是否已领完： true 领完，false 未领完 -->
            <!-- itm.status  状态：1-未使用，2-已使用、3-已过期 -->
            <div
              v-for="(itm, idx) in couponPackage && couponPackage.coupons"
              :key="idx"
              class="content-item"
              :style="{
                background: `url(${
                  itm.publishCountFinished
                    ? couponPackage.invalidCouponBackgroundImageUrl
                    : couponPackage.couponBackgroundImageUrl
                }) no-repeat`,
                backgroundSize: 'contain',
                backgroundPosition: `100% 100%`,
                filter: ` ${
                  itm.publishCountFinished || [2, 3].includes(+itm.status)
                    ? `grayscale(100%)`
                    : 'none'
                } `
              }"
              @click="() => {}"
            >
              <div class="content-item-sku" :style="{ color: couponPackage.titleColor }">
                {{ itm.name }}
              </div>
              <div class="content-item-money" :style="{ color: couponPackage.faceAmountColor }">
                <span :style="{ fontSize: calcSymbolFontSize(`${itm.faceValue}`) }">￥</span>
                <span :style="{ fontSize: calcFontSize(`${itm.faceValue}`) }">{{
                  itm.faceValue
                }}</span>
              </div>
              <div class="content-item-discount" :style="{ color: couponPackage.attrColor }">
                {{ itm.limitDesc }}
              </div>

              <div
                class="content-item-get"
                :style="{
                  background: couponPackage.buttonBackgroundColor,
                  color: couponPackage.buttonTextColor
                }"
              >
                <!-- {{ itm.publishCountFinished ? '已抢光' : '已领取' }} -->
                {{ showText(itm, couponPackage) }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </cr-popup>
    <cr-button
      v-if="tempShow"
      shape="circle"
      size="normal"
      type="primary"
      class="btn"
      @click="close"
      >确认</cr-button
    >
  </div>
</template>

<script>
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    sourceData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      tempShow: false
    };
  },
  computed: {
    couponPackage() {
      return this.sourceData.couponPackage;
    }
  },
  watch: {
    show(val) {
      this.tempShow = val;
    }
  },
  methods: {
    // 显示文案
    showText(itm, couponPackage) {
      if (itm.publishCountFinished) {
        return '已抢光';
      }
      if (itm.status && +itm.status === 2) {
        return '已使用';
      }
      if (itm.status && +itm.status === 3) {
        return '已过期';
      }
      if (!couponPackage.received) {
        return '立即领取';
      } else {
        return '已领取';
      }
    },
    calcFontSize(params) {
      let len = params.length;
      let fontSize = '60px';
      switch (len) {
        case 3:
          fontSize = '50px';
          break;
        case 4:
          fontSize = '34px';
          break;
        case 5:
          fontSize = '30px';
          break;
        case 6:
          fontSize = '20px';
          break;
        default:
          break;
      }
      return fontSize;
    },
    calcSymbolFontSize(params) {
      let len = params.length;
      let fontSize = '30px';
      switch (len) {
        case 3:
          fontSize = '24px';
          break;
        case 4:
          fontSize = '20px';
          break;
        case 5:
          fontSize = '18px';
          break;
        case 6:
          fontSize = '14px';
          break;
        default:
          break;
      }
      return fontSize;
    },
    close() {
      this.tempShow = false;
      this.$emit('update:getshow', false);
    }
  }
};
</script>

<style scoped lang="less">
.popup-coupon {
  .content {
    background: url(https://appsync.lkbang.net/FgqVFwO4mAfBHZL0BLplSdXsyQdc) no-repeat;
    background-position: top;
    background-size: contain;
    width: 100%;
    display: flex;
    justify-content: center;
    &-wrapper {
      padding-top: 121px;
      display: flex;
      flex-flow: row wrap;
      justify-content: space-between;
      margin: 0 16px;
      .content-item {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;
        background: url('https://appsync.lkbang.net/FkjsnxsOEvLxxKl_EoJc0-viYkA_') no-repeat;
        background-size: contain;
        background-position: 100% 100%;
        min-width: 136px;
        min-height: 157px;
        margin-bottom: 10px;
        &-sku {
          padding-top: 14px;
          .text-size(12);
          color: #fa4528;
          white-space: nowrap;
          text-align: center;
          width: 70px;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        &-money {
          color: #fa4528;
          letter-spacing: -4px;
          line-height: 0;
          span:first-child {
            .text-size(20);
          }
          span:last-child {
            .text-size(60);
            text-align: center;
            line-height: 60px;
            padding-right: 12px;
            letter-spacing: -1px;
            font-weight: @font-weight-bold;
          }
        }
        &-discount {
          .text-size(14);
          color: #fa4528;
        }
        &-get {
          margin-top: 15px;
          border-radius: 8px;
          padding: 0 8px;
          background: linear-gradient(to right, #ffffff, #ffece6);
          .text-size(14);
          color: #fa4528;
        }
      }
    }
  }
  .cr-popup--center {
    width: 317px;
    height: 522px;
    border-radius: 25px;
    display: flex;
    justify-content: center;
    /deep/ .cr-popup--close {
      color: #bc683b;
    }
  }
  .btn {
    background: linear-gradient(to right, #ff4b00, #ff7705);
    z-index: 100;
    display: flex;
    justify-content: center;
    position: fixed;
    left: 0;
    right: 0;
    width: -webkit-fill-available;
    width: -moz-available; /* WebKit-based browsers will ignore this. */
    width: fill-available;
    top: calc(50% + 290px);
    margin: 0 29px;
  }
  /deep/ .cr-overlay {
    z-index: 100;
  }
}
</style>
