<template>
  <view class="area-product-list" :class="sizeClass">
    <view class="area-header">
      <view class="header-bg"></view>
      <text class="area-title">{{ title }}</text>
      <u-button
        v-if="list.length > 0"
        type="primary"
        size="mini"
        shape="circle"
        :custom-style="{
          background: '#ffffff',
          border: 'none',
          position: 'relative',
          zIndex: 1,
          height: '46rpx',
          lineHeight: '46rpx',
          width: 'fit-content',
          padding: '0 12rpx',
          margin: '0',
        }"
        @click="goToMore"
      >
        <view class="more-btn-content">
          <text class="more-text">更多</text>
          <u-icon name="arrow-right" color="#005bac" size="14"></u-icon>
        </view>
      </u-button>
    </view>
    <view class="product-container">
      <view
        class="product-item"
        v-for="item in list"
        :key="item.pkWares"
        @click="handleProductClick(item)"
      >
        <image :src="item.cover1" class="product-image" mode="aspectFill" />
        <view class="product-info">
          <view class="product-name">{{ item.waresName }}</view>
          <view class="product-price-row">
            <view class="product-price">
              {{ formatCurrency(item.waresPrice) }}
            </view>
            <u-button
              v-if="false"
              type="primary"
              size="mini"
              shape="circle"
              :custom-style="{
                background: '#005bac',
                margin: '0px',
                width: '48rpx',
                height: '48rpx',
                minWidth: '48rpx',
              }"
              @click.native.stop="handleAddToCart(item)"
            >
              <u-icon name="plus" color="#ffffff" size="12"></u-icon>
            </u-button>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { numberToCurrencyNo, isLocal, isLocaled } from '@/util/numberToCurrency'
import { addShopping } from '@/config/goods'
import { formatCurrency } from '@/util/index.js'
export default {
  props: {
    title: {
      type: String,
      default: '',
    },
    list: {
      type: Array,
      default: () => [],
    },
    specialAreaId: {
      type: [Number, String],
      default: '',
    },
    size: {
      type: String,
      default: 'normal', // 'normal' or 'small'
      validator: value => ['normal', 'small'].includes(value),
    },
  },
  computed: {
    sizeClass() {
      return this.size === 'small' ? 'area-product-list--small' : ''
    },
  },
  methods: {
    isLocaled,
    formatCurrency,
    formatPrice(price) {
      // 使用numberToCurrencyNo和isLocal函数来格式化价格
      return isLocal(numberToCurrencyNo(price))
    },
    goToMore() {
      // 跳转到专区详情页面
      uni.navigateTo({
        url: `/pages/specialArea/index?id=${this.specialAreaId}`,
      })
    },
    handleProductClick(item) {
      // 商品点击事件，可根据需求实现跳转或其他逻辑
      this.$emit('product-click', item)
    },
    handleAddToCart(item) {
      const params = {
        pkCountry: this.pkCountry,
        specialArea: item.specialArea,
        number: 1,
        waresCode: item.waresCode,
        productGroup: item.productGroup,
      }

      addShopping(params).then(res => {
        if (res.code == 200) {
          uni.showToast({
            title: '购物车添加成功',
            icon: 'success',
            mask: true,
          })
          this.$store.dispatch('getCarLength')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.area-product-list {
  background-color: #ffffff;
  padding: 20rpx;
  padding-top: 0;
  border-radius: 16rpx;
  margin-bottom: 24rpx;
  position: relative;
  transition: all 0.3s ease; // Add transition for smoother size changes
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08); // 添加阴影效果

  &--small {
    padding: 16rpx;
    padding-top: 0;
    margin-bottom: 20rpx;
    border-radius: 12rpx;

    .area-header {
      margin-bottom: 20rpx;
      padding: 16rpx 0;
      height: 40rpx;
    }

    .header-bg {
      left: -16rpx;
      right: -16rpx;
      border-top-left-radius: 6px;
      border-top-right-radius: 6px;
    }

    .area-title {
      font-size: 28rpx;
    }

    .more-btn-content {
      // Adjust button style directly as u-button size='mini' might not be flexible enough
      :deep(.u-button) {
        height: 40rpx !important;
        line-height: 40rpx !important;
        padding: 0 10rpx !important;
      }
      .more-text {
        font-size: 20rpx;
        margin-right: 2rpx;
      }
      :deep(.u-icon) {
        font-size: 12px !important; // u-icon size might need !important
      }
    }

    .product-container {
      margin: 0 -8rpx;
    }

    .product-item {
      padding: 0 8rpx;
    }

    .product-image {
      width: 180rpx;
      height: 180rpx;
      border-radius: 6rpx;
    }

    .product-info {
      margin-top: 10rpx;
    }

    .product-name {
      font-size: 24rpx;
      margin-bottom: 6rpx;
    }

    .product-price-row .product-price {
      font-size: 20rpx;
    }
  }
}

.area-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24rpx;
  position: relative;
  padding: 20rpx 0;
  height: 46rpx;
}

.header-bg {
  position: absolute;
  top: 0;
  left: -20rpx;
  right: -20rpx;
  height: 100%;
  background: linear-gradient(to bottom, #add8e6, #ffffff);
  z-index: 0;
  border-top-left-radius: 8px;
  border-top-right-radius: 8px;
}

.area-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333333;
  position: relative;
  z-index: 1;
}

.more-btn-content {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  flex-grow: 0;
}

.more-text {
  font-size: 24rpx;
  color: #005bac;
  margin-right: 4rpx;
}

.product-container {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -10rpx;
}

.product-item {
  width: 33.33%;
  padding: 0 10rpx;
  box-sizing: border-box;
}

.product-image {
  width: 200rpx;
  height: 200rpx;
  border-radius: 8rpx;
  background-color: #f5f5f5;
  object-fit: cover;
  display: block;
  margin: 0 auto;
}

.product-info {
  margin-top: 12rpx;
  position: relative;
}

.product-name {
  font-size: 28rpx;
  color: #333;
  margin-bottom: 8rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-price-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .product-price {
    font-size: 24rpx;
    font-weight: bold;
    color: #f82c1a;
  }
}
</style>
