<template>
  <div class="navbar">
    <div class="left-menu">
      <el-popover popper-class="down-popover" placement="bottom" width="360" :visible-arrow="false" trigger="click">
        <div class="corp-switch">
          <div class="current-corp">{{ tenantName }}</div>
          <el-button type="text" style="color: #00B899;" @click="showProcessInstance()">
            <i class="el-icon-s-operation"></i>
            切换企业/团队
          </el-button>

        </div>
        <i class="el-icon-s-grid icon-menu" slot="reference"></i>
      </el-popover>
      <div class="divider"></div>
      <a class="text" href="/">
        <img :src="logo" class="menu-icon dashboard" />
        <!-- <div class="menu-icon dashboard" style="background-image: url(&quot;https://g.jdycdn.com/app/pc/311a8e2b1ed9.png&quot;);"></div> -->
        工作台
      </a>
    </div>




    <div class="right-menu">
      <template v-if="device !== 'mobile'">
        <el-tooltip content="文档地址" effect="dark" placement="bottom">
          <ruo-yi-doc id="ruoyi-doc" class="right-menu-item hover-effect" />
        </el-tooltip>

        <el-tooltip content="联系我们" effect="dark" placement="bottom">
          <div class="right-menu-item hover-effect">
            <i class="el-icon-service" @click="childMethod()"></i>
          </div>

        </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">
          <!-- <img src="https://static-legacy.dingtalk.com/media/lQLPD4bTZ9hFfEHNAl_NAl-wu2M_jTTO8eEGNqKVr0MUAA_607_607.png" class="user-avatar"> -->
          <span v-if="nickname" class="user-nickname">{{ nickname }}</span>
          <!-- <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="companySetting()" v-loading.fullscreen.lock="fullscreenLoading">
            <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 RuoYiGit from '@/components/RuoYi/Git'
import RuoYiDoc from '@/components/RuoYi/Doc'
import NotifyMessage from '@/layout/components/Message'
import { getPath } from "@/utils/ruoyi";
import logoImg from '@/assets/logo/work.png'
import router from '@/router'
import {getListByMenuId} from "@/api/home";

