<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>
        <!-- 导航栏 -->
        <HomePageNavigation1></HomePageNavigation1>
        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adList[0]" v-if="adList[0]"></HomeTopTen>
        <!-- 搜索框 -->
        <div class="search">
            <div class="inner">
                <input v-model="keywordInput" type="text" placeholder="请输入搜索内容">
                <button @click="goSearch">搜索</button>
            </div>
        </div>
        <div class="breadcrumb">
            <div class="inner">
                <span class="location">当前位置:</span>
                <el-breadcrumb :separator-icon="ArrowRight">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item :to="{ path: '/search/search' }">搜索</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <ul class="list" v-if="newsList.length >= 0">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :to="item.linkurl" target="_blank" v-show="item.islink == 1">{{ item.title }}
                            </NuxtLink>
                            <NuxtLink
                                :to="{ path: `/newsDetail/${item.id}`, query: { listId: routeId, listName: routeName } }"
                                target="_blank" v-if="item.islink == 0">{{ item.title }}</NuxtLink>
                        </li>
                    </ul>
                    <div v-if="newsList.length == 0" class="empty">
                        <div>
                            <img src="../../static/search/empty.png" alt="暂无内容">
                            <p>暂无搜索数据</p>
                        </div>
                    </div>
                    <!-- 分页器 -->
                    <div class="pagination">
                        <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                            v-model:page-size="pageSize" prev-text="上一页" next-text="下一页" @change="changePage" />
                    </div>
                </div>
            </div>
        </div>
        <!-- 列表页广告二 -->
        <HomeTopTen :imgurl="adList[1]" v-if="adList[1]"></HomeTopTen>
        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//1.页面依赖 start ---------------------------------------->
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
const route = useRoute();
const category_id = route.query.category_id; //获得该页面的id
const searchKey = route.query.keyword
const catids = ref([]);

//关键词
let total = useState("total", () => 0)
let page = useState("page", () => 1)
let pageSize = useState("pageSize", () => 20)
//1.页面依赖 end ---------------------------------------->

//2.页面数据 start ---------------------------------------->
//2.1 广告
let adList = ref([]);
const {data:adData,error:adError} = requestData('/web/getWebsiteAdvertisement',{method:'GET',query:{'ad_tag':'SEARCH'}});
if (adError.value) {
    console.error('广告列表请求失败:', adError.value);
} else {
    //当有值了以后再放进去，万恶之源，也是nuxt2和3都存在的一个问题，也许nuxt4会解决这个问题
    if (adData.value && adData.value.data) {
        adList.value = adData.value.data;
    }
}

// 新闻列表
const newsList = ref([]);
let keywordInput = ref("");

//直接搜索
let goSearch = async () => {
    const listData =  await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'keyword':keywordInput.value
        },
    });
    newsList.value = listData.data.rows;
    total.value = listData.data.count;
}

//基于导航池搜索
let goSearchFont = async (font) => {
    const listData =  await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'catid': font
        },
    });
    newsList.value = listData.data.rows;
    total.value = listData.data.count;
}

//基于导航池与关键词搜索
let goSearchFontandCid = async (font) => {
    const listData =  await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'catid': category_id,
            'keyword':searchKey
        },
    });
    newsList.value = listData.data.rows;
    total.value = listData.data.count;
}

const newslists = async () => {
    try {
        // 从地区跳转
        if (route.query.catids && route.query.catids.length > 0) {
            // 将字符串转换回数组，并过滤掉可能的空值
            catids.value = route.query.catids.split(',').filter(id => id);

            if (catids.value.length > 0) {
                // 有效的 catids 数组，可以进行后续操作
                console.log('接收到的分类ID数组：', catids.value);
                let font = catids.value.join(",")
                font = "[" + font + "]"

                goSearchFont(font);
            } else {
                // 处理空数组情况
                console.log('未接收到有效的分类ID');
            }
        } else {
            if (category_id == undefined) {
                //直接搜索 默认进来没有导航池的话 不执行搜索
                //goSearch();
            } else {
                goSearchFontandCid();
            }
        }
    } catch (error) {
        console.error(error);
    }
}

newslists();

//分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    page.value = value
    console.log(page.value);
    newslists()
}
//2.页面数据 end ---------------------------------------->


//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
async function getSeo() {
    const setData =  await requestDataPromise('/web/getWebsiteFootInfo', {
        method: 'GET',
        query: {},
    });
    seoSetup(setData.data.website_head.title,setData.data.website_head.description,setData.data.website_head.keywords)
}
getSeo();
//4.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import url('@/assets/css/search.less');
</style>