<template>
  <div class="navbar">
    <hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />

    <breadcrumb id="breadcrumb-container" class="breadcrumb-container" />

    <div class="right-menu" style="width:200px;position: relative;">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click" style="    position: absolute;    left: 0px;    top: 0px;">
        <div class="avatar-wrapper">
          <div style="padding: 10px;background: rgb(24 144 255);color: #fff;line-height: 24px;font-size: 14px;border-radius: 20%;">
            {{ name.length>2?name.substr(name.length-2,2):name }}
          </div>
          <!-- <img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar"> -->
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="login">
            <span style="display:block;">Log Out</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <el-popover
        placement="bottom"
        width="1000"
        trigger="click"
        style="position: absolute;    left: 100px;    top: 0px;"
      >
        <div>
          <el-button plain @click="clearTasks()">清理列表</el-button>
          <el-table :data="$store.getters.taskList" height="800">
            <el-table-column label="任务名" prop="jobName" />
            <el-table-column label="任务完成百分比" prop="jobProgress">
              <template slot-scope="r">
                <el-progress :percentage="r.row.jobProgress" />
              </template>
            </el-table-column>
            <el-table-column label="任务步骤" prop="step" />
            <el-table-column label="任务状态" prop="status" />
            <el-table-column label="任务开始时间" prop="createTime" />
          </el-table>
        </div>
        <el-button slot="reference" plain icon="el-icon-s-data">
          <el-badge v-if="$store.getters.taskList.length>0" is-dot class="item" />
        </el-button>
      </el-popover>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import { removeToken, getToken } from '@/utils/auth' // get token from cookie
import * as signalR from '@aspnet/signalr'
export default {
  components: {
    Breadcrumb,
    Hamburger
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'device',
      'name'
    ])
  },
  data() {
    return {
    }
  },
  mounted() {
    var that = this
    var taskList = []
    const signalRConn = new signalR.HubConnectionBuilder()
      .withUrl('http://120.24.149.148:9521/BatchInput', {
        skipNegotiation: true,
        transport: signalR.HttpTransportType.WebSockets,
        accessTokenFactory() {
          return getToken()
        }
        // qs: 'Authorization=' + getToken()
      })
      .configureLogging(signalR.LogLevel.Information)
      .build()
    signalRConn.on('AddTaskStatus', function(tasks) {
      console.info('获取到任务数量' + tasks.length)
      that.$store.commit('signalR/setTaskList', tasks)
    })
    signalRConn.on('UpdateTaskStatus', function(task) {
      that.$store.commit('signalR/setItem', task)
    })
    signalRConn.on('TaskError', error => {
      that.$alert(error)
    })
    signalRConn.start().then(() => {
      console.info('连接成功')
      that.$store.commit('signalR/setIsConnection', true)
    }).catch(() => {
      '连接失败'
    })
    this.$store.commit('signalR/setSignalR', signalRConn)
  },
  methods: {
    clearTasks() {
      this.$store.getters.connection.invoke('Clear').then(() => {
        console.info('清理成功')
        this.$store.commit('signalR/clearItem')
      }).catch(r => {
        this.$alert('清理失败' + r)
      })
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async login() {
      await this.$store.dispatch('user/resetToken')
      this.$router.push(`/login`)
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push(`/login?redirect=${this.$route.fullPath}`)
    }
  }
}
</script>
<style >
.right-menu .el-popover{
  height: 800px;
  overflow: auto;
}
</style>
<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;
  }

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

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

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      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 {
      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>
