<template>
  <div :class="classObj" class="app-wrapper">
    <div class="top-head-bar">
      <div class="top-head-bar__logo">
        <img style="height: 20px;" src="../assets/logo.png"></img>
      </div>

      <ul>
        <li v-for="item in $store.state.permission.rootMenu" :class="{'li-active':new RegExp(`^${item.path}`).test($route.path) && $route.path !== '/contract/system/menu'}" @click="menuClick(item)">{{ item.name }}</li>
      </ul>

      <div class="top-head-bar__user">
        <el-dropdown class="avatar-container" trigger="click">
          <div class="avatar-wrapper">
            <div class="avatar-wrapper__name">{{ $store.state.user.name }}</div>
            <i class="el-icon-caret-bottom" />
          </div>
          <el-dropdown-menu slot="dropdown" class="user-dropdown">
            <router-link to="/">
              <el-dropdown-item>
                系统首页
              </el-dropdown-item>
            </router-link>
            <el-dropdown-item divided>
              <span style="display:block;">退出</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />

    <template v-if="$route.path === '/worker'">
      <el-scrollbar style="padding-top: 50px;height: 100%;">
        <worker></worker>
      </el-scrollbar>
    </template>
    <template v-else-if="$route.path === '/bookmanage'">
      <iframe ref="bookIframe" style="width: 100%;height: 100%;padding-top: 50px;" src="http://localhost:8014/admin/#/">你的浏览器不支持该iframe</iframe>
    </template>
    <template v-else>
      <sidebar class="sidebar-container" />
      <div class="main-container">
        <div :class="{'fixed-header':fixedHeader}" v-if="false">
          <navbar />
        </div>
        <el-scrollbar style="height: 100%">
          <app-main />
        </el-scrollbar>
      </div>
    </template>
  </div>
</template>

<script>
  import {
    Navbar,
    Sidebar,
    AppMain
  } from './components'
  import ResizeMixin from './mixin/ResizeHandler'
import worker from "./components/worker"
  export default {
    name: 'Layout',
    components: {
      Navbar,
      Sidebar,
      AppMain,
      worker
    },
    mixins: [ResizeMixin],
    data(){
      return {
        active:0
      }
    },
    computed: {
      sidebar() {
        return this.$store.state.app.sidebar
      },
      device() {
        return this.$store.state.app.device
      },
      fixedHeader() {
        return this.$store.state.settings.fixedHeader
      },
      classObj() {
        return {
          hideSidebar: !this.sidebar.opened,
          openSidebar: this.sidebar.opened,
          withoutAnimation: this.sidebar.withoutAnimation,
          mobile: this.device === 'mobile'
        }
      }
    },
    methods: {
      handleClickOutside() {
        this.$store.dispatch('app/closeSideBar', {
          withoutAnimation: false
        })
      },


      menuClick(item){
        this.$router.push(item.path)

        console.log(this.$route.path)
      }
    },
    mounted() {

    },
  }
</script>

<style lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  @import "~@/styles/variables.scss";

  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;

    &.mobile.openSidebar {
      position: fixed;
      top: 0;
    }
  }

  .top-head-bar{
    background: #338DE3FF;
    height: 50px;
    filter: drop-shadow(0 1px 1px #49a0f3);
    display: grid;
    grid-template-columns: auto .5fr 6fr .2fr 1fr;
    grid-template-rows: 50px;
    grid-template-areas:
      "logo . menu . user";

    z-index: 9999;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;

    &__logo{
      height: 50px;
      grid-area:logo;
      display: flex;
      align-items: center;

      padding:0 20px;
    }
    @media screen and (max-width: 1000px){
      &__logo{
        display: none;
      }
    }

    &__user{
      grid-area: user;
    }

    ul{
      grid-area:menu;
      height: 100%;
      display: flex;
      justify-self: flex-end;
      align-items: flex-end;

      li{
        color:#fff;
        height: 40px;
        line-height: 40px;
        list-style: none;
        transition: all 300ms ease-out;
        border-top-right-radius: 6px;
        border-top-left-radius: 6px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        cursor: pointer;

        padding: 0 20px;
        position: relative;

        &::after{
          content:"";
          background: #338DE3FF;
          height: 3px;
          width: 0;
          border-radius: 1px;
          transition: all 300ms ease-out;

          position: absolute;
          left: 6px;
          bottom: 2px;
        }
        &+li{
          margin-left: 20px;
        }
        &:hover{
          background: #fff;
          color:rgb(100,100,100);

          &::after{
            width: calc(100% - 12px);
          }
        }
      }
      .li-active{
        background: #fff;
        color:rgb(100,100,100);

        &::after{
          width: calc(100% - 12px);
        }
      }
    }
  }
  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }

  .fixed-header {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 9;
    width: calc(100% - #{$sideBarWidth});
    transition: width 0.28s;
  }

  .hideSidebar .fixed-header {
    width: calc(100% - 54px)
  }

  .mobile .fixed-header {
    width: 100%;
  }

  .avatar-container {
    height: 100%;
    margin-right: 30px;

    .avatar-wrapper{
      height: 100%;
      display: flex;
      align-items: center;
      .user-avatar {
        cursor: pointer;
        width: 40px;
        height: 40px;
        border-radius: 10px;
      }
      &__name{
        color: #fff;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;

        padding: 0 6px;
      }
      .el-icon-caret-bottom {
        cursor: pointer;
        color: #FFF;
        position: absolute;
        right: -20px;
        font-size: 12px;
      }
    }
  }


</style>
