import Vue from 'vue'
import Vuex from 'vuex'
import { getMyButton } from '../utils/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    color: '#31C3A6',
    mode: true,
    // 按钮权限
    buttons: []
  },
  mutations: {
    setColor(state, data) {
      state.color = data
    },
    setMode(state, data){
      state.mode = data
    },
    SET_BUTTONS(state, data) {
      state.buttons = data
    }
  },
  actions: {
    setModeAsync(context, data) {
      context.commit('setMode' ,data)
    },
    setColorAsync(context, data) {
      context.commit('setColor', data)
    },
    // 获取用户按钮权限
    GetButtons({ commit }) {
      return new Promise((resolve, reject) => {
        getMyButton({userId: localStorage.getItem('UserId')}).then(res => {
          if(res.code === 10000) {
            commit('SET_BUTTONS', res.data)
            resolve(res.data)
          } else {
            reject(new Error(res.message))
          }
        }).catch(error => {
            reject(error)
          })
      })
    }
  },
  modules: {
  },
  getters: {
    buttons: state => state.buttons
  }
})
