<template>
  <div class="navbar">
    <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"/>

    <div class="right-menu">
      <template v-if="device!=='mobile'">
        <router-link class="right-menu-item" to="/chat/chat">捷哥</router-link>
        <div class="right-menu-item">
          <el-image style="width: 22px;height: 22px;cursor: pointer;" :src="helpIcon" @click="tohelp"></el-image>
        </div>
        <el-badge :value="matterNum !== 0 ? matterNum : '' " class="right-menu-item badge">
         <div @click="$router.push('/task/todo')" style="font-size: 28px; color: #cccccc;cursor: pointer;" class="el-icon-postcard"></div>
        </el-badge>


        <el-badge :value="notMessage !== 0 ? notMessage : '' " class="right-menu-item badge">
         <el-image style="width: 22px;height: 20px;cursor: pointer;" :src="unreadMessage" @click="notRead"></el-image>
        </el-badge>

        <el-badge :value="downloadCnt ? downloadCnt : '' " class="right-menu-item badge">
            <el-image style="width: 22px;height: 20px;cursor: pointer;" :src="unDownload" @click="handleDownload"></el-image>
        </el-badge>


        <search id="header-search" class="right-menu-item" />



<!--        <el-tooltip :content="$t('文档地址')" 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="$t('布局大小')" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip>

      </template>

      <!-- <el-select v-model="locale" :placeholder="$t('语言')" class="right-menu-item select-nav"  @change="localeChange">
        <el-option v-for="dict in langDatas" :key="dict.value" :label="dict.label" :value="dict.value"/>
      </el-select> -->

      <dict-selector :type="DICT_TYPE.SYSTEM_LOCALE" v-model="lang" defaultable class="right-menu-item select-nav" />

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">

        <div class="avatar-wrapper flex-center">
          {{$store.getters.name}}
          <img :src="avatar" class="user-avatar ml-10">
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>{{$t('个人中心')}}</el-dropdown-item>
          </router-link>
          <el-dropdown-item @click.native="setting = true">
            <span>{{ $t('布局设置') }}</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <span>{{ $t('退出登录') }}</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </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 RuoYiGit from '@/components/RuoYi/Git'
import RuoYiDoc from '@/components/RuoYi/Doc'
import {getLocale, saveLocale} from "@/utils/db";
import unreadMessage from  "@/assets/images/unread-message.png"
import helpIcon from  "@/assets/images/help.png"
import unDownload from  "@/assets/images/download.png"
import i18n from '@/i18n'
export default {
  data() {
    return {
      unreadMessage,
      helpIcon,
        unDownload,
      // locale: getLocale(),
      // 枚举
      // langDatas: LangEnum.LANG,
      notReadTotal:0,//要去取VUEX里面的未读数据总数，我不会,登录之后要调得到当前人未读记录总数接口放到VUEX中
      lang: i18n.locale, // 当前语言

      // 未下载文件数量
      notDownload: 0
    }
  },
  created() {
    // 未避免频繁内存溢出，减少请求记录感染，开发的时候不轮询消息
    if(process.env.NODE_ENV != 'development'){
      setInterval(() => {
        this.updateMessage()
      }, 20000)
    }
    this.updateMessage()

  },
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    RuoYiGit,
    RuoYiDoc
  },
  computed: {
    ...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
      }
    },
    notMessage(){
      return this.$store.state.user.notMessage
    },
    matterNum(){
      return this.$store.state.user.matterNum
    },
      downloadCnt(){
        return this.$store.state.user.download
      }
  },
  watch:{
    lang(val){
      console.log('切换了语言', val)
      this.$i18n.locale = val
      saveLocale(val)
    }
  },
  methods: {
    handleDownload(){
      this.$router.push('/system/download')
    },
    updateMessage(){
      this.$store.dispatch('getUserBadge');
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    /* localeChange(value) {
      console.log(value)
      saveLocale(value)
    }, */
    async logout() {
      this.$modal.confirm(this.$t('确定注销并退出系统吗？'), this.$t('提示')).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/';
        })
      }).catch(() => {});
    },
    notRead(){
      this.$router.push({path:'/system/internalMessage/my-internal-message',query:{status:'0'}});
    },
    tohelp(){
      this.$router.push({path:'/operation_manage/info'});
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);


  .hamburger-container {
    line-height: 46px;
    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 {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 50px;

    &:focus {
      outline: none;
    }
    ::v-deep.el-badge{
     .el-badge__content{
       top: 13px;
       right:15px;
       height: 20px;
     }
    }
    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;
      margin-right: 15px;
      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }
   ::v-deep.select-nav{
     width: 110px;
     height: 32px;
     vertical-align: top;
     .el-input__inner{
       font-size: 14px;
       font-family: PingFang SC;
       font-weight: 400;
       color: #D9D9D9;
     }
   }
    .avatar-container {
      margin-right: 30px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 32px;
          height: 32px;
          border-radius: 50%;
          vertical-align: sub;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -16px;
          top: 10px;
          font-size: 14px;
        }
      }
    }
  }
}
</style>
