<template>
  <div class="subject part-box">
    <div class="part-head">
      <h2>选择科目</h2>
    </div>
    <div class="subject-list">
      <ul>
        <li
          v-for="(item, index) in subjects"
          :key="index"
          @click="selectSubject(item)"
        >
          <span class="subject-name">{{ item.name }}</span>
          <span class="subject-icon"></span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import db from "../../../plugins/db";

export default {
  name: "subject",
  data() {
    return {
      subjects: []
    };
  },
  computed: {
    ...mapState("client", ["scanArea", "startCountTime"])
  },
  mounted() {
    const user = this.$ls.get("user");
    if (user && user.subjects) {
      this.subjects = user.subjects;
    }
  },
  methods: {
    ...mapMutations("client", ["setCurSubject"]),
    async selectSubject(subject) {
      const curSuject = this.scanArea[subject.id] || subject;
      this.$ls.set("curSuject", curSuject);
      this.setCurSubject(curSuject);

      // calc scanNo
      const curDate = this.startCountTime[subject.id];
      const scanNo = await db.getScanCount(curDate, subject.id);
      this.$store.commit("client/setScanNo", scanNo);

      this.$router.push({
        name: "Camera"
      });
    }
  }
};
</script>
