<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()

import { menuStatusStore, userInfoStore } from '@/stores';

import CpdmLeave from '../components/CpdmLeave.vue'
const leave = ref()
// import url from '../assets/home/trademark.png';
//选中的索引
const userInfo = userInfoStore()

const menuStatus = menuStatusStore()

const activeIndex = ref<string>()
activeIndex.value = menuStatus.activeIndex

const visible = ref<boolean>(false)

//选中菜单事件
const handleSelect = (key: string, keyPath: []) => {
    menuStatus.saveActiveIndex(key)
    activeIndex.value = key;
    if (key == '1') {
        router.push({ name: 'homeView' })
    } else if (key == '2') {
        router.push({ name: 'popularizationScience' })
    } else if (key == '3') {
        router.push({ name: 'community', })
    } else if (key == '4') {
        router.push({ name: 'plan', })
    } else if (key == '5') {
        router.push({ name: 'testRecord' })
    }
    // menuStatus.saveActiveIndex(key)
}
//点击登录按钮
const login = () => {
    menuStatus.saveActiveIndex('6')
    router.push({ name: 'login', params: {} })
}
const falgMenu = ref<boolean>(false)
//鼠标移入事件
const comeMenu = () => {
    falgMenu.value = true
}
//鼠标移出事件
const leaveMenu = () => {
    falgMenu.value = false
}

const updatePassFun = () => {
    menuStatus.saveActiveIndex('6')
    router.push({ name: 'updatePas' })
}

const exitFun = () => {
    //退出登录清除缓存
    visible.value = true
    // userInfo.clearUserInfo()
    //跳转到首页

}
const comT = () => {
    visible.value = false
    userInfo.clearUserInfo()
    //跳转到首页
    router.push({ name: 'homeView' })
}
//设置显示字段长度
const formatterName = (val: string) => {
    if (val == undefined) {
        return ''
    }
    if (val.length > 4) {
        return val.substring(0, 3) + '...'
    } else {
        return val
    }
}
//打开弹出框--显示弹出框
const leaveFun = () => {
    leave.value.open()
}
</script>

<template>

    <!-- :style="{ backgroundColor: activeIndex == '1' ? '#ffffff' : '' }" -->
    <div class="header_qp" :style="{ backgroundColor: activeIndex == '1' ? '#ffffff' : '' }">
        <div class="header_p">
            <div class="header_image">
                <!-- <img style="width: 239px; height: 55px" src="../assets/home/trademarkNew.png"></img> -->
                <!-- <img style="width: 239px; height: 55px" src="../assets/home/trademarkNew.png"></img> -->
                <div>郑大计智心理测评系统</div>
            </div>
            <el-menu :default-active="activeIndex" class="el-menu-demo" style="display: flex;" mode="horizontal"
                :ellipsis="false" @select="handleSelect">
                <el-menu-item index="1">首&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;页</el-menu-item>


                <!-- <el-badge :value="200" :max="99" class="item">
                <el-menu-item index="4">测试计划</el-menu-item>
            </el-badge> -->
                <el-menu-item index="4">
                    <div class="badge_custom" v-show="userInfo.planCurrentNum != 0">
                        {{ userInfo.planCurrentNum }}
                    </div>
                    <span>测试计划</span>
                </el-menu-item>
                <el-menu-item index="5">
                    <div class="badge_custom" v-show="userInfo.unreadNum != 0">
                        {{ userInfo.unreadNum }}
                    </div>
                    <span>测试记录</span>

                </el-menu-item>
                <el-menu-item index="2">更多测试</el-menu-item>
                <el-menu-item index="3">留言社区</el-menu-item>
            </el-menu>

            <div class="login_button_bg" @mouseenter="comeMenu" @mouseleave="leaveMenu"
                v-show="userInfo.userInfo.userName">
                <img width="20px" src="../assets/home/button_login.png" />
                &nbsp;&nbsp;&nbsp;&nbsp;
                <span :title="userInfo.userInfo.userName">{{ formatterName(userInfo.userInfo.userName) }}</span>
                <transition>
                    <div class="login_user" v-show="falgMenu">
                        <div class="update_pass" @click="updatePassFun">
                            修改密码
                        </div>
                        <div class="exit_login" @click="leaveFun">
                            我的请假
                        </div>
                        <div class="exit_login" @click="exitFun">
                            退出登录
                        </div>
                    </div>
                </transition>

            </div>
            <div class="login_button_bg" v-show="!userInfo.userInfo.userName" @click="login">
                <img width="20px" src="../assets/home/button_login.png" />
                &nbsp;&nbsp;&nbsp;&nbsp;

                <span v-if="!userInfo.userInfo?.userName">登录账号</span>

            </div>
            <el-dialog v-model="visible" :show-close="true" width="500" style="border-radius: 20px;" class="dig_kg">
                <template #header="{ close, titleId, titleClass }">
                    <div class="my-header" style="text-align: center;">
                        <div class="exit_login_title">退出登录</div>
                        <div class="exit_login_info">是否退出当前账号?</div>
                        <div class="start_button_out">
                            <div class="start_button_self" @click="comT">
                                确定
                            </div>
                        </div>

                    </div>
                </template>


            </el-dialog>
        </div>
    </div>
    <CpdmLeave ref="leave" />
