<template>
  <Banner :layout="layout" />
  <!-- 主体部分 -->
  <div class="xl:w-screen-xl m-auto py-4 mt-20">
    <el-page-header :icon="ArrowLeft" @back="goBack">
      <template #content>
        <span class="text-large font-600 mr-3"> 文档中心 </span>
      </template>
      <el-row :gutter="24" id="container" class="clearfix">
        <el-col v-for="(item,index) in list" :key="index" :span="6" class="left mb-6">
          <el-card shadow="hover" :body-style="{ padding: '0px' }" class=" hover:bg-white cursor-pointer" @click="navigateTo(`/docs/${item.navigationId}.html`)">
            <div class="flex-1 px-4 py-5 sm:p-4 !p-4">
              <div class="text-gray-700 dark:text-white text-base font-semibold flex flex-col items-center gap-1.5">
                <el-avatar
                    :src="item.icon" shape="square" :size="100" style="background-color: white;"/>
                <div class="flex-1 text-lg cursor-pointer flex flex-col text-left py-4">
                  {{ item.title }}
                </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-page-header>
  </div>
</template>
<script setup lang="ts">
import { ArrowLeft,View,Search } from '@element-plus/icons-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, getViews, paramsId} from "~/utils/common";
import dayjs from "dayjs";
import {getCmsNavigation, listCmsNavigation} from "~/api/cms/cmsNavigation";
import {pageCmsArticle} from "~/api/cms/cmsArticle";
import Banner from "~/components/Banner.vue";

const route = useRoute();
const router = useRouter();
const navId = ref();
const list = ref<CmsNavigation[]>([]);
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 () => {
  listCmsNavigation({model: 'docs'}).then(data => {
    console.log(data,'123.')
    list.value = data;
    // 获取栏目信息
    page.value = data
    layout.value.banner = data.banner;

    // 设置页面标题
    useSeoMeta({
      description: '文档中心',
      keywords: '文档中心',
      titleTemplate: `文档中心` + ' - %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,'加载失败...')
  })
}

const goBack = () => {
  router.back(); // 返回上一页
  // window.history.back();
}

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

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

</script>
