<template>
  <div class="nav-menu">
    <div class="header-logo">
      <span>SAAS 管理平台</span>
    </div>
    <el-menu
      ref="menu"
      router
      mode="horizontal"
      :default-active="this.$route.path"
      text-color="#fff"
      style="flex: 1"
      background-color="#1565C0"
      @click.native="onClickMenus"
      active-text-color="#ffd04b"
    >
      <template v-for="(nav,index) in menuList">
        <el-menu-item
          :key="index"
          v-if="nav.children.length === 0"
          :index="nav.index"
        >
          <template slot="title">
            <i :class="nav.icon"></i>
            <span class="label">{{ nav.label }}</span>
          </template>
        </el-menu-item>
        <el-submenu :key="index" v-else :index="nav.index">
          <template slot="title">
            <i :class="nav.icon"></i>
            <span class="label">{{ nav.label }}</span>
          </template>
          <el-menu-item
            :index="subs.index"
            v-for="(subs, index) in nav.children"
            :key="index"
          >
            <template slot="title">
              <i :class="subs.icon"></i>
              <span class="label">{{
                subs.label
              }}</span></template
            >
          </el-menu-item>
        </el-submenu>
      </template>
    </el-menu>
    <div class="user-area">
      <span class="user-name">{{
        userInfo && userInfo.userName
      }}</span>
      <el-button
        type="text"
        style="color: #fff"
        @click="logout"
        >退出登录</el-button
      >
    </div>
  </div>
</template>
<script>
import { mapGetters, mapState } from 'vuex'
import { logout } from '@/common/api/login'
import { setToken, setUser } from '@/utils/auth'
export default {
  name: 'navMenu',
  data() {
    return {
      menuList: [
        {
          id: 1,
          path: '',
          label: 'ERP管理',
          icon: 'el-icon-menu',
          index: '/saas/manage',
          children: [],
        },
        {
          id: 3,
          path: '',
          label: 'ERP菜单管理',
          icon: 'el-icon-menu',
          index: '/saas/menu',
          children: [],
        },
        {
          id: 4,
          path: '',
          label: '工单管理',
          icon: 'el-icon-s-order',
          index: '/saas/taskManage',
          children: [],
        },
        {
          id: 2,
          path: '',
          label: '系统管理',
          icon: 'el-icon-setting',
          index: '/system',
          children: [
            {
              id: 4,
              path: '',
              label: '用户管理',
              icon: 'el-icon-s-order',
              index: 'user',
              children: [],
            },
            {
              id: 4,
              path: '',
              label: '角色管理',
              icon: 'el-icon-s-order',
              index: 'user',
              children: [],
            },
          ],
        },
      ],
    }
  },
  computed: {
    ...mapGetters('tags', ['currentTag']),
    ...mapState(['userInfo']),
  },
  watch: {
    currentTag(v) {
      if (!v) return
      const parent = this.menuList.find((e) => {
        if (e.path === v.path) return true
        if (e.children) {
          return e.children.some((ee) => ee.path === v.path)
        }
        return false
      })
      if (parent) {
        this.$refs.menu.open(parent.path)
      }
    },
  },
  methods: {
    async logout() {
      try {
        await logout()
      } catch (e) {
        console.error(e)
      }

      setUser(null)
      setToken('')
      localStorage.setItem('tags', null)
      this.$store.commit('tags/removeAllTags')
      this.$router.push('/login')
    },
    onClickMenus() {},
  },
}
</script>
<style lang="scss" scoped>
.nav-menu {
  display: flex;
  height: 60px;
  overflow: hidden;
  background-color: #1565c0;

  &::v-deep {
    .el-menu {
      border-bottom: none;
    }
    .el-submenu__title i {
      color: #fff;
    }
  }
  i {
    color: #fff;
  }
}

.header-logo {
  font-size: 22px;
  color: #fff;
  font-weight: bold;
  padding: 14px 20px;
  background-color: #1565c0;
}

.label {
  font-size: 16px;
}
.user-area {
  float: right;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  padding-right: 16px;
  color: #fff;
  .user-name {
    margin-right: 10px;
  }
}
</style>
