<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>
        <!-- 导航栏 -->
        <HomePageNavigation></HomePageNavigation>
        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adImg1" v-if="adImg1"></HomeTopTen>
        <!-- 二级标题-->
        <HomeListSecondaryHeading v-if="parent_name != ''" :name="parent_name" :nav="secondNav" :pinyin="parent_pinyin"></HomeListSecondaryHeading>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <!-- 面包屑导航 -->
                    <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>{{ name }}</el-breadcrumb-item>
                            </el-breadcrumb>
                        </div>
                    </div>
                    <ul class="list">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :href="getLinkPathDetail(item)" :title="item.alias">
                                <span class="listTitle">{{ item.title }}</span>
                                <span class="time">{{ getTime(item.updated_at, 'month', 1) }}</span>
                            </NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" 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>
                <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';
//1.页面必备依赖 end ---------------------------------------->

//2.路径 start---------------------------------------->
//当前列表名称
const route = useRoute();
let articleId = 0;//列表需要使用的导航id 
let routeType;
const targetSegment = getRoutePath(2);//根据路由反向查询导航id
let pageNum = ref(1);//获得路由里面的分页数据

//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if(getRouteId.code == 200){
    articleId = getRouteId.data.category_id
    routeType = getRouteId.data.type
}else{
    console.log("后端错误反馈：",getRouteId.message)
}
pageNum.value = parseInt(route.params.id);
//2.路径 end---------------------------------------->

//3.列表 start ---------------------------------------->
let total = ref(1);
let pageSize = ref(20);

const newsList = ref([]);
let companyList = async () => {
    const listData = await requestDataPromise('/web/getWebsiteCompanyList', {
        method: 'GET',
        query: {
            'page': pageNum.value,
            'pageSize': pageSize.value,
            'category_id': articleId
        },
    });
    console.log("listData123111",listData);
    if (listData.code == 200) {
        newsList.value = listData.data.data;
        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 ---------->")
    }
}
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 ---------->")
    }
}
if(routeType == 1){
    newslists();
}else if(routeType == 6){
    companyList();
}

//分页
let changePage = (value) => {
    console.log("当前页码", value);
    navigateTo(`/${targetSegment}/${value}.html`)
}
//3.列表 end ---------------------------------------->


//4.面包屑 start---------------------------------------->
//3.1 获得页面名称
const name = ref('')
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("错误位置：设置页面标题")
    }
}
getPageName();

//3.2 获得父级栏目
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("错误位置：获取父级栏目")
    }
    getSecondNav();
}
getParentNav();

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

    if (listData.code == 200) {
        secondNav.value = listData.data;
    } else {
        console.log("错误位置：获取二级栏目列表")
    }
}
//4.面包屑 end ---------------------------------------->


//5.设置seo信息 start---------------------------------------->
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 }
        ]
    });
} else {
    console.log("错误位置：设置列表页面SEO数据")
}
//5.设置seo信息 end---------------------------------------->

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

onMounted(async ()  => {
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nzgxw_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=nzgxw_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];

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

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