<template>

  <Banner :layout="layout" />

  <!-- 主体部分 -->
  <div id="container" class="clearfix xl:w-screen-xl m-auto">

    <div class="left">
      <!-- 内页左侧组件 -->
      <Left :category="category" />
    </div>

    <div class="right">
      <div class="sitemp h-[32px] flex justify-between">
        <h2>
          {{ page.title }}
        </h2>
        <Breadcrumb :data="page" :categoryName="page.parentName || page.categoryName" />
      </div>
      <div class="content">

        <!-- 产品列表 -->
        <CmsProductList :data="list" />

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

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,
    () => {
      console.log('路由变化了',route.path)
      reload();
    },
    {immediate: true}
);
</script>
