export default {
  state: {
    user: null,
    token: '',
    session: '',
    kid: 0,
    phone: '',
    ifConnectOrder: false, // socket连接订单正常
    ifReloadOrder: false, // 刷新订单列表
    carts: null// 购物车
  },
  getters: {
    user(state) {
      const user = uni.getStorageSync('user')
      return state.user || user ? JSON.parse(user) : {}
    },
    ifConnectOrder(state) {
      return state.ifConnectOrder
    },
    ifReloadOrder(state) {
      return state.ifReloadOrder
    },
    kid(state) {
      return state.kid || uni.getStorageSync('kid')
    },
    phone(state) {
      return state.phone || uni.getStorageSync('phone')
    },
    token(state) {
      return state.token || uni.getStorageSync('token')
    },
    session(state) {
      return state.session || uni.getStorageSync('session')
    },
    carts(state) {
      const carts = uni.getStorageSync('carts')
      return state.carts || carts ? JSON.parse(carts) : []
    }
  },
  mutations: {
    CLEAR(state) {
      state.user = null
      state.token = ''
      state.session = ''
      state.phone = ''
      state.kid = 0
      state.carts = null
      uni.clearStorageSync()
    },
    SET_USER(state, user) {
      state.user = user
      uni.setStorage({
        key: 'user',
        data: JSON.stringify(user)
      })
    },
    SET_PHONE(state, phone) {
      state.phone = phone
      uni.setStorage({
        key: 'phone',
        data: phone
      })
    },
    SET_KID(state, kid) {
      state.kid = kid
      uni.setStorage({
        key: 'kid',
        data: kid
      })
    },
    SET_TOKEN(state, token) {
      state.token = token
      uni.setStorage({
        key: 'token',
        data: token
      })
    },
    SET_SESSION(state, session) {
      state.session = session
      uni.setStorage({
        key: 'session',
        data: session
      })
    },
    SET_CARTS(state, carts) {
      state.carts = carts
      uni.setStorage({
        key: 'carts',
        data: JSON.stringify(carts)
      })
    },
    SET_CONNECT(state, bool) {
      state.ifConnectOrder = bool
    },
    SET_RELOAD(state, bool) {
      state.ifReloadOrder = bool
    }
  },
  actions: {
    clear({ commit }) {
      return new Promise((resolve, reject) => {
        commit('CLEAR')
        resolve(true)
      })
    },
    setConnect({ commit }, bool) {
      commit('SET_CONNECT', bool)
    },
    setReload({ commit }, bool) {
      commit('SET_RELOAD', bool)
    },
    setUser({ commit }, user) {
      commit('SET_USER', user)
    },
    setPhone({ commit }, phone) {
      commit('SET_PHONE', phone)
    },
    setKid({ commit }, kid) {
      return new Promise((resolve, reject) => {
        commit('SET_KID', kid)
        resolve(kid)
      })
    },
    setToken({ commit }, token) {
      return new Promise((resolve, reject) => {
        commit('SET_TOKEN', token)
        resolve(token)
      })
    },
    setSession({ commit }, session) {
      commit('SET_SESSION', session)
    },
    setCarts({ commit }, carts) {
      commit('SET_CARTS', carts)
    }
  }
}
