// import Cookies from 'js-cookie'
import { getConfig } from '@/api/login'
import { getRem, getPublicKey, setPublicKey } from '../../utils/auth'

const state = {
  device: 'desktop', // 当前设备
  rem: getRem(), // 1rem等于多少px
  publicKey: getPublicKey(), // 公钥
  kaptcha: '', // 验证码
  sid: '' // sid
}
const mutations = {
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SYS_CONFIG: (state, config) => {
    state.kaptcha = config.kaptcha
    state.publicKey = config.publicKey
    state.sid = config.sid
    setPublicKey(config.publicKey)
  },
  SET_REM: (state, rem)=>{
    state.rem = rem
  }
}

const actions = {
  ToggleSideBar: ({ commit }) => {
    commit('TOGGLE_SIDEBAR')
  },
  CloseSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  ToggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  // 获取系统配置
  GetConfig({ commit }) {
    return new Promise((resolve, reject) => {
      getConfig().then(response => {
        const data = response.data
        commit('SYS_CONFIG', data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 设置rem
  SetRem({commit}, rem){
    commit('SET_REM', rem)
  }
}

export default {
  namespace: true,
  state,
  mutations,
  actions
}
