<template>
  <div
    class="goods-discount-con flex-default"
    @click="$emit('click')"
    v-show="showDiscount"
  >
    <div class="title">活动</div>
    <div class="content">
      <div class="item flex-default" v-show="showCoupon">
        <div class="promotion">领券</div>
        <div class="rule">{{ couponLabel }}</div>
      </div>
      <div class="item flex-default" v-show="showPromotion">
        <div class="promotion">{{ promotionLabel.type }}</div>
        <div class="rule">{{ promotionLabel.desc }}</div>
      </div>
      <div class="item flex-default" v-show="showExpress">
        <div class="express">包邮</div>
        <div class="rule">单件包邮</div>
      </div>
    </div>
    <m-icon icon="icon-more" :size="10" className="more-icon" />
  </div>
</template>

<script>
import { PromotionType, PromotionTypeLabel } from "@/enums";
export default {
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          couponList: [],
          promotionData: null,
        };
      },
    },
  },
  data() {
    return {};
  },
  computed: {
    showDiscount() {
      return this.showCoupon || this.showPromotion;
    },
    showCoupon() {
      return this.data.couponList && this.data.couponList.length > 0;
    },
    showPromotion() {
      return this.data.promotionData && this.data.promotionData.promotionId;
    },
    showExpress() {
      return false;
    },
    couponLabel() {
      if (this.showCoupon) {
        return this.data.couponList
          .map((c) => {
            let { conditionVal, couponVal } = c;
            return conditionVal == 0
              ? `减${couponVal}`
              : `满${conditionVal}减${couponVal}`;
          })
          .join(", ");
      }
      return "";
    },
    promotionLabel() {
      let type = "",
        desc = "";

      if (this.showPromotion) {
        let {
          type: pType,
          marketingPromotionRules: ruleList,
        } = this.data.promotionData;
        type = PromotionTypeLabel[pType];
        
        if (pType == PromotionType.FULL_REDUCTION) {
          desc = ruleList
            .map((r) => `满${r.conditionVal}减${r.promotionVal}`)
            .join(", ");
        } else if (pType == PromotionType.EVERY_REDUCTION) {
          desc = `每满${ruleList[0].conditionVal}减${ruleList[0].promotionVal}`;
        } else if (pType == PromotionType.DIRECT_REDUCTION) {
          desc = `直降${ruleList[0].promotionVal}`;
        } else if (pType == PromotionType.DISCOUNT) {
          desc = `${ruleList[0].promotionVal}折`;
        } else if (pType == PromotionType.FULL_DISCOUNT) {
          desc = ruleList
            .map((r) => `满${r.conditionVal}元${r.promotionVal}折`)
            .join(", ");
        }
      }

      return {
        type,
        desc,
      };
    },
  },
};
</script>

<style lang="less">
.goods-discount-con {
  padding: 14px 16px;
  background: #ffffff;
  border-radius: 10px;
  margin-bottom: 10px;
  align-items: flex-start;
  > .title {
    color: #140603;
    font-weight: bold;
    font-size: 12px;
    margin-right: 16px;
    line-height: 17px;
  }
  > .content {
    flex: 1;
    .item {
      margin-top: 6px;
      align-items: flex-start;
      flex-wrap: wrap;
      &:nth-of-type(1) {
        margin-top: 0;
      }
    }
    .coupon {
      line-height: 13px;
      font-size: 9px;
      margin-right: 10px;
    }
    .promotion {
      color: #ffffff;
      font-size: 9px;
      line-height: 13px;
      border-radius: 3px;
      background: linear-gradient(295deg, #f07c5b 0%, #dd2e41 100%);
      margin-right: 10px;
      margin-top: 5rpx;
      padding: 0 3px;
    }
    .express {
      color: #dd2e41;
      font-size: 9px;
      line-height: 11px;
      border-radius: 3px;
      border: 1px solid #dd2e41;
      background: #ffffff;
      margin-right: 10px;
      margin-top: 3px;
      padding: 0 2px;
    }
    .rule {
      flex: 1;
      color: #2b2b3a;
      font-size: 12px;
      line-height: 17px;
    }
  }
  .more-icon {
    margin-top: 3px;
  }
}
</style>