<template>
  <div class="text-center flex flex-col items-center py-10">
    <h2 class="text-3xl font-bold tracking-tight text-gray-900 dark:text-white sm:text-4xl lg:text-5xl">
      {{ title }}
    </h2>
    <div class="sub-title">
      <p class="text-gray-500 dark:text-gray-400 py-3">
        {{ comments }}
      </p>
    </div>
  </div>
  <div class="xl:w-screen-xl sm:flex xl:p-0 p-4 m-auto relative" v-infinite-scroll="load">
    <el-row :gutter="24" class="flex">
      <template v-for="(item,index) in list" :key="index">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6" class="mb-5 min-w-xs">
          <el-card shadow="hover" :body-style="{ padding: '0px' }" class="hover:bg-gray-50 cursor-pointer"
                   @click="openSpmUrl(`/detail`,item,item.articleId,true)">
            <el-image :src="item.image" fit="fill" :lazy="true" class="w-full md:h-[150px] h-[199px] cursor-pointer"/>
            <div class="flex-1 px-4 py-5 sm:p-6 !p-4">
              <div class="text-gray-700 dark:text-white text-base font-semibold  flex items-center gap-1.5">
                <span class="flex-1 text-xl cursor-pointer max-h-[57px] overflow-hidden">{{ item.title }}</span>
              </div>
              <div class="flex items-center gap-1.5 py-2 text-gray-500">
                <el-avatar :src="item.avatar" :size="20"/>
                <span>{{ item.nickname }} · {{ dayjs(item.createTime).format('MM-DD hh:mm') }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </template>
    </el-row>
  </div>
  <div v-if="disabled" class="p-4 text-center text-gray-500">
    没有更多了
  </div>
</template>

<script setup lang="ts">
import {openSpmUrl} from "~/utils/common";
import dayjs from "dayjs";
import {useServerRequest} from "~/composables/useServerRequest";
import type {ApiResult, PageResult} from "~/api";
import type {Article} from "~/api/cms/article/model";
import type {CompanyParam} from "~/api/system/company/model";

const props = withDefaults(
    defineProps<{
      list?: any[];
      disabled?: boolean;
      title?: string;
      comments?: string;
    }>(),
    {
      title: '卡片标题',
      comments: '卡片描述'
    }
);

const emit = defineEmits<{
  (e: 'done'): void;
}>();

const runtimeConfig = useRuntimeConfig();
const list = ref<Article[]>([]);
const page = ref<number>(1);
const resultText = ref('');
const disabled = ref(false);

// 搜索表单
const where = reactive<CompanyParam>({
  keywords: ''
});

const load = () => {
  if (!props.disabled) {
    page.value++;
    reload();
  }
}

// 请求数据
const reload = async () => {
  const {data: response} = await useServerRequest<ApiResult<PageResult<Article>>>('/cms/cms-article/page', {
    baseURL: runtimeConfig.public.apiServer, params: {
      page: page.value, limit: 8, keywords: where.keywords
    }
  })
  if (response.value?.data) {
    if (list.value.length < response.value?.data.count) {
      disabled.value = false;
      if (response.value?.data.list) {
        list.value = list.value.concat(response.value?.data.list);
      }
    } else {
      disabled.value = true;
    }
    if (response.value.data.count == 0) {
      resultText.value = '暂无相关结果'
    }
  }
}

reload();
</script>
