import PersistedState from '../PersistedState'
// initial state
const state = {
  isOnline: PersistedState.getStorage('seller','isOnline',false),
  token: PersistedState.getStorage('seller','token'),
  info: PersistedState.getStorage('seller','info'),
  isTokenInvalid: PersistedState.getStorage('seller','isTokenInvalid',false)
}

// mutations
const mutations = {
  sellerLogin (state, payload) {
    state.info = payload.info
    state.info.invalid_time = parseInt((new Date().getTime()) / 1000) + 3600// 一小时更新
    state.isOnline = true
    state.token = payload.token
    state.isTokenInvalid = false
    PersistedState.setStorage('seller',state)
  },
  sellerLogout (state) {
    state.info = null
    state.isOnline = false
    state.token = null
    PersistedState.setStorage('seller',state)
  },
  sellerUpdate (state, payload) {
    if (state.info) {
      state.info = payload.info
      state.info.invalid_time = parseInt((new Date().getTime()) / 1000) + 3600// 一小时更新
      PersistedState.setStorage('seller',state)
    }
  },
  sellerEdit (state, payload) {
    if (state.info) {
      let i
      for (i in payload) {
        state.info[i] = payload[i]
      }
      PersistedState.setStorage('seller',state)
    }
  }
}

export default {
  state,
  mutations
}
