import axios from 'axios'
import store from '@/store/index'
import router from '@/router/index'
import { LoginQuit } from '@/api/account'

export const TOKEN_KEY = 'integrared-token'
export const cookieExpires = 1

export const setToken = (token) => {
    // Cookies.set(TOKEN_KEY, token, { expires: cookieExpires || 1 })
    sessionStorage.setItem(TOKEN_KEY, token)
}

/* 从缓存中获取token */
export const getToken = () => {
    const token = sessionStorage.getItem(TOKEN_KEY)
    if (token) return token
    else return false
}

/* 退出 */
export const logOut = () => {
    LoginQuit({
        roleKey: store.state.roleKey,
        token: getToken()
    })
    store.commit('setToken', '')
    store.commit('setHasGetInfo', false)
    // router.go(-1)
    router.replace({
        name: 'Home',
        query: {
            plcCode: sessionStorage.getItem('plcCode'),
            door: sessionStorage.getItem('door')
        }
    })
}

/**
 * @description 判断当前版本 用于解决项目部署后页面没有即使显示问题
 */
export const checkVersion = () => {
    axios.defaults.headers.get['Cache-Control'] = 'no-cache'
    axios.get('/version.json').then(res => {
        let version = String(res.data.version)
        console.log(version, localStorage.getItem('_version'))
        if (version !== localStorage.getItem('_version')) {
            console.log('版本更新')
            localStorage.setItem('_version', version)
            window.location.reload()
        }
    })
}

/**
 * @description 根据plcCode和door显示当前仓库
 */
export const getStoreTitle = (plcCode, door) => {
    if (!(plcCode && door)) return '未知仓库'
    let ary = ["*", "一", "二", "三", "四", "五", "六", "七", "八", "九"]
    if (plcCode == 2) {
        return `四楼面料仓库-${ door }号门`
    } else {
        return `${ plcCode || '*' }楼面料仓-${ door || '*' }号门`
    }
}
