<template>
  <div class="home">
    <el-container>
      <el-header>
        <div class="space_between_in base_head">
          <topNav></topNav>
          <UserInfo></UserInfo>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px;">
          <div class="card_aside">
            <div class="card_menu">
              <el-menu :default-active="activePath" class="el-menu-vertical-demo" router @open="handleOpen"
                @close="handleClose">
                <el-menu-item class="menu_item" v-for="(item,index) in menus" :index="item.path" :key="index"
                  @click="saveNavState(item.path)">
                  <i :class="item.meta.icon" class="menu_icon iconfont"></i>
                  <span>{{item.meta.title}}</span>
                </el-menu-item>
              </el-menu>
            </div>
            <div class="card_support">
              <span>技术服务:宝智达科技</span>
              <span style="margin-top: 5px;">服务电话:400-600-0968</span>
            </div>
          </div>
        </el-aside>
        <el-main>
          <div class="main">
            <router-view></router-view>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  import topNav from './topNav.vue'
  import UserInfo from './userInfo.vue'
  export default {
    name: 'BaseView',
    components: {
      topNav,
      UserInfo
    },
    data() {
      return {
        menus: [],
        // 被激活的链接地址
        activePath: '',
      }
    },
    computed: {
      ...mapState({
        // 动态主路由
        mainMenu: state => state.permission.menu
      })
    },
    created() {
      this.activePath = localStorage.getItem('activePath')
      this.getLeftMenu()
    },
    methods: {
      getLeftMenu() {
        const mRoutes = this.$store.state.permission.menu;
        let mRoutesList = []
        mRoutes.forEach(item => {
          if (item.name == 'index') {
            mRoutesList = item.children
          }
        })
        this.menus = getpath(mRoutesList, '')

        function getpath(routerMap, parent) {
          routerMap.map(item => {
            item.path = `${(parent.path) || ''}/${item.name}`
            if (item.children && item.children.length > 0) {
              return getpath(item.children, item)
            }
          })
          return routerMap
        }
      },
      handleOpen(key, keyPath) {
        // console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        // console.log(key, keyPath);
      },
      // 保存链接的激活状态
      saveNavState(activePath) {
        localStorage.setItem("activePath", activePath);
        this.activePath = activePath
      }
    }
  }
</script>
<style lang="scss" scoped>
  ::v-deep .el-container {
    height: calc(100vh - 60px);
  }

  .el-header {
    box-shadow: 0 5px 10px -2px #dfe5f8;
    z-index: 2;
  }

  .base_head {
    height: 100%;
  }

  .card_aside {
    width: 200px;
    height: calc(100% - 20px);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow-y: auto;
    padding-top: 20px;
  }

  .el-menu-vertical-demo {
    border-right: none !important;
  }

  .menu_item {
    display: flex;
    align-items: center;
  }

  .menu_icon {
    font-size: 22px !important;
    margin-right: 10px;
  }

  .card_support {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-bottom: 10px;
    color: rgb(204, 204, 204);
    padding-left: 20px;

    span {
      font-size: 14px;
    }
  }

  ::v-deep .el-main {
    background: #fcfbff;
  }

  .main {
    height: 100%;
  }
</style>
