<template>
  <div class="grading home">
    <view-header>
      <div class="grading-subnav" slot="navSub">
        <div class="grading-title">
          <h1>{{ curSubject.name }}</h1>
          <span>当前阶段：{{ stepName }}</span
          ><span v-if="!IS_SCORE">进度：{{ stepProgress }}</span>
        </div>
        <div class="home-navs">
          <ul>
            <li
              :class="{ act: curNav.name === nav.name }"
              v-for="(nav, index) in navs"
              :key="index"
              @click="switchNav(nav)"
            >
              <i
                :class="[
                  'icon',
                  curNav.name === nav.name ? `${nav.icon}-act` : nav.icon
                ]"
                v-if="nav.icon"
              ></i>
              <span class="nav-item-cont">{{ nav.title }}</span>
            </li>
          </ul>
        </div>
      </div>
    </view-header>

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

        <view-footer></view-footer>
      </div>
    </div>
  </div>
</template>

<script>
import { subjectDetail, gradingProgressDetail } from "@/api";
import { grading, main } from "@/constants/authority";
import menuMixins from "@/components/homeMenuMixins";
import { SUBJECT_STAGE } from "@/constants/enumerate";

export default {
  name: "grading",
  mixins: [menuMixins],
  data() {
    return {
      subjectId: this.$route.params.subjectId,
      navs: [],
      curNav: {},
      curSubject: { name: "" },
      SUBJECT_STAGE,
      IS_SCORE: false,
      stepName: "",
      stepProgress: ""
    };
  },
  watch: {
    $route: {
      handler(val) {
        this.actSubNav();
      }
    }
  },
  mounted() {
    this.initData();
  },
  methods: {
    async initData() {
      this.curSubject = await subjectDetail(this.subjectId);
      this.IS_SCORE = this.curSubject.stage === "SCORE";
      this.getProgressDetail();
      this.stepName =
        this.curSubject.test === 2
          ? "试评"
          : this.SUBJECT_STAGE[this.curSubject.stage];
      this.buildNavs();
      this.actSubNav();
    },
    async getProgressDetail() {
      const data = await gradingProgressDetail({
        workId: this.curSubject.workId,
        subject: this.curSubject.subject
      });
      this.stepProgress = data.totalProgress.progress.toFixed(2) + "%";
    },
    buildNavs() {
      const user = this.$ls.get("user", {});
      this.navs = [...grading];
      if (user.role === "ADMIN") {
        this.navs.pop();
        this.navs = [...main.slice(0, 2), ...this.navs];
        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>
