import { ref } from 'vue'
import { museumApi } from '@/api/indexPage'

/**
 * 典藏页面API请求的组合式函数
 * @param {Object} options - 配置选项
 * @param {boolean} options.isPreviewMode - 是否为预览模式
 * @returns {Object} 包含各种API请求函数和响应式数据
 */
export function useCollectionApi(options = {}) {
  const { isPreviewMode = ref(false) } = options
  // 响应式数据
  const artifactList = ref([])
  const loading = ref(false)
  const hasMore = ref(true)
  const currentPage = ref(1)
  const pageSize = ref(10)
  const searchTitle = ref('')
  const selectedType = ref(0)

  /**
   * 获取文物列表
   * @param {Object} params - 请求参数
   * @param {number} params.pageNum - 页码，默认1
   * @param {number} params.pageSize - 每页数量，默认10
   * @param {number} params.status - 状态，默认1
   * @param {string} params.title - 搜索标题
   * @param {number} params.type - 类型，0为全部
   * @param {boolean} isLoadMore - 是否为加载更多
   */
  const getArtifactList = async (params = {}, isLoadMore = false) => {
    if (loading.value) return

    loading.value = true

    const defaultParams = {
      pageNum: isLoadMore ? currentPage.value : 1,
      pageSize: pageSize.value,
      status: isPreviewMode.value ? -1 : 1,
      title: searchTitle.value,
      type: selectedType.value,
      ...params,
    }

    try {
      const response = await museumApi.getArtifactList(defaultParams)
      console.log('文物列表数据:', response)

      if (response && response.records) {
        if (isLoadMore) {
          // 加载更多，追加数据
          artifactList.value = [...artifactList.value, ...response.records]
        } else {
          // 重新加载，替换数据
          artifactList.value = response.records
          currentPage.value = 1
        }

        // 判断是否还有更多数据
        hasMore.value = response.records.length === pageSize.value

        if (isLoadMore) {
          currentPage.value++
        }
      } else {
        artifactList.value = []
        hasMore.value = false
      }

      return response
    } catch (error) {
      console.error('获取文物列表数据失败:', error)
      // 如果接口失败，使用默认数据
      if (!isLoadMore) {
        artifactList.value = [
          {
            id: 1,
            title: '地形测量手簿',
            category: '三维文物',
            description: 'AAA级主要用于考察性的地域动力学研究、地形...',
            image: '',
          },
          {
            id: 2,
            title: '地质调查记录本',
            category: '三维文物',
            description: '用于记录地质调查的重要发现和数据...',
            image: '',
          },
        ]
      }
      throw error
    } finally {
      loading.value = false
    }
  }

  /**
   * 搜索文物
   * @param {string} title - 搜索标题
   */
  const searchArtifacts = async (title = '') => {
    searchTitle.value = title
    currentPage.value = 1
    hasMore.value = true
    await getArtifactList({}, false)
  }

  /**
   * 切换类型
   * @param {number} type - 类型ID，0为全部
   */
  const changeType = async (type = 0) => {
    selectedType.value = type
    currentPage.value = 1
    hasMore.value = true
    await getArtifactList({}, false)
  }

  /**
   * 加载更多数据
   */
  const loadMore = async () => {
    if (!hasMore.value || loading.value) return
    await getArtifactList({}, true)
  }

  /**
   * 重置搜索和筛选
   */
  const resetFilters = async () => {
    searchTitle.value = ''
    selectedType.value = 0
    currentPage.value = 1
    hasMore.value = true
    await getArtifactList({}, false)
  }

  /**
   * 初始化数据
   * @param {Object} options - 配置选项
   */
  const initData = async (options = {}) => {
    const { title = '', type = 0 } = options
    searchTitle.value = title
    selectedType.value = type
    currentPage.value = 1
    hasMore.value = true

    try {
      await getArtifactList({}, false)
    } catch (error) {
      console.error('初始化典藏数据失败:', error)
    }
  }

  return {
    // 响应式数据
    artifactList,
    loading,
    hasMore,
    currentPage,
    pageSize,
    searchTitle,
    selectedType,

    // API方法
    getArtifactList,
    searchArtifacts,
    changeType,
    loadMore,
    resetFilters,
    initData,
  }
}
