<template>
  <div class="navbar">
    

    <el-row :gutter="20">
      <el-col :span="22">
        <div style="float: left;padding: 5px;">
          <img src="@/assets/logo/top-left-logo.png" alt="" style="margin-top: 15px;">
        </div>
    
          <!-- 点击左侧二级菜单滑开 -->
        <!-- <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" /> -->
    
        <breadcrumb id="breadcrumb-container" class="breadcrumb-container" v-if="!topNav"/>
    
    
        <!-- 顶部导航 -->
        <top-nav id="topmenu-container" class="topmenu-container" v-if="topNav"/>
      </el-col>
      <el-col :span="2" style="float: right;">
    <div class="right-menu">
      <!-- <template v-if="device!=='mobile'">
        <search id="header-search" class="right-menu-item" />

        <el-tooltip content="源码地址" effect="dark" placement="bottom">
          <ruo-yi-git id="ruoyi-git" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip content="文档地址" effect="dark" placement="bottom">
          <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
        </el-tooltip>

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>

      </template> -->
      
          <div class="formatDate">
            <span>{{ nowDate }}</span>
          </div>
          <!-- <div class="yhm">{{showUserName}}</div> -->
        
      
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <!-- <img :src="avatar" class="user-avatar">
          <i class="el-icon-caret-bottom" /> -->
          <div class="yhm">{{showUserName}}</div>
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item @click.native="setting = true">
            <span>布局设置</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
  
    </div>
  </el-col>
</el-row>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import TopNav from '@/components/TopNav'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import Cookie from 'js-cookie'
export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search
  },
  data() {
    return {
      nowDate: "", // 当前日期
    };
  },
  computed: {
    showUserName(){
      return Cookie.get('username')
    },
    ...mapGetters([
      'sidebar',
      'avatar',
      'device'
    ]),
    setting: {
      get() {
        return this.$store.state.settings.showSettings
      },
      set(val) {
        this.$store.dispatch('settings/changeSetting', {
          key: 'showSettings',
          value: val
        })
      }
    },
    topNav: {
      get() {
        return this.$store.state.settings.topNav
      }
    }
    
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    },
    currentTime() {
      setInterval(this.formatDate, 500);
    },
    formatDate() {
      let date = new Date();
      let year = date.getFullYear(); // 年
      let month = date.getMonth() + 1; // 月
      let day = date.getDate(); // 日
      let week = date.getDay(); // 星期
      let weekArr = [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ];
      let hour = date.getHours(); // 时
      hour = hour < 10 ? "0" + hour : hour; // 如果只有一位，则前面补零
      let minute = date.getMinutes(); // 分
      minute = minute < 10 ? "0" + minute : minute; // 如果只有一位，则前面补零
      let second = date.getSeconds(); // 秒
      second = second < 10 ? "0" + second : second; // 如果只有一位，则前面补零
      this.nowDate = `${year}/${month}/${day}`;
    }
  },
  mounted() {
    this.currentTime();
  },
  beforeDestroy() {
    if (this.formatDate) {
      clearInterval(this.formatDate); // 在Vue实例销毁前，清除时间定时器
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 80px;
  width: 100%;
  left: 0;
  /* line-height: 80px; */
  overflow: hidden;
  position: fixed;
  background: #053B6A;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);

  .hamburger-container {
    /* line-height: 80px; */
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color:transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    background-color: red;
    position: absolute;
    left: 350px;
    background-color: #053B6A;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }
  .right-menu {
    float: right;
    width: 100%;
    height: 100%;
    /* line-height: 80px; */
      
    

    &:focus {
      outline: none;
    }
    .formatDate{
      width: 100%;
      height: 40px;
      line-height: 50px;
      text-align: center;
      font-size: 18px;
      color: #fff;
    }
    .yhm{
      font-size: 20px;
      color: #fff;
      width: 100%;
      text-overflow:ellipsis; 
      white-space: nowrap;
      overflow: hidden;
    }
    .right-menu-item {
      display: inline-block;
      width: 100%;
      line-height: 30px;
      height: 40px;
      text-align: center;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

    .avatar-container {
      width: 70%;
      margin-left: 15%;

      .avatar-wrapper {
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
