<template>
    <div>
        <HomePageHead></HomePageHead>
        <HomePageNavigation></HomePageNavigation>
        <HomeSecondaryHeading :titleData="pageData" :pinyinid="routeId"></HomeSecondaryHeading>
        <!-- 频道页 -->
        <main class="index_main">
            <!--板块1-->
            <section class="channel_1 clearfix">
                <div class="channel_1_left">
                    <div class="channel_1_box">
                        <HomeSwiperSnscChannel :data="pageData[0].data"></HomeSwiperSnscChannel>
                    </div>
                </div>
                <div class="channel_1_right">
                    <div class="channel_name_ul">
                        <div class="channel_name_ul_li" v-for="item in pageData[0].data2">
                            <NuxtLink class="channel_name_ul_li_tag"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            >
                                民工动态
                            </NuxtLink>
                            <NuxtLink class="channel_name_ul_li_dot1 dot1"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            >
                                {{item.title}}
                            </NuxtLink>
                        </div>
                    </div>
                </div>
            </section>
            <!--板块2-->
            <section class="channel_2 clearfix">
                <div class="channel_1_left clearfix">
                    <!--open 商贸活动-->
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[1])" 
                            v-if="pageData[1]" 
                            :title="pageData[1].alias"
                        >
                            {{ pageData[1].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[1])" 
                            v-if="pageData[1]" 
                            :title="pageData[1].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class=" clearfix">
                        <div class="channel_1_box_1 clearfix">
                            <div class="channel_img_big_1_ul">
                                <div class="channel_img_big_1_li" v-for="item in pageData[1].data">
                                    <NuxtLink class="channel_img_big_1_li_a"
                                        :href="getLinkPathDetail(item)"
                                        :title="item.title"
                                        :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'"
                                    >
                                        <img class="channel_img_big_1_li_img" :src="item.imgurl" alt="item.title">
                                        <div class="channel_img_big_1_li_dot1 dot1">
                                            {{ item.title }}
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                        <div class="channel_1_box_2 clearfix">
                            <div class="channel_ul_2">
                                <NuxtLink class="channel_ul_2_a dot1"
                                    :href="getLinkPathDetail(item)"
                                    :title="item.title"
                                    v-for="item in pageData[1].data2"
                                    :key="item.id"
                                    :target="item.islink == 1 ? '_blank' : '_self'"
                                >
                                    {{ item.title }}
                                </NuxtLink>
                            </div>
                        </div>
                    </div>
                    <!--end 商贸活动-->
                    <!--open 科教活动-->
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[2])" 
                            v-if="pageData[2]" 
                            :title="pageData[2].alias"
                        >
                            {{ pageData[2].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[2])" 
                            v-if="pageData[2]" 
                            :title="pageData[2].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class=" clearfix">
                        <div class="channel_1_box_1 clearfix">
                            <div class="channel_img_big_2_ul">
                                <div class="channel_img_big_2_li" v-for="item in pageData[2].data">
                                    <NuxtLink class="channel_img_big_2_li_a"
                                        :href="getLinkPathDetail(item)"
                                        :title="item.title"
                                        :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'"
                                    >
                                        <img class="channel_img_big_2_li_img" :src="item.imgurl" alt="item.title">
                                        <div class="channel_img_big_2_li_dot1 dot1">
                                            {{ item.title }}
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                        <div class="channel_1_box_2 clearfix">
                            <div class="channel_ul_3">
                                <NuxtLink class="channel_ul_3_a dot1"
                                    :href="getLinkPathDetail(item)"
                                    :title="item.title"
                                    v-for="item in pageData[2].data2"
                                    :key="item.id"
                                    :target="item.islink == 1 ? '_blank' : '_self'"
                                >
                                    {{ item.title }}
                                </NuxtLink>
                            </div>
                        </div>
                    </div>
                    <!--end 科教活动-->
                </div>
                <div class="channel_1_right clearfix">
                    <!--open 产业动态-->
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[3])" 
                            v-if="pageData[3]" 
                            :title="pageData[3].alias"
                        >
                            {{ pageData[3].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[3])" 
                            v-if="pageData[3]" 
                            :title="pageData[3].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class="channel_img_big_3_ul clearfix">
                        <div class="channel_img_big_3_li" v-for="item in pageData[3].data">
                            <NuxtLink class="channel_img_big_3_li_a"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            >
                                <img class="channel_img_big_3_li_img" :src="item.imgurl" alt="item.title">
                                <div class="channel_img_big_3_li_dot1 dot1">
                                    {{ item.title }}
                                </div>
                            </NuxtLink>
                        </div>
                    </div>
                    <div class="channel_ul_4">
                        <div class="channel_ul_4_li dot1" v-for="(item, index) in pageData[3].data2">
                            <div class="channel_ul_4_li_num">{{ index + 1 }}</div>
                            <NuxtLink class="channel_img_big_3_li_a"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            >
                                <div class="channel_ul_4_a dot1">
                                    {{ item.title }}
                                </div>
                            </NuxtLink>
                        </div>
                    </div>
                    <!--end 产业动态-->
                </div>
            </section>
            <!--广告1-->
            <HomeAdvertising :data="adImg1"></HomeAdvertising>
            
            <section class="channel_3 clearfix">
                <!--open 农贸节庆-->
                <div class="channel_1_left clearfix">
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[4])" 
                            v-if="pageData[4]" 
                            :title="pageData[4].alias"
                        >
                            {{ pageData[4].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[4])" 
                            v-if="pageData[4]" 
                            :title="pageData[4].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class=" clearfix">
                        <div class="channel_1_box_1 clearfix">
                            <div class="channel_img_big_4_ul">
                                <div class="channel_img_big_4_li" v-for="item in pageData[4].data">
                                    <NuxtLink class="channel_img_big_4_li"
                                        :href="getLinkPathDetail(item)"
                                        :title="item.title"
                                        :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'"
                                    >
                                        <img class="channel_img_big_4_li_img" :src="item.imgurl" alt="item.title">
                                        <div class="channel_img_big_4_li_dot2 dot2">
                                            {{ item.title }}
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                            <div class="channel_img_ul_1 clearfix">
                                <div class="channel_img_ul_1_li" v-for="item in pageData[4].data2">
                                    <NuxtLink class="channel_img_ul_1_li_a"
                                        :href="getLinkPathDetail(item)"
                                        :title="item.title"
                                        :key="item.id"
                                        :target="item.islink == 1 ? '_blank' : '_self'"
                                    >
                                        <img class="channel_img_ul_1_li_img" :src="item.imgurl" alt="item.title">
                                        <div class="channel_img_ul_1_li_dot2 dot2">
                                            {{ item.title }}
                                        </div>
                                    </NuxtLink>
                                </div>
                            </div>
                        </div>
                        <div class="channel_1_box_2 clearfix">
                            <div class="channel_ul_5">
                                <NuxtLink class="channel_ul_5_a dot1"
                                    :href="getLinkPathDetail(item)"
                                    v-for="item in pageData[4].data3"
                                    :title="item.title"
                                    :key="item.id"
                                    :target="item.islink == 1 ? '_blank' : '_self'"
                                >
                                    {{ item.title }}
                                </NuxtLink>
                            </div>
                        </div>
                    </div>
                </div>
                <!--end 农贸节庆-->
                <!--open 市场行情-->
                <div class="channel_1_right clearfix">
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[5])" 
                            v-if="pageData[5]" 
                            :title="pageData[5].alias"
                        >
                            {{ pageData[5].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[5])" 
                            v-if="pageData[5]" 
                            :title="pageData[5].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class="channel_img_ul_2 clearfix">
                        <div class="channel_img_ul_2_li" v-for="item in pageData[5].data">
                            <NuxtLink class="channel_img_ul_2_li"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            >
                                <img class="channel_img_ul_2_li_img" :src="item.imgurl" alt="item.title">
                                <div class="channel_img_ul_2_li_dot1 dot1">
                                    {{ item.title }}
                                </div>
                            </NuxtLink>
                        </div>
                    </div>
                    <div class="channel_ul_6">
                        <NuxtLink class="channel_ul_6_a dot1"
                            :href="getLinkPathDetail(item)"
                            :title="item.title"
                            v-for="item in pageData[5].data2"
                            :key="item.id"
                            :target="item.islink == 1 ? '_blank' : '_self'"
                        >
                            {{ item.title }}
                        </NuxtLink>
                    </div>
                    <!--end 市场行情-->
                </div>
            </section>
            <section class="channel_4 clearfix">
                <div class="channel_1_left clearfix">
                    <!--open 业界交流-->
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[6])" 
                            v-if="pageData[6]" 
                            :title="pageData[6].alias"
                        >
                            {{ pageData[6].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[6])" 
                            v-if="pageData[6]" 
                            :title="pageData[6].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class="channel_ul_7">
                        <NuxtLink class="channel_ul_7_a dot1"
                            :href="getLinkPathDetail(item)"
                            :title="item.title"
                            v-for="item in pageData[6].data"
                            :key="item.id"
                            :target="item.islink == 1 ? '_blank' : '_self'"
                        >
                            {{ item.title }}
                        </NuxtLink>
                    </div>
                    <!--end 业界交流-->
                </div>
                <!--open 主题活动-->
                <div class="channel_1_right clearfix">
                    <div class="channel_head">
                        <NuxtLink class="channel_head_name" 
                            :href="getLinkPath(pageData[7])" 
                            v-if="pageData[7]" 
                            :title="pageData[7].alias"
                        >
                            {{ pageData[7].alias }}
                        </NuxtLink>
                        <NuxtLink class="channel_head_more" 
                            :href="getLinkPath(pageData[7])" 
                            v-if="pageData[7]" 
                            :title="pageData[7].alias"
                        >
                            栏目简介
                        </NuxtLink>
                    </div>
                    <div class="channel_img_big_5_ul">
                        <div class="channel_img_big_5_li" v-for="item in pageData[7].data">
                            <NuxtLink class="channel_img_big_5_li_a"
                                :href="getLinkPathDetail(item)"
                                :title="item.title"
                                v-for="item in pageData[7].data"
                                :key="item.id"
                                :target="item.islink == 1 ? '_blank' : '_self'"
                            > 
                                <img class="channel_img_big_5_li_img" :src="item.imgurl" alt="item.title">
                                <div class="channel_img_big_5_li_dot2 dot2">{{ item.title }}</div>
                            </NuxtLink>
                        </div>
                    </div>
                    <!--end 主题活动-->
                </div>
            </section>
        </main>
        <!-- 页脚部分 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//0.加载页面依赖 start ---------------------------------------->
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
//0.加载页面依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
const targetSegment = getRoutePath(1);
//1.1 获得当前的路由id
let routeId;
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    routeId = getRouteId.data.category_id
} else {
    console.log("获得路由id出错！", getRouteId.message)
}
//1.获得路由id end ---------------------------------------->

