import {setLocalJSON, getLocalJSON, removeLocal} from "../storageIO";

let axios = null;
export default {
    name: "FORM_USER_GLOBAL",

    install(Vue,opts){
        axios = opts.axios;
    },
    clearLoginToken(){
        setLocalJSON('USERTOKEN', "");
        localStorage["Authorization" + (window.projectSuffix || "")] = "";
        axios.defaults.headers.common["Authorization"] = "";
    },
    setLoginToken(token){
        setLocalJSON('USERTOKEN', "pl_token_" + token);
        localStorage["Authorization" + (window.projectSuffix || "")] = "pl_token_" + token;
        axios.defaults.headers.common["Authorization"] = "pl_token_" + token;
    },

    getLoginUser() {
        return getLocalJSON('FORM_LOGINUSER') || {};
    },
    setLoginUser(user) {
        setLocalJSON('FORM_LOGINUSER', user);
    },

    logout(){
        removeLocal("LOGINUSER");
        removeLocal("USERTOKEN");
        localStorage.loginUser = null;
        localStorage["Authorization" + (window.projectSuffix || "")] = "";
        axios.defaults.headers.common["Authorization"] = "";
    },
}