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

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

    install(Vue,opts){
        axios = opts.spcpFormAxios;
    },
    clearLoginToken(){
        setLocalJSON('USERTOKEN', "");
        localStorage["Authorization" + (window.projectSuffix || "")] = "";
        axios.defaults.headers.common["Authorization"] = "";
    },
    setLoginToken(token){
        let customToken = window.customToken;
        if(!customToken) {
            customToken = "pl_token_";
        }
        setLocalJSON('USERTOKEN', customToken + token);
        localStorage["Authorization" + (window.projectSuffix || "")] = customToken + token;
        axios.defaults.headers.common["Authorization"] = customToken + 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"] = "";
    },
}
