import { apiBarList } from '../../views/place/list/api'
import { Toast } from 'vant'

const state = {
  phone: '', // 登录手机号
  token: '', // APP登录
  BarList: [], // 所有酒吧列表
  objCurrentBarInfo: {} // 当前所在酒吧
}

const getters = {
  phone (state) {
    return state.phone
  },
  token (state) {
    return state.token
  },
  BarList (state) {
    return state.BarList
  },
  objCurrentBarInfo (state) {
    return state.objCurrentBarInfo
  }
}

const mutations = {
  UPDATE_PHONE (state, value) {
    state.phone = value
  },
  UPDATE_TOKEN (state, value) {
    state.token = value
  },
  UPDATE_BARLIST (state, value) {
    state.BarList = value
  },
  UPDATE_OBJCURRENTBARINFO (state, value) {
    state.objCurrentBarInfo = value
  }
}

const actions = {
  async fetchBarList ({ commit }) {
    try {
      const { data, status, msg } = await apiBarList()
      if (status) {
        if (data.length) {
          commit('UPDATE_BARLIST', data)
          commit('UPDATE_OBJCURRENTBARINFO', data[0])
        } else {
          Toast('门店不存在')
        }
      } else {
        Toast(msg)
      }
    } catch (err) {
      console.log(err)
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
