<template>
  <transition name="move-right-left" mode="out-in">
    <div class="shop-hot" v-if="info.id" :key="info.id">
      <div class="img-box">
        <img
        :src="real_img"
        alt="" />
      </div>
      <div class="content">
        <div class="name">{{ info.name }}</div>
        <div class="type" v-if="info.type == 2">
          <div class="tag">
            {{ info.source == 1 ? '外链' : info.source == 2 ? '口令' : '' }}
          </div>
        </div>
        <div class="price-box">
          <div class="icon">￥</div>
          <span class="price">{{ info.price }}</span>
          <span class="shop_price" v-if="info.shop_price">
            <div class="icon">￥</div>
            {{ info.shop_price }}
          </span>
        </div>
      </div>
      <div class="pay">
        <div class="btn" @click="buyShop(info)">去看看</div>
      </div>
      <div class="close" @click="close"></div>
    </div>
  </transition>
</template>
<script>
import { getRecommendGoods, getTopGoodsInfo } from '@/plugins/API/apiShop';
import { mapGetters } from 'vuex';
export default {
  name: 'shop-hot',
  props: {
    menuId: {},
    isMarket: {
      type: Boolean,
      default: false
    },
  },
  data() {
    return {
      info: {},
    };
  },
  computed: {
    ...mapGetters({
      channelInfo: 'channelInfo'
    }),
    real_img() {
      const {info} = this
      const url = Array.isArray(info.real_img) ? info.real_img[0] : info.real_img
      return process.env.private ? url : url + `?x-oss-process=style/commodity_cover`
    }
  },
  mounted() {
    this.getRecommendGoods();
    this.$Bus.$on('bus-editShop', this.editShop);
    this.$Bus.$on('bus-deleteShop', this.deleteShop);
    this.$Bus.$on('bus-cancelShopBind', this.cancelShopBind);
    this.$Bus.$on('bus-setTopShop', this.setTopShop);
    this.$Bus.$on('bus-cancelTopShop', this.cancelTopShop);
    this.$Bus.$on('bus-setCaseTopGoods', this.setCaseTopGoods);
    this.$Bus.$on('bus-unSetCaseTopGoods', this.unSetCaseTopGoods);
  },
  methods: {
    async getRecommendGoods() {
      if (this.menuId) {
      const res = await getRecommendGoods({
        includeId: this.menuId,
        uin: this.channelInfo.uin,
        type: 1
      });
      if (res.code === 200 && res.errorCode === 0) {
        if (Object.keys(res.data).length !== 0) {
          this.info = res.data;
        }
      } else {
        this.$layer.msg(res.errorMessage);
        }
      }
      const topRes = await getTopGoodsInfo({
        includeId: this.channelInfo.id,
        uin: this.channelInfo.uin,
        type: 'live'
      });
      if (topRes.code === 200 && topRes.errorCode === 0) {
        if (Object.keys(topRes.data).length !== 0) {
          this.info = topRes.data;
          this.info.market = true;
        }
      } else {
        this.$layer.msg(topRes.errorMessage);
      }
    },
    editShop(data) {
      const { type, includeId, goodInfo } = data;
      if (type === 1 && this.menuId === includeId) {
        if (this.info.id === goodInfo.id) {
          this.info = goodInfo;
        }
      }
    },
    deleteShop(data) {
      const { type, includeId, goodInfo } = data;
      if (type === 1 && this.menuId === includeId) {
        if (this.info.id === goodInfo.id) {
          this.info = {};
        }
      }
    },
    cancelShopBind(data) {
      const { type, includeId, goodInfo } = data;
      if (type === 1 && this.menuId === includeId) {
        if (goodInfo.id.some(id => id === this.info.id)) {
          this.info = {};
        }
      }
    },
    setCaseTopGoods(data) {
        this.info = data;
    },
    unSetCaseTopGoods(data) {
        this.info = {};
    },
    setTopShop(data) {
      const { type, includeId, goodInfo } = data;
      if (type === 1 && this.menuId === includeId) {
        this.info = goodInfo;
      }
    },
    cancelTopShop(data) {
      const { type, includeId, goodInfo } = data;
      if (type === 1 && this.menuId === includeId) {
        if (this.info.id === goodInfo.id) {
          this.info = {};
        }
      }
    },
    buyShop() {
      this.$Bus.$emit(`${this.isMarket ? 'market' : 'bus' }-buyShop`, this.info);
    },
    close() {
      this.info = {};
    }
  },
  beforeDestroy() {
    this.$Bus.$off('bus-editShop', this.editShop);
    this.$Bus.$off('bus-deleteShop', this.deleteShop);
    this.$Bus.$off('bus-cancelShopBind', this.cancelShopBind);
    this.$Bus.$off('bus-setTopShop', this.setTopShop);
    this.$Bus.$off('bus-cancelTopShop', this.cancelTopShop);
  }
};
</script>

<style lang="less" scoped>
.shop-hot {
  position: relative;
  padding: 0.04rem 0.12rem;
  display: flex;
  align-items: center;
  height: 0.7rem;
  background: #fff;
  .img-box {
    margin-right: 0.12rem;
    width: 0.52rem;
    height: 0.52rem;
    border-radius: 0.04rem;
    img {
      display: block;
      width: 100%;
      height: 100%;
    }
  }
  .content {
    flex: 1;
    width: 0;
    height: 100%;
    display: flex;
    flex-direction: column;
    .name {
      flex: 0 0 auto;
      height: 0.2rem;
      line-height: 0.2rem;
      font-size: 0.12rem;
      color: #26242d;
      white-space: nowrap;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .type {
      flex: 1;
      .tag {
        display: inline-block;
        padding: 0 0.06rem;
        line-height: 0.18rem;
        border-radius: 0.02rem;
        border: 0.01rem solid #feebf1;
        font-size: 0.12rem;
        color: #e9355f;
      }
    }
    .price-box {
      flex: 0 0 auto;
      height: 0.2rem;
      line-height: 0.2rem;
      .icon {
        color: #e9355f;
        font-size: 0.14rem;
        margin-right: -.06rem;
        transform: scale(0.8);
        display: inline-block;
      }
      .price {
        color: #e9355f;
        font-size: 0.14rem;
      }
      .shop_price {
        .icon {
          color: #999999;
          font-size: 0.11rem;
          margin-right: -.06rem;
          transform: scale(0.8);
          display: inline-block;
        }
        text-decoration: line-through;
        color: #999999;
        font-size: 0.11rem;
      }
    }
  }
  .pay {
    position: absolute;
    bottom: 0.04rem;
    right: 0.04rem;
    .btn {
      display: inline-block;
      width: 0.72rem;
      height: 0.26rem;
      line-height: 0.26rem;
      text-align: center;
      background: linear-gradient(#ff7a9b, #ff366a);
      color: #fff;
      font-size: 0.12rem;
      border-radius: 0.15rem;
    }
  }
  .close {
    position: absolute;
    top: 0;
    right: 0;
    width: 0.18rem;
    height: 0.18rem;
    background: url(./img/icon-close.png) no-repeat center center;
    background-size: 0.14rem auto;
  }
}
</style>
