<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">
      <template v-if="device!=='mobile'">
        <search id="header-search" class="right-menu-item" />
        <div class="right-menu-item hover-effect" @click="gotoTodo" title="代办事项">
          <el-icon class="el-icon-document-checked" />
        </div>
        <div class="right-menu-item hover-effect" @click="gotoWarning" title="告警信息">
          <el-badge :value="hasWarning || ''" class="nav-badge">
            <el-icon class="el-icon-bell" />
          </el-badge>
        </div>

        <screenfull id="screenfull" class="right-menu-item hover-effect" />

        <div class="right-menu-item hover-effect" @click="refresh" title="刷新">
          <el-icon class="el-icon-refresh" />
        </div>
        <!-- <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select id="size-select" class="right-menu-item hover-effect" />
        </el-tooltip> -->
      </template>

      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <img :src="avatar" class="user-avatar">
          <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 @click.native="setting = true">
            <span>布局设置</span>
          </el-dropdown-item>
          <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 SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import { listMaterialWarning } from '@/api/wms/materialWarning'
// import RuoYiGit from '@/components/RuoYi/Git'
// import RuoYiDoc from '@/components/RuoYi/Doc'

export default {
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    // SizeSelect,
    Search,
    // RuoYiGit,
    // RuoYiDoc
  },
  data() {
    return {
      hasWarning: false,
      warningTimer: null
    }
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      '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
      }
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    refresh() {
      window.location.reload(true)
      // this.$store.dispatch('app/refresh')
    },
    gotoTodo() {
      this.$router.push({ path: '/wms/todo' })
    },
    gotoWarning() {
      this.$router.push({ path: '/wms/materialWarning' })
    },
    checkWarning() {
      listMaterialWarning({ pageNum: 1, pageSize: 1, warningStatus: '0' }).then(response => {
        this.hasWarning = response.total
      }).catch(() => {
        this.hasWarning = 0
      })
    },
    async logout() {
      this.$confirm('确定注销并退出系统吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = process.env.VUE_APP_CONTEXT_PATH + "index";
        })
      }).catch(() => {});
    }
  },
  mounted() {
    this.checkWarning()
    this.warningTimer = setInterval(() => {
      this.checkWarning()
    }, 60000)
  },
  beforeDestroy() {
    if (this.warningTimer) {
      clearInterval(this.warningTimer)
      this.warningTimer = null
    }
  }
}
</script>

<style lang="scss" scoped>
.navbar {
  height: 50px;
  overflow: hidden;
  position: relative;
  // 金属质感渐变背景
  // background: #454c51;
  // border-bottom: 1px solid #8d939b;
  // box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1), 
              // 0 1px 1px rgba(255, 255, 255, 0.08) inset;

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: all .3s ease;
    -webkit-tap-highlight-color: transparent;
    padding: 0 15px;
    border-right: 1px solid rgba(0, 0, 0, 0.05);

    &:hover {
      background: rgba(0, 0, 0, 0.05);
      box-shadow: inset 1px 1px 2px rgba(0, 0, 0, 0.08),
                  inset -1px -1px 2px rgba(255, 255, 255, 0.05);
    }
  }

  .breadcrumb-container {
    float: left;
    padding-left: 15px;
  }

  .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 12px;
      height: 100%;
      font-size: 18px;
      // 金属风格文字色
      color: #111;
      vertical-align: text-bottom;
      transition: all 0.2s ease;

      &.hover-effect {
        cursor: pointer;
        position: relative;

        &:hover {
          background: rgba(0, 0, 0, 0.05);
          box-shadow: inset 1px 1px 2px rgba(0, 0, 0, 0.08),
                      inset -1px -1px 2px rgba(255, 255, 255, 0.05);
          color: #333;
        }
      }
    }

    .avatar-container {
      margin-right: 15px;

      .avatar-wrapper {
        margin-top: 5px;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 8px;
          // 头像金属边框
          border: 1px solid #a0a6ad;
          box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -15px;
          top: 25px;
          font-size: 12px;
          color: #666;
        }
      }
    }

    .nav-badge {
      ::v-deep .el-badge__content.is-fixed {
        top: 14px;
        right: 14px;
      }
    }
  }
}
</style>