<template>
  <div class="home">
    <div class="home-header">
      <div class="head-back">
        <i-button type="default" @click="toBack"
          ><i class="icon icon-left"></i>返回</i-button
        >
      </div>
      <div class="head-user">
        <span class="user-name"
          ><Icon type="md-person" size="16" /> {{ username }}</span
        >
        <span class="user-logout" @click="logout">
          <Icon type="md-power" size="20" />
        </span>
      </div>
      <div class="head-info">
        <span>{{ examName }}</span>
        <span>{{ curSubject.name }}</span>
      </div>
    </div>
    <div class="home-body">
      <router-view />
    </div>
  </div>
</template>

<script>
import uploadTaskMixin from "../mixins/uploadTaskMixin";
import { mapState } from "vuex";

export default {
  name: "home",
  mixins: [uploadTaskMixin],
  data() {
    return {
      showProgress: false,
      examName: "",
      // 路由历史只前进，不后退
      backRouters: {
        Camera: "Subject",
        ScanArea: "Camera",
        CheckInfo: "ScanArea",
        GroupScan: "",
        LineScan: ""
      }
    };
  },
  computed: {
    ...mapState("client", ["curSubject", "clientConfig"]),
    username() {
      return this.$store.state.user.name;
    }
  },
  created() {
    this.examName = this.$ls.get("user", { examName: "" }).examName;
    const scanBackRouter = this.clientConfig.isLevelKnown
      ? "CheckInfo"
      : "ScanArea";
    this.backRouters.GroupScan = scanBackRouter;
    this.backRouters.LineScan = scanBackRouter;
  },
  methods: {
    logout() {
      this.$ls.clear();
      this.$router.push({ name: "Login" });
    },
    toBack() {
      const curRouteName = this.$route.name;
      const backRouter = this.backRouters[curRouteName];
      if (curRouteName === "GroupScan" || curRouteName === "LineScan") {
        this.$confirm({
          content: "当前正处于采集状态，确定要退出吗?",
          onOk: () => {
            this.$router.push({ name: backRouter });
          }
        });
      } else {
        this.$router.push({ name: backRouter });
      }
    }
  },
  beforeDestroy() {
    this.stopUpload();
  }
};
</script>
