<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>
                        <NuxtLink to="/">首页</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item>搜索</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" v-show="item.islink == 1" :title="item.title">{{ item.title }}
                            </NuxtLink>
                            <NuxtLink
                                :to="{ path: `/newsDetail/${item.id}`, query: { listId: routeId, listName: routeName } }"
                                v-if="item.islink == 0" :title="item.title">{{ item.title }}</NuxtLink>
                        </li>
                    </ul>
                    <div v-if="newsList.length == 0" class="empty">
                        <div>
                            <img src="../../public/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 pageCatids = route.query.catids;
const pageDepartment_id = route.query.department_id;

//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 广告
const adList = ref([]);
async function getAdData(){
    const mkdata = await requestDataPromise('/web/getWebsiteAdvertisement',{
        method:'GET',
        query:{
            'ad_tag':'SEARCH'
        }
    });
    adList.value = mkdata.data;
}
getAdData();


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

//搜索
let goSearch = async () => {
    console.log(111222)
    console.log(page.value)
    console.log(pageSize.value)
    console.log(keywordInput.value)
    console.log(pageCatids)
    console.log(pageDepartment_id)
    const listData =  await requestDataPromise('/web/selectWebsiteCategory', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'keyword':keywordInput.value,
            'cityid':pageCatids,
            'department_id':pageDepartment_id
        },
    });
    if(listData.data.rows){
        console.log(listData)
        newsList.value = listData.data.rows;
        total.value = listData.data.count;
    }
   
}

goSearch();
//基于导航池搜索
// 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();


// 查询导航池的方法
// 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);
    goSearch()
}
//2.页面数据 end ---------------------------------------->


//4.设置seo信息 start---------------------------------------->
//4.1 设置seo信息
const setData =  await requestDataPromise('/web/getWebsiteFootInfo', {
    method: 'GET',
    query: {},
});

let seoTitle = setData.data.website_head.title;
let seoDescription = setData.data.website_head.description;
let seoKeywords = setData.data.website_head.keywords;
let seoSuffix = setData.data.website_head.suffix;
let seoName = setData.data.website_head.website_name;

useSeoMeta({
    title: seoTitle + "_" + seoSuffix,
    meta: [
        { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix },
        { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix }
    ]
});
//4.设置seo信息 end---------------------------------------->
</script>

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