<template>
    <!-- 头部 -->
    <templateHead v-if="skinId<=4"></templateHead>
    <!-- 皮肤5头部 -->
    <templateHead5 v-if="skinId==5"></templateHead5>
    <!-- 皮肤6头部 -->
    <templateHead6 v-if="skinId==6"></templateHead6>
    <!-- 菜单 -->
    <templateMenu v-if="skinId<=4"></templateMenu>
    <!-- 皮肤5菜单 -->
    <templateMenu5 v-if="skinId>=5"></templateMenu5>
    <!-- 内容 -->
    <div v-for="(item,index) in templateData" :key="index">
        <!--1.频道菜单-->
        <div v-if="item.sectorName=='channelMenu'">
            <templateChannelMenu :skinId="skinId" :routeId="routeId" :navigateData="navigateData.data" :templateData="item.componentList"></templateChannelMenu>
        </div>
        <!--2.广告组件-->
        <div v-if="item.sectorName=='adSector'">
            <templateAd :skinId="skinId" :adData="adData" :adTag="item.ad.ad_tag" ></templateAd>
        </div>
        <!--3.滚动图文组合-->
        <div v-if="item.sectorName=='scrollTextSector'">
            <templateScrollList :skinId="skinId" :templateData="item.componentList"></templateScrollList>
        </div>
        <!--4.图文组合1-->
        <div v-if="item.sectorName=='manyPictureSector'">
            <templateNewSector1 :skinId="skinId" :templateData="item.componentList"></templateNewSector1>
        </div>
        <!--5.图文组合2-->
        <div v-if="item.sectorName=='commentSector'">
            <templateNewSector2 :skinId="skinId" :templateData="item.componentList"></templateNewSector2>
        </div>
        <!--6.图文组合3-->
        <div v-if="item.sectorName=='listSector'">
            <templateNewSector3 :skinId="skinId" :templateData="item.componentList"></templateNewSector3>
        </div>
        <!-- 7.带标题的图文组合 -->
        <div v-if="item.sectorName=='channelNewsSector'">
            <templateNewSector4 :skinId="skinId" :templateData="item.componentList"></templateNewSector4>
        </div>
        <!-- 8.不带标题的图文组合 -->
        <div v-if="item.sectorName=='newsPhotoSector'">
            <templateNewPhotoSector :skinId="skinId" :templateData="item.componentList"></templateNewPhotoSector>
        </div>
        <!-- 9.带标题的文字新闻通栏 -->
        <div v-if="item.sectorName=='channelTextSector'">
            <templateNewTextSector :skinId="skinId" :templateData="item.componentList"></templateNewTextSector>
        </div>
        <!-- 10.不带标题的图文组合 -->
        <div v-if="item.sectorName=='newListPhotoSector'">
            <templateNewListPhotoSector :skinId="skinId" :templateData="item.componentList"></templateNewListPhotoSector>
        </div>
        <!-- 11.皮肤6 带标题图文组合 -->
        <div v-if="item.sectorName=='ChannelNewsPhotoSector'">
            <templateChannelNewsPhotoSector :skinId="skinId" :templateData="item.componentList"></templateChannelNewsPhotoSector>
        </div>
        <!-- 12.皮肤6 不带标题图文组合1 -->
        <div v-if="item.sectorName=='ChannelNewsSector'">
            <templateChannelNewsSector :skinId="skinId" :templateData="item.componentList"></templateChannelNewsSector>
        </div>
        <!-- 13.皮肤6 带标题图文组合2 -->
        <div v-if="item.sectorName=='ChannelNewsPhoto'">
            <templateChannelNewsPhoto :skinId="skinId" :templateData="item.componentList"></templateChannelNewsPhoto>
        </div>
        <!-- 14.皮肤6 不带标题图文组合2 -->
        <div v-if="item.sectorName=='ChannelNewsAndPhoto'">
            <templateChannelNewsAndPhoto :skinId="skinId" :templateData="item.componentList"></templateChannelNewsAndPhoto>
        </div>
    </div>
    <!-- 底部 -->
    <templateFoot v-if="skinId<=4"></templateFoot>
    <!-- 皮肤5底部 -->
    <templateFoot1 v-if="skinId>=5"></templateFoot1>
