<template>
  <div class="top-menu">
    <el-menu :default-active="activeIndex" mode="horizontal" text-color="#333">
      <el-menu-item index="0" @click.native="openHome(itemHome)" key="0">
        <template slot="title" v-if="loginType !== 'Type-S'">
          <span>{{generateTitle(itemHome)}}</span>
        </template>
        <template slot="title" v-if="loginType === 'Type-S'">
          <span class="workTitle">{{nickName}}</span>
        </template>
      </el-menu-item>
      <template v-for="(item,index) in items">
        <el-menu-item :index="item.id+''" @click.native="openMenu(item)" :key="index">
          <template slot="title">
            <i :class="item.source" style="padding-right: 5px;"></i>
            <span>{{generateTitle(item)}}</span>
          </template>
        </el-menu-item>
      </template>
    </el-menu>
  </div>
</template>

<script>
import { mapGetters, mapState } from "vuex";

export default {
  name: "top-menu",
  data () {
    return {
      itemHome: {
        code: "wel",
        name: '欢迎进入德阳市智慧就业服务平台',
        source: 'iconfont iconhome',
      },
      activeIndex: "0",
      items: [],
    };
  },
  inject: ["index"],
  created () {
    this.getMenu();
  },
  computed: {
    ...mapState({
      nickName: (state) => state.user.userInfo.nick_name,
      loginType: (state) => state.user.userInfo.login_type,
    }),
    ...mapGetters(["tagCurrent", "menu"])
  },
  methods: {
    openHome (itemHome) {
      this.index.openMenu(itemHome);
      this.$router.push({
        path: this.$router.$avueRouter.getPath({ name: itemHome.name, src: '' }, {})
      });
    },
    openMenu (item) {
      this.index.openMenu(item)
    },
    getMenu () {
      this.$store.dispatch("GetTopMenu").then(res => {
        this.items = res;
      });
    },
    generateTitle (item) {
      return this.$router.$avueRouter.generateTitle(
        item.name,
        (item.meta || {}).i18n
      );
    },
  }
};
</script>
<style scoped>
.el-menu-item [class^="iconfont"] {
  margin-right: 5px;
  width: 24px;
  font-weight: 400;
  text-align: center;
  font-size: 14px;
  vertical-align: middle;
  color: rgb(51, 51, 51);
}
  .workTitle{
    width: 217px;
    height: 30px;
    font-size: 22px;
    font-weight: bold;
    color: #333333;
    line-height: 30px;
  }
</style>
