<template>
    <!-- 页面头部 -->
    <header>
        <!-- 顶部导航 -->
        <nav class="headerNav">
            <div class="inner">
                <div class="left">
                    <span>中农兴业网团</span>
                    <span>旗下网站：
                        <span>农业百强网站（政府类30强）</span>
                    </span>
                </div>
                <div class="right">
                    <div class="menu">
                        <button class="reg" @click="goSearch">
                            <img src="../../static/head/searchicon.png" alt="搜索">
                            搜索
                        </button>
                        <button class="reg" @click="goTopic">
                            <img src="../../static/head/topicon1.png" alt="商圈">
                            商圈
                        </button>
                        <button class="reg" @click="goAdvertising">
                            <img src="../../static/head/topicon2.png" alt="广告服务">
                            广告服务
                        </button>
                    </div>
                    <div class="right-top-menu">
                        <button class="reg" @click="goLogin" v-show="!showToken">
                            <img src="../../static/head/topicon3.png" alt="登录">
                            登录
                        </button>
                        <button class="reg" @click="goRegister" v-show="!showToken">
                            <img src="../../static/head/topicon4.png" alt="注册">
                            注册
                        </button>
                        <el-popover placement="top-start" :width="100" trigger="hover" algin="center">
                            <template #reference>
                                <span v-show="showToken">
                                    <em class="iconfont icon-gerenzhongxin"></em>
                                    {{ username }}
                                </span>
                            </template>
                            <p class="exit" @click="gotosystem">个人中心</p>
                            <p class="exit" @click="exit">退出</p>
                        </el-popover>
                    </div>
                </div>

            </div>
        </nav>
        <!-- 头部logo -->
        <div class="headerLogo">
            <div class="inner">
                <img src="../../static/image/logo1.png" alt="三农资讯网" class="left" @click="backHome">
                <div class="titlead">
                    <a href="http://www.baidu.com">
                        <img src="../../static/index/titlead.png" alt="">
                    </a>                 
                </div>
            </div>
           
        </div>
        <el-dialog v-model="dialogTableVisible" title="" width="800">
            <div class="tips">
                <p>
                    <img src="../../static/topic/tips.png" alt="">
                    提示:注册请联系管理员操作!
                </p>
                <p>
                    联系电话:010-56019387
                </p>
                <p>
                    QQ ： 2909421493 、213552413
                </p>
            </div>
        </el-dialog>
    </header>
</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 { fa } from 'element-plus/es/locale/index.mjs';
// 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 goLogin = () => {
    isDel.value = 0
    token1.value = getToken()
    // window.location.href = ("http://admindev.bjzxtw.org.cn/#/login?backurl=http%3a%2f%2fnwdev.bjzxtw.org.cn%2f")
    window.location.href = ("http://admindev.bjzxtw.org.cn/auth/login.php?backurl=http://nwdev.bjzxtw.org.cn")
    token1.value = getToken()
    // navigateTo.back()
}
let dialogTableVisible = ref(false)

//点击注册按钮
let goRegister = () => {
    dialogTableVisible.value = true
}

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

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

    isDel.value = 1
    token1.value = getToken()
    // const currentUrl = window.location.href;
    // console.log('currentUrl', currentUrl);

    // // 查找参数部分起始位置（?符号位置）
    // const index = currentUrl.indexOf('?');
    // if (index !== -1) {
    //     // 截取去除参数后的URL
    //     var newUrl = currentUrl.slice(0, index);
    //     // 重新加载页面（赋值给location.href会触发页面刷新）
    //     window.location.href = newUrl;
    // }
    // 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 backHome = () => {
    // window.location.href = ('http://localhost:3000/')
    window.location.href = ('http://nwdev.bjzxtw.org.cn/')
}

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

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

//点击商圈
let goTopic = () => {
    // window.open(`http://localhost:3000/topic`, '_blank');
    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){
            
        }else if(response.data.code == 0){
            //退出登录
            exit();
        }
    } catch (error) {
        console.error(error);
    }
}

</script>

<style lang="less" scoped>
.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;
    }

}

/* 页面头部 */
header {
    width: 100%;
    height: 201px;
    font-size: 12px;
    font-family: PingFang SC-Regular;
    color: #666666;
    background: url("../../static/head/headtopbg.png") repeat-x;
}

.headerNav {
    width: 100%;
    height: 33px;
    line-height: 33px;
}

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

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

    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: 33px;

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

.headerNav .right {
    float: right;
    font-size: 12px;

    span {
        margin-left: 26px;
    }

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

    .home {
        background-image: url("../../static/image/Iconly／Light／Home.png");
    }

    .collection {
        background-image: url("../../static/image/Iconly／Light／Star.png");
    }

    .exit {
        line-height: 30px;
    }

    // .exit:hover{
    //     color: #028E21;
    // }
}

/* // 头部logo */
.headerLogo {
    height: 185px;
    position: relative;
    background: url("../../static/head/topBg.png") no-repeat center;
    .titlead {
        position: absolute;
        bottom: 54px;
        right: 134px;
    }
    .inner {
        position: relative;
        height: 185px;
    }
    .inner>img {
        width: 342px;
        height: 72px;
        margin-top: 51px;
        cursor: pointer;
    }

    /* // 搜索框部分 */
    .search {
        float: left;
        width: 440px;
        height: 40px;
        background-color: #fff;
        line-height: 40px;
        margin-left: 39px;
        margin: 71px 39px 48px;
        position: relative;

        em {
            display: inline-block;
            width: 30px;
            height: 30px;
            background: url("../../static/image/Iconly／Broken／Search.png") no-repeat;
            position: absolute;
            top: 5px;
            left: 15px;
        }

        input {
            float: left;
            width: 351px;
            height: 40px;
            border: none;
            outline: none;
            padding-left: 65px;
            box-sizing: border-box;
            border: 1px solid #028E21;
            border-right: none;
            border-radius: 4px 0px 0px 4px;
        }

        button {
            float: right;
            width: 89px;
            height: 40px;
            background-color: #028E21;
            border-radius: 0px 4px 4px 0px;
            border: none;
            font-size: 14px;
            color: #fff;
            font-family: PingFang SC, PingFang SC;
            font-weight: 500;
        }

    }

    /* // 右侧小图标 */
    .serve {
        float: right;
        height: 60px;
        margin-top: 60px;
        margin-right: 60px;

        >li {
            float: left;
            height: 64px;
            margin-left: 48px;
        }

        >li>a {
            display: inline-block;
            height: 64px;
        }

        >li img {
            width: 29px;
            height: 29px;
            padding: 8px 14px;
        }

        >li p {
            height: 28px;
            text-align: center;
            color: #666666;
        }
    }
}
</style>