<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">
<!--      <span @click="toMyCartPage">
        <el-badge :value="totalCartCount >= 99 ? '99+' : totalCartCount" class="item">
          <span class="top-msg">消息</span>
        </el-badge>
      </span>-->
      <span class="top-version">v1.0.0</span>
      <el-popover
        placement="right"
        width="400"
        trigger="hover">
        <el-table
          :data="msgTableData"
          border
          style="width: 100%">
          <el-table-column
            prop="name"
            label="消息通知类型">
          </el-table-column>
          <el-table-column
            prop="count"
            label="消息通知数量">
          </el-table-column>
          <el-table-column label="操作" width="100" >
            <template slot-scope="scope">
              <el-button type="text" @click="handleToMsgView(scope.row)">前往查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <span style="cursor: pointer;" @mouseover="toMyCartPage" slot="reference">
          <template v-if="totalCartCount == 0">
            <el-badge value="" class="item">
            <el-tooltip class="item" effect="light" content="消息通知" placement="bottom">
              <i class="el-icon-bell"></i>
            </el-tooltip>
          </el-badge>
          </template>
          <template v-else>
            <el-badge :value="totalCartCount >= 99 ? '99+' : totalCartCount" class="item">
            <el-tooltip class="item" effect="light" content="消息通知" placement="bottom">
              <i class="el-icon-bell"></i>
            </el-tooltip>
          </el-badge>
          </template>
       </span>
      </el-popover>


      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <span style="font-size: 16px"><i class="el-icon-user"/> {{nickName}}</span>
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/user/profile">
            <el-dropdown-item>个人中心</el-dropdown-item>
          </router-link>
          <el-dropdown-item divided @click.native="logout">
            <span>退出登录</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 Search from '@/components/HeaderSearch'
import { getUserMsgCount } from '@/api/my_business/workflow'

export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    Search
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'nickName',
      '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
      }
    },
    totalCartCount() {
      return this.$store.state.user.unreadMsgNumber ? this.$store.state.user.unreadMsgNumber : 0
    },
  },
  data() {
    return {
      msgVisible: false,
      msgTableData: []
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = '/index';
        })
      }).catch(() => {});
    },
    // 跳转到消息中心
    toMyCartPage() {
      getUserMsgCount().then(res => {
        this.msgTableData = [];
        let msgItem = {};
        msgItem['type'] = 'msg';
        msgItem['name'] = '消息中心数量';
        msgItem['count'] = res.msgCount;
        let taskItem = {};
        taskItem['type'] = 'task';
        taskItem['name'] = '待办消息数量';
        taskItem['count'] = res.taskCount;
        this.msgTableData.push(msgItem);
        this.msgTableData.push(taskItem);
      }).catch(error => {
      })
      this.msgVisible = true;
    },
    handleToMsgView(row){
      this.msgVisible = false;
      if(row.type === 'msg'){
        this.$router.push({ path: '/message', query: {'states':'1'} })
      } else {
        this.$router.push({ path: '/workstuff/dispose'})
      }
    }
  }
}
</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;
    }

    .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>
