import { getAllAuthorities,addAuthority,deleteAuthority,updateAuthority } from '@/api/authority'




const state = {
  routes: [],
  addRoutes: [],
  allAuthorities:[]
}

const mutations = {
  SET_ALL_AUTHORITIES:(state,authorities)=>{
    console.log('save all authorities ',authorities)
    state.allAuthorities=authorities
  }
}

const actions = {
  getAllAuthorities({ commit }) {
    console.log('start to get all authorities 1')
    return new Promise((resolve,reject)=>{
      getAllAuthorities().then(res=>{

        console.log(' get all authorities',res)
        if(res.data.success)
        {
          commit('SET_ALL_AUTHORITIES',res.data.data)
        }
        resolve(res)
      }).catch(error=>{

        console.log('get all authorites faild!!!')
        reject(error)
      })
    })
  },
  addAuthority({commit},data)
  {
    console.log('start to add authority1',data)
    return new Promise((resolve,reject)=>{
      addAuthority(data).then(res=>{
        console.log('start to add authority2',res)
        resolve(res)
      }).catch(error=>{
        console.log('add authority failed!',data)
        reject(error)
      })
    })
  },
  updateAuthority({commit},data){
    console.log('start to update auth',data)
    return new Promise((resolve,reject)=>{
      updateAuthority(data).then(res=>{
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    })
  },
  deleteAuthority({commit},data){
    console.log('start to delete authority1',data)
    return new Promise((resolve,reject)=>{
    deleteAuthority(data).then(res=>{
       console.log('start to delete authority2',data)
        resolve(res)


    }).catch(error=>{
      console.log('delete authority failed!',data)
        reject(error)
    })
  })
  },
}

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