<template>
  <div>
    <div v-loading="loading" v-if="list.length">
      <div class="w-full max-w-6xl mx-auto">
        <div
          v-for="(item, index) in list"
          :key="index"
          class="bg-white rounded-lg shadow-sm mb-6 overflow-hidden"
          :style="{ '--dynamic-color': item.color }"
        >
          <div
            class="flex items-center justify-between pr-4 pl-4 pt-2 pb-2 bg-green-50 border-b border-green-100"
            :style="{ backgroundColor: item.color, '--dynamic-color': item.color }"
          >
            <h3 class="text-lg font-medium text-gray-800 flex items-center">
              <span class="w-1 h-5 mr-2 bg-#444"></span>
              {{ item.title }}
            </h3>
            <div
              class="flex items-center cursor-pointer"
              @click="
                router.push({
                  path: `/columnSearchList/${item.id}`,
                  query: {
                    columnTitle: item.title,
                  },
                })
              "
            >
              <span class="mr-1 text-14px color-#606266">查看更多 >></span>
            </div>
          </div>

          <div class="p-4">
            <div v-if="item.yaColumnVoList.length" class="grid grid-cols-1 md:grid-cols-3 gap-4">
              <div
                v-for="i in item.yaColumnVoList"
                :key="i.articleId"
                class="group cursor-pointer"
                @click="jumpToArticleDetailPage({ type: i.type, id: i.articleId })"
              >
                <div class="relative mb-3 overflow-hidden rounded-lg">
                  <img
                    :src="i.faceUrl"
                    class="w-full aspect-[5/3] object-cover group-hover:scale-105 transition-transform duration-300"
                  />
                  <div
                    v-if="i.isRecommend"
                    class="absolute top--1 left--1 w-15 h-7 z-1000 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>
                <h3 class="text font-medium text-gray-800 mb-2 transition-colors line-clamp-1">
                  {{ i.title }}
                </h3>
                <!-- 因为是富文本 暂时不显示 -->
                <!-- <p class="text-sm text-gray-500 mb-3 line-clamp-1">
                  {{ i.content }}
                </p> -->
                <div class="flex items-center justify-between text-xs text-gray-500">
                  <div class="flex items-center gap-2">
                    <span class="flex items-center">
                      <el-icon class="mr-1">
                        <IEpView />
                      </el-icon>
                      {{ i.viewCount }}
                    </span>
                    <span class="flex items-center">
                      <el-icon class="mr-1">
                        <IEpChatDotRound />
                      </el-icon>
                      {{ i.replyCount }}
                    </span>
                    <span class="flex items-center">
                      <el-icon class="mr-1">
                        <IEpStar />
                      </el-icon>
                      {{ i.collectCount }}
                    </span>
                  </div>
                  <span>
                    {{
                      smallerThanXl
                        ? dayjs(i.createTime * 1000).format('YYYY-MM-DD')
                        : dayjs(i.createTime * 1000).format('YYYY-MM-DD HH:mm:ss')
                    }}
                  </span>
                </div>
              </div>
            </div>
            <div v-else class="flex items-center justify-center h-48">
              <el-empty description="暂无数据" />
            </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="[15, 30, 45, 60]"
                  layout="prev, pager, next, jumper, total"
                  :total="total"
                  class="custom-pagination"
                  @current-change="
                    (e) => {
                      ;(handleBackTop(), goToPage(e))
                    }
                  "
                  @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 { getColumnList } from '@/api'
import { usePageSearch, useScrollTop } from '@/hooks'
import { TABS_REF_KEY } from '@/constants'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import { jumpToArticleDetailPage } from '@/utils'
import { breakpointsTailwind, useBreakpoints } from '@vueuse/core'
console.log(breakpointsTailwind, 'breakpointsTailwind')
const breakpoints = useBreakpoints(breakpointsTailwind)
const smallerThanXl = breakpoints.smaller('xl')

const router = useRouter()
const tabsRef = inject(TABS_REF_KEY)
const { handleBackTop, ScrollTopComp } = useScrollTop(tabsRef!)

const { list, total, searchParams, goToPage, changePageSize, loading, refresh } = usePageSearch(
  getColumnList,
  {
    defaultSize: 3,
    immediate: false,
  },
)
defineExpose({
  refresh: () => {
    // searchParams.value.current = 0
    refresh()
  },
})
</script>

<style scoped></style>