</template>

<script setup lang="ts">
//0.加载全局模板组件 start---------------------------------------->
//0.1全局通栏
import templateHead from '@/components/template/sector/head/1200x200/1.vue'
import templateHead5 from '@/components/template/sector/head/1200x250/1.vue'
import templateHead6 from '@/components/template/sector/head/1200x250/6.vue'

import templateMenu from '@/components/template/sector/menu/1200x130/1.vue'
import templateMenu5 from '@/components/template/sector/menu/1200x187/1.vue'
import templateFoot from '@/components/template/sector/foot/1200x580/1.vue'
import templateFoot1 from '@/components/template/sector/foot/1200x680/1.vue'
//0.2局部通栏
//0.2.1广告组件
import templateAd from '@/components/template/sector/body/ad/1200x90/1.vue'
//0.2.2 频道菜单
import templateChannelMenu from '@/components/template/sector/body/class/menu/1200x100/1.vue'
//0.2.3 滚动图文组合
import templateScrollList from '@/components/template/sector/body/class/banner/1200x410/1.vue'
//0.2.3 图文组合1
import templateNewSector1 from '@/components/template/sector/body/index/list/1200x470/1.vue'
//0.2.4 图文组合2
import templateNewSector2 from '@/components/template/sector/body/index/list/1200x470/2.vue'
//0.2.5 图文组合3
import templateNewSector3 from '@/components/template/sector/body/index/list/1200x980/1.vue'
//0.2.6 带标题的图文组合
import templateNewSector4 from '@/components/template/sector/body/index/list/1200x600/1.vue'
//0.2.7 不带标题的图文组合
import templateNewPhotoSector from '@/components/template/sector/body/index/list/1200x430/1.vue'
//0.2.8 带标题的文字新闻通栏
import templateNewTextSector from '@/components/template/sector/body/index/list/1200x500/1.vue'
//0.2.9 不带标题的图文组合
import templateNewListPhotoSector from '@/components/template/sector/body/index/list/1200x500/2.vue'
// 0.2.10 皮肤6 带标题图文组合
import templateChannelNewsPhotoSector from '@/components/template/sector/body/index/list/1200x430/6_2.vue'
// 0.2.11 皮肤6 不带标题图文组合2
// import templateChannelNewsSector from '@/components/template/sector/body/index/list/1200x600/6.vue'
// 0.2.12 皮肤6 带标题图文组合2
// import templateChannelNewsPhoto from '@/components/template/sector/body/index/list/1200x600/6.vue'
// 0.2.13 皮肤6 不带标题图文组合2
// import templateChannelNewsAndPhoto from '@/components/template/sector/body/index/list/1200x600/6.vue'
//0.加载全局模板组件 end---------------------------------------->

