import db from "../plugins/db";
import { getLocalDate } from "../plugins/utils";

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

      this.$store.commit(
        "setCodeArea",
        storeDict.codeArea ? JSON.parse(storeDict.codeArea) : {}
      );
      this.$store.commit(
        "setTailorArea",
        storeDict.tailorArea ? JSON.parse(storeDict.tailorArea) : {}
      );
      this.$store.commit("setImageRotate", storeDict.imageRotate || 0);

      // 今日事今日毕的前提下，采集数和上传数只取当天的记录数。
      const curDate = getLocalDate();
      const scanNo = await db.countUploadList({
        createdTime: curDate
      });
      const uploadNo = await db.countUploadList({
        fininshTime: curDate,
        isUpload: 1
      });

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