"use strict";
const common_vendor = require("../common/vendor.js");
const api_index = require("../api/index.js");
const useStore = common_vendor.defineStore("main2", {
  state: () => {
    return {
      // 所有这些属性都将自动推断其类型
      isLogin: false,
      userInfo: null,
      access_token: "",
      historySearchWords: "",
      course_detail_id: 0,
      timer: null,
      surplus_seconds: null,
      // 倒计时
      test_time: 0,
      //测试用时
      test_timer: null,
      total_score: 0,
      status_bar_height: 0,
      debounceTimer: null,
      systemInfo: null,
      cartList: [],
      cartChange: false,
      remark_text: "",
      address: ""
    };
  },
  actions: {
    //应用初始化，在这里获取必要的基础信息
    appInit() {
      const { statusBarHeight } = common_vendor.index.getSystemInfoSync();
      this.status_bar_height = statusBarHeight;
    },
    countdownFunc(minutes) {
      this.surplus_seconds = minutes * 60;
    },
    saveToken(token) {
      common_vendor.index.setStorage({
        key: "access_token",
        data: token,
        success: function() {
          console.log("token was saved");
        }
      });
    },
    saveVisitState() {
      common_vendor.index.setStorage({
        key: "visitState",
        data: 1,
        success: function() {
          console.log("visitState was saved");
        }
      });
    },
    checkLogin() {
      let access_token = common_vendor.index.getStorageSync("access_token");
      console.log(access_token);
      if (access_token) {
        this.access_token = access_token;
        this.isLogin = true;
      }
    },
    async getUserInfo() {
      const res = await api_index.API.getUserInfo();
      if (res.code == 1) {
        this.userInfo = res.data;
        common_vendor.index.setStorageSync("userInfo", res.data);
      }
    },
    logout() {
      this.userInfo = null;
      this.isLogin = false;
      common_vendor.index.removeStorageSync("access_token");
      common_vendor.index.removeStorageSync("visitState");
    },
    clearStorageFunc() {
      common_vendor.index.removeStorageSync("surplus_seconds");
      common_vendor.index.removeStorageSync("test_time");
      common_vendor.index.removeStorageSync("saved_right_indexs_storage");
    },
    toLogin() {
      common_vendor.index.navigateTo({
        url: "/pages/user/login"
      });
    },
    showMsg(msg, type) {
      common_vendor.index.showToast({
        title: msg,
        icon: type || "none",
        duration: 2e3
      });
    },
    async getCartList() {
      const res = await api_index.API.getCartList();
      if (res.code === 1) {
        this.cartList = res.data;
      }
    }
  }
});
exports.useStore = useStore;
