<template>

  <Banner :layout="layout" />

  <!-- 主体部分 -->
  <div class="p-5">

    <div class="m-page">
      <div class="sitemp h-[32px] flex justify-between">
        <h2>
          {{ page.title }}
        </h2>
      </div>
      <div class="content">

        <!-- 文章模型 -->
        <ul class="news_listn clearfix pt-2">
          <template v-for="(item,index) in list" key="index">
            <li class="clearfix">
              <a :href="mDetail(item)" class=" fl mr-1">
                <el-image :src="item.image" :fit="`scale-down`" class="w-[120px] h-[80px]" :alt="item.title" />
              </a>
              <div class="list">
                <h3 class="text-lg"><a :href="mDetail(item)" :title="item.title" class="line-clamp-1 text-left">{{ item.title }}</a></h3>
                <div v-html="item.comments" class="line-clamp-2"></div>
<!--                <div class="date">发布日期：{{ dayjs(item.createTime).format('YYYY-MM-DD') }}</div>-->
<!--                <div class="n-more" ><a :href="detail(item)"  >查看更多>></a></div>-->
              </div>
            </li>
          </template>
          <div class="clearboth"></div>
        </ul>

        <Pagination :total="total" @done="search" />

      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import Banner from "@/components/Banner.vue";
import {useLayout, usePage} from "~/composables/configState";
import type {CmsNavigation} from "~/api/cms/cmsNavigation/model";
import type {CmsArticle, CmsArticleParam} from "~/api/cms/cmsArticle/model";
import dayjs from "dayjs";
import type { ComponentSize } from 'element-plus'
import {mDetail, paramsId} from "~/utils/common";
import {getCmsNavigation, listCmsNavigation} from "~/api/cms/cmsNavigation";
import {pageCmsArticle} from "~/api/cms/cmsArticle";

const route = useRoute();

// 页面信息
const list = ref<CmsArticle[]>([]);
const i18n = useI18n();
const category = ref<CmsNavigation[]>([]);
const total = ref(0);

// 获取状态
const page = usePage();
const layout = useLayout();


// 搜索表单
const where = reactive<CmsArticleParam>({
  keywords: '',
  page: 1,
  limit: 20,
  status: 0,
  parentId: undefined,
  categoryId: undefined,
  lang: i18n.locale.value
});


// 加载页面数据
const reload = async () => {
  getCmsNavigation(paramsId()).then(data => {
    // 获取栏目信息
    page.value = data
    layout.value.banner = data.banner;

    // 设置页面标题
    useSeoMeta({
      description: data.comments || data.title,
      keywords: data.title,
      titleTemplate: `${data?.title}` + ' - %s',
    })

    // 二级栏目分类
    listCmsNavigation({
      parentId: data.parentId == 0 ? data.navigationId : data.parentId
    }).then(categoryData => {
      category.value = categoryData;
      // 加载文章列表
      if(data.parentId == 0 && category.value.length > 0){
        where.parentId = data.navigationId;
      }else {
        where.categoryId = data.navigationId;
      }
      pageCmsArticle(where).then(response => {
        if(response){
          total.value = response?.count;
          list.value = response?.list;
        }
      })
    })

  }).catch(err => {
    console.log(err,'加载失败...')
  })
}

/**
 * 搜索
 * @param data
 */
const search = (data: CmsArticleParam) => {
  where.page = data.page;
  reload();
}

watch(
    () => route.path,
    () => {
      reload();
    },
    {immediate: true}
);

</script>
