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

const app = {
  state: {
    device: 'phone',
    version: '1.0.0',
    kaptcha: '',
    publicKey: getPublicKey(),
    sid: '',
    cid: getCid(),
    overtimeModule: true,
    pageYOffset: 0
  },
  mutations: {
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SYS_CONFIG: (state, config) => {
      // state.kaptcha = config.kaptcha
      // state.sid = config.sid
      state.publicKey = config.publicKey
      setPublicKey(config.publicKey)
    },
    SET_CLIENTID: (state, cid) => {
      console.log('set cid: '+cid)
      state.cid = cid
      setCid(cid)
    },
    SET_PAGE_OFFSET(state, val){
      state.pageYOffset = val
    }
  },
  actions: {
    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 => {
          const data = {
            publicKey:'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCu5k8gcTFJy5UtMfmtKiR3f/EcUe93kzfteRj6+sM5fHRvOib82+uqQMjgaRZiMcr3HiHIFY5vA6c+Rfb5R2COitoOhxyA9U2eLXknzxLUQIAqXAvKQO8KnerA1Qjcds7xhJB2XPhWFlY4RVtMT2wr2lMd29QDE4F/kzyDhNBpxwIDAQAB'
          }
          commit('SYS_CONFIG',data)
          reject(error)
        })
      })
    }
  }
}

export default app
