<template>
  <div class="basic-layout-wrapper" v-if="userMenu && userMenu.length">
    <div class="basic-layout-vertical">
      <!--  左侧菜单  -->
      <el-menu :default-active="activeMenu"
               class="basic-layout-menu" @open="handleOpen" @close="handleClose"
               background-color="#475266"
               text-color="#eee"
               :router="true"
               unique-opened
               :collapse="isCollapse">
        <!--  logo与名称  -->
        <div class="logo">
          <img src="../../assets/logo/logo.png" @click="toggle"/>
          <p v-if="!isCollapse">Octopus Pro</p>
        </div>
        <!--  展开收缩按钮  -->
        <div class="basic-layout-menu-fold" @click="toggle" :class="isCollapse ? 'fold' : 'unfold'">
        </div>
        <sidebar-item v-for="route in userMenu" :key="route.path" :item="route" :base-path="route.path" />
      </el-menu>
      <!--  右侧区域  -->
      <div class="basic-layout-right">
        <!--  topbar  -->
        <div class="basic-layout-right-top">
          <el-col :span="4" :offset="20" class="avatar-wrapper">
            <el-avatar size="small" src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                       style="margin-right: 6px;"></el-avatar>
            <el-dropdown>
              <span class="el-dropdown-link">
                王小虎<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>查看信息</el-dropdown-item>
                <el-dropdown-item>个人设置</el-dropdown-item>
                <el-dropdown-item divided>退出</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-col>
        </div>
        <router-view class="basic-layout-right-main width-limit"/>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'BasicLayout',
  data () {
    return {
      isCollapse: false, // 是否展示或隐藏
    }
  },
  computed: {
    ...mapState(['userMenu']),
    activeMenu () {
      const route = this.$route
      const { meta, path } = route
      // 如果路由中设置了高亮，则取对应设置的值
      if (meta.activeMenu) {
        return meta.activeMenu
      }
      return path
    }
  },
  created () {
    // TODO [开发指引] 这是一条开发指引，你可以在开发过程中觉得此处是否需要保留
    if (!this.userMenu || !this.userMenu.length) {
      this.$notify.error({
        title: '错误',
        message: '未发现用户菜单，请先在user/auth页面获取用户菜单',
        duration: 0
      })
    }
  },
  methods: {
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    handleOpen (key, keyPath) {
      console.log('handleOpen', key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log('handleClose', key, keyPath)
    }
  }
}
</script>

<style scoped lang="scss">
  .logo {
    margin-bottom: 6px;
    display: flex;
    align-items: center;
    width: 100%;
    height: 50px;
    overflow: hidden;

    img {
      margin: 0 16px;
      width: 32px;
      height: 32px;
    }

    p {
      color: #fff;
      font-weight: 600;
      font-size: 16px;
      white-space: nowrap;
    }
  }

  .basic-layout-wrapper {
    width: 100%;
    height: 100%;
    background: #f5f5f5;
  }

  .basic-layout-horizontal {
    width: 100%;
    height: 100%;
    min-width: 1200px;
    display: flex;
    flex-direction: column;
  }

  .basic-layout-vertical {
    width: 100%;
    height: 100%;
    display: flex;
  }

  .basic-layout-menu:not(.el-menu--collapse) {
    width: 200px;
    flex-shrink: 0;
  }

  .basic-layout-menu {
    position: relative;
    box-shadow: 4px 0 6px 0 rgba(204, 204, 204, 0.1);
    z-index: 100;
    height: 100%;
    min-height: 100%;
  }

  .basic-layout-right {
    flex: 1;
    min-width: 1140px;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    overflow: auto;
  }

  .basic-layout-horizontal-top {
    background: #596a8a;
    display: flex;
    height: 50px;
    flex-shrink: 0;
    justify-content: space-between;
    overflow: hidden;
  }

  .basic-layout-horizontal-top-menu {
    flex: 1;
  }

  .basic-layout-horizontal-main {
    display: flex;
    width: 100%;
    flex: 1;
  }

  .basic-layout-right-top {
    height: 50px;
    flex-shrink: 0;
    background: #fff;
  }

  .avatar-wrapper {
    /* width: 200px; */
    padding-right: 20px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }

  .basic-layout-right-main {
    flex: 1;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    min-height: 100%;
    overflow: auto;
  }

  .basic-layout-vertical .basic-layout-right-main {
    min-height: calc(100% - 50px);
  }

  .basic-layout-menu-fold {
    position: absolute;
    z-index: 100;
    width: 12px;
    height: 80px;
    right: -12px;
    margin-top: -80px;
    top: 50%;
    cursor: pointer;

    &.unfold {
      background-image: url('../../assets/image/btn-fold.png');
    }

    &.fold {
      background-image: url('../../assets/image/btn-unfold.png');
    }
  }

</style>
<style lang="scss" scoped>
  /* 顶部横置菜单样式修改 */
  .el-menu-item {
    border-bottom-color: transparent !important;
  }

  ::v-deep .el-menu--collapse {
    .el-submenu {
      .el-submenu__icon-arrow {
        display: none;
      }
      & > .el-submenu__title {
        & > span {
          height: 0;
          width: 0;
          overflow: hidden;
          visibility: hidden;
          display: inline-block;
        }
      }
    }
  }

</style>
