<!-- 日期和用户 -->
<template>
    <div class="user-info">
        <div class="block">
            <i class="el-icon-date"></i>
            <div>{{ time }}</div>
        </div>
        <div class="block">
            <i class="el-icon-user"></i>
            <el-dropdown trigger="click" @command="handler">
                <span class="dropdown-txt"> {{ $x_user ? $x_user.username : '未登录' }} </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="quit">注销</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>
    </div>
</template>

<script>
    import {
        dateFormat
    } from '@/common/util.js'
    export default {
        data() {
            return {
                userTimer: null, // 存放定时器
                time: ''
            };
        },
        mounted() {
            this.$nextTick(() => {
                this.initTimer();
            });
        },
        beforeDestroy() {
            window.clearTimeout(this.userTimer);
            this.userTimer = null;
        },
        methods: {
            // 初始化定时器
            initTimer() {
                this.getTime();
                this.userTimer = setInterval(this.getTime, 1000);
            },
            // 获取当前时间
            getTime() {
                this.time = dateFormat(new Date(), 'YYYY/MM/DD HH:mm:ss z');
            },
            // 点击用户名的下拉菜单命令
            handler(command) {
                switch (command) {
                    case 'quit':
                        this.logout();
                        break;
                }
            },
            // 注销，这里没清除用户，是考虑还可以返回，不重新登录
            logout() {
                this.$router.push('/login');
            }
        }
    };
</script>

<style lang="scss" scoped>
    .user-info {
        font-size: 1rem;
        color: $color-cyan;
        width: 100%;
        height: 1.875rem;
        display: flex;
        justify-content: flex-start;
        align-items: center;

        .block {
            height: 100%;
            display: flex;
            align-items: center;
            margin-right: 1.875rem;

            i {
                margin-right: 0.625rem;
            }

            .dropdown-txt {
                color: $color-cyan;
                cursor: pointer;
            }
        }
    }
</style>
