<template>
  <div v-if="showPanel" class="address-selection-panel">
    <!-- 提示区域 -->
    <div class="shop-prompt">
      <van-cell title="请选择机柜地址：" center />
    </div>

    <!-- 商店网格 -->
    <div class="shop-grid">
      <div
        v-for="shop in shopList"
        :key="shop.shopId"
        class="shop-item"
        @click="handleShopSelect(shop.shopId)"
      >
        <van-image
          :src="toHttpsUrl(shop.coverImg) || `${publicPath}product-image.png`"
          class="shop-cover-img"
          fit="cover"
        />
        <div class="shop-info">
          <van-icon name="shop-o" size="16" class="shop-icon" />
          <div class="shop-name van-ellipsis">{{ shop.shopName }}</div>
        </div>
      </div>
      <!-- 占位列，确保每行4列 -->
      <div
        v-for="n in (4 - (shopList.length % 4))"
        :key="'placeholder-' + n"
        v-if="shopList.length % 4 !== 0"
        class="shop-item placeholder-col"
      ></div>
    </div>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-state">
      <van-loading type="spinner" />
      <span class="loading-text">正在加载地址列表...</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { publicPath } from "@/common/utils/path"
import { toHttpsUrl } from "@/common/utils/common"

// Props
interface Props {
  shopList: any[]
  loading?: boolean
  showPanel?: boolean
}

const props = withDefaults(defineProps<Props>(), {
  shopList: () => [],
  loading: false,
  showPanel: true
})

// Emits
const emit = defineEmits<{
  (e: 'select', shopId: number): void
}>()

// 处理商店选择
const handleShopSelect = (shopId: number) => {
  emit('select', shopId)
}
</script>

<style scoped>
.address-selection-panel {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw !important;
  height: 100vh !important;
  max-width: 100vw !important;
  max-height: 100vh !important;
  overflow: hidden;
  background: #f5f5f5;
}

.shop-prompt {
  flex-shrink: 0;
  margin: 8px 16px;
  height: 44px !important;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}



.placeholder-col {
  visibility: hidden;
  pointer-events: none;
}

.shop-grid {
  flex: 1;
  margin: 8px 16px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  row-gap: 8px;
  column-gap: 6px;
  overflow-y: auto;
  align-content: start;
}

.shop-item {
  min-height: 150px;
  padding: 0;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
}

.shop-item:active {
  transform: scale(0.98);
}

.shop-cover-img {
  width: 100%;
  height: 100px;
  flex-shrink: 0;
  object-fit: cover;
}

.shop-info {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  flex: 1;
}

.shop-icon {
  margin-right: 4px;
  flex-shrink: 0;
}

.shop-name {
  white-space: normal;
  word-break: break-word;
  font-size: 12px;
  color: #333;
  font-weight: 500;
  margin: 0;
  text-align: center;
  line-height: 1.3;
}

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 64px 0;
  text-align: center;
  flex-shrink: 0;
}

.loading-text {
  margin-top: 24px;
  font-size: 14px;
  color: #666;
}
</style>