<template>
  <div :class="['collection-container', !isFromParam ? 'home-tabar' : '']">
    <!-- 顶部导航和搜索区域 -->
    <div class="top-section">
      <!-- 顶部导航栏 -->
      <div class="nav-scroll-container">
        <el-menu :default-active="activeCategory" class="category-menu" mode="horizontal" :ellipsis="false"
          background-color="#f8f3e8" @select="handleCategorySelect">
          <el-menu-item index="all"><span class="category-text">全部</span></el-menu-item>
          <el-menu-item v-for="(category, index) in categories" :key="index" :index="category.id">
            <span class="category-text">{{ category.name }}</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 搜索框 -->
      <div class="search-container-collect">
        <el-input id="realy-input" v-model="searchText" placeholder="请输入要搜索的内容..." class="search-input" clearable>
          <template #suffix>
            <el-icon :size="16">
              <Search />
            </el-icon>
          </template>
        </el-input>
        <div id="vir-input" class="vir-input">请输入要搜索的内容...
          <el-icon class="vir-icon" :size="16">
            <Search />
          </el-icon>
        </div>
      </div>
    </div>

    <!-- 内容区域 -->
    <div class="content-section" ref="contentSection">
      <div class="collection-list">
        <div class="collection-item" v-for="(item, index) in artifactList" :key="item.artifactId || index">
          <div class="item-image-container">
            <img :src="getFieldValue(item, 'image') || getFieldValue(item, 'img') || '@/assets/indexPage/zhanweitu.png'"
              alt="藏品图片" class="item-image" />
            <div class="view-button" @click="goCollectDetail(item)">
              <span>查看</span><el-icon>
                <Right />
              </el-icon>
            </div>
            <div class="item-info">
              <div class="item-category">{{ getFieldValue(item, 'category') || '三维文物' }}</div>
              <div class="item-title">{{ getFieldValue(item, 'title') || '未知标题' }}</div>
              <div class="item-description">{{ getFieldValue(item, 'remark') || '暂无描述' }}</div>
            </div>
          </div>
        </div>

        <!-- 加载状态 -->
        <div v-if="loading" class="loading-container">
          <div class="loading-text">加载中...</div>
        </div>

        <!-- 没有更多数据 -->
        <div v-if="!hasMore && artifactList.length > 0" class="no-more-container">
          <div class="no-more-text">没有更多数据了</div>
        </div>

        <!-- 暂无数据 -->
        <div v-if="!loading && artifactList.length === 0" class="empty-container">
          <div class="empty-text">暂无数据</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, watch, nextTick } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { useCollectionApi } from '@/setup/useCollectionApi'

const store = useStore()
const router = useRouter()
const route = useRoute()

// 从vuex获取isFromParam
const isFromParam = computed(() => store.getters.getIsFrom)

// 检测是否为预览模式
const isPreviewMode = computed(() => {
  return route.query.preview === '1'
})

// 获取字段值的函数，预览模式下优先使用B字段
const getFieldValue = (item, fieldName) => {
  if (isPreviewMode.value && item[fieldName + 'B']) {
    return item[fieldName + 'B']
  }
  return item[fieldName]
}

// 使用典藏API组合式函数
const {
  artifactList,
  loading,
  hasMore,
  searchTitle,
  selectedType,
  searchArtifacts,
  changeType,
  loadMore,
  initData
} = useCollectionApi({ isPreviewMode })

// 搜索文本
const searchText = ref('')

// 当前激活的分类
const activeCategory = ref('all')

// 内容区域引用
const contentSection = ref(null)

// 分类数据
const categories = [
  { id: '1', name: '平面纸质类' },
  { id: '2', name: '棉麻丝绸类' },
  { id: '3', name: '专业器物类' },
]

// 监听搜索文本变化
watch(searchText, async (newValue) => {
  await searchArtifacts(newValue)
})

// 处理分类选择
const handleCategorySelect = async (index) => {
  activeCategory.value = index
  const typeId = index === 'all' ? 0 : parseInt(index)
  await changeType(typeId)
}

// 跳转到藏品详情页
const goCollectDetail = (item) => {
  const query = {
    id: item.artifactId
  }

  // 如果当前URL有preview=1参数，则在跳转时保留
  if (isPreviewMode.value) {
    query.preview = '1'
  }

  router.push({
    path: '/collectDetail',
    query: query
  })
}

