const state = () => ({
  openid: "",
  isReg: "",
  id: "",
  name: "",
  number: "",
  password: "",
  phone: "",
  status: "",
  type: "",
  username: "",
  userBasicInfo: {},
});

// getters
const getters = {
  // doneOpenid: (state, getters, rootState) => {
  // 	return state.openid
  // },
};

// actions
const actions = {
  async login({ commit, dispatch, state }, params) {
    const login_result = await uni.$u.api.getLoginApi({
      ...params,
    });

    if (login_result) {
      let { hroUserToken = {} } = login_result;
      let workai_vuex_token = "";

      if (hroUserToken && JSON.stringify(hroUserToken) != "") {
        workai_vuex_token = `Bearer ${hroUserToken.access_token}`;
      }

      let vuex_token = `Bearer ${login_result.access_token}`;
      uni.$u.vuex("vuex_token", vuex_token);
      uni.$u.vuex("workai_vuex_token", workai_vuex_token);
      uni.$u.vuex("vuex_phone", login_result.phone);
      uni.$u.vuex("vuex_userid", login_result.userId);
      uni.$u.vuex("vuex_refresh_token", login_result.refresh_token);

      switch (login_result.status) {
        case "not_reg": //未注册
          break;

        case "annulled": //已注销
          uni.$u.route({
            url: "/pages/student/my/written-off/written-off",
            type: "navigateTo",
          });
          break;

        case "annulling": //注销中
          uni.$u.route({
            url: "/pages/student/my/freezing/freezing",
            type: "navigateTo",
          });
          break;

        case "active": //已注册
          const userInfo = await dispatch(`getUserInfo`);
          const resume = await dispatch(`getUserResume`);

          if (userInfo) {
            const { student = {} } = userInfo;

            if (params.way == "code" || params.way == "name") {
              uni.$u.api.getactivenoticeUrl();
              login_result["loginType"] = "success";
              return login_result;
            }

            if (resume && resume.expect_job && resume.expect_job.length > 0) {
              uni.$u.vuex("vuex_expect_job", resume.expect_job[0].expect_job);
            }

            if (resume.id == "") {
              console.log(resume);
              uni.$u.route({
                url: "/pages/student/my/firstLogin-resume/firstLogin-resume",
                params: {
                  jump: "1",
                },
              });
            } else if (
              student &&
              JSON.stringify(student) != "{}" &&
              student.status == "verified"
            ) {
              uni.$u.api.getactivenoticeUrl();
              login_result["loginType"] = "success";
            } else {
              if (this.vuex_role == "student") {
                uni.$u.route({
                  url: `/pages/common/student-done/student-done`,
                  params: {},
                });
              } else {
                uni.$u.api.getactivenoticeUrl();
                login_result["loginType"] = "success";
              }
            }
          }

          break;
      }

      // uni.$u.api.editUserTypeApi({
      // 	phone: login_result.phone,
      // 	identity: 'c',
      // });

      return login_result;
    }
  },

  async login1({ commit, dispatch, state }, params) {
    let formType = "";
    if (params.formType) {
      formType = params.formType;

      Reflect.deleteProperty(params, "formType");
    }

    const login_result = await uni.$u.api.getLoginApi({
      ...params,
    });

    if (login_result) {
      let { hroUserToken = {} } = login_result;
      let workai_vuex_token = "";

      if (hroUserToken && JSON.stringify(hroUserToken) != "") {
        workai_vuex_token = `Bearer ${hroUserToken.access_token}`;
      }

      let vuex_token = `Bearer ${login_result.access_token}`;
      uni.$u.vuex("vuex_token", vuex_token);
      uni.$u.vuex("workai_vuex_token", workai_vuex_token);
      uni.$u.vuex("vuex_phone", login_result.phone);
      uni.$u.vuex("vuex_userid", login_result.userId);
      uni.$u.vuex("vuex_refresh_token", login_result.refresh_token);

      switch (login_result.status) {
        case "active": //已注册
          const userInfo = await dispatch(`getUserInfo`);

          if (userInfo) {
            const { student = {} } = userInfo;

            if (formType == "2") {
              uni.$u.route({
                url: "pages/student/tabBar/internship/internship",
                type: "switchTab",
              });
            } else if (formType == "3") {
            } else {
              if (
                student &&
                JSON.stringify(student) != "{}" &&
                student.status == "verified"
              ) {
                uni.$u.route({
                  url: "pages/student/tabBar/home/home",
                  // type: "switchTab",
                });
              }
            }
          }

          break;
      }
      return login_result;
    }
  },

  async loginOut({ commit, dispatch, state }, params) {
    uni.$u.vuex("vuex_user", {});
    uni.$u.vuex("vuex_tenant", {});
    uni.$u.vuex("vuex_token", "");
    uni.$u.vuex("vuex_userid", "");
    uni.$u.vuex("vuex_refresh_token", "");
    // await uni.clearStorageSync();

    // uni.$u.route('/pages/student/my/login/login');
    return;
  },

  async getUserInfo({ commit, dispatch, state }, params) {
    const userInfo = await uni.$u.api.getUserApi();
    await uni.$u.vuex("vuex_user", userInfo);

    return userInfo;
  },

  async getUserResume({ commit, dispatch, state }, params) {
    const resume = await uni.$u.api.getResumeDetailApi();
    // await uni.$u.vuex('vuex_user', userInfo);

    return resume;
  },
  async phoneLogin({ commit, dispatch, state }, params) {
    const res = await uni.$u.api.getLoginApi({
      ...params,
    });
    if (res.code && res.code > 300) {
    } else {
      const { user, refresh_token, access_token } = res;
      const { mobile, id: userId } = user;
      let vuex_token = `Bearer ${access_token}`;
      uni.$u.vuex("vuex_token", vuex_token);
      uni.$u.vuex("vuex_phone", mobile);
      uni.$u.vuex("vuex_userid", userId);
      uni.$u.vuex("vuex_refresh_token", refresh_token);
      uni.$u.vuex("vuex_user", user);
      res["loginType"] = "success";
    }
    return res;
  },
  async resumePhoneLogin({ commit, dispatch, state }, params) {
    const res = await uni.$u.api.resumeLogin({
      ...params,
    });
    if (res.code && res.code > 300) {
    } else {
      const { user, refresh_token, access_token } = res;
      const { mobile, id: userId } = user;
      let vuex_token = `Bearer ${access_token}`;
      uni.$u.vuex("vuex_token", vuex_token);
      uni.$u.vuex("vuex_phone", mobile);
      uni.$u.vuex("vuex_userid", userId);
      uni.$u.vuex("vuex_refresh_token", refresh_token);
      uni.$u.vuex("vuex_user", user);
      res["loginType"] = "success";
    }
    return res;
  },
};

// mutations
const mutations = {
  setUserInfo(state, userInfo) {
    state.id = userInfo.id;
    state.name = userInfo.name;
    state.phone = userInfo.phone;
    state.username = userInfo.username;
    state.userBasicInfo = userInfo.userBasicInfo;
  },
};

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
};