<template>
  <Banner :layout="layout" />
  <!-- 主体部分 -->
  <div class="xl:w-screen-xl m-auto py-4 mt-12 px-4 sm:px-0 sm:mt-20">
    <el-page-header :icon="ArrowLeft" @back="goBack">
      <template #content>
        <span class="text-large font-600"> {{ '产品文档' }} </span>
      </template>
<!--      <div class="text-lg">-->
<!--        <el-result-->
<!--          icon="warning"-->
<!--          :title="`权限不足`"-->
<!--        />-->
<!--      </div>-->
      <el-row id="container" :gutter="24" class="clearfix">
        <el-col v-for="(item,index) in list" :key="index" :span="6" :xs="24" class="left mb-6">
          <el-card :body-style="{ padding: '0px' }" class=" hover:bg-gray-50 cursor-pointer" shadow="hover" @click="navigateTo(`/detail/${item.articleId}.html`)">
            <el-image
                :lazy="true"
                :src="item.image"
                class="w-full md:h-[166px] h-[199px] cursor-pointer bg-gray-50" fit="cover"/>
            <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 flex-col gap-1.5">
                <div class="flex-1 text-xl cursor-pointer flex min-h-[56px]">
                  {{ item.title }}
                </div>
              </div>
<!--              <div class="flex items-center gap-1.5 py-2 text-gray-500 justify-between">-->
<!--                <div class="text-gray-500 line-clamp-2">{{ item.comments }}</div>-->
<!--              </div>-->
              <div class="button-group flex justify-between items-center mt-3 text-sm">
                <el-space class="flex items-end">
                  <div class="text-gray-400 gap-1 flex items-center"><el-icon><View /></el-icon><span>{{ getViews(item) }}</span></div>
                </el-space>
                <div class="text-gray-400">
                  <el-avatar v-if="item.avatar" :src="`${item.avatar}`" size="small" />
                    {{ dayjs(item.createTime).format('YYYY-MM-DD') }}
                  </div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-page-header>
    <Pagination :total="total" @done="search" />
  </div>
</template>
<script lang="ts" setup>
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";
import {getCmsWebsiteAll} from "~/api/cms/cmsWebsite";
import type {CmsWebsite} from "~/api/cms/cmsWebsite/model";

const route = useRoute();
const router = useRouter();
const navId = ref();
const website = ref<CmsWebsite>();
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,
  status: 0,
  parentId: undefined,
  categoryId: undefined
});

// 加载页面数据
const reload = async () => {
  getCmsWebsiteAll(navId.value).then(data => {
    // 获取栏目信息
    website.value = data
    where.tenantId = data.tenantId;
    pageCmsArticle(where).then(response => {
      if(response){
        total.value = response?.count;
        list.value = response?.list;
      }
    })

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

  }).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,
    (id) => {
      navId.value = getNavIdByParamsId(id);
      reload();
    },
    { immediate: true }
);

</script>
