<template>
  <view class="home-list">
    <view class="home-list-container">
      <view v-for="(item, index) in goodsList" :key="index" class="Hl-container-item">
        <image
          lazy-load
          class="Hlc-item-img"
          mode="aspectFit"
          :src="item.goods.goodsImage"
          @click="goDetail(item)"
        />
        <view class="Hlc-item-info">
          <view class="Hlc-item-info-title" @click="goDetail(item)">
            <text>{{ item.goods.goodsName }}</text>
          </view>
          <view class="Hlc-item-tag" @click="goDetail(item)">
            <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-count">
            <div @click="goDetail(item)">
              <view class="Hlc-item-info-count-left">
                <text>¥{{ item.goods.goodsSalePrice }}</text>
                <!-- <text v-if="item.goods.goodsPrice">¥{{ item.goods.goodsPrice }}</text> -->
              </view>
              <view class="Hlc-item-info-count-right">
                <text v-if="item.goods.saleCount">已售{{ item.goods.saleCount }}件</text>
              </view>
            </div>
            <view class="Hlc-item-info-count-icon">
              <text class="iconfont icon-cart" @click="addProduct(item)" />
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import { debounce } from "@/utils";
import api from "../api/addProduct.api.js";
export default {
  name: "GoodsCard",
  props: {
    goodsList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {};
  },
  methods: {
    goDetail(good) {
      debounce(
        uni.navigateTo({
          url: `/pages/product/goodDetail?skuNo=${
            good.goods.goodsId
          }&count=1&receiverId=&goodsName=${encodeURIComponent(good.goods.goodsName)}&goodsImage=${
            good.goods.goodsImage
          }`
        }),
        300
      );
    },

    async addProduct(good) {
      const shopCartBaseList = [
        {
          skuId: good.goods.goodsId,
          skuNum: 1,
          skuSource: good.goods.goodsType,
          type: 1
        }
      ];
      await api.addProduct({ shopCartBaseList, selected: true });
      this.$emit("addProduct", good.goods.goodsSalePrice);
      uni.showToast({
        title: "购物车添加成功",
        icon: "none"
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.home-list {
  background: $white;
  padding: 0 $space-md * 2;
  &-container {
    .Hl-container-item {
      display: flex;
      margin: 0 0 40rpx 0;
      .Hlc-item-img {
        width: 270rpx;
        height: 270rpx;
        border-radius: 12rpx;
        // background: #aaa;
      }
      .Hlc-item-info {
        width: 450rpx;
        height: 260rpx;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-left: 20rpx;
        &-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;
          }
        }
        &-count {
          display: flex;
          justify-content: space-between;
          &-left {
            color: #ec3333;
            margin: 1px;
            text {
              &:nth-child(1) {
                margin-right: 10rpx;
                font-size: $uni-font-size-lg;
                line-height: 34rpx;
                color: #ec3333;
                font-weight: bold;
              }
              &:nth-child(2) {
                font-size: $uni-font-size-tiny;
                line-height: 24rpx;
                color: #999999;
                text-align: right;
                text-decoration: line-through;
              }
            }
          }
          &-right {
            font-size: $uni-font-size-tiny;
            line-height: 24rpx;
            color: #999999;
          }
          &-icon {
            align-items: center;
            height: 28px;
            width: 28px;
            line-height: 28px;
            padding-left: 4px;
            border: 1px solid #f2f3f5;
            background-color: #f2f3f5;
            border-radius: 50%;
          }
        }
      }
      .Hlc-item-tag {
        display: flex;
        flex-wrap: wrap;
        justify-content: flex-start;
        height: 60rpx;
        margin-top: -40rpx;

        &-normal {
          // display: inline-block;
          // font-size: 22rpx;
          // box-sizing: border-box;
          // border: 1px solid #ec1500;
          // color: #ec1500;
          // padding: 0px 4px;
          // border-radius: 3px;
          // margin-right: 5px;
          // // line-height: 14px;
          height: 32rpx;
          /* #ifdef MP-ALIPAY */
          line-height: 30rpx;
          /* #endif */
          display: flex-inline;
          font-size: $uni-font-size-tiny;
          justify-content: center;
          align-items: center;
          margin-top: 8rpx;
          border-radius: $uni-border-radius-base;
          color: $font-color-search;
          padding: 0 $padding-sm;
          margin-right: $padding-sm;
          border: 1px solid $font-color-search;
          ::v-deep .uni-tag {
            width: 10rpx;
          }
        }
        &-icon {
          height: 15px;
          margin-right: 5px;
        }
      }
    }
  }
}
</style>
