<template>
  <!-- 区分是否是固定高度的还是非固定高度-默认传了图片高度就是要统一高度 -->
  <view class="dec-box-good" :class="className" @tap.stop="go">
    <!-- 图片 角标 dec-row-flex -->
    <view class="good-img" :style="{ height: imgH }">
      <image
        v-if="imgH"
        class="imgH"
        :src="decImgDomain(good.targetImg)"
        mode="aspectFill"
      />
      <image
        v-else
        class="imgW"
        :src="decImgDomain(good.targetImg)"
        mode="widthFix"
      />
    </view>
    <!-- 名称 -->
    <view class="good-info dec-row-flex">
      <view class="good-title" :class="titleClass">{{ good.targetName }}</view>
    </view>
    <!-- 价格 购买 -->
    <view class="price-sale">
      {{ salePrice }}
    </view>
    <!-- 剩余多少件 -->
    <view class="good-stock" v-if="show.stock">
      <text class="text dec-truncate">剩余{{ good.stock || 0 }}件</text>
    </view>
    <!-- 到期时间 -->
    <!-- <view class="good-valid" v-if="show.stop">
      <text class="text dec-truncate">{{good.endTime}}到期</text>
    </view> -->
  </view>
</template>
<script>
export default {
  inject: ["decImgDomain", "decAxios"],
  props: {
    moudle: {
      type: String,
      default: "goodList",
    },
    good: {
      type: Object,
      default: () => {
        return {};
      },
    },
    show: {
      type: Object,
      default: () => {
        return {};
      },
    },
    imgArg: {
      type: String,
      default: "", // big middle small
    },
    imgH: {
      type: String,
      default: "",
    },
  },
  computed: {
    className() {
      let str = [];
      if (this.imgArg) str.push(this.imgArg);
      if (this.show.radius) str.push("radius");
      return str;
    },
    titleClass() {
      if (!this.imgH) {
        return "dec-truncate2";
      } else {
        return "dec-truncate";
      }
    },
    hasShop() {
      const hasShop = this.show.price || this.show.origin;
      const hasShop2 = this.show.bug && this.imgArg != "small";
      return hasShop || hasShop2;
    },
    salePrice() {
      let arr = [];
      if (this.good.integral && this.good.integral > 0)
        arr.push(this.good.integral + "积分");
      if (this.good.paymentAmount && this.good.paymentAmount > 0)
        arr.push(this.good.paymentAmount + "元");
      return arr.length > 0 ? arr.join("+") : 0;
    },
  },
  methods: {
    go() {
      this.decAxios.decEventHandler(
        {
          moudle: this.moudle,
          type: "detail",
          desc: "跳商品详情页",
        },
        this.good,
        null
      );
    },
    buy() {
      this.decAxios.decEventHandler(
        {
          moudle: this.moudle,
          type: "buy",
          desc: "购买商品",
        },
        this.good,
        null
      );
    },
  },
};
</script>
