<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>

        <!-- 导航栏 -->
        <HomePageNavigation1></HomePageNavigation1>

        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adList[0]" v-if="adList[0]"></HomeTopTen>

        <!-- 面包屑导航 -->
        <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>{{ name }}</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
        </div>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <ul class="list">
                        <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="`/newsDetail/${item.id}`" v-if="item.islink==0" :title="item.title">{{ item.title }}</NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" v-if="total">
                        <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 class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </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'
//获得跳转过来的id
const route = useRoute();
const articleId = route.params.id; //获得该页面的id
const category_id = route.query.catid; //获得该页面的id
//当前列表名称
const name = ref('')

const { $webUrl, $CwebUrl, $BwebUrl } = useNuxtApp()
//1.页面必备依赖 end ---------------------------------------->


//2.页面数据 start ---------------------------------------->
let id = useState("id", () => "")
let total = useState("total", () => 0)
let page = useState("page", () => 1)
let pageSize = useState("pageSize", () => 20)

id.value = route.query.id;

//2.1 获得所有广告
//广告列表
let adList = ref([]);
async function getAdData(){
    const adData = await requestDataPromise('/web/getWebsiteAdvertisement',{method:'GET',query:{'ad_tag':'LIST'}});
    if(adData.code==200){
        adList.value = adData.data;
    }else{
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取广告列表")
        console.log("后端错误反馈：",adData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
getAdData();

//2.2新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData =  await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': page.value,
            'pageSize': pageSize.value,
            'catid':articleId
        },
    });
    if(listData.code==200){
        newsList.value = listData.data.rows;
        total.value = listData.data.count;
    }else{
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：",listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
newslists();

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

//2.3获得页面名称
let getPageName = async () => {
    const pageName =  await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if(pageName.code==200){
        name.value = pageName.data.alias
    }else{
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：设置页面标题")
        console.log("后端错误反馈：",pageName.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//获得列表
getPageName();

//2.页面数据 end ---------------------------------------->

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

if(setData.code == 200){
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoName + "_" + seoSuffix,
        meta: [
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix },
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix }
        ]
    });
}else{
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：设置列表页面SEO数据")
    console.log("后端错误反馈：",setData.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}


//4.设置seo信息 end---------------------------------------->
</script>

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