</template>
<style scoped>
:deep(.dig_kg> .el-dialog__header) {
    padding-right: 0px !important;
}
</style>
<style lang="scss" scoped>
.el-menu {
    background-color: transparent !important
}

.exit_login_title {
    text-align: center;
    color: #222222;
    font-weight: 700;
    font-size: 20px;
    letter-spacing: 3px;
}

.exit_login_info {
    text-align: center;
    letter-spacing: 3px;
    font-size: 18px;
    margin-top: 20px;
}

.start_button_out {
    margin-top: 40px;
    // margin-bottom: 40px;
    display: flex;
    flex-direction: row;
    justify-content: center;

    .start_button_self {
        cursor: pointer;
        // width: 100px;
        border-radius: 12px;
        border: 3px solid #48D68E;
        color: #ffffff;
        background-color: #000000;
        // margin-right: 20px;
        padding: 5px 50px;
        cursor: pointer;
        display: flex;
        align-items: center;

    }
}

.badge_custom {
    position: absolute;
    background-color: #48D68E;
    left: 90px;
    top: 0px;

    line-height: 18px;
    color: 10px;
    padding: 0px 8px;
    font-weight: 300;
    font-size: 10px;
    color: #ffffff;
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;
}

//整个菜单底部样式
.el-menu--horizontal.el-menu {

    border-bottom: 0px solid var(--el-menu-border-color);
    display: flex;
    align-items: center;
}

.el-menu--horizontal>.el-menu-item {
    width: 120px;
    height: 40px;
    align-items: center;
    border-bottom: 0px solid transparent;
    color: var(--el-menu-text-color);
    display: inline-flex;
    // height: 100%;
    justify-content: center;
    align-items: center;
    border-radius: 12px;
    margin: 0;
}

.el-menu--horizontal .el-menu-item:not(.is-disabled):focus,
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover {
    background-color: #48D68E;
    color: #000000 !important;
    border-radius: 12px;
}

//激活某个菜单时   底部样式
.el-menu--horizontal>.el-menu-item.is-active {
    border-bottom: 0px solid var(--el-menu-active-color);
    // color: var(--el-menu-active-color) !important;
    color: #000000 !important;
    background: #48D68E;
    border-radius: 12px;
    height: 40px;
    font-weight: 700;

}

.header_qp {

    display: flex;
    justify-content: center;
    width: 100%;

    .header_p {
        // background-color: #ffffff;
        padding-top: 20px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        width: 1200px;


        .header_image {
            // flex: 1
            // margin-right: 100px;

            div {
                color: #48D68E;
                font-weight: 700;
                font-size: 26px;
                -webkit-transform: skew(-10deg);
                letter-spacing: 2px;
                /* for Chrome||Safari */
                -ms-transform: skew(-10deg);
                /* for IE */
                -moz-transform: skew(-10deg);
                /* for Firefox */
                -o-transform: skew(-10deg);
                /* for Opera */
            }
        }

        .login_button_bg {
            position: relative;
            background-color: #000000;
            border-radius: 12px;
            border: 3px solid #48D68E;
            color: #ffffff;
            padding: 8px 20px;
            cursor: pointer;
            display: flex;
            align-items: center;

            .login_user {
                border-bottom-left-radius: 20px;
                border-bottom-right-radius: 20px;
                width: 80%;
                position: absolute;
                background-color: #ffffff;
                // bottom: -105px;
                bottom: -145px;
                margin-left: calc(10% - 20px);
                z-index: 200;
                color: #000000;

                .update_pass {
                    text-align: center;
                    margin-top: 20px;
                    // padding: 5px;
                    color: #999999
                }

                .update_pass:hover {
                    text-align: center;
                    margin-top: 20px;
                    color: #48D68E // padding: 5px;
                }

                .exit_login {
                    text-align: center;
                    // padding: 5px;
                    margin-top: 20px;
                    margin-bottom: 20px;
                    color: #999999
                }

                .exit_login:hover {
                    text-align: center;
                    // padding: 5px;
                    margin-top: 20px;
                    margin-bottom: 20px;
                    color: #48D68E // padding: 5px;
                }
            }
        }

        .flex-grow {
            flex: 1;
        }

        .menu_gen {
            width: 80px;
            height: 20px;
            font-family: Alibaba PuHuiTi 2.0;
            font-weight: normal;
            font-size: 20px;
            color: #333333;
            line-height: 26px
        }
    }

}
</style>
