<template>
  <nav class="site-navbar" :class="'site-navbar--' + navbarLayoutType">
    <div class="site-navbar__header">
      <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
        <a class="site-navbar__brand-lg" href="javascript:;">木之云平台</a>
        <a class="site-navbar__brand-mini" href="javascript:;">木之云</a>
      </h1>
    </div>
    <div class="site-navbar__body clearfix">
      <el-menu
        class="site-navbar__menu"
        mode="horizontal">
        <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
          <icon-svg name="zhedie"></icon-svg>
        </el-menu-item>
      </el-menu>
      <el-menu
        class="site-navbar__menu site-navbar__menu--right"
        mode="horizontal">
        <el-menu-item index="0" @click="$router.push({ name: 'msgCenter' })">
          <template slot="title">
            <el-badge :value="cnt" :hidden="cnt < 1">
              <icon-svg name="lingdang" class="el-icon-lingdang"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item index="1" @click="previewPic">
          <template slot="title">
            <el-badge value="">
              <icon-svg name="minprogram" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
         <el-menu-item index="2" @click="previewPic">
          <template slot="title">
            <el-badge value="">
              <icon-svg name="minprogram" class="el-icon-setting"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item index="0" @click="$router.push({ name: 'msgWork' })">
          <template slot="title">
            <el-badge value="">
              <icon-svg name="gongyimg" class="el-icon-lingdang"></icon-svg>
            </el-badge>
          </template>
        </el-menu-item>
        <el-menu-item class="site-navbar__avatar" index="4">
          <el-dropdown :show-timeout="0" placement="bottom">
            <span class="el-dropdown-link">
              <img src="~@/assets/img/avatar.png" :alt="userName">{{ userName }}
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
              <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-menu-item>
      </el-menu>
    </div>
    <!-- 弹窗, 修改密码 -->
    <update-password v-if="updatePasswordVisible" ref="updatePassowrd"></update-password>
    <Msg ref="msgDialog" @noticeAdded="noticeAdded"/>
    <youli-component v-if="previewVisible" ref="preview"/>
  </nav>
</template>

<script>
  import UpdatePassword from './main-navbar-update-password'
  import Msg from './common/msg'
  import { clearLoginInfo } from '@/utils'
  import PreviewComponent from '@/views/modules/common/preview-component'
  import YouliComponent from '@/views/modules/common/youli-component'
  import Vue from 'vue'
export default {
    data () {
      return {
        updatePasswordVisible: false,
        cnt: 0,
        previewVisible: false
      }
    },
    components: {
      YouliComponent,
      PreviewComponent,
      UpdatePassword,
      Msg
    },
    computed: {
      navbarLayoutType: {
        get () { return this.$store.state.common.navbarLayoutType }
      },
      sidebarFold: {
        get () { return this.$store.state.common.sidebarFold },
        set (val) { this.$store.commit('common/updateSidebarFold', val) }
      },
      mainTabs: {
        get () { return this.$store.state.common.mainTabs },
        set (val) { this.$store.commit('common/updateMainTabs', val) }
      },
      userName: {
        get () {
        // return this.$store.state.user.name
          return Vue.cookie.get('name')
        }
      }
    },
    methods: {
      // 修改密码
      updatePasswordHandle () {
        this.updatePasswordVisible = true
        this.$nextTick(() => {
          this.$refs.updatePassowrd.init()
        })
      },
      // 退出
      logoutHandle () {
        this.$confirm(`确定进行[退出]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          clearLoginInfo()
          this.$router.push({ name: 'login' })
          // this.$http({
          //   url: this.$http.adornUrl('/sys/logout'),
          //   method: 'post',
          //   data: this.$http.adornData()
          // }).then(({data}) => {
          //   if (data && data.code === 0) {
          //     clearLoginInfo()
          //     this.$router.push({ name: 'login' })
          //   }
          // })
        }).catch(() => {})
      },
      // 通知数量变化
      noticeAdded (num) {
        this.cnt = num
      },
      // 小程序图片预览
      previewPic () {
        this.previewVisible = true
        this.$nextTick(() => {
          this.$refs.preview.init()
        })
      }
    }
  }
</script>
