import db from "../plugins/db";

export default {
  methods: {
    async initScanArea(subjects) {
      const storeDict = await db.getAllDict();
      const needInitDict = ["scanArea"];
      for (let key in needInitDict) {
        if (
          !Object.prototype.hasOwnProperty.call(storeDict, needInitDict[key])
        ) {
          await db.initDict(needInitDict[key]);
        }
      }

      const storeScanArea = await db.getDict("scanArea", "");
      const scanArea = storeScanArea ? JSON.parse(storeScanArea) : {};

      let newScanArea = {};
      subjects.map(subject => {
        let info = { ...subject };
        if (scanArea[subject.id] && scanArea[subject.id].collectConfig)
          info.collectConfig = scanArea[subject.id].collectConfig;

        newScanArea[subject.id] = info;
      });

      this.$store.commit("client/setScanArea", newScanArea);
    },
    async initStore() {
      const curDate = this.$store.state.client.startCountTime;

      const scanNo = await db.getScanCount(curDate);
      const uploadNo = await db.getUploadCount(curDate);

      this.$store.commit("client/setScanNo", scanNo);
      this.$store.commit("client/setUploadNo", uploadNo);
    }
  }
};
