<template>
<!-- 商城首页 -->
<nav class="shop_nav clearfix phone_none">
    <div class="shop_nav_head">
        <NuxtLink class="shop_nav_head_a" to="/xiangcunshangcheng/index.html" title="商城首页"></NuxtLink>
    </div>
    <div class="shop_nav_in clearfix">
        <NuxtLink 
            :class="['shop_nav_in_a', { 'shop_nav_in_a_active': item.category_id == navId }]"
            :to="`/xiangcunshangcheng/${item.aLIas_pinyin}/index.html`"
            v-for="item in shopNav"
            :title="item.alias"
        >
            {{item.alias}}
        </NuxtLink>
    </div>
</nav>
<!-- 商城首页 -->
 
<div class="phone_nav pc_none">
                    <NuxtLink 
                        class="phone_nav_a phone_nav_a_head"
                        to="/xiangcunshangcheng/index.html" >
                        商城首页  
                    </NuxtLink>
                    <div class="phone_nav_in">
                      

                        <NuxtLink 
                            :class="['phone_nav_a', { 'phone_nav_a_active': item.category_id == navId }]"
                            :to="`/xiangcunshangcheng/${item.aLIas_pinyin}/index.html`"
                            v-for="item in shopNav"
                             
                        >
                            {{item.alias}}
                        </NuxtLink>
                    </div>
</div>



<!-- 地区选择 -->
<div class="shop_name_out">
    <div class="shop_name_box clearfix">
        <div class="shop_name">地区选择</div>
        <div class="shop_name_right">
            <div class="shop_name_in clearfix" ref="shop_name_parent">
                <div class=" clearfix" v-show="shop_name_num == 1">
                    <span 
                        @click="setUrlParam(item)"
                        :title="item.name" 
                        v-for="item in provinceList1"
                        :class="['shop_name_a', { 'shop_name_a_only': item.id == cityId }]"
                    >
                        {{ item.abbreviation }}
                    </span>
                </div>
                <div class=" clearfix" v-show="shop_name_num == 2">
                    <span 
                        :class="['shop_name_a', { 'shop_name_a_only': item.id == cityId }]"
                        @click="setUrlParam(item)"
                        :title="item.name" 
                        v-for="item in provinceList2"
                    >
                        {{ item.abbreviation }}
                    </span>
                </div>
                <div class=" clearfix" v-show="shop_name_num == 3">
                    <span 
                        :class="['shop_name_a', { 'shop_name_a_only': item.id == cityId }]"
                        @click="setUrlParam(item)"
                        :title="item.name" 
                        v-for="item in provinceList3"
                    >
                        {{ item.abbreviation }}
                    </span>
                </div>
            </div>
            <div class="shop_name_btn hand"
                @click="shop_name_num >= shop_name_leng ? shop_name_num = 1 : shop_name_num++">
            </div>
        </div>
    </div>
</div>
<!-- 地区选择 -->





</template>

<script setup>
import { ref, onMounted ,watch } from 'vue'

//1.获得商城导航 start ---------------------------------------->
//1.1 获得商城导航
const shopNav = ref("")
let getShowNav = async () => {
    const responseStatus = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            placeid:0,
            pid:346,
            num:8
        },
    });
    shopNav.value = responseStatus.data
}
getShowNav();

//1.2 标记当前选择的地区
const route = useRoute();
const router = useRouter();
//获得详情id
const cityId = ref(route.query.cityid)

//1.3 获得频道导航的id，并且标记导航
//获得当前的完整路径
const fullPath = route.path;
//拆分，取出来中间这一段，然后提取数字部分
const segments = fullPath.split('/');
const targetSegment = segments[2];

const navId = ref(0)//当前导航标记哪个
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    navId.value = parseInt(getRouteId.data.category_id)
} else {
    console.log("错误位置：通过url路径查询导航池id")
}

//1.4设置url中的参数
const setUrlParam = (item) => {

    //获得当前的url
    const currentUrl = window.location.href;
    //判断url中是否含有select参数，如果有就删掉
    cityId.value = item.id;
    // 复制当前的 query 对象
    const newQuery = { ...route.query };
    // 更新 cityid
    newQuery.cityid = item.id;
    // 检查是否有 select 参数，如果有则删除
    if ('select' in newQuery) {
        delete newQuery.select;
    }
    // 使用 router.replace 更新 URL
    router.replace({
        path: route.path,
        query: newQuery
    });
}

//1.5 监听路由变化
watch(() => route.query.cityid, (newVal, oldVal) => {
    if(newVal == undefined){
        cityId.value = ''
    }
})


//1.获得商城导航 end ---------------------------------------->

//2.获得省列表 start ---------------------------------------->
const provinceList1 = ref([]);//省列表1
const provinceList2 = ref([]);//省列表2
const provinceList3 = ref([]);//省列表3
onMounted(async ()  => {
    //获得所有的省
    try {
        const { $webUrl, $CwebUrl } = useNuxtApp();
        const response = await fetch($webUrl + '/web/selectWebsiteArea', {
            headers: {
                'Content-Type': 'application/json',
                'Userurl': $CwebUrl,
                'Origin': $CwebUrl
            }
        });
        const result = await response.json();
        for(let index in result.data){
            if(index < 15){
                provinceList1.value.push(result.data[index])
            }else if(index > 14 && index < 30){
                provinceList2.value.push(result.data[index])
            }else if(index > 29){
                provinceList3.value.push(result.data[index])
            }
        }
    } catch (error) {
        console.error('获取行政区划数据失败:', error);
    }

   
})
//2.获得商城导航 start ---------------------------------------->

