<template>
    <div>
        <!-- 顶部导航 -->
        <nav class="headerNav">
            <div class="inner">
                <div class="left">
                    <span>中农兴业网团</span>
                    <span>旗下网站：
                        <span>农业百强网站（政府类30强）</span>
                    </span>
                    <span class="ad">商圈</span>
                </div>
                <div class="right">
                    <div class="menu">
                        <button class="reg" @click="goTopic">
                            <img src="../../public/head/topicon1.png" alt="商圈">
                            商圈
                        </button>
                        <button class="reg" @click="goAdvertising">
                            <img src="../../public/head/topicon2.png" alt="广告服务">
                            广告服务
                        </button>
                    </div>
                    <div class="right-top-menu">
                        <button class="reg" @click="goLogin" v-show="!showToken">
                            <img src="../../public/head/topicon3.png" alt="登录">
                            登录
                        </button>
                        <button class="reg" @click="goRegister" v-show="!showToken">
                            <img src="../../public/head/topicon4.png" alt="注册">
                            注册
                        </button>
                        <button class="reg" @click="userCenter" v-show="showToken">
                            <img src="../../public/head/topicon3.png" alt="用户信息">
                            {{ username }}
                        </button>
                        <button class="reg" @click="goSearch">
                            <img src="../../public/head/searchicon.png" alt="搜索">
                            搜索
                        </button>
                    </div>
                    <ul class="userInfo11" v-if="isShow">
                        <li @click="gotosystem">个人中心</li>
                        <li @click="exit">退出</li>
                    </ul>

                </div>
            </div>
        </nav>
        <el-dialog v-model="dialogTableVisible" title="" width="800">
            <div class="tips">
                <p>
                    <img src="../../public/topic/tips.png" alt="">
                    提示:注册请联系管理员操作!
                </p>
                <p>
                    联系电话:010-56019387
                </p>
                <p>
                    QQ ： 2909421493 、213552413
                </p>
            </div>
        </el-dialog>
    </div>

</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import { ElPopover, ElDialog } from 'element-plus'
import { getToken, setToken, removeToken } from '@/store/useCookieStore'
import { setTicket, removeTicket } from '@/store/useticketStore'
// import { navigateTo } from '@nuxtjs/composables';

const nuxtApp = useNuxtApp();
console.log("===:", nuxtApp) // 输出: someValue
const axios = nuxtApp.$axios;

let ticket = ref('')
let token = ref('')
let route = useRoute()
ticket.value = route.query.ticket
token.value = route.query.admintoken
if (ticket.value) {
    setTicket(ticket.value)
}
if (token.value) {
    setToken(token.value)
}


console.log('查询参数：', ticket.value);
console.log('查询参数：', token.value);

definePageMeta({
    middleware: 'auth'
});

let keyword = useState('keyword', () => '')
let username = useState('username', () => '')
let isDel = useState('isDel', () => 1)
let isShow = useState('isShow', () => false)

//点击登录按钮
let goLogin = () => {
    isDel.value = 0
    token1.value = getToken()
    window.location.href = ("http://admindev.bjzxtw.org.cn/auth/login.php?backurl=http://nwdev.bjzxtw.org.cn")
    token1.value = getToken()
}
let dialogTableVisible = ref(false)

//点击注册按钮
let goRegister = () => {
    dialogTableVisible.value = true
}
let userCenter = () => {
    if(isShow.value){
        isShow.value = false
        return
    }else{
        isShow.value = true
        return
    }
}

let gotosystem = () => {
    window.location.href = ("http://admindev.bjzxtw.org.cn/#/login?userurl=http://nwdev.bjzxtw.org.cn")
}

//点击退出按钮
let exit = () => {
    window.location.href = ('http://nwdev.bjzxtw.org.cn/')

    isDel.value = 1
    token1.value = getToken()

    // axios.get(`http://admindev.bjzxtw.org.cn/auth/logoutapi.php?token=${token}`).then(response => {
    //     console.log("退出登录", response);
    //     isDel.value = response.data.isDel
    // })
    window.location.href = (`http://admindev.bjzxtw.org.cn/auth/logout.php?backurl=http://nwdev.bjzxtw.org.cn&admintoken=${token1}`)
    removeToken()
    removeTicket()
}

