<template>
  <view class="dec-goodSpecial" :style="className" v-if="bgImg" @tap="go">
    <image class="img" mode="widthFix" :src="bgImg" />
  </view>
</template>

<script>
// 设置分组
export default {
  inject: ["decAxios", "decImgDomain"],
  props: {
    tab: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      bgImg: "",
    };
  },
  computed: {
    className() {
      const arg = this.tab.arg;
      let arr = [];
      if (arg.space) arr.push(`margin: 0 ${arg.space}px`);
      if (arg.radius) arr.push(`borderRadius: ${arg.radius}px`);
      if (arg.bgColor && arg.bgColor != "transparent")
        arr.push(`background-color: ${arg.bgColor}`);
      if (arg.bgSize) arr.push(`padding: 0 ${50 - arg.bgSize * 0.5}%`);
      return arr.join(";");
    },
  },
  created() {
    const arg = this.tab.arg;
    const api = this.decAxios.getGoodFineData;
    const select = arg.id,
      that = this;
    if (select.id) {
      api({ id: select.id }).then(({ code, result }) => {
        that.bgImg = "";
        if (code == 200) {
          result = result || {};
          const endTime = (result.endTime || "2022-5-20").replace(/-/g, "/");
          const startTime = (result.startTime || "2022-5-20").replace(
            /-/g,
            "/"
          );
          if (that.initTime(endTime, startTime) != "end") {
            that.bgImg = that.decImgDomain(result.picture);
          }
        }
      });
    } else {
      this.bgImg = "";
    }
  },
  methods: {
    initTime(endTime, startTime) {
      const end = new Date(endTime),
        start = new Date(startTime),
        current = new Date();
      let state = "";
      if (current - start > 0) {
        if (end - current > 0) {
          state = "ing";
        } else {
          state = "end";
        }
      } else {
        state = "start";
      }
      return state;
    },
    go() {
      this.decAxios.decEventHandler(
        {
          moudle: "goodFine",
          type: "detail",
          desc: "跳优惠限购活动页",
        },
        this.tab.arg.id,
        null
      );
    },
  },
};
</script>
