<template>
  <view>
    <view
      v-for="(item, index) in list"
      :key="index"
      class="Hl-container-item"
      @click="toDetail(item.goods, index)"
    >
      <view class="Hl-container-item-img">
        <image lazy-load class="Hlc-item-img" mode="aspectFit" :src="item.goods.goodsImage" />
      </view>
      <view class="Hlc-item-title">
        <text>{{ item.goods.goodsName }}</text>
      </view>
      <!-- 商品标签 -->
      <view class="Hlc-item-tag">
        <view 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"
            mode="heightFix"
            class="Hlc-item-tag-icon"
            :src="tag.icon"
            alt=""
          />
        </view>
      </view>
      <view class="Hlc-item-info">
        <view class="Hlc-item-info-price">
          <text>¥</text>
          <text>{{ item.goods.goodsSalePrice }}</text>
          <text v-if="item.goods.saleCount">已售{{ item.goods.saleCount }}件</text>
        </view>
        <view v-if="showCart" class="Hlc-item-info-cart" @click.stop="addToCart(item.goods)">
          <text class="iconfont icon-cart" />
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import { saTrackEvent } from "@/utils/sa.js";
import { addSkuToCart } from "@/api/home.api.js";
import { debounce } from "@/utils";
export default {
  name: "GoodsCard",
  props: {
    list: {
      type: Array,
      default: () => []
    },
    // isDelete: {
    //   type: Boolean,
    //   default: true
    // },
    // noShare: {
    //   type: Boolean,
    //   default: false
    // },
    showCart: {
      type: Boolean,
      default: false
    }
  },
  // data() {
  //   return {
  //     goodsMaskIdx: -1
  //   };
  // },
  // mounted() {
  //   console.log(this.list, "mounted");
  // },
  methods: {
    toDetail(good, index) {
      const currentPage = getCurrentPages();
      uni.reportAnalytics("c_selectionpagecommoditycard", {
        page_id: currentPage[currentPage.length - 1].route,
        commodity_id: good.goodsId,
        position_number: index
        // uuid: uni.getStorageSync("userAuthInfo").uuid || "",
        // channel_id: uni.getStorageSync("vccChannel") || ""
      });

      saTrackEvent("PD_YXMMAEC_UserClickSelectionPageCommodityCard", {
        page_id: currentPage[currentPage.length - 1].route,
        commodity_id: good.goodsId,
        position_number: index
      });
      debounce(
        uni.navigateTo({
          url: `/pages/product/goodDetail?skuNo=${
            good.goodsId
          }&count=1&receiverId=&goodsName=${encodeURIComponent(good.goodsName)}&goodsImage=${
            good.goodsImage
          }`
          // url: `/pages/product/goodDetail?skuNo=144652147824&count=1&receiverId=&goodsName=${good.goods.goodsName}`
        }),
        300
      );
    },
    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) {
        uni.showToast({
          title: "购物车添加成功",
          icon: "none"
        });
        uni.$emit("cartUpdate");
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/style/var.scss";
.Hl-container-item {
  padding: 20rpx;
  position: relative;
  margin-bottom: 25rpx;
  width: 340rpx;
  background: #fff;
  border-radius: 12rpx;
  &-img {
    height: 340rpx;
    display: flex;
    align-items: center;
  }
  .Hlc-item-img {
    width: 100%;
    height: 346rpx;
    border-radius: 12rpx;
  }
  .Hlc-item-tag {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    // margin: 3px 5px 0 14px;
    &-wrap {
      // margin-top: 3px;
    }

    &-normal {
      display: inline-block;
      font-size: 22rpx;
      box-sizing: border-box;
      border: 1rpx solid $red;
      color: $red;
      padding: 0px 4px;
      border-radius: 3px;
      margin-right: 5px;
      // line-height: 14px;
    }
    &-icon {
      height: 14px;
      margin-right: 5px;
    }
  }
  .Hlc-item-title {
    text {
      font-size: 26rpx;
      line-height: 36rpx;
      color: #333333;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }

  .Hlc-item-info {
    // margin: 0 20rpx 14rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 20rpx;
    &-price {
      display: flex;
      align-items: flex-end;
      text {
        &:nth-child(1) {
          font-size: 20rpx;
          color: #ec3333;
          font-weight: bold;
        }
        &:nth-child(2) {
          margin-right: 6rpx;
          font-size: 30rpx;
          line-height: 34rpx;
          color: #ec3333;
          font-weight: bold;
        }
        &:nth-child(3) {
          font-size: 19rpx;
          line-height: 26rpx;
          color: #999999;
          text-align: right;
        }
      }
    }
    &-cart {
      border-radius: 50%;
      // padding: 10rpx;
      width: 60rpx;
      height: 60rpx;
      text-align: center;
      line-height: 60rpx;
      background: $gray-1;
      .iconfont {
        color: $black;
      }
    }
  }
}
</style>