// 滚动加载处理
const handleScroll = async () => {
  if (!contentSection.value || loading.value || !hasMore.value) return

  const { scrollTop, scrollHeight, clientHeight } = contentSection.value

  // 当滚动到底部附近时加载更多
  if (scrollTop + clientHeight >= scrollHeight - 50) {
    await loadMore()
  }
}

// 初始化
onMounted(async () => {
  await initData()

  // 添加滚动监听
  await nextTick()
  if (contentSection.value) {
    contentSection.value.addEventListener('scroll', handleScroll)
  }
})
</script>
<style>
.search-container-collect {
  padding: 10px 15px;
}

.search-container-collect .search-input {
  border-radius: 20px;
  border-color: #5B472E;
}

.vir-input {
  display: none;
  position: relative;
  border-radius: 20px;
  border: 1px solid #5B472E;
  height: 45px;
  align-items: center;
  color: #5B472E;
  padding: 0 15px;
}

.vir-icon {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
}

.search-container-collect .el-input__wrapper {
  height: 45px;
  border-radius: 20px;
  background-color: transparent;
}

.search-container-collect .el-input__inner::placeholder {
  color: rgba(88, 71, 53, 0.5);
  font-size: 16px;
}
</style>
<style lang="scss" scoped>
.collection-container {
  height: calc(100vh); // 减去底部导航栏高度
  padding-bottom: 78px; // 为底部导航栏留出空间
  background: url('@/assets/collection/bg.png') no-repeat;
  background-size: cover;
  // overflow-y: auto;
  box-sizing: border-box;
}

@media (min-width: 1024px) {
  .collection-container {
    width: 400px;
  }
}

.top-section {
  position: sticky;
  top: 0;
  z-index: 10;
  background-color: rgba(245, 242, 235, 0.95);
}

.nav-scroll-container {
  overflow-x: auto;
  white-space: nowrap;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  /* Firefox */

  &::-webkit-scrollbar {
    display: none;
    /* Chrome, Safari, Edge */
  }
}

:deep(.category-menu) {
  display: flex;
  border-bottom: none;
  background-color: transparent;
  margin-top: 10px;

  .el-menu-item {
    height: 40px;
    line-height: 40px;
    font-size: 16px;
    color: #584735;
    padding: 0 15px;
    border-bottom: none;

    &.is-active {
      color: #584735 !important;
      font-weight: bold;
      font-size: 20px;
      border-bottom: none;

      .category-text {
        display: inline-flex;
        border-bottom: 1px solid #F3B200;
      }
    }

    &:hover {
      color: #B1967B;
      background-color: transparent;
    }

    &:focus {
      color: #B1967B;
      background-color: transparent;
    }

    &:focus-visible {
      outline: none;
    }
  }
}

.content-section {
  padding: 15px;
  height: calc(100vh - 230px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.collection-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.collection-item {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.item-image-container {
  position: relative;
  height: 280px;
  overflow: hidden;

  .item-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .view-button {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 80px;
    height: 36px;
    top: 12px;
    right: 12px;
    background-color: rgba(0, 0, 0, 0.2);
    color: #fff;
    padding: 4px 8px;
    border-radius: 50px;
    font-size: 12px;
    border: 1px solid #fff;

    span {
      font-size: 14px;
      margin-right: 6px;
    }
  }
}

.item-info {
  position: absolute;
  width: 90%;
  bottom: 16px;
  left: 24px;

  .item-category {
    font-size: 14px;
    color: #fff;
  }

  .item-title {
    font-size: 36px;
    font-weight: bold;
    color: #fff;
  }

  .item-description {
    font-size: 14px;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

// 加载和状态样式
.loading-container,
.no-more-container,
.empty-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  margin: 20px 0;
}

.loading-text,
.no-more-text,
.empty-text {
  color: #584735;
  font-size: 14px;
  opacity: 0.7;
}

.loading-text {
  position: relative;

  &::after {
    content: '';
    display: inline-block;
    width: 12px;
    height: 12px;
    margin-left: 8px;
    border: 2px solid #584735;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>