//2.选项卡 start ---------------------------------------->
let showTabs = ref(1)
let qhtabs = function (number) {
    console.log(number)
    showTabs.value = number
}
//2.选项卡 end ---------------------------------------->

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

    //广告3
    let url3 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_category_0003`
    const responseAd3 = await fetch(url3, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd3 = await responseAd3.json();
    adImg3.value = resultAd3.data[0];

    //广告4
    let url4 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_category_0004`
    const responseAd4 = await fetch(url4, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd4 = await responseAd4.json();
    adImg4.value = resultAd4.data[0];
})
//3.广告 end ---------------------------------------->

//4.页面数据 start ---------------------------------------->
const pageData = ref([
    //示例：
    // {
    //     id: 1, 
    //     title: "", //该导航的标题
    //     data: [], //数据1
    //     data1: [], //数据2
    //     data2: [], //数据3
    //     data3: [], //数据4
    //     data4: [], //数据5
    //     category_id1: "",  //子导航id1
    //     category_id2: "", 
    //     category_id3: "",
    //     category_id4: "",
    //     title1: "", //子导航标题1
    //     title2: "",
    //     title3: "",
    //     title4: "",
    //     cid: "" //该导航的id
    // },
])

//4.1 获取所有导航
try {
    const navigateData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': routeId,
            'num': 8
        },
    });
    if (navigateData.code == 200) {
        // 遍历可用的导航池放到页面中
        for (let index in navigateData.data) {
            let data = {
                title: navigateData.data[index].name,
                cid: navigateData.data[index].category_id,
                children_count: navigateData.data[index].children_count,
                alias: navigateData.data[index].alias,
                aLIas_pinyin: targetSegment + "/" + navigateData.data[index].aLIas_pinyin,
                data: [],
                data1: [],
                data2: [],
                data3: [],
                data4: [],
                category_id1: "",
                category_id2: "",
                category_id3: "",
                category_id4: "",
                title1: "",
                title2: "",
                title3: "",
                title4: ""
            };
            if (navigateData.data[index].is_url == 1) {
                // 处理 URL 的逻辑
            } else {
                //每个页面最多8个模块
                pageData.value.push(data);
            }
        }
        //导航池加载完毕，开始申请模块数据
        let getJson = [
            { "parent": routeId + ",0,0", "child": pageData.value[0].cid + ",5,10" },//模块1 顶部没有标题的那个
            { "parent": routeId + ",0,0", "child": pageData.value[1].cid + ",1,6" },//模块2
            { "parent": routeId + ",0,0", "child": pageData.value[2].cid + ",1,6" },//模块3
            { "parent": routeId + ",0,0", "child": pageData.value[3].cid + ",1,6" },//模块4
            { "parent": routeId + ",0,0", "child": pageData.value[4].cid + ",3,10" },//模块5
            { "parent": routeId + ",0,0", "child": pageData.value[5].cid + ",2,6" },//模块6
            { "parent": routeId + ",0,0", "child": pageData.value[6].cid + ",0,6" },//模块7
            { "parent": routeId + ",0,0", "child": pageData.value[7].cid + ",1,0" },//模块8
        ]
        let jsonString = JSON.stringify(getJson);
        getPageAllData(jsonString);

    } else {
        console.log("错误：渲染8个模块环节出错，请检查是否存在没有数据的模块！")
    }
} catch (error) {
    console.log("错误：导航池渲染执行接口出错！请检查频道页渲染的模块数据是否完整！")
}


