<template>
  <div class="collectpage">
    <!-- 搜索页面 -->
    <div v-if="showSearchPage" class="search-page">
      <SearchPageComponent
        :initialSearch="searchParams"
        :categoryOptions="categoryOptions"
        :levelOptions="levelOptions"
        :materialOptions="materialOptions"
        :eraOptions="eraOptions"
        :orderByOptions="orderByOptions"
        @search="handleSearch"
        @close="closeSearchPage"
      />
    </div>

    <!-- 主要内容 -->
    <div v-if="!showSearchPage" class="main-content">
            <!-- 搜索输入框 -->
      <div class="search-container">
        <div class="search-wrapper">
          <!-- 搜索输入框和检索按钮 -->
          <div class="search-input-wrapper">
            <el-input v-model="searchParams.searchText" placeholder="请输入关键词..." class="search-input cut-corner-input" @keyup.enter="quickSearch">
              <template #suffix>
                <el-button @click="quickSearch" class="quick-search-btn">
                  <img src="@/assets/img/icon_search.png" alt="检索" class="search-icon" />
                </el-button>
              </template>
            </el-input>
            <button @click="openSearchPage" class="search-btn">
              <img src="@/assets/img/icon_select.png" alt="检索" class="search-icon" />
              <span>检索</span>
            </button>
          </div>

          <!-- 复选框区域 -->
          <div class="checkbox-group">
            <el-checkbox @change="quickSearch" v-model="filters.hasImage" label="二维文物" />
            <el-checkbox @change="quickSearch" v-model="filters.hasModel" label="三维文物" />
          </div>
        </div>
      </div>

      <!-- 搜索结果列表 -->
      <div v-loading="loading" class="search-results" @scroll="handleScroll">
        <div
          v-for="item in searchResults"
          :key="item.id"
          class="result-item"
          :class="{ 'loading-waterfall': item.isLoading }"
          @click="viewDetails(item)"
        >
          <div class="waterfall-mask" v-if="item.isLoading"></div>
          <img :src="item.image" :alt="item.name" class="result-image" />
          <div class="result-info">
            <div class="result-name">{{ item.name }}</div>
            <div class="more-info"><img src="@/assets/img/icon_collect_more.png" alt="更多" class="more-icon" /></div>
          </div>
        </div>

        <!-- 加载更多 -->
        <div class="loading-more" v-if="loading">
          <span>加载中...</span>
        </div>
      </div>

      <!-- 暂无数据提示 -->
      <div v-if="searched && searchResults.length === 0" class="no-data">
        <p>暂无符合条件的藏品</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import SearchPageComponent from './components/SearchPageComponent.vue'
import getBookCountApi from '@/api'
import { useStore } from 'vuex'

const store = useStore()

defineOptions({
  name: 'CollectPage'
})
const imgUrl = import.meta.env.VITE_COS_BASE_URL
const router = useRouter()

// 响应式数据
const showSearchPage = ref(false)
const searchParams = ref({searchText: '', dim: 3, orderBy: 'grade', level: '', category: '', material: '', era: ''})
const searchResults = ref([]) // 当前显示的搜索结果（分页显示）
const searched = ref(false)
const loading = ref(false)

// 前端分页相关数据
const allSearchResults = ref([]) // 所有搜索结果数据（API返回的完整数据集）
const currentPage = ref(1)
const pageSize = 20 // 每页显示20条

const filters = ref({
  hasImage: false,
  hasModel: false
})

// 所有选项列表 - 从接口获取
const levelOptions = ref([])
const categoryOptions = ref([])
const materialOptions = ref([])
const eraOptions = ref([])
const orderByOptions = ref([])

// 计算总页数
const totalPages = computed(() => {
  return Math.ceil(allSearchResults.value.length / pageSize);
})

// 构建orderbyList的通用方法
const buildOrderbyList = (orderByValue = searchParams.value.orderBy) => {
  let orderbyList = [];
  if (orderByValue) {
    if (orderByValue === 'grade') {
      levelOptions.value.forEach(item => {
        orderbyList.push(item.value);
      });
    } else if (orderByValue === 'category') {
      categoryOptions.value.forEach(item => {
        orderbyList.push(item.value);
      });
    } else if (orderByValue === 'texture') {
      materialOptions.value.forEach(item => {
        orderbyList.push(item.value);
      });
    } else if (orderByValue === 'era') {
      eraOptions.value.forEach(item => {
        orderbyList.push(item.value);
      });
    }
  }
  console.log('移动端主页buildOrderbyList:', orderByValue, orderbyList);
  return orderbyList;
};

