<template>
  <div class="navbar">
    <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container"/>
    <breadcrumb class="breadcrumb-container"/>
    <div v-for="item in company" :key="item.id">
      <!--<div v-if="scope.row.type === item.value">{{ item.label }}</div>-->
      <div style="text-align: center;position: absolute;z-index: -1;width: 100%">{{ item }}</div>
    </div>
    <div class="right-menu">
	  <div style="display: inline-block;height: 50px;line-height: 50px;position: absolute;right: 230px">
        <a target="_blank" href="http://pests.jingkongyun.com/">
              虫情监测
          </a>
      </div>
      <div style="display: inline-block;height: 50px;line-height: 50px;position: absolute;right: 130px">
        <router-link v-if="user.username === 'demo'" to="/demoScreen" target="_blank" tag="a" >可视化大屏</router-link>
        <router-link v-else to="/daping" target="_blank" tag="a" >可视化大屏</router-link>
      </div>
      <template v-if="device!=='mobile'">
        <el-tooltip content="全屏" effect="dark" placement="bottom">
          <screenfull class="screenfull right-menu-item"/>
        </el-tooltip>
      </template>
      <el-dropdown class="avatar-container right-menu-item" trigger="click">
        <div class="avatar-wrapper">
          <img :src="user.avatar ? baseApi + '/avatar/' + user.avatar : Avatar" class="user-avatar">
          <i class="el-icon-caret-bottom"/>
        </div>
        <el-dropdown-menu slot="dropdown">
          <a target="_blank" href="https://docs.auauz.net/">
            <el-dropdown-item>
              项目文档
            </el-dropdown-item>
          </a>
          <span style="display:block;" @click="show = true">
            <el-dropdown-item>
              布局设置
            </el-dropdown-item>
          </span>
          <router-link to="/user/center">
            <el-dropdown-item>
              个人中心
            </el-dropdown-item>
          </router-link>
          <span style="display:block;" @click="open">
            <el-dropdown-item divided>
              退出登录
            </el-dropdown-item>
          </span>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import Screenfull from '@/components/Screenfull'
import Avatar from '@/assets/avatar/avatar.png'
import { loadSysByUserid } from '@/api/sSysmanage'
import { getWarnmsgCount } from '@/api/sWarnmsg'
export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull
  },
  data() {
    return {
      company: [],
      Avatar: Avatar,
      count: 0,
      dialogVisible: false
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'user',
      'device',
      'baseApi'
    ]),
    show: {
      get() {
        return this.$store.state.settings.showRightPanel
      },
      set(val) {
        this.$store.dispatch('changeSetting', {
          key: 'showRightPanel',
          value: val
        })
      }
    }
  },
  created() {
    this.loadSysByUserid()
    this.showWarn()
  },
  methods: {
    showWarn() {
      this.getWarnmsgCount()
      // 主动调用 1000 * 60 * 5   5分钟
      setInterval(this.getWarnmsgCount, 1000 * 60 * 1)
    },
    getWarnmsgCount() {
      getWarnmsgCount().then(res => {
        this.count = res
        if (this.count > 0) {
          this.showNotify()
        }
      }).catch(err => {
        console.log(err.response.data.message)
      })
    },
    showNotify() {
      const audio = new Audio() // 在VUE中使用audio标签
      // audio.src = 'http://hao.haolingsheng.com/ring/000/994/bfd6be2bb9070bfe6a369339b90b77f0.mp3' tip.mp3
      audio.src = '/static/audio/tip.mp3'
      audio.play()// 进行播放
      this.$notify({
        title: '报警提醒',
        dangerouslyUseHTMLString: true,
        message: '<strong>有&nbsp;&nbsp;<a href="/warn/warnmsg" style="color: #1890FF">' + this.count + '条</a>&nbsp;&nbsp;设备报警提醒，请点击处理</strong>',
        type: 'warning',
        position: 'bottom-right'
      })
    },
    open() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.logout()
      })
    },
    loadSysByUserid() {
      loadSysByUserid().then(res => {
        // console.log(res, '22')
        this.company.push(res.companyname)
      })
    },
    toggleSideBar() {
      this.$store.dispatch('ToggleSideBar')
    },
    logout() {
      this.dialogVisible = false
      this.$store.dispatch('LogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .navbar {
    height: 50px;
    line-height: 50px;
    border-radius: 0px !important;
    .hamburger-container {
      line-height: 58px;
      height: 50px;
      float: left;
      padding: 0 10px;
    }
    .breadcrumb-container{
      float: left;
    }
    .errLog-container {
      display: inline-block;
      vertical-align: top;
    }
    .right-menu {
      float: right;
      height: 100%;
      &:focus{
        outline: none;
      }
      .right-menu-item {
        display: inline-block;
        margin: 0 8px;
      }
      .screenfull {
        height: 20px;
      }
      .international{
        vertical-align: top;
      }
      .theme-switch {
        vertical-align: 15px;
      }
      .avatar-container {
        height: 50px;
        margin-right: 30px;
        .avatar-wrapper {
          margin-top: 5px;
          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>
