<template>
    <header>
        <div class="headerContainer">
            <!-- 欢迎导航 -->
            <div class="headerBox">
                <div class="headerMain">
                    <div class="headerBox_left">欢迎来到全国环保法制调研中心!</div>
                    <div class="headerBox_right">
                        <NuxtLink to="#">收藏</NuxtLink>
                        <span>|</span>
                        <NuxtLink to="/">人员查询</NuxtLink>
                        <span>|</span>
                        <NuxtLink to="/">车辆查询</NuxtLink>
                        <span>|</span>
                        <NuxtLink to="/" @click="phone_close_fun">用户登录</NuxtLink>
                    </div>
                </div>
            </div>
            <!-- banner部分 -->
            <div class="logoBox">
                <!-- logo -->
                <div class="logoBox_left">
                    <NuxtLink to="/">
                        <img src="../../public/header/logo.png" alt="logo">
                    </NuxtLink>
                </div>
                <!-- 文字 -->
                <div class="logoBox_right">
                    <div class="logoBox_right_top">
                        <NuxtLink to="https://www.mee.gov.cn/" target="_blank">生态环境</NuxtLink>
                        <NuxtLink to="https://www.moj.gov.cn/" target="_blank">司法部</NuxtLink>
                        <NuxtLink to="https://www.ccdi.gov.cn/" target="_blank">中纪委</NuxtLink>
                        <NuxtLink to="https://www.gov.cn/" target="_blank">政府门户</NuxtLink>
                    </div>
                    <div class="logoBox_right_bottom">
                        <NuxtLink to="/">
                            <h3>
                                全国环保法制调研中心
                                <span>官网</span>
                            </h3>
                            <h4>
                                <span>全国政务信息一体化应用平台</span>
                                <span> hbfzdy.org.cn</span>
                            </h4>
                        </NuxtLink> 
                    </div>
                </div>
            </div>
        </div>
    </header>

</template>

<script setup>
// 引入全局状态-HomePanel_out
const panelVisible = useState('panelVisible')
//1.加载基本依赖 start ---------------------------------------->
import { ref, watch, onMounted } from 'vue'
import { ElDialog, ElMessage } from 'element-plus'
import { getToken, setToken, removeToken } from '@/store/useCookieStore'
import { setTicket, removeTicket } from '@/store/useticketStore'
//网站地址
const { $webUrl, $CwebUrl, $BwebUrl, $LoginWebUrl } = useNuxtApp()
//1.加载基本依赖 end ---------------------------------------->
//1.登录逻辑 start ---------------------------------------->
let ticket = ref('')
let token = ref('')
let route = useRoute()
//页面每次刷新先判断一下用户状态是否已经过期了
//如果没有过期再储存token
ticket.value = route.query.ticket
token.value = route.query.admintoken
if (ticket.value) {
    setTicket(ticket.value)
}
if (token.value) {
    setToken(token.value)
}
//搜索框
let keyword = useState('keyword', () => '')
//用户名
let username = useState('username', () => '用户中心')
//是否删除
let isDel = useState('isDel', () => 1)
//是否显示用户中心
let isShow = useState('isShow', () => false)
let token1 = useState("token1", () => '')
let showToken = useState("showToken", () => '')
token1.value = getToken()
//检测登录状态
let tokenStatus = ref('');
tokenStatus.value = getToken()
if (tokenStatus.value == undefined) {
    console.log("-------------------------------------未获取到用户token")
} else {
    console.log("--------------------------------已获取到用户token")
}
//点击登录按钮
let goLogin = () => {
    //开启线上登录模式 start---------------------------------------->
    isDel.value = 0
    token1.value = getToken()
    window.open($BwebUrl + "/#/login?backurl=" + $CwebUrl, '_blank');
    //开启线上登录模式 end---------------------------------------->
    //开启本地登录模式 start---------------------------------------->
    // window.location.href = $BwebUrl + "/auth/login.php?backurl=" + $CwebUrl;
    //"http://adminpre.bjzxtw.org.cn/auth/login.php?backurl=http://nwpre.bjzxtw.org.cn";
    //开启本地登录模式 end---------------------------------------->
}
let dialogTableVisible = ref(false)
let dialogTableVisible_phone = ref(false)
//点击注册按钮
let goRegister = () => {
    dialogTableVisible.value = true
}
//点击用户中心
let userCenter = () => {
    if (isShow.value) {
        isShow.value = false
        return
    } else {
        isShow.value = true
        return
    }
}
// 添加点击事件处理函数
const handleClickOutside = (event) => {
    // 获取用户信息菜单元素
    const userInfoMenu = document.querySelector('.userInfo11');
    // 获取用户按钮元素
    const userButton = document.querySelector('.reg img[alt="用户信息"]')?.parentElement;
    // 如果点击的不是菜单本身且不是用户按钮，则隐藏菜单
    if (userInfoMenu &&
        !userInfoMenu.contains(event.target) &&
        !userButton?.contains(event.target)) {
        isShow.value = false;
    }
}
//2.登录逻辑 end ---------------------------------------->