export default {
  data() {
    return {
      fullscreenLoading:false,
      logo: logoImg
    }
  },
  components: {
    Breadcrumb,
    TopNav,
    Hamburger,
    Screenfull,
    SizeSelect,
    Search,
    RuoYiGit,
    RuoYiDoc,
    NotifyMessage
  },
  computed: {
    ...mapGetters([
      'sidebar',
      'avatar',
      'nickname',
      'device',
      'tenantName'
    ]),
    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: {
    showProcessInstance() {
      this.$parent.showTeamWork();
    },
    childMethod() {
      this.$parent.fatherMethod();
    },
    toggleSideBar() {
      this.$store.dispatch('app/toggleSideBar')
    },
    async companySetting() {
      let item = {
        id: 6,
        parentId: 5,
        path: "/system/companyInfo",
      }
      this.fullscreenLoading = true;
      getListByMenuId(item.id).then(response => {
        let menuList = response.data;
        const menus = [
          {
            "id": 6,
            "parentId": 5,
            "name": "企业管理",
            "path": "/system",
            "component": null,
            "componentName": null,
            "icon": "system",
            "visible": true,
            "keepAlive": true,
            "alwaysShow": true,
            "children": [
              {
                "id": 109,
                "parentId": 6,
                "name": "企业信息",
                "path": "companyInfo",
                "component": "system/companyInfo/index",
                "componentName": "SystemCompanyInfo",
                "icon": "tree-table",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              },
              {
                "id": 102,
                "parentId": 6,
                "name": "菜单管理",
                "path": "menu",
                "component": "system/menu/index",
                "componentName": "SystemMenu",
                "icon": "tree-table",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              },
              {
                "id": 103,
                "parentId": 6,
                "name": "部门管理",
                "path": "dept",
                "component": "system/dept/index",
                "componentName": "SystemDept",
                "icon": "tree",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              },
              {
                "id": 100,
                "parentId": 6,
                "name": "用户管理",
                "path": "user",
                "component": "system/user/index",
                "componentName": "SystemUser",
                "icon": "user",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              },
              {
                "id": 101,
                "parentId": 6,
                "name": "角色管理",
                "path": "role",
                "component": "system/role/index",
                "componentName": "SystemRole",
                "icon": "peoples",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              },

              {
                "id": 105,
                "parentId": 6,
                "name": "字典管理",
                "path": "dict",
                "component": "system/dict/index",
                "componentName": "SystemDictType",
                "icon": "dict",
                "visible": true,
                "keepAlive": true,
                "alwaysShow": true,
                "children": null
              }
            ]
          }
        ]
        localStorage.setItem("parentId", item.parentId)
        localStorage.setItem("menus", JSON.stringify(menuList))
        this.$store.dispatch('GenerateRoutes', menuList).then(accessRoutes => {
          console.log(accessRoutes);
          // 根据 roles 权限生成可访问的路由表
          router.addRoutes(accessRoutes) // 动态添加可访问路由表
        })
        setTimeout(() => {
          this.fullscreenLoading = false;
          this.$router.push({ path: item.path });
        }, 200);
      });
    },
    async logout() {
      this.$modal.confirm('确定注销并退出系统吗？', '提示').then(() => {
        this.$store.dispatch('LogOut').then(() => {
          location.href = getPath('/index');
        })
      }).catch(() => { });
    }
  }
}
</script>
<style lang="scss">
.down-popover {
  top: 60px !important;
}
</style>
<style lang="scss" scoped>
a {
  outline: none;
  text-decoration: none;
}

.corp-switch {
  align-items: center;
  display: flex;
  height: 65px;
  justify-content: space-between;
  padding-left: 20px;

  .current-corp {
    color: #141e31;
    font-size: 18px;
    line-height: 32px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 240px;
  }
}

.navbar {
  height: 60px;
  overflow: hidden;
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, .08);

  .hamburger-container {
    line-height: 46px;
    height: 100%;
    float: left;
    cursor: pointer;
    transition: background .3s;
    -webkit-tap-highlight-color: transparent;

    &:hover {
      background: rgba(0, 0, 0, .025)
    }
  }

  .breadcrumb-container {
    float: left;
  }

  .topmenu-container {
    position: absolute;
    left: 50px;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .left-menu {
    font-size: 16px;
    padding: 0px 15px;
    float: left;
    height: 100%;
    // line-height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;

    .icon-menu:hover {
      background: #e6e8ed;
      border-radius: 3px;
    }

    .icon-menu {
      color: #525967;
      cursor: pointer;
      display: inline-block;
      font-size: 20px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      width: 30px;
    }

    .divider {
      height: 20px;
      margin: 0 5px;
      border: solid #EBECEE;
      border-width: 0 0 0 1px;
      display: inline-block;
      height: .9em;
      // margin: 0 15px;
      vertical-align: middle;
    }

    .text:hover {
      background: #e6e8ed;
    }

    .text {
      height: 30px;
      align-items: center;
      border-radius: 4px;
      color: #141e31;
      display: flex;
      padding: 0 5px;
      transition: background .2s ease;

      .menu-icon {
        background-size: 100px 20px;
        height: 20px;
        margin-right: 5px;
        width: 20px;
      }
    }
  }

  .right-menu {
    float: right;
    height: 100%;
    line-height: 60px;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      padding: 0 8px;
      height: 100%;
      font-size: 18px;
      color: #5a5e66;
      vertical-align: text-bottom;

      &.hover-effect {
        cursor: pointer;
        transition: background .3s;

        &:hover {
          background: rgba(0, 0, 0, .025)
        }
      }
    }

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

      .avatar-wrapper {
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;

        .user-avatar {
          cursor: pointer;
          width: 35px;
          height: 35px;
          border-radius: 50%;
        }

        .user-nickname {
          margin-left: 5px;
          font-size: 14px;
        }

        .el-icon-caret-bottom {
          cursor: pointer;
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
