<!-- components/ComparisonButton.vue -->
<template>
  <view 
    class="comparison-btn" 
    @click="showComparisonPanel = !showComparisonPanel"
    v-if="selectedProducts.length > 0"
  >
    <text class="count">{{ selectedProducts.length }}</text>
    <uni-icons type="list" size="24" color="#fff"></uni-icons>
    
    <!-- 已选产品面板 -->
    <view class="comparison-panel" v-if="showComparisonPanel">
      <view class="panel-header">
        <text class="panel-title">已选对比产品</text>
        <button class="clear-btn" @click="clearComparison">清空</button>
      </view>
      
      <view class="product-list">
        <view class="product-item" v-for="product in selectedProducts" :key="product.id">
          <image 
            class="product-logo" 
            :src="product.companyLogo" 
            :alt="product.companyName"
          ></image>
          <text class="product-name">{{ product.name }}</text>
          <button 
            class="remove-btn" 
            @click.stop="removeFromComparison(product.id)"
          >
            <uni-icons type="close" size="18" color="#fff"></uni-icons>
          </button>
        </view>
      </view>
      
      <button 
        class="compare-btn" 
        @click="goToComparisonResult"
        :disabled="selectedProducts.length < 2"
      >
        去对比
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { useProductComparison } from '@/util/useProductComparison';

// 引入对比逻辑
const {
  selectedProducts,
  removeFromComparison,
  clearComparison,
  goToComparisonResult
} = useProductComparison();

// 控制面板显示/隐藏
const showComparisonPanel = ref(false);
</script>

<style scoped>
.comparison-btn {
  position: fixed;
  right: 30rpx;
  bottom: 150rpx;
  width: 80rpx;
  height: 80rpx;
  background-color: #007aff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.2);
  z-index: 999;
}

.count {
  position: absolute;
  top: -10rpx;
  right: -10rpx;
  width: 40rpx;
  height: 40rpx;
  background-color: #ff3b30;
  color: #fff;
  font-size: 22rpx;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.comparison-panel {
  position: absolute;
  right: 0;
  bottom: 90rpx;
  width: 500rpx;
  background-color: #fff;
  border-radius: 15rpx;
  box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
  padding: 15rpx;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 15rpx;
  border-bottom: 1px solid #eee;
  margin-bottom: 15rpx;
}

.panel-title {
  font-size: 28rpx;
  font-weight: bold;
  color: #333;
}

.clear-btn {
  font-size: 24rpx;
  color: #007aff;
  background-color: transparent;
  padding: 0;
}

.product-list {
  max-height: 300rpx;
  overflow-y: auto;
  margin-bottom: 20rpx;
}

.product-item {
  display: flex;
  align-items: center;
  padding: 10rpx 0;
  border-bottom: 1px solid #f5f5f5;
}

.product-logo {
  width: 50rpx;
  height: 50rpx;
  border-radius: 5rpx;
  margin-right: 10rpx;
}

.product-name {
  flex: 1;
  font-size: 26rpx;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.remove-btn {
  width: 40rpx;
  height: 40rpx;
  border-radius: 50%;
  background-color: #ff3b30;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.compare-btn {
  width: 100%;
  background-color: #007aff;
  color: #fff;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
}

.compare-btn:disabled {
  background-color: #ccc;
}
</style>