<template>

  <Banner :layout="layout" />

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

    <div class="left">
      <!-- 内页左侧组件 -->
      <Left :category="category" :title="$t('product.title')" />
    </div>


    <div class="right">
      <div class="sitemp h-[32px] flex justify-between">
        <h2>
          {{ $t('label') }}：{{ where.tags }}
        </h2>
        <Breadcrumb :data="page" :categoryName="$t('label')" />
      </div>
      <el-alert v-if="where.tags" :title="`${$t('search.results')}：${$t('search.find')} ${total} ${$t('search.nums')}`" type="warning" :closable="false" />
      <div class="content">

        <ul class="news_listn clearfix">
          <template v-for="(item,index) in list" key="index">
            <li class="clearfix">
              <a :href="detail(item)" target="_blank" class="n-left fl">
                <el-image :src="item.image" :fit="`scale-down`" class="w-[240px] h-[158px]" :alt="item.title" />
              </a>
              <div class="n-right fr">
                <h3><a :href="detail(item)" target="_blank" :title="item.title" v-html="replaceKeywords(item.title)"></a></h3>
                <div v-html="replaceKeywords(item.comments)" class="line-clamp-2"></div>
                <div class="date">{{ $t('createTime') }}：{{ dayjs(item.createTime).format('YYYY.MM.DD') }}</div>
                <div class="date">{{ $t('search.column') }}：{{ item.categoryName }}</div>
                <div class="n-more"><a :href="detail(item)">{{ $t('seeMore') }}>></a></div>
              </div>
            </li>
          </template>
          <div class="clearboth"></div>
        </ul>

        <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 dayjs from "dayjs";
import {ArrowRight} from '@element-plus/icons-vue'
import {detail, getPath} from "~/utils/common";
import Left from "~/components/Left.vue";
import {listCmsNavigation} from "~/api/cms/cmsNavigation";
import {findTags, pageCmsArticle, pageTags} from "~/api/cms/cmsArticle";
import {listCmsModel} from "~/api/cms/cmsModel";

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>({
  tags: '',
  page: 1,
  limit: 10,
  status: 0,
  parentId: undefined,
  categoryId: undefined,
  lang: i18n.locale.value
});

const replaceKeywords = (text: any) => {
  return text.replace(`${where.tags}`,'<font color=#ff0000>' + where.tags + '</font>');
}

// 加载页面数据
const reload = async () => {
  listCmsModel({
    model: getPath(1)
  }).then(response => {
    const data = response[0];
    if(data){
      // 获取栏目信息
      page.value = data
      layout.value.banner = data.banner
      // 设置页面标题
      useSeoMeta({
        description: data?.comments || `${route.params.id}`,
        keywords: `${route.params.id}`,
        titleTemplate: `【搜索结果】${route.params.id}` + ' - %s',
      })
      // 二级栏目分类
      listCmsNavigation({
        parentId: i18n.locale.value == 'en' ? 1073 : 998,
      }).then(categoryData => {
        category.value = categoryData;
        // 加载文章列表
        if(!getPath(1)){
          return ElMessage.error('请输入搜索关键词！');
        }
        where.tags = `${route.params.id}`;
        pageTags({
          tags: `${route.params.id}`
        }).then(response => {
          if(response){
            total.value = response?.length;
            list.value = response;
          }
        })
      })
    }

  }).catch(err => {
    console.log(err,'加载失败...')
  })
}

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

watch(
    () => route.path,
    () => {
      reload();
    },
    {immediate: true}
);
</script>
<style scoped lang="less">
.sitemp h2{
  width: 500px !important;
}
</style>
