<template>
  <dl class="group-item" :class="{ disabled: groupStop }" @click="onButtonClick">
    <dt class="group-item-thumb">
      <cr-image :src="goodsItem.thumbImageUrl" />
      <span class="group-item-tag">{{ goodsItem.groupBuyLimitUserCount }}人团</span>
    </dt>
    <dd class="group-item-content">
      <div class="group-item-title">{{ goodsItem.goodsName }}</div>
      <!-- 进度条：已拼团消耗库存/该活动商品初始库存 -->
      <cr-progress
        class="group-item-progress"
        stroke-width="5"
        color="#EC1500"
        track-color="#F7F8F9"
        :show-pivot="false"
        :percentage="handleProgressByStatus(goodsItem)"
      />
      <div class="group-item-already">已拼{{ goodsItem.groupBuySuccessCount }}团</div>
      <div class="group-item-price">
        成团价 ¥ <sup class="group-item-sup">{{ goodsItem.activityPrice }}</sup>
      </div>
      <div class="group-item-delprice">售价 ￥{{ goodsItem.price }}</div>
      <!-- 立即开团 -->
      <!-- 已抢光 -->
      <!-- 未开始 -->
      <!-- 已结束 -->
      <cr-button
        size="small"
        shape="circle"
        type="danger"
        :disabled="btnByStatus.d"
        @click.stop="onButtonClick"
        >{{ btnByStatus.t }}</cr-button
      >
    </dd>
  </dl>
</template>
<script>
import { registeredEvents } from '@/service/sa.service';
export default {
  name: 'GoodsCard',
  props: {
    topicCfg: {
      type: Object,
      default: () => {}
    },
    goodsItem: {
      type: Object,
      default: () => {}
    },
    index: {
      type: Number
    }
  },
  data() {
    return {};
  },
  computed: {
    groupStop() {
      const { hasStop, hasStart } = this.topicCfg;
      const { goodsCount } = this.goodsItem;
      return !hasStart ? false : hasStop ? true : goodsCount !== 0 ? false : true;
    },
    btnByStatus() {
      const { hasStop, hasStart } = this.topicCfg;
      const { goodsCount } = this.goodsItem;
      if (!hasStart) {
        return { t: '未开始', d: true, s: 0 };
      }
      if (hasStop) {
        return { t: '已结束', d: true, s: 1 };
      }
      if (goodsCount !== 0) {
        return { t: '立即开团', d: false, s: 2 };
      }
      return { t: '已抢光', d: true, s: 3 };
      // return !hasStart
      //   ? { t: '未开始', d: true, s: 0 }
      //   : hasStop
      //   ? { t: '已结束', d: true, s: 1 }
      //   : goodsCount !== 0
      //   ? { t: '立即开团', d: false, s: 2 }
      //   : { t: '已抢光', d: true, s: 3 };
    }
  },
  methods: {
    handleProgressByStatus(item) {
      if (!item) return 0;
      let percentage = 0;
      const { showSaleCount, goodsCount } = item;
      percentage = (goodsCount / (goodsCount + showSaleCount)) * 100;
      percentage = percentage < 0 ? 0 : percentage;
      percentage = this.btnByStatus.d ? 0 : percentage;
      return Math.round(percentage * 100) / 100;
    },
    onButtonClick() {
      const {
        skuNo,
        goodsSpecialId,
        activityTemplateId,
        activityTemplateDetailId,
        activityInfoId
      } = this.goodsItem;
      const { s } = this.btnByStatus;
      if (s === 3) {
        this.$notify({ type: 'warning', message: '该商品已抢完' });
        return;
      }
      if (s === 2) {
        // skuNo=2305164578817
        // goodsSpecialId=59
        // activityId=47
        // templateId=58
        // templateDetailId=83
        registeredEvents('H5_GroupZeroYuanPurchaseActivityPageOpenGroupImmediatelyBtnClick', {
          skuNo
        });
        this.$router.push({
          path: '/groupBuy/skuInfo',
          query: {
            h: 0,
            skuNo,
            goodsSpecialId,
            activityId: activityInfoId,
            templateId: activityTemplateId,
            templateDetailId: activityTemplateDetailId
          }
        });
        // 正常跳转
        return false;
      }
      this.$dialog({
        message: '该活动已结束~',
        confirmButtonText: s === 3 ? '重新选择' : '订阅消息',
        onConfirm: () => {
          // todo 跳转到小程序
          // let getScheme = async function() {
          //   const [res] = await groupBuy.getScheme({
          //     miniUrl: 'pages/user/login'
          //   });
          //   const url = res;
          //   window.location.href = url; // todo 需要验证
          // };
          // getScheme();
          // getScheme = null;
        }
      });

      // this.$emit(EVENT_BUTTON_CLICK, this.goodsItem);
    }
  }
};
</script>
<style scoped lang="less">
.group-item {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 0;
  border-bottom: #ededed solid 1px;
  position: relative;
  &:last-of-type {
    border-bottom: none;
  }
  &-thumb {
    box-sizing: border-box;
    width: 108px;
    height: 108px;
    background-color: #f7f8f9;
    border-radius: 6px;
    overflow: hidden;
    position: relative;
    // padding: 3px;
    margin-right: 8px;
    .cr-image {
      width: 100%;
      height: 100%;
    }
  }
  &-content {
    flex: 1;
    height: 100%;
  }
  &-tag {
    position: absolute;
    top: 0;
    left: 0;
    font-size: @font-size-12;
    color: @white;
    width: 45px;
    height: 18px;
    line-height: 18px;
    text-align: center;
    background-image: linear-gradient(269deg, #ff5d00 12%, #ff1900 86%);
    border-radius: 6px 0 6px 0;
  }
  &-title {
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    line-height: 18px;
  }
  &-progress {
    width: 163px;
    overflow: hidden;
    border-radius: 5px;
    margin-top: 4px;
  }
  &-already {
    font-size: @font-size-12;
    color: #999999;
    margin-top: 4px;
    margin-bottom: 7px;
  }
  &-price {
    font-size: @font-size-14;
    color: #f70d0d;
    margin-bottom: 4px;
    font-weight: bold;
  }
  &-sup {
    font-size: @font-size-18;
  }

  &-delprice {
    color: #999999;
    font-size: @font-size-12;
  }
  .cr-button {
    width: 90px;
    height: 28px;
    line-height: 28px;
    border-radius: 15px;
    position: absolute;
    bottom: 12px;
    right: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &.disabled {
    opacity: 0.3;
    .group-item-price {
      color: #000;
    }
  }
}
</style>