// 打开搜索页
const openSearchPage = () => {
  showSearchPage.value = true
}

// 关闭搜索页
const closeSearchPage = () => {
  showSearchPage.value = false
}

// 快速搜索（回车键触发）
const quickSearch = () => {
  searchParams.value.dim = getSelectedDim()
  const orderbyList = buildOrderbyList()
  console.log('searchParams', searchParams.value)
  handleSearch({...searchParams.value, orderbyList})
}

// 计算dim参数值
const getSelectedDim = () => {
  const { hasImage, hasModel } = filters.value

  // 都选中或都不选中时传3-全部
  if ((hasImage && hasModel) || (!hasImage && !hasModel)) {
    return 3;
  }
  // 只选hasImage时传1-二维
  if (hasImage && !hasModel) {
    return 1;
  }
  // 只选hasModel时传2-三维
  if (!hasImage && hasModel) {
    return 2;
  }

  return 3; // 默认全部
}

// 加载显示的数据（分页）
const loadDisplayedData = () => {
  const startIndex = (currentPage.value - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  const newData = allSearchResults.value.slice(startIndex, endIndex);

  if (currentPage.value === 1) {
    // 第一页，直接替换
    searchResults.value = newData.map(item => ({ ...item, isLoading: true }));
  } else {
    // 后续页，追加数据
    const newItems = newData.map(item => ({ ...item, isLoading: true }));
    searchResults.value.push(...newItems);
  }

  // 延迟触发瀑布效果
  setTimeout(() => {
    newData.forEach((item, index) => {
      setTimeout(() => {
        const foundItem = searchResults.value.find(listItem => listItem.id === item.id && listItem.isLoading);
        if (foundItem) {
          foundItem.isLoading = false;
        }
      }, index * 150 + 600);
    });
  }, 100);
};

// 处理搜索
const handleSearch = async (searchFilters = {}) => {
  showSearchPage.value = false
  loading.value = true

  // 合并搜索参数，保留现有值，只更新传入的非空值
  Object.keys(searchFilters).forEach(key => {
    // if (searchFilters[key] !== undefined && searchFilters[key] !== null && searchFilters[key] !== '') {
      searchParams.value[key] = searchFilters[key]
    // }
  })

  // 确保dim参数正确更新
  searchParams.value.dim = getSelectedDim()

  // 存储排序参数到store
  if (searchFilters.orderBy) {
    store.dispatch('setOrderBy', searchFilters.orderBy)
  }
  if (searchFilters.orderbyList) {
    store.dispatch('setOrderbyList', searchFilters.orderbyList)
  }

  try {
    const searchObj = {
      dim: searchParams.value.dim,
      searchText: searchParams.value.searchText,
      grade: searchParams.value.level,
      category: searchParams.value.category,
      texture: searchParams.value.material,
      agetype: searchParams.value.era,
      orderBy: searchParams.value.orderBy,
      orderbyList: searchFilters.orderbyList || []
    }
    // 重置分页数据
    currentPage.value = 1;
    searchResults.value = []; // 清空当前显示的数据

    // 调用实际的搜索API，使用与CollectionContent相同的API
    const response = await getBookCountApi.getArtifactListApi(searchObj)

    // 处理返回的数据
    const artifacts = response.list || response.records || response || [];

    console.log('获取到搜索结果:', artifacts.length, '条');

    // 数据格式化处理
    const processedArtifacts = artifacts.map(item => ({
      id: item.id,
      name: item.name || item.title,
      description: item.description || item.desc || '暂无描述',
      era: item.era || item.agetype || '近现代',
      category: item.category || '其他',
      level: item.level || item.grade || '未定级',
      material: item.texture || '其他',
      image: `${imgUrl}${item.thumbnail}`,
      hasImage: item.hasImage !== false,
      hasModel: item.hasModel !== false,
      isLoading: false
    }))

    // 存储所有结果数据
    allSearchResults.value = processedArtifacts;

    // 加载第一页数据
    loadDisplayedData();

    searched.value = true

  } catch (error) {
    console.error('搜索失败:', error)
    allSearchResults.value = []
    searchResults.value = []
    searched.value = true
  } finally {
    loading.value = false
  }
}

// 查看详情
const viewDetails = (item) => {
  router.push({
    path: '/collectDetail',
    query: {
      id: item.id
    }
  })
}

// 滚动处理 - 加载更多
const handleScroll = async (event) => {
  const container = event.target;
  if (!container || loading.value) return;

  // 检查是否滚动到底部
  const isScrolledToBottom = container.scrollTop + container.clientHeight >= container.scrollHeight - 50;

  if (isScrolledToBottom && currentPage.value < totalPages.value) {
    console.log('加载下一页数据...');
    currentPage.value++;
    loadDisplayedData();
  }
};

// 获取所有字典数据
const loadDictionaryData = async () => {
  try {
    const dictionaryData = await getBookCountApi.getDictionaryListApi();
    console.log('获取到字典数据:', dictionaryData);
    // 处理等级数据
    if (dictionaryData.grade) {
      levelOptions.value = dictionaryData.grade.map(item => ({
        label: item,
        value: item
      }));
    }

    // 处理分类数据
    if (dictionaryData.category) {
      const processedCategories = dictionaryData.category.map(item => ({
        label: item,
        value: item
      }));
      categoryOptions.value = processedCategories;
    }

    // 处理材质数据
    if (dictionaryData.texture) {
      materialOptions.value = dictionaryData.texture.map(item => ({
        label: item,
        value: item
      }));
    }

    // 处理年代数据
    if (dictionaryData.agetype) {
      eraOptions.value = dictionaryData.agetype.map(item => ({
        label: item,
        value: item
      }));
    }

    // 处理排序数据
    orderByOptions.value = [
      { label: '等级', value: 'grade' },
      { label: '类别', value: 'category' },
      { label: '材质', value: 'texture' },
      { label: '年代', value: 'era' }
    ];

    // 字典数据加载完成后，进行初始化搜索
    console.log('移动端字典数据加载完成，开始初始化搜索');
    performInitialSearch();

  } catch (error) {
    console.error('获取字典数据失败:', error);
    // 如果接口失败，使用默认值
    levelOptions.value = [
      { label: '一级', value: '一级' },
      { label: '二级', value: '二级' },
      { label: '三级', value: '三级' },
      { label: '一般', value: '一般' },
      { label: '未定级', value: '未定级' }
    ];
    materialOptions.value = [
      { label: '石', value: '石' },
      { label: '铜', value: '铜' },
      { label: '铜宝玉石', value: '铜宝玉石' },
      { label: '瓷', value: '瓷' },
      { label: '陶', value: '陶' },
      { label: '宝玉石', value: '宝玉石' },
      { label: '玻璃', value: '玻璃' },
      { label: '其他', value: '其他' }
    ];
    eraOptions.value = [
      { label: '近现代', value: '近现代' }
    ];
    orderByOptions.value = [
      { label: '等级', value: 'grade' },
      { label: '类别', value: 'category' },
      { label: '材质', value: 'material' },
      { label: '年代', value: 'era' }
    ];

    // 即使使用默认数据，也要进行初始化搜索
    console.log('移动端使用默认字典数据，开始初始化搜索');
    performInitialSearch();
  }
};

// 执行初始化搜索
const performInitialSearch = () => {
  // 检查是否有来自store的搜索关键词
  const homeSearchText = store.getters.getHomeSearchText;
  if (homeSearchText) {
    searchParams.value.searchText = homeSearchText;
    store.dispatch('setHomeSearchText', ''); // 清除store中的关键词
  }

  // 构建orderbyList（使用默认的grade排序）
  const orderbyList = buildOrderbyList();

  // 执行搜索
  handleSearch({
    ...searchParams.value,
    orderbyList
  });
};

// 组件挂载
onMounted(() => {
  // 加载字典数据，字典数据加载完成后会自动进行初始化搜索
  loadDictionaryData();
})
</script>

<style lang="scss" scoped>
.collectpage {
  width: 100%;
  height: 100vh;
  position: relative;
  background: #f5f3ec;
}

.search-page {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  background: #f5f3ec;
}

.main-content {
  width: 100%;
  height: 100%;
  position: relative;
}

.top-title {
  position: absolute;
  top: 20px;
  left: 0;
  width: 100%;
  text-align: center;
  z-index: 10;

  img {
    max-width: 300px;
    height: auto;
  }
}

.background-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;

  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
}

