<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 { grading } from "@/constants/authority";
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.initData();
  },
  methods: {
    async initData() {
      this.curSubject = await subjectDetail(this.subjectId);
      this.buildNavs();
      this.actSubNav();
    },
    buildNavs() {
      const user = this.$ls.get("user", {});
      this.navs = [...grading];
      if (user.role === "ADMIN") {
        this.navs.pop();
        this.navs.unshift({ title: "总览", name: "WorkOverview" });
        if (this.curSubject.stage === "LEVEL") {
          this.navs[this.navs.length - 1].title = "打分分组";
        }
      } else {
        this.navs.splice(2, 2);
      }
    },
    actSubNav() {
      const router = this.$route.matched.filter(
        item => item.name !== "Grading"
      )[0];
      this.actCurNav(router);
    }
  }
};
</script>
