<template>
    <div class='page-header'>
        <div id='page-header-logo' class='page-logo'>
            <a @click="toIndex">
                <img src='@/assets/logo.png' alt='logo' class='logo-default'>
            </a>
            <div class='menu-toggler sidebar-toggler' @click.stop='toggleSidebar'>
                <span>
                    <i class='el-icon-fa-bars'></i>
                </span>
            </div>
        </div>
        <div id='page-header-data-menu' class='data-menu'>
            <el-select v-model='selectedProjectId' placeholder='请选择'>
                <el-option v-for='item in projects' :key='item.id' :label='item.name' :value='item.id'></el-option>
            </el-select>
        </div>
        <div id='page-header-user-menu' class='user-menu'>
            <el-dropdown trigger='hover' class='user-menu-dropdown' @command='userMenuCommand'>
                <span class='el-dropdown-link'>
                    {{userInfo.userName}}
                    <i class='el-icon-fa-user el-icon--right'></i>
                </span>
                <el-dropdown-menu slot='dropdown'>
                    <el-dropdown-item icon='el-icon-plus' command='logout'>退出</el-dropdown-item>
                    <el-dropdown-item icon='el-icon-circle-plus'>修改密码</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
            <ul class="header-nav">
                <li class="header-nav-notification">
                    <message-sever></message-sever>
                </li>
            </ul>
        </div>
    </div>
</template>
<script>
import frameworkApi from '@/api/framework'
import { mapGetters, mapMutations } from 'vuex'
import MessageSever from '../components/messagesever/index'
export default {
    name: 'PageHeader',
    props: [],
    data() {
        return {
            selectedProjectId: null
        }
    },
    computed: {
        ...mapGetters('layout', ['sidebarClosed', 'userInfo', 'projects', 'projectId'])
    },
    watch: {
        selectedProjectId(n, o) {
            this.setprojectId(n)
        }
    },
    methods: {
        ...mapMutations('layout', ['setSidebarClosed', 'setprojectId']),
        handleSelect() {},
        toggleSidebar() {
            this.setSidebarClosed(!this.sidebarClosed)
        },
        userMenuCommand(cmd) {
            // console.log('userMenuCommand ', cmd)
            if (cmd == 'logout') {
                console.log("点击登出l")
                frameworkApi.toLogout()
            }
        },
        toIndex() {
            this.$router.push('/')
        }
    },
    created() {
        let cacheInfo = JSON.parse(localStorage.getItem('_sagacloud_admin_store_cacheInfo'))?JSON.parse(localStorage.getItem('_sagacloud_admin_store_cacheInfo')):{}
        if(cacheInfo[this.userInfo.userName] && 
            cacheInfo[this.userInfo.userName].projectId && 
            this.projects.some((item) => {return item.id == cacheInfo[this.userInfo.userName].projectId})
        ){
            this.selectedProjectId = cacheInfo[this.userInfo.userName].projectId
        } else {
            this.selectedProjectId = this.projectId
        }
    },
    mounted() {},
    components: {
        MessageSever
    }
}
</script>