async function getPageAllData(jsonString) {
    const mkdata = await requestDataPromise('/web/getWebsiteAllArticle', {
        method: 'GET',
        query: {
            'id': jsonString
        },
    });

    if (mkdata.code == 200) {
        //模块1 顶部大图和列表
        pageData.value[0].data = mkdata.data[0].child.imgnum;
        pageData.value[0].data2 = mkdata.data[0].child.textnum;
        //模块2
        pageData.value[1].data = mkdata.data[1].child.imgnum;
        pageData.value[1].data2 = mkdata.data[1].child.textnum;
        //模块3
        pageData.value[2].data = mkdata.data[2].child.imgnum;
        pageData.value[2].data2 = mkdata.data[2].child.textnum;
        //模块4
        pageData.value[3].data = mkdata.data[3].child.imgnum;
        pageData.value[3].data2 = mkdata.data[3].child.textnum;
        //模块5
        for(let index in mkdata.data[4].child.imgnum){
            if(index<1){
                pageData.value[4].data.push(mkdata.data[4].child.imgnum[index]);
            }else{
                pageData.value[4].data2.push(mkdata.data[4].child.imgnum[index]);
            }
        }
        pageData.value[4].data3 = mkdata.data[4].child.textnum;
        //模块6
        pageData.value[5].data = mkdata.data[5].child.imgnum;
        pageData.value[5].data2 = mkdata.data[5].child.textnum;
        //模块7
        pageData.value[6].data = mkdata.data[6].child.textnum;
        //模块8
        pageData.value[7].data = mkdata.data[7].child.imgnum;

    } else {
        ElMessage.error(mkdata.message)
    }
}
//4.页面数据 end ---------------------------------------->

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

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 + "_" + seoSuffix,
        meta: [
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
        ]
    });
} else {
    console.log("设置频道页SEO出错！",setData.message)
}
//5.设置seo信息 end---------------------------------------->
</script>

<style lang="less" scoped>
@import '@/assets/css/class.less';
</style>