//1.获得基本信息单元 start---------------------------------------->
//1.1获得页面依赖
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
//1.2使用url查询导航池id
const targetSegment = getRoutePath(1);
const routeId = ref<number>(0);
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    routeId.value = getRouteId.data.category_id
}
//1.4获得pinia源
import { useTemplateBaseStore } from '@/stores/templateBase'
const templateBaseStore:any = useTemplateBaseStore()
//1.5获得该页的皮肤id - 在每个组件中也是同样的获得方法
const skinId = ref<number>(0)
const websiteId = ref<number>(0)
//1.6获得站点基本信息
const responseStatus = await requestDataPromise('/web/getWebsiteAllinfo', {
    method: 'GET',
    query: {
        'link_textnum':24,
        'link_imgnum':18,
        'link_footnum':4
    },
});
if (responseStatus.code == 200) {
    if(responseStatus.data.website_foot.foot_info.status == 1){
        //网站模板已停用，直接转入404页面
        navigateTo('/error?findPage=index')
    }else{
        //1.6.1设置站点基本信息
        templateBaseStore.setWebSiteInfo(responseStatus.data)
        websiteId.value = responseStatus.data.website_head.id;//获得网站id
        //1.6.2设置皮肤id
        skinId.value = templateBaseStore.webSiteInfo.website_foot.foot_info.template_id;
    }
}
//1.7.seo
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': routeId.value
    },
});
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;
useHead({
    title: seoTitle + "_" + seoName + "_" + seoSuffix,
    meta: [
        { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
        { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
    ]
});
//1.获得基本信息单元 end---------------------------------------->

//2.页面数据 start---------------------------------------->
//2.1预创建请求数据
const templateGetData:any = [
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0},
    {"parent":routeId.value+",0,0","child":"","imgnum":0,"textnum":0}
]
//2.2获得模板数据
const response:any = await requestDataPromise('/client/indexData', {
    method: 'POST',
    body: {
        'website_id':websiteId.value,
        'getpage':'class'
    },
});
const templateData = response.data.template.class;
//是否启用搜索功能
templateBaseStore.setIsSearch(response.data.isSearch)
console.log('templateData111111111',templateData)
//2.2.1循环一下模板数据，把所有需要请求后端内容的组件里面imgSize和textSize拿出来
const getIndex = ref<number>(0);
for(let item of templateData){
    if(item.sectorName == 'scrollTextSector'||item.sectorName == 'manyPictureSector'||item.sectorName == 'commentSector'||item.sectorName == 'listSector' || item.sectorName == 'channelNewsSector' || item.sectorName == 'newsPhotoSector' || item.sectorName == 'channelTextSector' || item.sectorName == 'newListPhotoSector' || item.sectorName == 'ChannelNewsPhotoSector'){
        for(let i of item.componentList){
            templateGetData[getIndex.value].imgnum = i.componentData.imgSize;
            templateGetData[getIndex.value].textnum = i.componentData.textSize;
            getIndex.value++;
        }
    }
}
//2.3 获得导航数据
const navigateData = await requestDataPromise('/web/getWebsiteModelCategory', {
    method: 'GET',
    query: {
        'placeid': 1,
        'pid': routeId.value,
        'num': 8
    },
});
const getNavIndex = ref<number>(0);
for(let item of navigateData.data){
    templateGetData[getNavIndex.value].child = item.category_id;
    getNavIndex.value++;
}
//获得完整的请求数据
const jsonString = JSON.stringify(getTransformArray(templateGetData));
//2.4获得页面完整的请求数据
const mkdata = await requestDataPromise('/web/getWebsiteAllArticle', {
    method: 'GET',
    query: {
        'id': jsonString
    },
});
if (mkdata.code == 200) {
    //获得所有子频道的内容
    let childData = ref<any>([]);
    for(let item of mkdata.data){
        childData.value.push(item.child)
    }
    let dataSort = ref<number>(0);
    const childDataLength = childData.value.length;
    //如果当前循环的这个组件数据长度已经超过了所有子频道的总数居量，则从0开始循环
    //此操作是为了防止B端创建的组件数超过了后台的导航数量
    for(let item of templateData){
        if(item.sectorName == 'scrollTextSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'manyPictureSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'commentSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'listSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[2].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'channelNewsSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'newsPhotoSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'channelTextSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'newListPhotoSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
        if(item.sectorName == 'ChannelNewsPhotoSector'){
            item.componentList[0].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
            item.componentList[1].componentData.data = childData.value[dataSort.value % childDataLength];
            dataSort.value++;
        }
    }
    //console.log(templateData)
}
//2.5广告数据
const adData:any = ref([]);
adData.value.push(response.data.ad.top)
for(let item of response.data.ad.class){
    adData.value.push(item)
}
templateBaseStore.setAdList(adData.value)
//2.页面数据 end---------------------------------------->
</script>

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