<template>
  <PageBanner :layout="layout" />

  <CardList :list="list" />

</template>
<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import type {ApiResult, PageResult} from "~/api";
import {useServerRequest} from "~/composables/useServerRequest";
import {useWebsite} from "~/composables/configState";
import type {Navigation} from "~/api/cms/navigation/model";
import {getPath} from "~/utils/common";
import type {CompanyParam} from "~/api/system/company/model";
import type {Tenant} from "~/api/system/tenant/model";
import type {Website} from "~/api/cms/website/model";
import type {Article} from "~/api/cms/article/model";

const route = useRoute();

// 页面信息
const runtimeConfig = useRuntimeConfig();
const list = ref<Tenant[]>([]);
const page = ref<number>(0);
const resultText = ref('');
const layout = ref<any>();

// 获取状态
const form = ref<Navigation>();
const website = useWebsite();

// 搜索表单
const where = reactive<CompanyParam>({
  keywords: ''
});

const navTo = (item: Website) => {
  if(item.domain){
    openSpmUrl(`https://${item.domain}`,item,item.tenantId)
    return;
  }
  openSpmUrl(`https://${item.websiteCode}.wsdns.cn`,item,item.tenantId)
}

// 请求数据
const reload = async (path: string) => {
// 加载数据
  const {data: response} = await useServerRequest<ApiResult<PageResult<Article>>>('/cms/cms-article/page',{baseURL: runtimeConfig.public.apiServer, params: {
      page: page.value, keywords: where.keywords
    }})
  if(response.value?.data){
    if (response.value?.data.list) {
      list.value = response.value?.data.list;
    }
    if(response.value.data.count == 0){
      resultText.value = '暂无相关结果'
    }
  }
  const { data: nav } = await useServerRequest<ApiResult<Navigation>>('/cms/cms-navigation/getNavigationByPath',{query: {path}})
  if(nav.value?.data){
    form.value = nav.value?.data;
  }
  // 页面布局
  if(form.value?.layout){
    layout.value = JSON.parse(form.value?.layout)
  }

  useHead({
    title: `搜索结果 - ${website.value.websiteName}`,
    bodyAttrs: {
      class: "page-container",
    }
  });
}
//
// // 搜索结果
// const handleClick = async () => {
//   const {data: response} = await useServerRequest<ApiResult<PageResult<Article>>>('/cms/cms-article/page',{baseURL: runtimeConfig.public.apiServer, params: {
//       page: page.value, keywords: where.keywords
//     }})
//   if(response.value?.data){
//     if (response.value?.data.list) {
//       list.value = response.value?.data.list;
//     }
//     if(response.value.data.count == 0){
//       resultText.value = '暂无相关结果'
//     }
//   }
// }

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


</script>
