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

export default {
  methods: {
    // initDb(name) {
    //   db.init(name);
    // },
    getLastLoginUser() {
      return db.getDict("lastLoginUser", "");
    },
    async setLastLoginUser(user) {
      await db.setDict("lastLoginUser", JSON.stringify(user));
    },
    async initStore() {
      const storeDict = await db.getAllDict();
      const needInitDict = [
        "startCountTime",
        "lastLoginUser",
        "domain",
        "schoolCode"
      ];
      for (let i = 0; i < needInitDict.length; i++) {
        if (!Object.prototype.hasOwnProperty.call(storeDict, needInitDict[i])) {
          await db.initDict(needInitDict[i]);
        }
      }

      const domain = storeDict["domain"] || "";
      this.$store.commit("setDomain", domain);
      this.$ls.set("domain", domain);

      const schoolCode = storeDict["schoolCode"] || "";
      this.$store.commit("setSchoolCode", schoolCode);
      this.$ls.set("schoolCode", schoolCode);
    }
  }
};
