import { login, logout, getInfo } from "@/api/user";
import { getToken, setToken, removeToken } from "@/utils/auth";
import { resetRouter } from "@/router";
import store from "..";

const getDefaultState = () => {
  return {
    token: getToken(),
    name: "",
    avatar: "",
    nickname: "",
    account: "",
    id: "",
    roles: "",
    staff_id: "",
    organization_ids: "",
    is_reset_password: ""
  };
};

const state = getDefaultState();

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState());
  },
  SET_TOKEN: (state, token) => {
    console.log(token, "data");
    state.token = token;
  },
  SET_NAME: (state, name) => {
    state.name = name;
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar;
  },
  SET_NICKNAME: (state, nickname) => {
    state.nickname = nickname;
    console.log(nickname, "data");
  },
  SET_ACCOUNT: (state, account) => {
    state.account = account;
  },
  SET_ID: (state, id) => {
    state.id = id;
    console.log(id, "data");
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles;
    console.log(roles, "data");
  },
  SET_STAFF: (state, staff_id) => {
    state.staff_id = staff_id;
    console.log(staff_id, "data");
  },
  SET_ORGANIZE: (state, organization_ids) => {
    state.organization_ids = organization_ids;
    console.log(organization_ids, "data");
  },
  SET_ISRESET_PWD: (state, is_reset_password) => {
    state.is_reset_password = is_reset_password;
  }
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo;
    return new Promise((resolve, reject) => {
      login({ account: username.trim(), password: password })
        .then(response => {
          const { data } = response;

          commit("SET_TOKEN", data.access_token);

          setToken(data.access_token);
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // get user info
  getInfo({ commit, state }) {
    console.log(state.token, "data");
    console.log(state, "state.token");
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then(response => {
          const { data } = response;
          //debugger
          if (!data) {
            return reject("Verification failed, please Login again.");
          }
          const {
            nickname,
            account,
            id,
            roles,
            staff_id,
            organization_ids,
            is_reset_password,
          } = data;
          console.log(data, "data4545");
          commit("SET_ID", id);
          commit("SET_ROLES", roles);
          commit("SET_STAFF", staff_id);
          sessionStorage.setItem("userId", id);
          commit("SET_NICKNAME", nickname);
          commit("SET_ACCOUNT", account);
          commit("SET_ORGANIZE", organization_ids);
          commit("SET_ISRESET_PWD", is_reset_password);
          console.log(store, "store");
          resolve(data);
        })
        .catch(error => {
          console.log(error, "error");
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          removeToken(); // must remove  token  first
          resetRouter();
          commit("RESET_STATE");
          commit("SET_ROLES", []);
          resolve();
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken(); // must remove  token  first
      commit("RESET_STATE");
      commit("SET_ROLES", []);

      resolve();
    });
  }
};

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