.search-container {
  position: absolute;
  top: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  width: calc(100% - 40px);
  max-width: 600px;
}

.search-wrapper {
  display: flex;
  flex-direction: column;
  border-bottom: 1px dotted #d3bfa2;
  border-radius: 6px;
  gap: 2px;
}

.checkbox-group {
  display: flex;
  gap: 6px;
  white-space: nowrap;

  :deep(.el-checkbox) {
    .el-checkbox__label {
      font-size: 14px;
      color: #666;
      font-weight: 400;
    }

    .el-checkbox__inner {
      border-color: #d3bfa2;

      &:hover {
        border-color: #d3bfa2;
      }
    }

    &.is-checked .el-checkbox__inner {
      background-color: #d3bfa2;
      border-color: #d3bfa2;
    }

    &:hover .el-checkbox__label {
      color: #333;
    }
  }
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  gap: 2px;
  .quick-search-btn {
    position: relative;
    width: 30px;
    height: 30px;
    border: none;
    background: url('@/assets/img/btn_active.png') no-repeat;
    background-size: 30px 30px;
    .search-icon{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 20px;
      height: 20px;
      object-fit: contain;
    }
  }
}

.search-input {
  flex: 1;
  width: 288px;

  :deep(.el-input__wrapper) {
    height: 40px;
    border-radius: 4px;
  }

  :deep(.el-input__inner) {
    font-size: 14px;
    color: #333;

    &::placeholder {
      color: #aaa;
    }
  }
}

