<template>

  <Banner :layout="layout" />

    <!-- 主体部分 -->
  <div class="xl:w-screen-xl m-auto">
    <el-row :gutter="24" id="container" class="clearfix">
      <el-col :span="5" class="left">
        <!-- 内页左侧组件 -->
        <Left :category="category"  />
      </el-col>
      <el-col :span="19" class="right">
        <div class="sitemp h-[32px] flex justify-between">
          <h2>{{ page.title }}</h2>
          <Breadcrumb :data="page" :categoryName="$t('articleTitle')" />
        </div>
        <div class="content">

          <!-- 文章模型 -->
          <ul class="news_listn clearfix">
            <CmsArticleList :data="list" />
          </ul>

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

        </div>
      </el-col>
    </el-row>
  </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 {getNavIdByParamsId, paramsId} from "~/utils/common";
import Left from "~/components/Left.vue";
import {getCmsNavigation, listCmsNavigation} from "~/api/cms/cmsNavigation";
import {pageCmsArticle} from "~/api/cms/cmsArticle";
import CmsArticleList from "~/components/CmsArticleList.vue";

const route = useRoute();
const navId = ref();
const list = ref<CmsArticle[]>([]);
const i18n = useI18n();
const category = ref<CmsNavigation[]>([]);
const total = ref<number>(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(navId.value).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;
      console.log(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.params.id,
    (id) => {
      navId.value = getNavIdByParamsId(id);
      reload();
    },
    { immediate: true }
);

</script>
