const user = {
  state() {
    return {
      token: undefined,
      name: undefined,
      id: undefined,
      phone: undefined,
      username: undefined,
      email: undefined,
      photo: undefined,
      role: undefined,
      signature: undefined,
      createdTime: undefined,
      valid: undefined,
    };
  },

  mutations: {
    state_reset_user: function (state) {
      state.token = undefined;
      state.name = undefined;
      state.id = undefined;
      state.phone = undefined;
      state.username = undefined;
      state.email = undefined;
      state.photo = undefined;
      state.role = undefined;
      state.signature = undefined;
      state.createdTime = undefined;
      state.valid = undefined;
    },
    state_set_user: function (state, data) {
      state.token = data.token;
      state.name = data.name;
      state.id = data.user.id;
      state.phone = data.user.phone;
      state.username = data.user.username;
      state.email = data.user.email;
      state.photo = data.user.photo;
      state.role = data.user.role;
      state.signature = data.user.signature;
      state.createdTime = data.user.createdTime;
      state.valid = data.user.valid;
    },
    storage_save_user: function (state) {
      sessionStorage.token = state.token;
      sessionStorage.name = state.name;
      sessionStorage.id = state.id;
      sessionStorage.username = state.username;
      sessionStorage.email = state.email;
      sessionStorage.photo = state.photo;
      sessionStorage.role = state.role;
      sessionStorage.signature = state.signature;
      sessionStorage.createdTime = state.createdTime;
      sessionStorage.valid = state.valid;
      sessionStorage.phone = state.phone;
    },
    storage_load_user: function (state) {
      state.token = sessionStorage.token;
      state.name = sessionStorage.name;
      state.id = sessionStorage.id;
      state.username = sessionStorage.username;
      state.email = sessionStorage.email;
      state.photo = sessionStorage.photo;
      state.role = sessionStorage.role;
      state.signature = sessionStorage.signature;
      state.createdTime = sessionStorage.createdTime;
      state.valid = sessionStorage.valid;
      state.phone = sessionStorage.phone;
    },
    storage_delete_user: function () {
      sessionStorage.removeItem("token");
      sessionStorage.removeItem("name");
      sessionStorage.removeItem("id");
      sessionStorage.removeItem("username");
      sessionStorage.removeItem("email");
      sessionStorage.removeItem("photo");
      sessionStorage.removeItem("role");
      sessionStorage.removeItem("signature");
      sessionStorage.removeItem("createdTime");
      sessionStorage.removeItem("valid");
    },
    update_user_name: function (state, data) {
      state.name = data;
    },
    update_user_email: function (state, data) {
      state.email = data;
    },
    update_user_signature: function (state, data) {
      state.signature = data;
    },
  },

  actions: {
    update_user_name: function ({ commit, state }, that) {
      that.$apis.saveNameAPI({ newName: that.newName })
        .then(res => {
          if (res.data.code === 0) {
            that.$message({ showClose: true, message: "修改成功", type: "success" });
            commit("update_user_name", that.newName);
            that.newName = "";
          }
        });
    },
    update_user_email: function ({ commit, state }, that) {
      that.$apis.saveEmailAPI({ newEmail: that.newEmail })
        .then(res => {
          if (res.data.code === 0) {
            that.$message({ showClose: true, message: "修改成功", type: "success" });
            commit("update_user_email", that.newEmail);
            that.newEmail = "";
          }
        });
    },
    update_user_signature: function ({ commit, state }, that) {
      that.$apis.saveSignatureAPI({ newSignature: that.newSignature })
        .then(res => {
          if (res.data.code === 0) {
            that.$message({ showClose: true, message: "修改成功", type: "success" });
            commit("update_user_signature", that.newSignature);
            that.newSignature = "";
          }
        });
    },
  },

  getters: {
    isTeacher(state) {
      return ["TEACHER"].indexOf(state.role) !== -1;
    },
    isStudent(state) {
      return ["STUDENT"].indexOf(state.role) !== -1;
    },
    isLogin(state) {
      return state.token !== undefined;
    },
    isStudentGroup: (state) => {
      return state.role === "STUDENT" ||
          state.role === "STAFF" ||
          state.role === "ASSISTANT" ||
          state.role === "SEEC" ||
          state.role === "SEEC";
    },
    isTeacherGroup: (state) => {
      return state.role === "TEACHER" || state.role === "HR";
    },
    isAssistantGroup: (state) =>
        state.role === "ASSISTANT" ||
        state.role === "SEEC" ||
        state.role === "SEECX",
    isAdmin: (state) => state.id === 0, // TODO: bad design,
  }
};

export default user;
