<template>
  <div ref="listRef">
    <div v-loading="loading" v-if="list.length">
      <div class="space-y-3 sm:space-y-4">
        <div
          v-for="item in list"
          :key="item.id"
          class="relative group bg-white rounded-lg p-6 cursor-pointer transition-all duration-300 hover:shadow-lg hover:shadow-gray-100 hover:-translate-y-1 border border-gray-100 hover:border-gray-200 mb-3 sm:mb-4"
          @click="jumpToArticleDetailPage({ type: item.type, id: item.id })"
        >
          <div class="flex gap-3 justify-between">
            <div
              v-if="item.isRecommend"
              class="absolute top-0 left-0 w-13 h-6 bg-#FFF9B9 flex items-center justify-center border-2px border-solid border-#f4f0eb rounded-tl-lg rounded-br-lg"
            >
              <img class="w-6" src="@/assets/img/culture/recommend.png" alt="" />
              <div class="text-12px text-#000 line-height-12px">推荐</div>
            </div>
            <!-- 内容区域 -->
            <div class="flex-1 min-w-0 flex flex-col justify-between h-24">
              <!-- 标题 -->
              <h2
                class="text-xl font-semibold text-gray-900 line-clamp-1 group-hover:text-blue-600 transition-colors duration-200 leading-tight"
              >
                {{ item.title }}
              </h2>

              <!-- 内容摘要 -->
              <div v-if="!item.content?.includes('</')" class="my-2 space-y-1">
                <p
                  class="text-gray-600 text-sm sm:text-base leading-relaxed line-clamp-1 break-all"
                >
                  {{ item.content }}
                </p>
              </div>

              <!-- 统计信息 -->
              <div class="flex items-center gap-2 xl:gap-4 text-gray-500 text-xs lg:text-sm">
                <!-- 发布人名称和头像 -->
                <div class="flex items-center gap-2">
                  <el-avatar :size="24" :src="item.showAvatar" />
                  <span class="text-sm text-gray-500">{{ item.showName }}</span>
                </div>
                <!-- 时间 -->
                <span class="text-gray-500 font-medium ml-0">
                  <span class="hidden sm:inline">{{
                    dayjs(item.createTime * 1000).format('YYYY-MM-DD HH:mm')
                  }}</span>
                </span>
                <span class="text-sm text-gray-500">{{
                  articleTypeListOptions.find((i) => i.value === item.type)?.label
                }}</span>
                <!-- 分隔符 -->
                <div class="hidden sm:block w-1 h-1 bg-gray-300 rounded-full"></div>
                <div class="flex items-center gap-1 hover:text-blue-500 transition-colors">
                  <el-icon class="text-sm"><IEpView /></el-icon>
                  <span class="font-medium">{{
                    item.type === ArticleTypeEnum.VIDEO
                      ? Math.max(item.playCount, item.viewCount)
                      : item.viewCount
                  }}</span>
                </div>
                <div class="flex items-center gap-1 hover:text-red-500 transition-colors">
                  <el-icon class="text-sm"> <IEpChatDotRound /></el-icon>
                  <span class="font-medium">{{ item.replyCount }}</span>
                </div>
                <div class="flex items-center gap-1 hover:text-yellow-500 transition-colors">
                  <el-icon class="text-sm"><IEpStar /></el-icon>
                  <span class="font-medium">{{ item.collectionCount }}</span>
                </div>
              </div>
            </div>

            <!-- 图片区域 -->
            <div v-show="item.faceUrl" class="relative flex-shrink-0 w-30 h-20 xl:w-36 xl:h-24">
              <el-image
                :src="item.faceUrl + '?x-oss-process=image/format,webp'"
                alt="文章配图"
                fit="cover"
                :lazy="true"
                class="w-full h-full object-cover rounded-lg sm:rounded-lg group-hover:scale-105 transition-transform duration-300"
                loading="lazy"
              />
              <!-- 图片遮罩效果 -->
              <div
                class="absolute inset-0 bg-black bg-opacity-0 group-hover:bg-opacity-5 rounded-lg sm:rounded-lg transition-all duration-300"
              ></div>
            </div>
          </div>
        </div>
      </div>

      <!-- 底部分页 -->
      <div class="bottom-pagination backdrop-blur-8 border-t border-gray-200">
        <div class="max-w-7xl mx-auto py-4">
          <div class="flex items-center justify-between">
            <!-- 左侧：回到顶部按钮 -->
            <div class="left">
              <ScrollTopComp />
            </div>

            <!-- 右侧：分页器 -->
            <div class="right">
              <div
                class="pagination-wrapper bg-white rounded-lg shadow-sm border border-gray-100 p-2"
              >
                <el-pagination
                  v-model:current-page="searchParams.current"
                  v-model:page-size="searchParams.size"
                  :page-sizes="[5, 10, 20]"
                  layout="prev, pager, next, jumper, total"
                  :total="total"
                  class="custom-pagination"
                  @current-change="
                    async (e) => {
                      await goToPage(e)
                      handleBackTop()
                    }
                  "
                  @size-change="changePageSize"
                  :pager-count="5"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <template v-else>
      <div class="flex items-center justify-center h-full">
        <el-empty description="暂无数据" />
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { usePageSearch } from '@/hooks'
import { getArticleList } from '@/api'
import { TABS_REF_KEY, ArticleTypeEnum } from '@/constants'
import { useScrollTop } from '@/hooks'
import dayjs from 'dayjs'
import { jumpToArticleDetailPage } from '@/utils'
import { articleTypeListOptions } from '@/constants'

const { list, total, searchParams, loading, goToPage, changePageSize, refresh } = usePageSearch(
  getArticleList,
  {
    defaultParams: { sortLogic: 0 },
    defaultCurrent: 1,
    defaultSize: 8,
    immediate: false,
  },
)
const tabsRef = inject(TABS_REF_KEY)

const { ScrollTopComp, handleBackTop } = useScrollTop(tabsRef!)

defineExpose({
  refresh: (sortLogic?: number) => {
    console.log('sortLogic', sortLogic)
    searchParams.value.sortLogic = sortLogic
    refresh()
  },
})
</script>
