<template>
  <div class="home">
    <div class="home-header">
      <div class="head-back">
        <Button type="default" @click="toBack">返回</Button>
      </div>
      <div class="head-actions">
        <span class="action-icon" title="最小化" @click="minWin"
          ><Icon type="md-remove" size="16"
        /></span>
        <span class="action-icon" title="最大化" @click="maxWin"
          ><Icon type="md-browsers" size="16"
        /></span>
        <span class="action-icon" title="关闭" @click="close"
          ><Icon type="md-close" size="16"
        /></span>
        <span
          class="action-icon action-logout"
          @click="logout"
          title="退出"
          v-if="isDev"
        >
          <Icon type="md-power" size="16" />
        </span>
      </div>
      <div class="head-info">
        <span>{{ examName }}</span>
        <span v-if="curSubject && curSubject.name"></span>
        <span>{{ curSubject.name }}</span>
      </div>
    </div>
    <div class="home-body">
      <router-view />
    </div>
  </div>
</template>

<script>
import uploadTaskMixin from "../mixins/uploadTaskMixin";
import { mapState, mapMutations } from "vuex";
const { ipcRenderer } = require("electron");

export default {
  name: "home",
  mixins: [uploadTaskMixin],
  data() {
    return {
      showProgress: false,
      examName: "",
      isDev: process.env.NODE_ENV === "development",
      // 路由历史只前进，不后退
      backRouters: {
        Subject: "Login",
        Camera: "Subject",
        ScanArea: "Camera",
        CheckInfo: "ScanArea",
        GroupScan: "",
        LineScan: ""
      }
    };
  },
  computed: {
    ...mapState("client", ["curSubject", "clientConfig"])
  },
  created() {
    this.examName = this.$ls.get("user", { examName: "" }).examName;
    const scanBackRouter = this.clientConfig.paperStage
      ? "CheckInfo"
      : "ScanArea";
    this.backRouters.GroupScan = scanBackRouter;
    this.backRouters.LineScan = scanBackRouter;
    this.initUploadTask();
    this.setCurSubject({});
  },
  methods: {
    ...mapMutations("client", ["setCurSubject"]),
    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.$Modal.confirm({
          content: "当前正处于采集状态，确定要退出吗？",
          onOk: () => {
            this.$router.push({ name: backRouter });
          }
        });
      } else {
        this.$router.push({ name: backRouter });
      }
    },
    minWin() {
      ipcRenderer.send("minimize-window");
    },
    maxWin() {
      ipcRenderer.send("maximize-window");
    },
    close() {
      this.$Modal.confirm({
        content: "请确保当前窗口没有任务正在进行，确认要关闭当前窗口吗？",
        onOk: () => {
          ipcRenderer.send("close-window");
        }
      });
    }
  },
  beforeDestroy() {
    this.stopUpload();
  }
};
</script>
