import {storageIO} from "sasp-base";
import apiOutAdmin from "../api/apiOutAdmin";
let axios = null;

let {setLocalJSON, getLocalJSON, removeLocal} = storageIO;


export default {
    name: "CACHE_GLOBAL",

    DATA_CACHE: {},
    ARR_USER_INFO:[],//所有用户信息

    ARR_ALL_USER_INFO:[], //所有用户信息（包含已锁定）

    LOCK_USER_INFO:[], //锁定用户信息
    ARR_ALL_POS_USER_INFO:[], //所有组织岗位用户信息（包含已锁定）
    install(Vue,opts){
        axios = opts.axios;
    },

    clearCache(){
        this.ARR_USER_INFO=[];
        this.ARR_ALL_USER_INFO = [];
    },

    init() {
        this.DATA_CACHE = {};
    },

    getLoginUser() {
        return getLocalJSON('LOGINUSER') || {};
    },
    /*
    ** 将登录用户存入sessionStorage中
    */
    setLocalJSON(key,value){
        setLocalJSON(key, value);
    },
    setLoginUser(user, token) {
        setLocalJSON('LOGINUSER', user);
    },
    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;
    },

    clearLoginToken(token) { // 登陆前清空登录认证token
        setLocalJSON('USERTOKEN', "");
        localStorage["Authorization" + (window.projectSuffix || "")] = "";
        axios.defaults.headers.common["Authorization"] = "";
    },

    /**
     * 将登录用户的部门信息存入sessionStorage中
     * @param deptInfo
     */
    setLoginDept(deptInfo) {
        setLocalJSON('LOGINUSERDEPT', deptInfo);
    },

    clearLoginDept() { // 登陆前清空登录部门信息
        setLocalJSON('LOGINUSERDEPT', "");
    },

    /*
    ** 从sessionStorage中获取登录用户
    */
    getLoginToken() {
        return getLocalJSON('USERTOKEN');
    },
    /*
    ** 从sessionStorage中获取登录用户
    */

    getLocalJSON(key){
        return getLocalJSON(key);
    },
    /*
    ** 将登录用户存入sessionStorage中
    */
    setSysDataObj(sysdata,sourceKey) {
        setLocalJSON('SYSDATA_'+sourceKey, sysdata);
    },

    /*
    ** 从sessionStorage中获取登录用户
    */
    getSysDataObj(sourceKey) {
        return getLocalJSON('SYSDATA_' + sourceKey);
    },


    /**
     * 从sessionStorage中获取登录用户的部门信息
     * @returns {*}
     */
    getLoginDept() {
        return getLocalJSON('LOGINUSERDEPT');
    },

    /**
     * 获取纯前端地址
     */
    getWebUrl(route) {
        let path = route.path;
        return location.href.substring(0, location.href.lastIndexOf(path));
    },

    /**
     * 查询所有组织岗位用户信息
     */
    getAllPosUser(isShowLock){
        return new Promise(resolve => {
            let vm=this;
            if(vm.ARR_ALL_POS_USER_INFO.length == 0){ // LOCK_USER_INFO
                axios.post(apiOutAdmin.apis.sysOrgPosUser.findNoPage).then(function (res) {
                    axios.post(apiOutAdmin.apis.sysUser.getOnlyUsers,{"userType":"ent","order":"USER_NAME_QP asc"}).then(function (resUser) {
                        (resUser.data ||[]).forEach(item =>{
                            if(item.userStatus != '10'){
                                vm.LOCK_USER_INFO.push({
                                    orgName:"",
                                    userName:item.userName,
                                    userId:item.id,
                                    isLockUser:true
                                });
                            }
                        });

                        vm.ARR_ALL_POS_USER_INFO = res.data || [];
                        if(isShowLock){
                            resolve(vm.ARR_ALL_POS_USER_INFO.concat(vm.LOCK_USER_INFO));
                        }else{
                            resolve(vm.ARR_ALL_POS_USER_INFO);
                        }
                    });
                });
            }else{
                if(isShowLock){
                    resolve(vm.ARR_ALL_POS_USER_INFO.concat(vm.LOCK_USER_INFO));
                }else{
                    resolve(vm.ARR_ALL_POS_USER_INFO);
                }
            }
        });
    },
};
