<template>
    <div class="top-bar">
        <el-dropdown
            class="dropdown"
            trigger="click"
            @command="handleCommand"
        >
            <span class="el-dropdown-link">
                {{ me.name }}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
                <el-dropdown-item divided command="logout">退出</el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>
    </div>
</template>

<script>
import authAPI from '../api/auth'
import { mapState, mapMutations } from 'vuex'

export default {
    computed: {
        ...mapState({
            me: state => state.basics.me
        })
    },
    methods: {
        ...mapMutations('basics', [
            'setToken',
            'setLoggedIn'
        ]),
        handleCommand (command) {
            switch (command) {
                case 'logout': this.logout(); break;
            }
        },
        logout () {
            authAPI.logout(localStorage.getItem('token'), () => {
                this.setToken('')
                console.log(this.$route)
                this.$router.push({
                    path: '/login',
                    query: {
                        redirect: this.$route.path
                    }
                })
                this.setLoggedIn()
            })
        }
    }
}
</script>

<style lang="less" scoped>
.top-bar {
    width: 100%;
    background-color: #fff;
    padding: 10px 20px;
    text-align: right;
    box-sizing: border-box;
    .el-dropdown-link {
        cursor: pointer;
    }
}
</style>