<template>
  <view class="dec-img-good" :class="className" @tap.stop="go">
    <image
      class="good-img"
      v-if="imgH"
      :style="{ height: imgH }"
      :src="decImgDomain(good.productImg)"
      mode="aspectFill"
    />
    <image
      class="good-img"
      v-else
      :src="decImgDomain(good.productImg)"
      :mode="imgArg == 'big' ? 'widthFix' : 'aspectFill'"
    />
    <view class="good-info" :class="show.nameStyle">
      <view class="good-title dec-truncate2" :class="show.nameStyle">{{
        good.productName
      }}</view>
      <view class="price-wrap dec-row-flex j-between" v-if="!simple">
        <view class="dec-row-flex a-bottom" v-if="show.price">
          <view class="dot">￥</view>
          <view class="sale">{{ good.salePrice || 0 }}</view>
          <view class="origin" :class="show.nameStyle" v-if="opriceShow"
            >￥{{ good.smarketPrice }}</view
          >
        </view>
        <view class="good-reward" v-if="show.reward && predictReward"
          >预计返{{ predictReward }}元</view
        >
      </view>
    </view>
  </view>
</template>

<script>
export default {
  inject: ["decImgDomain", "decAxios"],
  props: {
    moudle: {
      type: String,
      default: "goodList",
    },
    simple: {
      type: Boolean,
      default: true,
    },
    show: {
      type: Object,
      default: () => {
        return {};
      },
    },
    good: {
      type: Object,
      default: () => {
        return {};
      },
    },
    imgArg: {
      type: String,
      default: "",
    },
    imgH: {
      type: String,
      default: "",
    },
  },
  computed: {
    className() {
      let str = [];
      if (this.imgArg) str.push(this.imgArg);
      if (this.show.radius) str.push("radius");
      return str;
    },
    predictReward() {
      let val = this.good.predictReward || 0;
      val = parseInt(val * 100);
      if (val > 0) {
        val = `${val / 100}`;
        const arr = val.split(".");
        if (arr[1]) {
          val = arr[1].length == 1 ? `${arr[0]}.${arr[1]}0` : val;
        } else {
          val = `${val}.00`;
        }
      } else {
        val = "";
      }
      return val;
    },
    opriceShow() {
      if (this.show.origin && this.good.smarketPrice) {
        return this.good.smarketPrice > this.good.salePrice;
      }
      return false;
    },
  },
  methods: {
    go() {
      this.decAxios.decEventHandler(
        {
          moudle: this.moudle,
          type: "buy",
          desc: "跳商品详情页",
        },
        this.good,
        null
      );
    },
  },
};
</script>
