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

/**
 * 首页API请求的组合式函数
 * @returns {Object} 包含各种API请求函数和响应式数据
 */
export function useIndexPageApi() {
  // 响应式数据
  const bannerData = ref([])
  const newsList = ref([])
  const exhibitionList = ref([])
  const activeList = ref([])
  const loading = ref(false)

  /**
   * 获取轮播图数据
   * @param {Object} params - 请求参数
   * @param {number} params.pageNum - 页码，默认1
   * @param {number} params.pageSize - 每页数量，默认10
   * @param {number} params.status - 状态，默认1
   */
  const getBannerData = async (params = {}) => {
    const defaultParams = {
      pageNum: 1,
      pageSize: 10,
      status: 1,
      ...params,
    }

    try {
      loading.value = true
      const response = await museumApi.getCarouselList(defaultParams)
      console.log('轮播图数据:', response)
      // 根据接口返回的数据结构设置bannerData
      bannerData.value = response.records || response.list || response.data || response || []
      return response
    } catch (error) {
      console.error('获取轮播图数据失败:', error)
      // 如果接口失败，使用默认数据
      bannerData.value = [{ id: 1, title: '轮播图1', imageUrl: '' }]
      throw error
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取资讯列表
   * @param {Object} params - 请求参数
   * @param {number} params.pageNum - 页码，默认1
   * @param {number} params.pageSize - 每页数量，默认3
   * @param {number} params.status - 状态，默认1
   */
  const getNewsList = async (params = {}) => {
    const defaultParams = {
      pageNum: 1,
      pageSize: 3,
      status: 1,
      ...params,
    }

    try {
      loading.value = true
      const response = await museumApi.getNewsList(defaultParams)
      console.log('资讯列表数据:', response)
      newsList.value = response.records || response.list || response.data || response || []
      return response
    } catch (error) {
      console.error('获取资讯列表数据失败:', error)
      // 如果接口失败，使用默认数据
      newsList.value = [
        { id: 1, title: '资讯1', imageUrl: '' },
        { id: 2, title: '资讯2', imageUrl: '' },
      ]
      throw error
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取展览列表
   * @param {Object} params - 请求参数
   * @param {number} params.pageNum - 页码，默认1
   * @param {number} params.pageSize - 每页数量，默认5
   * @param {number} params.status - 状态，默认1
   */
  const getExhibitionList = async (params = {}) => {
    const defaultParams = {
      pageNum: 1,
      pageSize: 5,
      status: 1,
      ...params,
    }

    try {
      loading.value = true
      const response = await museumApi.getExhibitionList(defaultParams)
      console.log('展览列表数据:', response)
      // 根据接口返回的数据结构设置exhibitionList
      exhibitionList.value = response.records || response.list || response.data || response || []
      return response
    } catch (error) {
      console.error('获取展览列表数据失败:', error)
      // 如果接口失败，使用默认数据
      exhibitionList.value = []
      throw error
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取活动列表
   * @param {Object} params - 请求参数
   * @param {number} params.pageNum - 页码，默认1
   * @param {number} params.pageSize - 每页数量，默认5
   * @param {number} params.status - 状态，默认1
   */
  const getActiveList = async (params = {}) => {
    const defaultParams = {
      pageNum: 1,
      pageSize: 5,
      status: 1,
      ...params,
    }

    try {
      loading.value = true
      const response = await museumApi.getSocialActivityList(defaultParams)
      console.log('活动列表数据:', response)
      // 根据接口返回的数据结构设置activeList
      activeList.value = response.records || response.list || response.data || response || []
      return response
    } catch (error) {
      console.error('获取活动列表数据失败:', error)
      // 如果接口失败，使用默认数据
      activeList.value = []
      throw error
    } finally {
      loading.value = false
    }
  }

  /**
   * 获取资讯详情
   * @param {string|number} id - 资讯ID
   */
  const getInformationDetail = async (id) => {
    if (!id) {
      console.error('缺少资讯ID参数')
      return null
    }

    loading.value = true
    try {
      const response = await museumApi.getInformationDetail(id)
      return response?.data || null
    } catch (error) {
      console.error('获取资讯详情失败:', error)
      return null
    } finally {
      loading.value = false
    }
  }

  /**
   * 初始化所有数据
   * @param {Object} options - 配置选项
   * @param {Object} options.bannerParams - 轮播图请求参数
   * @param {Object} options.newsParams - 资讯请求参数
   * @param {Object} options.exhibitionParams - 展览请求参数
   * @param {Object} options.activeParams - 活动请求参数
   */
  const initAllData = async (options = {}) => {
    const { bannerParams = {}, newsParams = {}, exhibitionParams = {}, activeParams = {} } = options

    try {
      // 并行请求所有数据
      await Promise.allSettled([
        getBannerData(bannerParams),
        getNewsList(newsParams),
        getExhibitionList(exhibitionParams),
        getActiveList(activeParams),
      ])
    } catch (error) {
      console.error('初始化数据失败:', error)
    }
  }

  return {
    // 响应式数据
    bannerData,
    newsList,
    exhibitionList,
    activeList,
    loading,

    // API方法
    getBannerData,
    getNewsList,
    getExhibitionList,
    getActiveList,
    getInformationDetail,
    initAllData,
  }
}