//3.跳转菜单逻辑 start ---------------------------------------->
let gotosystem = () => {
    let url = $BwebUrl + '/#/login?userurl=' + $CwebUrl;
    isShow.value = false;
    //window.location.href = url; 
    window.open(url, '_blank');
}
//点击退出按钮
let exit = () => {
    window.location.href = $CwebUrl;
    isDel.value = 1
    token1.value = getToken()
    isShow.value = false;
    let tokendata = getToken()
    let url = $LoginWebUrl + '/api/logout?backurl=' + $CwebUrl + '&admintoken=' + tokendata;
    window.location.href = url;
    removeToken()
    removeTicket()
}
//点击返回首页
let backHome = () => {
    window.location.href = $CwebUrl;
}
//点击搜索按钮
let goSearch = () => {
    const route = `/search/search?keyword=${keyword.value}`;
    window.location.href = route;
}
// 点击广告服务
let goAdvertising = () => {
    // window.open('/advertising?activeName=1', '_blank');//测试
    //本地启动广告服务
    //线上启动
    if (getToken()) {
        window.open($CwebUrl + '/advertising?activeName=1', '_blank');
    } else {
        goLogin()
    }
}
//点击商圈
let goTopic = () => {
    if (getToken()) {
        window.open($CwebUrl + '/topic', '_blank');
    } else {
        goLogin()
    }
}
//点击用户==手机版11
let user_fun = () => {
    if (getToken()) {
        //已经登录
        goLogin()
    } else {
        //没有登录
        dialogTableVisible_phone.value = true
        return
    }
}
// 添加关闭对话框处理函数
const phone_close_fun = () => {
    // 在这里可以添加关闭对话框前需要执行的逻辑
    dialogTableVisible_phone.value = false;
    // 确保对话框关闭
    goLogin()
}
// 监听token状态
watch(token1, (newval, oldval) => {
    showToken.value = newval
}, {
    deep: true,
    immediate: true,
})
// 获得网站logo
const logo = ref("")
const webSiteName = ref("")
let getLogo = async () => {
    const responseStatus = await requestDataPromise('/web/getWebsiteHead', {
        method: 'GET',
        query: {},
    });
    if (responseStatus.code == 200) {
        logo.value = responseStatus.data.logo;
        webSiteName.value = responseStatus.data.website_name;
    } else if (responseStatus.code == 0) {
        window.location.href = $CwebUrl + '/404';
    }
}
getLogo();
//3.跳转菜单逻辑 end ---------------------------------------->
//4.获取广告 start ---------------------------------------->
let adImg = ref({})
//4.获取广告 end ---------------------------------------->
//5.获取登录状态 start ---------------------------------------->
// 在组件卸载时移除事件监听
onUnmounted(() => {
    document.removeEventListener('click', handleClickOutside);
})
onMounted(async () => {
    //添加监听关闭个人中心菜单
    document.addEventListener('click', handleClickOutside);
    //从客户端获取登录状态
    //从客户端阶段开始 持续查询登录状态
    let getLoginStatus = async () => {
        //获得token
        const token = getToken();
        const { $webUrl, $CwebUrl } = useNuxtApp();
        if (token == undefined) {
            //如果没有获取到token 不访问后端获取在线状态
            console.log("没有获取到token！无需查询登录状态！")
            showToken.value = false;
        } else {
            //如果获取到了token 访问后端查询状态
            console.log("已获取到用户token,开始查询登录状态！")
            const queryParams = new URLSearchParams({
                token: getToken()
            });
            let url = `${$webUrl}/api/loginStatus?${queryParams.toString()}`
            const responseStatus = await fetch(url, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                    'Userurl': $CwebUrl,
                    //'token': getToken(),
                    'Origin': $CwebUrl
                }
            });
            const result = await responseStatus.json();
            console.log(result)
            if (result.code == 200) {
                console.log("用户已经登录！");
                showToken.value = true;
            }
            if (result.code == -1) {
                isShow.value = false;
                showToken.value = false;
                removeToken();
                removeTicket();
                ElMessage.error('您没有权限登录该网站！')
            }
            if (result.code == -2) {
                isShow.value = false;
                showToken.value = false;
                removeToken();
                removeTicket();
                ElMessage.error('您已退出登录！')
            }
        }
    }
    getLoginStatus();
    setInterval(getLoginStatus, 3000);
    //从客户端获取广告
    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=snzxw_top`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg.value = resultAd1.data[0];
    //从客户端获取用户名
    let userInfoUrl = `${$webUrl}/user/getUserInfo`
    const userInfoResponse = await fetch(userInfoUrl, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const userInfo = await userInfoResponse.json();
    if (userInfo.code == 200) {
        username.value = userInfo.data.nickname
    }
})
//5.获取登录状态 end ---------------------------------------->
//点击手机
// 添加全局状态
// const panelVisible = useState('panelVisible', () => false)
const togglePanel = () => {
  panelVisible.value = true
}
</script>

<style lang="less" scoped>
//自适应 start ---------------------------------------->
//大于1400px
@media screen and (min-width: 1401px) {
    .headerContainer {
        width: 100%;
    }
    .headerBox {
        width: 97.9%;
        height: 70PX !important;
        padding: 0 20PX;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #15792f;
        .headerMain {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100% !important;
            .headerBox_left {
                font-size: 24PX !important;
                color:#fff;
                overflow: hidden;
            }
            .headerBox_right {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                overflow: hidden;
                a {
                    font-size: 24PX !important;
                    color:#fff;
                }
                span {
                    font-size: 24PX !important;
                    color: #fff;
                    padding: 0 12PX;
                    
                }
            }
        }
    }
    .logoBox {
        width: 1401PX !important;
        height: auto !important;
        margin: 0 auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        // background: red;
        .logoBox_left {
            width: 210PX !important;
            height: auto;
            // background: blue;
            margin-top: 45PX;
            img {
                width: 100% !important;
            }
        }
        .logoBox_right {
            width: 77% !important;
            height: auto !important;
            // background:blue;
            .logoBox_right_top {
                width: 100%;
                height: 50PX;
                line-height: 50PX;
                overflow: hidden;
                text-align: right;
                margin-top: 20PX;
                a {
                    font-size: 26PX !important;
                    color: #000;
                    font-weight: bold;
                    margin-left: 20PX;
                }
            }
            .logoBox_right_bottom{
                width: 100%;
                height: auto;
                overflow: hidden;
                a{
                    color: #337ab7;
                    text-decoration: none;
                    background-color: transparent;
                    h3{
                        display: block;
                        width: 100%;
                        height: auto;
                        overflow: hidden;
                        line-height: 105PX;
                        font-size: 60PX;
                        font-weight: bold;
                        color: #ef0000;
                        span{
                            float: right;
                        }
                    }
                    h4{
                        display: block;
                        width: 100%;
                        height: 50PX;
                        line-height: 50PX;
                        overflow: hidden;
                        span{
                            float: left;
                            font-size: 30PX;
                            color: #000;
                            font-weight: normal;
                        }
                        span:nth-child(2){
                            float: right;
                        }
                    }
                }
            }
        }
    }
}
//大于800px小于等于1400px
@media screen and (min-width: 801px) and (max-width: 1400px) {
    .headerContainer {
        width: 100%;
    }
    .headerBox {
        width: 97.9%;
        height: 70PX !important;
        padding: 0 20PX;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #15792f;
        .headerMain {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100% !important;
            .headerBox_left {
                font-size: 24PX !important;
                color:#fff;
                overflow: hidden;
            }
            .headerBox_right {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                overflow: hidden;
                a {
                    font-size: 24PX !important;
                    color:#fff;
                }
                span {
                    font-size: 24PX !important;
                    color: #fff;
                    padding: 0 12PX;
                    
                }
            }
        }
    }
    .logoBox {
        min-width: 801PX !important;
        max-width: 1400PX !important;
        height: auto !important;
        margin: 0 auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        // background: red;
        .logoBox_left {
            width: 210PX !important;
            height: auto;
            // background: blue;
            margin-top: 45PX;
            img {
                width: 100% !important;
            }
        }
        .logoBox_right {
            width: 77% !important;
            height: auto !important;
            // background:blue;
            .logoBox_right_top {
                width: 96%;
                height: 50PX;
                line-height: 50PX;
                overflow: hidden;
                text-align: right;
                margin-top: 20PX;
                a {
                    font-size: 16px !important;
                    color: #000;
                    font-weight: bold;
                    margin-left: 20PX;
                }
            }
            .logoBox_right_bottom{
                width: 96%;
                height: auto;
                overflow: hidden;
                a{
                    color: #337ab7;
                    text-decoration: none;
                    background-color: transparent;
                    h3{
                        display: block;
                        width: 100%;
                        height: auto;
                        overflow: hidden;
                        line-height: 105PX;
                        font-size: 26px;
                        font-weight: bold;
                        color: #ef0000;
                        span{
                            float: right;
                        }
                    }
                    h4{
                        display: block;
                        width: 100%;
                        height: 50PX;
                        line-height: 50PX;
                        overflow: hidden;
                        span{
                            float: left;
                            font-size: 12px;
                            color: #000;
                            font-weight: normal;
                        }
                        span:nth-child(2){
                            float: right;
                        }
                    }
                }
            }
        }
    }
}
//小于等于800px
@media screen and (max-width: 800px) {
    .headerContainer {
        width: 100%;
        // background: RED;
    }
    .headerBox {
        width: 100%;
        height: 40px;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: rgba(255, 255, 255, .6);
        display: none;
        .headerMain {
            display: flex;
            align-items: center;
            justify-content: space-between;
            width: 100%;
            .headerBox_left {
                font-size: 18px;
                color:#0f50d5;
            }
            .headerBox_right {
                display: flex;
                align-items: center;
                justify-content: flex-end;
                a {
                    font-size: 18px;
                    color:#0f50d5;
                    margin-left: 6px;
                }
                span {
                    margin-left: 6px;
                }
            }
        }
    }

    .logoBox {
        max-width: 800PX !important;
        height: 140PX !important;
        margin: 0 auto;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20PX 10PX 0;
        
        .logoBox_left {
            width: 20% !important;
            // margin-left: 15PX;
            // float: left;
            // margin-top: 0;
            img {
                width: 100% !important;
                height: 100%!important;
                margin-top: 20PX !important;
            }
        }
        .logoBox_right {
            width: 78% !important;
            height: auto !important;
            // background:blue;
            .logoBox_right_top {
                display: none;
                width: 100%;
                height: 50PX;
                line-height: 50PX;
                overflow: hidden;
                text-align: right;
                margin-top: 20PX;
                // background: red;
                a {
                    font-size: 26PX !important;
                    color: #000;
                    font-weight: bold;
                    margin-left: 20PX;
                }
            }
            .logoBox_right_bottom{
                width: 100%;
                // height: auto;
                overflow: hidden;
                // background: red;
                margin-top: 40PX;
                height: 56PX;
                a{
                    // color: #337ab7;
                    text-decoration: none;
                    background-color: transparent;
                    // line-height: 56PX;
                    h3{
                        display: block;
                        width: 100%;
                        height: auto;
                        overflow: hidden;
                        // line-height: 50PX;
                        font-size: 38px;
                        font-weight: bold;
                        color: #569b08;
                        -webkit-text-stroke: 1PX #FFF;
                        // text-align: right;
                        text-align: center;
                        margin-left: 14PX;
                        margin-bottom: 3PX;
                        // background: green;
                        span{
                            display: block;
                            margin-right: 30PX;
                            float: right;
                            // text-align: center;
                            // background: blue;
                            // padding: 0 10PX;
                        }
                    }
                    h4{
                        display: block;
                        width: 100%;
                        height: auto;
                        // line-height: 50PX;
                        overflow: hidden;
                        span{
                            float: left;
                            font-size: 24px;
                            color: #569b08;
                            font-weight: normal;
                        }
                        span:nth-child(2){
                            float: right;
                        }
                    }
                }
            }
        }
    }
}
//自适应 end ---------------------------------------->
</style>

