<template>
  <view class="dec-discount-s-good" :class="className" @tap="go">
    <view class="image" :style="{ height: imgH }">
      <image
        class="img"
        mode="aspectFill"
        :src="decImgDomain(good.productImg || '')"
        alt=""
      />
    </view>
    <view class="flex time" v-if="imgArg != 'small' && show.time">
      <text class="start">{{ inLine }}</text>
      <view class="limit">
        <template v-if="inLine2">
          <text>{{ inLine2 }}</text
          ><br />
        </template>
        <text>{{ endTime }}</text>
      </view>
    </view>
    <view class="info">
      <view class="title dec-truncate2" v-if="show.title">
        {{ good.productName }}
      </view>
      <view class="desc dec-truncate" v-if="show.desc">{{
        good.introduction || good.productName
      }}</view>
      <!-- 价格 购买 -->
      <view
        class="price-wrap dec-row-flex a-bottom"
        v-if="show.price || show.origin"
      >
        <text class="dot" v-if="show.price">¥</text>
        <text class="sale" v-if="show.price">
          {{ good.activityPrice || 0 }}</text
        >
        <text class="origin" v-if="opriceShow">￥{{ good.smarketPrice }}</text>
      </view>
    </view>
  </view>
</template>
<script>
export default {
  inject: ["decImgDomain", "decAxios"],
  props: {
    good: {
      type: Object,
      default: () => {
        return {};
      },
    },
    show: {
      type: Object,
      default: () => {
        return {};
      },
    },
    imgArg: {
      type: String,
      default: "",
    },
    imgH: {
      type: String,
      default: "auto",
    },
    time: {
      type: Object,
      default: () => {
        return {};
      },
    },
    state: {
      type: String,
      default: "",
    },
  },
  computed: {
    endTime() {
      if (!this.time.hour) {
        return "活动" + (this.state || "未开始");
      }
      if (this.imgArg == "big") {
        if (this.time.day) {
          return `${this.time.day} 天 ${this.time.hour} 时 ${this.time.minutes} 分 ${this.time.seconds}`;
        }
        return `${this.time.hour} 时 ${this.time.minutes} 分 ${this.time.seconds}`;
      } else {
        const arr = (this.good.limit || "--").split("-");
        if (this.time.day) {
          return `${this.time.day}天:${this.time.hour}:${this.time.minutes}:${this.time.seconds}`;
        }
        return `${this.time.hour}:${this.time.minutes}:${this.time.seconds}`;
      }
    },
    inLine() {
      let name = "";
      if (this.imgArg == "big") {
        name = ["已结束", "未开始"].includes(this.state) ? "" : "限时抢购";
      } else {
        name = ["已结束", "未开始"].includes(this.state) ? "" : "距结束";
      }
      return name;
    },
    inLine2() {
      let name = "";
      if (this.imgArg == "big") {
        name = ["已结束", "未开始"].includes(this.state)
          ? "活动状态"
          : "距结束仅剩";
      }
      return name;
    },
    remain() {
      return parseInt(this.good.activityStock - this.good.saleCount);
    },
    className() {
      let str = [];
      if (this.imgArg) str.push(this.imgArg);
      if (this.show.border) str.push(this.show.border);
      if (this.show.radius) str.push("radius");
      return str;
    },
    opriceShow() {
      if (this.show.origin && this.good.smarketPrice) {
        return this.good.smarketPrice > this.good.activityPrice;
      }
      return false;
    },
  },
  methods: {
    go() {
      this.decAxios.decEventHandler(
        {
          moudle: "goodDiscount",
          type: "detail",
          desc: "跳折扣商品详情页",
        },
        this.good,
        null
      );
    },
    buy() {
      this.decAxios.decEventHandler(
        {
          moudle: "goodDiscount",
          type: "buy",
          desc: "购买折扣商品",
        },
        this.good,
        null
      );
    },
  },
};
</script>