.search-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  border-radius: 4px;
  color: #B49D7E;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.3s ease;
  height: 40px;
  min-width: 80px;
  background: transparent;

  .search-icon {
    width: 30px;
    height: 30px;
    object-fit: contain;
  }

  &:hover {
    background: #6a5a36;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  &:active {
    transform: translateY(0);
  }
}

.search-results {
  position: absolute;
  top: 110px;
  left: 0;
  width: 100%;
  height: calc(100% - 130px);
  overflow-y: auto;
  z-index: 10;
  padding: 0 20px;
  box-sizing: border-box;
}

.result-item {
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.9);
  margin-bottom: 15px;
  padding: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &:hover {
    background: rgba(255, 255, 255, 1);
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  }

  // 瀑布加载状态
  &.loading-waterfall {
    .result-image, .result-info {
      opacity: 0.3;
      filter: grayscale(100%);
      transition: opacity 0.3s ease, filter 0.3s ease;
    }
  }

  .waterfall-mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(180deg,
      rgba(0, 0, 0, 0.9) 0%,
      rgba(0, 0, 0, 0.8) 50%,
      rgba(0, 0, 0, 0.9) 100%);
    z-index: 10;
    transform: translateY(-100%);
    border-radius: 8px;
    animation: waterfallDown 1.0s ease-out forwards, waterfallUp 1.0s ease-in 1.0s forwards;
    will-change: transform, opacity;
  }
}

.result-image {
  width: 100%;
  height: 181px;
  object-fit: cover;
  margin-right: 15px;
  transition: opacity 0.3s ease, filter 0.3s ease;
}

.result-info {
  flex: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: opacity 0.3s ease, filter 0.3s ease;

  .result-name {
    width: 85%;
    font-size: 16px;
    color: #464646;
    font-weight: 400;
    padding-top: 6px;
  }

  .more-info {
    width: 15%;
    display: flex;
    justify-content: flex-end;
  }

  .result-category,
  .result-era {
    margin: 4px 0;
    font-size: 14px;
    color: #666;
  }
}

.no-data {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  z-index: 10;

  p {
    font-size: 16px;
    color: #999;
  }
}

.loading-more {
  grid-column: 1 / -1;
  text-align: center;
  padding: 20px;
  color: #666;
}

// 瀑布动画关键帧
@keyframes waterfallDown {
  0% {
    transform: translateY(-100%);
    opacity: 0;
  }
  15% {
    opacity: 1;
  }
  100% {
    transform: translateY(0%);
    opacity: 1;
  }
}

@keyframes waterfallUp {
  0% {
    transform: translateY(0%);
    opacity: 1;
  }
  85% {
    opacity: 1;
  }
  100% {
    transform: translateY(100%);
    opacity: 0;
  }
}
</style>
