<template>
    <!-- 企业列表 company -->
    <main class="index_main" v-if="routeType == 6">
        <!-- 三农市场网-商城-资讯分类页 1-->
        <section class="index_1 clearfix">
            <div class="shop_head_1 clearfix">
                <div class="shop_head_1_name">{{ parent_name }}</div>
            </div>
            <div class="categ_color_box clearfix">
                <div class="categ_color_name clearfix">直达：</div>
                <div class="categ_color_in clearfix">
                    <NuxtLink class="categ_color_a" :href="`/${item.aLIas_pinyin}/list-1.html`" :title="item.title"
                        v-for="(item, index) in companyData" :key="index"
                        :class="{ categ_color_a_only: navCid == item.cid }">
                        {{ item.alias }}
                    </NuxtLink>
                </div>
            </div>
        </section>
        <!-- 三农市场网-商城-资讯分类页2 -->
        <!-- 三农市场网-商城-资讯列表 1-->
        <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>
                <div class="info_ul_1 clearfix">
                    <div class="info_li_1 clearfix" v-for="(item, index) in newsList" :key="index">
                        <NuxtLink class="info_li_1_a dot1" :href="`/${item.pinyin}/${item.id}.html`" title="">
                            {{ item.title }}
                        </NuxtLink>
                        <time class="info_li_1_time">{{ getTime(item.updated_at, 'month', 1) }}</time>
                    </div>
                    <div class="empty" v-if="newsList == false">
                        <img src="@/public/topic/empty.png" alt="" class="empty_img">
                        <span class="empty_text">当前暂无数据</span>
                    </div>
                </div>
                <!-- 分页器 -->
                <div class="pagination" v-if="newstotal">
                    <el-pagination background layout="prev, pager, next" :total="newstotal" :page-size="pageSize_news"
                        prev-text="上一页" next-text="下一页" @current-change="changePage" />
                </div>
            </div>
            <aside class="info_1_aside">
                <DetailHotNews></DetailHotNews>
                <DetailHotNews2></DetailHotNews2>
            </aside>
        </section>
    </main>
</template>

<script setup>
//0.加载页面依赖 start ---------------------------------------->
import { ref, onMounted } from 'vue';
import { ElMessage, ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus';
import { ArrowRight } from '@element-plus/icons-vue'
import { NuxtLink } from '#components';
//0.加载页面依赖 end ---------------------------------------->

//1.获得路由id start ---------------------------------------->
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 ---------------------------------------->

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


//4.1 获取所有导航
try {
    const navigateData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': routeId,
            'num': 8,
            'type': 1
        },
    });
    // console.log('获取三级导航1111111', navigateData);

    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 + "/" + targetSegment1 + "/" + navigateData.data[index].aLIas_pinyin,
                type: navigateData.data[index].type,
                // 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 {
                if (navigateData.data[index].type == 6) {
                    companyData.value.push(data);
                } 
                //每个页面最多8个模块
                pageData.value.push(data);
            }
        }
        // console.log('获取三级导航pageData', pageData.value);
        // console.log('获取三级导航company', companyData.value);

    } else {
        console.log("错误：渲染8个模块环节出错，请检查是否存在没有数据的模块！")
    }
} catch (error) {
    console.log("错误：导航池渲染执行接口出错！请检查频道页渲染的模块数据是否完整！")
}
// 获取企业数据列表 start--------------------------------------->
const newsList = ref()
const page_news = ref(1)
const pageSize_news = ref(20)
const newstotal = ref('')
const getNewsList = await requestDataPromise('/web/getWebsiteCompanyList', {
    method: 'GET',
    query: {
        'page': page_news.value,
        'pageSize': pageSize_news.value,
        'category_id': navCid.value,
    },
});
console.log("获取企业列表company", getNewsList.data);

if (getNewsList.code == 200) {
    newsList.value = getNewsList.data.data
    newstotal.value = getNewsList.data.count
}
// 获取企业数据 end----------------------------------------->

//4.页面数据 end ---------------------------------------->

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

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 url("@/assets/css/shop/newsList.less");
@import url("@/assets/css/shop/goodsList.less");

.empty {
    width: 100%;
    height: 300px;
    line-height: 300px;
    text-align: center;
}

.empty_img {
    vertical-align: -30px;
    margin-right: 20px;
}

.empty_text {
    font-size: 26px;
    color: #999;
}

.pagination {
    height: 100px;
    margin-top: 20px;
}

.pagination1 {
    height: 100px;
    margin-top: 20px;
}
</style>