//点击搜索按钮
let goSearch = () => {
    const route = `/search/search?keyword=${keyword.value}`;
    window.open(route, '_blank');
}

// 点击广告服务
let goAdvertising = () => {
    // window.location.href = ('http://localhost:3000/advertising?activeName=${1}')
    if (getToken()) {
        window.open(`http://nwdev.bjzxtw.org.cn/advertising?activeName=${1}`, '_blank');
    } else {
        goLogin()
    }
}

//点击商圈
let goTopic = () => {
    // window.location.href = ('http://localhost:3000/topic')
    if (getToken()) {
        window.open('http://nwdev.bjzxtw.org.cn/topic', '_blank');
    } else {
        goLogin()
    }
}


//获取用户信息
let getUserInfo = async () => {
    try {
        const response = await axios.get("/user/getUserInfo");
        console.log('获取用户信息', response);
        username.value = response.data.nickname

    } catch (error) {
        console.error(error);
    }
}

let token1 = useState("token1", () => '')
let showToken = useState("showToken", () => '')
token1.value = getToken()

watch(token1, (newval, oldval) => {
    console.log(newval, oldval);
    showToken.value = newval
}, {
    deep: true,
    immediate: true,
})

onMounted(() => {

    //获取用户信息
    getUserInfo()

    //获取登录状态
    getLoginStatus()
})

//检测登录状态
//http://admindev.bjzxtw.org.cn/auth/loginstatus.php?token=XXXXXXXXXXXX
let tokenStatus = ref('');
tokenStatus.value = getToken()
console.log(tokenStatus.value);
//获取用户信息
let getLoginStatus = async () => {
    try {
        const response = await axios.get(`http://nwdev.bjzxtw.org.cn/auth/loginstatus.php?token=${tokenStatus}`);
        console.log('登录状态', response);
        if (response.data.code == 200) {
            if(response.data.data.islogin == true){
                showToken.value = true 
            }else{
                showToken.value = false
                exit()
            }
        } else if (response.data.code == 0) {
            //退出登录
            exit();
        }
    } catch (error) {
        console.error(error);
    }
}


</script>

<style lang="less" scoped>
// 广告页面头部
.headerNav {
    width: 100%;
    height: 56px;
    line-height: 56px;
    border-bottom: 1px solid #E5E5E5;
    // background-color: red;
}

.headerNav .left>span {
    margin-right: 47px;
    color: #666666;
}

.headerNav .left>.ad {
    border-left: 1px solid #E5E5E5;
    padding-left: 45px;
    color: #333333;
    font-weight: 600px;
}

.headerNav .right {
    display: flex;
    margin-right: 10px;
    position: relative;

    button {
        font-size: 12px;
        color: #666666;
        border: none;
        background: none;
        cursor: pointer;
    }

    .login {
        width: 36px;
        height: 19px;
        color: #fff;
        border-radius: 4px;
        background-color: #028E21;
        margin-right: 15px;
        cursor: pointer;
    }

    .menu,
    .right-top-menu {
        display: flex;
    }

    .reg {
        margin-right: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 50px;

        img {
            margin-right: 5px;
        }
    }
}

.headerNav .right {
    float: right;
    font-size: 12px;
    // margin-top: 10px;

    span {
        margin-left: 26px;
    }

    .home,
    .collection {
        display: inline-block;
        width: 16px;
        height: 16px;
        vertical-align: -3px;
    }

    .exit {
        line-height: 30px;
    }

}

.userInfo11 {
    width: 100px;
    height: 80px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    border-radius: 3px;
    position: absolute;
    top: 50px;
    right: 0;
    font-size: 14px;
    z-index: 999;

    li {
        height: 40px;
        line-height: 40px;
        color: #333333;
        padding-left: 10px;
    }

    >li:hover {
        color: #028E21;
    }
}

//提示信息
.tips {
    padding: 20px 0;
    text-align: center;
    font-size: 20px;
    color: #40663B;
    font-weight: bold;

    img {
        width: 78px;
        height: 78px;
        vertical-align: middle;
        margin-right: 20px;

        p {
            line-height: 30px;
        }
    }

    p {
        line-height: 40px;
    }

    >p:first-child {
        margin-bottom: 30px;
    }

}
</style>