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

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

        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adImg1" v-if="adImg1"></HomeTopTen>

        <!-- 二级标题-->
        <div class="sannongzhichuang" v-if="parent_name != ''">
            <div class="inner">
                <h2>
                    <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                    <em class="iconfont icon-xingzhuang-zhijiaosanjiaoxing-copy"></em>
                </h2>
                <p class="introduction">
                    <strong>频道介绍</strong>
                    <div v-for="(item, index) in secondNav" :key="index">
                        <span v-if="index <= 7">
                            <NuxtLink :to="{ path: `/${targetRoute}/${item.aLIas_pinyin}/list-1.html`}">{{ item.alias }}</NuxtLink>
                        </span>
                        <b v-else></b>
                    </div>
                </p>
            </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 v-if="parent_name !=''">
                        <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                    </el-breadcrumb-item>
                    <el-breadcrumb-item class="phone_breadcrumb_text">{{ 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 :href="getLinkPathDetail(item)" :title="item.alias">
                                {{ item.title }}
                            </NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->

                    <div class="pagination pagination_phone_none" v-if="total > 0">
                        <el-pagination 
                            size="small" 
                            background 
                            layout="prev, pager, next" 
                            :total="total" 
                            class="mt-4"
                            :page-size="pageSize" 
                            :current-page="pageNum"
                            prev-text="上一页" 
                            next-text="下一页" 
                            @current-change="changePage" 
                        />
                    </div>
                    <div class="pagination pagination_pc_none" v-if="total > 0">
                        <el-pagination 
                            pager-count="5"
                            size="small" 
                            background 
                            layout="pager " 
                            :total="total" 
                            class="mt-4"
                            :page-size="pageSize" 
                            :current-page="pageNum"
                            @current-change="changePage" 
                        />
                    </div>

                </div>
                <div class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </div>
            </div>
        </div>

        <!-- 列表页广告二 -->
        <HomeTopTen :imgurl="adImg2" v-if="adImg2"></HomeTopTen>

        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//1.页面必备依赖 start ---------------------------------------->
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue';
//当前列表名称
const name = ref('')
const { $webUrl, $CwebUrl, $BwebUrl } = useNuxtApp()

//格式化跳转路径
const getLinkPathDetail = (item) => {
    if (item.islink == 1) {
        return `${item.linkurl}`;
    } else {
        //return `/${item.aLIas_pinyin}/${item.id}`;
        //return `/newsDetail/${item.id}`
        return `/${item.pinyin}/${item.id}.html`;
    }
}
//1.页面必备依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
const route = useRoute();
let articleId = 0;//路由id
let pageNum = ref(2);
let total = ref(1);
let pageSize = ref(20);

//获得当前的完整路径
const fullPath = route.path;
//拆分，取出来中间这一段，然后提取数字部分
const segments = fullPath.split('/'); 
const targetSegment = segments[2]; 
const targetRoute = segments[1]; 

//const numberPart = targetSegment.match(/\d+$/)?.[0]; 
//let routeId = 20 //排除路径错误可以打开这个
//articleId = numberPart;

//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetRoute+'/'+targetSegment,
    },
});
if(getRouteId.code == 200){
    articleId = getRouteId.data.category_id
}else{
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：通过url路径查询导航池id")
    console.log("后端错误反馈：",getRouteId.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}
//获得html前的数字
// const pageUrl = segments[3]; 
// const pageNumber = pageUrl.split('.')[0];
// console.log("当前URL中的页码：")
// console.log(pageNumber)//2
// pageNum.value = parseInt(pageNumber);
pageNum.value = parseInt(route.params.id);

//1.获得路由id end ---------------------------------------->

//2.页面数据 start ---------------------------------------->
//2.2新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData = await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': pageNum.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);
    navigateTo(`/${targetSegment}/${value}.html`)
}

//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 ---------------------------------------->


//3.二级栏目 start ---------------------------------------->
//3.1通过id获取父栏目
const parent_name = ref([]);
const parent_id = ref([]);
const parent_pinyin = ref("");
let getParentNav = async () => {
    const listData = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        console.log(111999)
        console.log(listData.data);
        parent_name.value = listData.data.parent_name;
        parent_id.value = listData.data.parent_id;
        parent_pinyin.value = listData.data.parent_pinyin;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
    getSecondNav();
}
//获得父级栏目详情
getParentNav();

