<template>
  <!-- 主体部分 -->
  <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>
      <template #extra>
        <el-radio-group v-model="where.model" @change="reload">
          <el-radio-button label="文档" value="docs" />
          <el-radio-button label="资讯" value="article" />
        </el-radio-group>
      </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-gray-50 cursor-pointer">
            <nuxt-link :to="`/${item.detail}/${item.articleId}.html`">
              <el-image
                  :src="item.image"
                  fit="cover"
                  :lazy="true" class="w-full md:h-[166px] h-[199px] cursor-pointer bg-gray-50"/>
              <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]">
                    <span v-html="replaceKeywords(item.title)"></span>
                  </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">
                    {{ dayjs(item.createTime).format('YYYY-MM-DD') }}
                  </div>
                </div>
              </div>
            </nuxt-link>
          </el-card>
        </el-col>
      </el-row>
      <Pagination :total="total" @done="search" :keywords="where.keywords" />
    </el-page-header>
  </div>
</template>
<script setup lang="ts">
import Banner from "@/components/Banner.vue";
import { ArrowLeft,View,Search } from '@element-plus/icons-vue'
import {useLayout, usePage} from "~/composables/configState";
import type {CmsArticle, CmsArticleParam} from "~/api/cms/cmsArticle/model";
import dayjs from "dayjs";
import {getViews} from "~/utils/common";
import {pageCmsArticle} from "~/api/cms/cmsArticle";

const route = useRoute();
const router = useRouter();
// 页面信息
const list = ref<CmsArticle[]>([]);
const i18n = useI18n();
const total = ref(0);

// 获取状态
const page = usePage();
const layout = useLayout();

// 搜索表单
const where = reactive<CmsArticleParam>({
  keywords: undefined,
  page: 1,
  limit: 12,
  status: 0,
  parentId: undefined,
  categoryId: undefined,
  lang: i18n.locale.value
});

// 加载页面数据
const reload = async () => {
  list.value = [];

  pageCmsArticle(where).then(response => {
    list.value = response?.list || [];
    total.value = response?.count || 0;
  })

  // 设置页面标题
  useSeoMeta({
    description: `${where.keywords || route.params.keywords}`,
    keywords: `${where.keywords || route.params.keywords}`,
    titleTemplate: `【搜索结果】${where.keywords || route.params.keywords}` + ' - %s',
  })
}

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

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

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

watch(
    () => route.params.keywords,
    (keywords) => {
      where.keywords = String(keywords);
      if(where.keywords == '关键词不能为空！'){
        where.keywords = undefined;
      }
      reload();
    },
    { immediate: true }
);
</script>
<style scoped lang="less">
</style>