//3.地区滚动 start ---------------------------------------->
// 地区选择
const shop_name_num = ref(1)
const shop_name_parent = ref(null)
const shop_name_leng = ref(1)
onMounted(() => {
    const shop_name_son = shop_name_parent.value.querySelectorAll("div")
    shop_name_leng.value = shop_name_son.length
}) 
//3.地区滚动 end ---------------------------------------->
</script>

<style lang="less" scoped>
.shop_name_out {
    margin-top: 20px;
}
.shop_name_box {
    border-top: solid 1px #fff;

    .shop_name_box_a {
        height: 44px;
        line-height: 44px;
        color: #fff;
        font-weight: bold;
        font-size: 20px;
    }

    .shop_name {
        float: left;
        height: 44px;
        line-height: 44px;
        color: #fff;
        font-weight: bold;
        position: relative;
        z-index: 11;
        text-align: center;
        font-size: 20px;
        background: #A01C0E;
        padding: 0px 14px;
    }

    .shop_name_right {
        height: 40px;
        border-bottom: 1px solid #E8E9EC;
        margin-top: 4px;
        background: #F8F8F8;
        position: relative;
        z-index: 2;
    }

    .shop_name_right::after {
        content: '';
        display: block;
        position: absolute;
        top: 0px;
        right: 0px;
        height: 100%;
        width: 16px;
        background: url(@/public/img/9.png) no-repeat 0px bottom #fff;
        background-size: 100% auto;
    }

    .shop_name_btn {
        width: 24px;
        height: 24px;
        float: right;
        margin: 10px 22px 0px 0px;
        background: url(@/public/img/8.png) no-repeat center center;
        border-radius: 50%;
        background-size: 100% 100%;
    }
}
.shop_name_in {
    float: left;
    height: 40px;
    width: 1046px;
    float: left;
    box-sizing: border-box;

    .shop_name_a {
        float: left;
        height: 27px;
        line-height: 27px;
        color: #222;
        font-size: 20px;
        position: relative;
        margin: 7px 14.5px 0;
        padding: 0px 10px;
        cursor: pointer;
    }

    .shop_name_a::after {
        content: '/';
        display: block;
        position: absolute;
        top: 0px;
        right: -18px;
        height: 100%;
        line-height: 27px;
        color: #E9E9E9;
        font-size: 20px;
    }

    .shop_name_a:nth-last-of-type(1)::after {
        content: '';
        display: none;
    }

    .shop_name_a_only {
        background: #A01C0E;
        color: #fff;
    }
}

.shop_name_out {
    margin-top: 20px;
}

.shop_name_box {
    border-top: solid 1px #fff;

    .shop_name_box_a {
        height: 44px;
        line-height: 44px;
        color: #fff;
        font-weight: bold;
        font-size: 20px;
    }

    .shop_name {
        float: left;
        height: 44px;
        line-height: 44px;
        color: #fff;
        font-weight: bold;
        position: relative;
        z-index: 11;
        text-align: center;
        font-size: 20px;
        background: #A01C0E;
        padding: 0px 14px;
    }

    .shop_name_right {
        height: 40px;
        border-bottom: 1px solid #E8E9EC;
        margin-top: 4px;
        background: #F8F8F8;
        position: relative;
        z-index: 2;
    }

    .shop_name_right::after {
        content: '';
        display: block;
        position: absolute;
        top: 0px;
        right: 0px;
        height: 100%;
        width: 16px;
        background: url(@/public/img/9.png) no-repeat 0px bottom #fff;
        background-size: 100% auto;
    }

    .shop_name_btn {
        width: 24px;
        height: 24px;
        float: right;
        margin: 10px 22px 0px 0px;
        background: url(@/public/img/8.png) no-repeat center center;
        border-radius: 50%;
        background-size: 100% 100%;
    }
}
.shop_nav {
    border: solid 1px #E9E9E9;
    background: #fafafa;
    margin-top: 20px;

    .shop_nav_head {
        float: left;
        height: 28px;
        width: 138px;
        margin: 30px 52px 0px 25px;
    }

    .shop_nav_head_a {
        display: block;
        height: 28px;
        width: 138px;
        background: url(@/public/img/7.png) no-repeat left top;
        background-size: 100% 100%;
    }
}
.clearfix {
    overflow: hidden;
}
.shop_nav_in_a {
    float: left;
    height: 75px;
    line-height: 75px;
    color: #333;
    font-size: 16px;
    background: #f8f5f5;
    width: 118px;
    border-bottom: solid 5px #A01C0E;
    font-weight: bold;
    margin: 5px 0;
    text-align: center;
}

.shop_nav_in_a:nth-of-type(4) {
    margin-right: 8px;
}

.shop_nav_in_a:hover {
    color: #A01C0E;
}

.shop_nav_in_a_active {
    color: #A01C0E;
}

</style>

<style lang="less" scoped>

 
    @media screen and (min-width:801px){/*pc*/
         
		
            .pc_none{display:none;}
    }
    @media screen and (max-width:800px){/*ipad_phone*/

            .index_main{width:100%;margin:0px auto;}

            .phone_nav{ 

                width:100%; box-sizing:border-box;background:#fafafa; 
                margin:10px auto;height:33px;padding:0px 4%;

                .phone_nav_in{  
                    overflow-x:auto;overflow-y:hidden;word-break: keep-all; white-space: nowrap;
                        height:33px;line-height:33px;
                }

                .phone_nav_in::-webkit-scrollbar{height:0px;}

                .phone_nav_a_head{float:left;}
                .phone_nav_a{display:inline-block;line-height:33px;height:33px;
                    color:#333;font-size:14px;margin:0px 10px;
                }
                .phone_nav_a:nth-of-type(1){margin-left:0px;}
 



            }

            .router-link-exact-active.phone_nav_a{
                color:#A01C0E;
            }
            .shop_name_out{display:none;}
            .phone_none{display:none;}
    }


</style>


