<template>
  <div class="grading home">
    <div class="home-header">
      <view-header>
        <h1 slot="logo">{{ curSubject.name }}</h1>
        <div class="home-navs" slot="info">
          <ul>
            <li
              :class="{ act: curNav.name === nav.name }"
              v-for="(nav, index) in navs"
              :key="index"
              @click="switchNav(nav)"
            >
              <span class="nav-item-cont">{{ nav.title }}</span>
            </li>
          </ul>
        </div>
      </view-header>
    </div>

    <div class="home-body">
      <div class="home-main home-main-nofooter">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script>
import { navs } from "@/routers/grading";
import menuMixins from "@/components/homeMenuMixins";
import { subjectDetail } from "@/api";

export default {
  name: "grading",
  mixins: [menuMixins],
  data() {
    return {
      subjectId: this.$route.params.subjectId,
      navs: [],
      curNav: {},
      curSubject: { name: "" }
    };
  },
  watch: {
    $route: {
      handler(val) {
        this.actSubNav();
      }
    }
  },
  mounted() {
    this.buildNavs();
    this.actSubNav();
    this.getSubjectDetail();
  },
  methods: {
    async getSubjectDetail() {
      this.curSubject = await subjectDetail(this.subjectId);
    },
    buildNavs() {
      const user = this.$ls.get("user", {});
      this.navs = navs;
      if (user.role === "ADMIN") {
        this.navs = [{ title: "总览", name: "WorkOverview" }, ...navs];
      }
    },
    actSubNav() {
      const router = this.$route.matched.filter(
        item => item.name !== "Grading"
      )[0];
      this.actCurNav(router);
    }
  }
};
</script>
