<template>
    <!-- 资讯详情 -->
    <div class="news">
        <main class="index_main">
            <section class="clearfix">
                <div class="info_1_left">
                    <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="p_parent_name != ''">
                                    <NuxtLink :to="`/${targetSegment}/index.html`"> {{ p_parent_name }}</NuxtLink>
                                </el-breadcrumb-item>
                                <el-breadcrumb-item v-if="parent_name != ''">
                                    <NuxtLink :to="`/${targetSegment}/${targetSegment1}/index.html`"> {{ parent_name }}
                                    </NuxtLink>
                                </el-breadcrumb-item>
                                <el-breadcrumb-item>{{ navTitle }}</el-breadcrumb-item>
                            </el-breadcrumb>
                        </div>
                    </div>
                    <h4 class="show_1_h4">{{ newsDetail.title }}</h4>
                    <div class="show_1_title_box clearfix">
                        <span class="show_1_title">来源：{{ newsDetail.copyfrom }}</span>
                        <span class="show_1_title">作者：{{ newsDetail.author }}</span>
                        <span class="show_1_title">时间：{{ getTime(newsDetail.updated_at, 'year', 1) }}</span>
                    </div>
                    <article class="show_1_article" v-html="newsDetail.content">
                    </article>
                    <footer class="show_foot" v-if="newsDetail.copyfrom != '本网'">
                        原文链接：{{ newsDetail.fromurl }}
                        <br>
                        [免责声明]本文来源于网络转载，仅供学习交流使用，不构成商业目的。 版权归原作者所有，如涉及作品内容，版权和其他问题，请在30日与本网联系，我们将第一时间处理。
                    </footer>
                    <div v-if="articleChoice">
                        <HomeSurveyvote></HomeSurveyvote>
                    </div>
                    <!-- 推荐阅读 -->
                    <div class="recommendRead" v-if="newsDetail.commendArticle != false">
                        <div class="recommendReadTitle">
                            推荐阅读
                        </div>
                        <div class="recommendReadList">
                            <div class="recommendReadListTitle" v-for="(item, index) in newsDetail.commendArticle"
                                :key="item.id">
                                <a :href="`/${item.alias_pinyin}/${item.id}.html`" v-if="index < 3">
                                    {{ item.title }}
                                </a>
                                <span v-if="index < 3">{{ getTime(item.created_at, "month", 1) }}</span>
                            </div>
                        </div>
                    </div>
                </div>
                <aside class="info_1_aside">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </aside>
            </section>
        </main>
    </div>
</template>
<script setup>
//0.页面依赖 start ---------------------------------------->
import { onMounted } from 'vue'
import { ElBreadcrumb, ElBreadcrumbItem, ElRadio, ElRadioGroup, ElCheckbox, ElCheckboxGroup, ElMessage, ElInput } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
//0.页面依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
const route = useRoute();
const articleId = parseInt(route.params.id);
const targetSegment = getRoutePath(1);
const targetSegment1 = getRoutePath(2);
const targetSegment2 = getRoutePath(3);
//1.1 获得当前的路由id
let routeId;
let routeType;
let navTitle = ref('')//二级导航标题
let navCid = ref('')//二级导航id
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment2,
    },
});
console.log("getRouteId", getRouteId);

if (getRouteId.code == 200) {
    navTitle.value = getRouteId.data.alias
    navCid.value = getRouteId.data.category_id
} else {
    console.log("获得路由id出错！", getRouteId.message)
}
//1.2 获取二级栏目
let parent_name = ref('');//父级名称
let parent_id = ref('');//父级id
let parent_pinyin = ref('');//父级拼音
const getRoutePName = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment1,
    },
});
console.log("getRoutePName", getRoutePName);

if (getRoutePName.code == 200) {
    routeId = getRoutePName.data.category_id
    parent_id.value = getRoutePName.data.category_id
    parent_name.value = getRoutePName.data.alias
    routeType = getRoutePName.data.type
} else {
    console.log("获得路由id出错！", getRoutePName.message)
}
//1.3 获取一级栏目
let p_parent_name = ref('');//父级名称
let p_parent_id = ref('');//父级id
let p_parent_pinyin = ref('');//父级拼音
const getRoutePName_parent = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
console.log("getRoutePName_parent", getRoutePName_parent);

if (getRoutePName_parent.code == 200) {
    p_parent_id.value = getRoutePName_parent.data.category_id
    p_parent_name.value = getRoutePName_parent.data.alias
    // p_parent_pinyin.value = getRoutePName_parent.data.aLIas_pinyin
} else {
    console.log("获得路由id出错！", getRoutePName_parent.message)
}
//1.4 获取某个栏目
const getParentId = await requestDataPromise('/web/getOneWebsiteCategory', {
    method: 'GET',
    query: {
        'catid': parent_id.value,
    },
});
console.log("getParentId", getParentId);

if (getParentId.code == 200) {
    parent_pinyin.value = getParentId.data.aLIas_pinyin
} else {
    console.log("获得路由id出错！", getParentId.message)
}
//1.获得路由id end ---------------------------------------->


//4.新闻详情 start ---------------------------------------->
//4.1 资讯详情
const newsDetail = ref({})
const routeNewsTtitle = ref("");
//4.2 发布日期
const time = ref("");
//4.3 路径
const routLevelTitle = ref("");
const routLevelId = ref("");
//4.4 是否展示投票
const articleChoice = ref(false);
//4.5 获取详情
async function getPageData() {
    const mkdata = await requestDataPromise('/web/selectWebsiteArticleInfo', {
        method: 'GET',
        query: {
            'articleid': articleId
        },
    });
    console.log('111111111111111', mkdata.data);

    if (mkdata.code == 200) {
        //判断是否显示投票
        if (mkdata.data.is_survey == 1) {
            console.log("本篇文章含有投票！")
            articleChoice.value = true;
        }
        //获取内容
        newsDetail.value = mkdata.data;
        //获取路径
        routLevelTitle.value = newsDetail.value.cat_name;
        routLevelId.value = newsDetail.value.category_id;
        //获取发布时间
        time.value = newsDetail.value.updated_at.split(' ')[0];
        //修正标题长度
        if (newsDetail.value.title.length >= 20) {
            routeNewsTtitle.value = newsDetail.value.title.substr(0, 20) + "...";
        } else {
            routeNewsTtitle.value = newsDetail.value.title
        }
    } else {
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
        console.log("错误位置：获取详情内容")
        console.log("后端错误反馈：", mkdata.message)
        console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    }
}
getPageData();
//4.新闻详情 end ---------------------------------------->


//5.广告 start ---------------------------------------->
let adImg1 = ref([]);
let adImg2 = ref([]);
onMounted(async () => {
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snscw_detail_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=snscw_detail_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 ---------------------------------------->

//6.设置seo信息 start---------------------------------------->
// const setData = await requestDataPromise('/web/selectWebsiteArticleInfo', {
//     method: 'GET',
//     query: {
//         'articleid': articleId
//     },
// });
// if (setData.code == 200) {
//     let seoTitle = setData.data.title;
//     let seoDescription = setData.data.introduce;
//     let seoKeywords = setData.data.keyword;
//     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("获取广告数据失败！", setData.message)
// }
//6.设置seo信息 end---------------------------------------->

//8.页面图片放大 start---------------------------------------->
const previewVisible = ref(false)
const selectedImage = ref(' ')

const openPreview = (event) => {
    if (event.target.tagName === 'IMG') {
        selectedImage.value = event.target.src;
        previewVisible.value = true;
    }
}
const closePreview = () => {
    previewVisible.value = false;
}
//8.页面图片放大 end---------------------------------------->
</script>

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