<template>
  <div :style="{backgroundImage:'url('+headerUrl+')'}" class="app-header">
    <h3>{{ title }}</h3>
    <!--子系统选项-->
    <el-dropdown class="systemSelect" @command="handleCommand">
      <span class="el-dropdown-link">{{ currentSystem.name }}<i class="el-icon-arrow-down el-icon--right"/>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item>
          <span style="display:block;" @click="backToIndex">回首页</span>
        </el-dropdown-item>
        <el-dropdown-item v-for="system in systems" :command="system" :key="system.id">{{ system.name }}</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <div v-show="currentSystem.code === 'ctrl'" class="back-to-overview">
      <el-button type="info" round @click="backToOverview">运行总览</el-button>
    </div>

    <el-dropdown class="avatar-container">
      <div class="avatar-wrapper">
        <span style="line-height:50px;">欢迎您，{{ name }}</span>
        <i class="el-icon-caret-bottom"/>
      </div>
      <el-dropdown-menu slot="dropdown" class="user-dropdown">
        <el-dropdown-item>
          <span style="display:block;" @click="resetPwd">修改密码</span>
        </el-dropdown-item>
        <el-dropdown-item divided>
          <span style="display:block;" @click="logout">注销</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
    <reset-pwd v-show="showSetPwd" ref="retPwd"/>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import ResetPwd from '../../system/user/resetPwd'
import { getProject } from '@/utils/baseConfig'

export default {
  name: 'AppHeader',
  components: { ResetPwd },
  data() {
    return {
      name: this.$store.getters.name,
      title: getProject().title,
      systems: [
        { code: 'ctrl', ismenu: '0', levels: 1, name: '综合控制室子系统', resourceType: '02', url: '/ctrl' },
        { code: 'door', ismenu: '0', levels: 1, name: '营门管理子系统', resourceType: '02', url: '/door' },
        { code: 'fence', ismenu: '0', levels: 1, name: '周界防卫子系统', resourceType: '02', url: '/fence' },
        { code: 'region', ismenu: '0', levels: 1, name: '全域监控子系统', resourceType: '02', url: '/region' },
        { code: 'key', ismenu: '0', levels: 1, name: '重点区域管控子系统', resourceType: '02', url: '/key' }
      ],
      showSetPwd: false,
      headerUrl: require('../../../assets/global_images/header.png')// 头部图片
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar'
    ]),
    currentSystem() {
      return this.$store.getters.currentSystem
    }
  },
  created() {
    this.getSystems()
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('ToggleSideBar')
    },
    logout() {
      this.$store.dispatch('LogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
      })
    },
    resetPwd() {
      this.showSetPwd = true
      this.$refs.retPwd.initDialog(true)
    },
    backToIndex() {
      this.$router.replace('/dashboard')
    },
    backToOverview() {
      this.$router.replace('/ctrl/overview')
    },
    // 切换子系统
    handleCommand(command) {
      if (typeof command !== 'undefined') {
        console.log('切换子系统:' + command.name)
        this.$store.commit('SET_CHANGEFLAG', '1')
        this.$store.commit('SET_SYSTEM', command)
        this.$store.dispatch('tagsView/delAllViews', true).then(({ visitedViews }) => {
          this.$router.replace(command.url)
        })
      }
    },
    // 获取子系统
    getSystems() {
      this.$store.dispatch('GetSystems').then(() => {
        console.log('获取子系统列表成功-appHeader')
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-header{
    height:60px;
    background-color:#000000;
    background-repeat: no-repeat;
    -webkit-background-size: 100% 100%;
    background-size: 100% 100%;
    color:white;
  }
  h3{
    margin-left:40px;
    display: inline-block;
    float:left;
  }
  .avatar-container {
    height: 50px;
    display: inline-block;
    float: right;
    z-index:500;
    position: fixed;
    top:5px;
    right: 35px;
    .avatar-wrapper {
      color:#d3dce6;
      cursor: pointer;
      margin-top: 5px;
      position: relative;
      line-height: initial;
      .user-avatar {
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
      .el-icon-caret-bottom {
        /*position: absolute;*/
        /*right: 0px;*/
        /*top: 25px;*/
        font-size: 12px;
      }
    }
  }
  .systemSelect{
    height: 50px;
    display: inline-block;
    float: left;
    z-index:500;
    position: fixed;
    margin-left:60px;
    top:10px;
    /*left: 220px;*/
    color:#d3dce6;
    line-height: 50px;
    cursor: pointer;
  }

  .back-to-overview {
    position: absolute;
    top: 15px;
    right: 200px;
  }

</style>