// 3.2获取二级栏目
const secondNav = ref([]);
let getSecondNav = async () => {
    const listData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': parent_id.value,
            'num': 8,
        },
    });
    console.log('listData', listData);

    if (listData.code == 200) {
        secondNav.value = listData.data;
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取新闻列表")
        console.log("后端错误反馈：", listData.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
//3.二级栏目 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 , tagPriority: 10 },
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix , tagPriority: 10 },
            { name: 'viewport', content: 'width=device-width,initial-scale=1,user-scalable=no',tagPriority: 10 }
        ]
    });
} 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---------------------------------------->


//5.广告 start---------------------------------------->
let adImg1 = ref({});
let adImg2 = ref({});

onMounted(async ()  => {

    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_list_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];
    
    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_list_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];

})
//5.广告 end---------------------------------------->
</script>

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

<style lang="less" scoped>

   @media screen and (min-width:801px){/*pc*/
    
        .pagination_pc_none{display:none!important;}

        .pc_none{display:none;}  
    }        
@media screen and (max-width:800px){/*ipad_phone*/
    .sannongzhichuang .inner{width:100%;height:33px;background:#fafafa;}    
    .sannongzhichuang h2{display:none;}

    .sannongzhichuang p.introduction{background:#fafafa;
        height:33px;line-height:33px;right:4%;box-sizing:border-box;
        width:92%;display:block;word-break: keep-all; white-space: nowrap;
        overflow-x:auto;
        overflow-y:hidden;

          &::-webkit-scrollbar {
                height:0px;
            }

    }

    .sannongzhichuang p.introduction > strong{display:none!important;}
    .sannongzhichuang p.introduction div{display:inline-block;height:33px;line-height:33px;}
    .sannongzhichuang p.introduction div span{display: block;height:33px;line-height:33px;margin-top:0px;width:auto;}
    .sannongzhichuang p.introduction div:nth-of-type(1) span{padding-left:0px;}

    .sannongzhichuang p.introduction div span{display:block;height:33px;line-height:33px;font-size:14px;padding:0px 10px;}
    .sannongzhichuang p.introduction div a{display:block;height:33px;line-height:33px;font-size:14px;}

    .introduction .router-link-exact-active{color:#139602!important;}

    .location{float:left;width:auto!important;} 
       .breadcrumb .el-breadcrumb{width:100%;display:block;}
        .breadcrumb{margin:15px auto 10px;}
        // .breadcrumb .inner{ height:24px; }
        .breadcrumb .phone_breadcrumb_text{
            display:block;float:none;   
            word-break: keep-all; white-space: nowrap;overflow:hidden;text-overflow:ellipsis;
        }   
          .breadcrumb .location{ font-size: 14px; margin-right:5px;}
            .breadcrumb span { font-size: 14px; }

.newsList .inner{width:92%!important;}
            .newsList .inner .innerLeft > .list > li{width:96%;margin:0px auto;}

            .newsList .inner .innerLeft{width:100%;}


            .newsList .inner .innerLeft > .list > li{line-height:50px;}
            
            
            .newsList .inner .innerLeft > .list > li > a{
                width:100%;display:block; height:50px;line-height:50px;font-size:16px;
                word-break: keep-all; white-space: nowrap;overflow:hidden;text-overflow:ellipsis;
            }
            

            .newsList .inner .innerLeft > .list > li a{float:left;width:92%; }
            .newsList .inner .innerLeft > .list > li:nth-of-type(-n+2)::after{display: block;height:22px;margin:16px 0px 0px 0px;line-height:22px;float:right;}

            .newsList .inner .innerLeft > .pagination{width:100%;}
            .newsList .inner .innerLeft > .list{margin-bottom:11px;}
            .newsList .inner .innerLeft > .pagination{margin-bottom:11px;}

            .newsList .inner .innerRight {width:100%;display:none;}

            .index_foot{margin-top:11px;}


             ::v-deep .el-pager li{
                margin:0px 4px!important;

            }
            
            .sannongzhichuang{margin:10px auto 10px;}
            .pagination_phone_none{display:none!important;}



}
</style>




