<template>
  <div>
    <div
      v-for="(item, index) in list"
      :key="index"
      class="Hl-container-item"
      @click="toDetail(item.goods, index)"
    >
      <div class="Hl-container-item-img">
        <cr-image class="Hlc-item-img" :src="item.goods.goodsImage | imogr({ w: 173, h: 173 })" />
      </div>
      <div class="Hlc-item-title">
        <span>{{ item.goods.goodsName }}</span>
      </div>
      <div v-if="item.goods.tagList.length" class="Hlc-item-tag">
        <div v-for="(tag, i) in item.goods.tagList" :key="i" class="Hlc-item-tag-wrap">
          <span v-if="tag.type == 2" :key="i" class="Hlc-item-tag-normal">{{ tag.name }}</span>
          <img v-if="tag.type == 1" class="Hlc-item-tag-icon" :src="tag.icon" />
        </div>
      </div>
      <div class="Hlc-item-info">
        <div class="Hlc-item-info-price">
          <span>¥</span>
          <span>{{ item.goods.goodsSalePrice }}</span>
          <span>已售{{ item.goods.saleCount }}件</span>
        </div>
        <div v-if="showCart" class="Hlc-item-info-cart" @click.stop="addToCart(item.goods)">
          <i class="iconfont icon-cart" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { addSkuToCart } from '@/api/shopCart.api.js';
export default {
  name: 'GoodsCard',
  props: {
    list: {
      type: Array,
      default: () => []
    },
    showCart: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    toDetail(good) {
      this.$router.push({
        path: '/goodDetail',
        query: {
          count: 1,
          skuNo: good.goodsId,
          goodsName: good.goodsName,
          // receiverId: '',
          goodsImage: good.goodsImage
        }
      });
    },
    async addToCart(goods) {
      const params = {
        shopCartBaseList: [
          {
            skuId: goods.goodsId,
            skuNum: 1,
            skuSource: goods.goodsType,
            type: 1 // 1添加商品到购物车，2、购物车增加商品数量，3、购物车减数量，4、购物车删除商品，5、购物车数据变更
          }
        ],
        selected: true
      };
      const [data] = await addSkuToCart(params);
      if (data) {
        this.$toast('购物车添加成功');
      }
    }
  }
};
</script>
<style lang="less" scoped>
.Hl-container-item {
  padding: 10px;
  position: relative;
  margin-bottom: 12px;
  width: 170px;
  background: @white;
  border-radius: 6px;
  box-sizing: border-box;
  &-img {
    height: 170px;
    display: flex;
    align-items: center;
  }
  .Hlc-item-img {
    width: 100%;
    height: 173px;
    border-radius: 6px;
  }
  .Hlc-item-tag {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    &-normal {
      .text-size(11);
      display: inline-block;
      box-sizing: border-box;
      border: 1px solid @red;
      color: @red;
      padding: 0px 4px;
      border-radius: 3px;
      margin-right: 5px;
    }
    &-icon {
      height: 14px;
      margin-right: 5px;
    }
  }
  .Hlc-item-title {
    span {
      .text-size(13);
      color: @black;
      line-height: 18px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

  .Hlc-item-info {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 10px;
    &-price {
      display: flex;
      align-items: flex-end;
      span {
        &:nth-child(1) {
          .text-size(12);
          color: @red-light;
          font-weight: bold;
        }
        &:nth-child(2) {
          .text-size(15);
          margin-right: 3px;
          line-height: 17px;
          color: @red-light;
          font-weight: bold;
        }
        &:nth-child(3) {
          .text-size(9);
          color: @gray-4;
          text-align: right;
        }
      }
    }
    &-cart {
      border-radius: 50%;
      width: 30px;
      height: 30px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: @gray-1;
      .iconfont {
        color: @black;
      }
    }
  }
}
</style>
