import { getChildren,getRootDevices,getChildrenDataSources,getAllDevices,addDevice,deleteDevice } from "@/api/device";
const state={
    childrenDevices:[],
    rootDevices:[],
    deviceList:[],
}
const mutations ={
    SET_ROOT_DEVICES:(state,rootDevices)=>{
        state.rootDevices=rootDevices
      },
    SET_DEVICE_LIST:(state,deviceList)=>{
        state.deviceList=deviceList
      },
}
const actions={
    addDevice({commit},data){
        console.log('start to add   device',data)
        return new Promise((resolve,reject)=>{
           addDevice(data).then(res=>{
                console.log('add device res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    deleteDevice({commit},data){
        console.log('start to delete   device',data)
        return new Promise((resolve,reject)=>{
           deleteDevice(data).then(res=>{
                console.log('delete device res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    getChildren({commit},data){
        console.log('start to get children  devices')
        return new Promise((resolve,reject)=>{
           getChildren(data).then(res=>{
                console.log('children devices res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    getRootDevices({commit},data){
        console.log('start to get root  devices')
        return new Promise((resolve,reject)=>{
           getRootDevices(data).then(res=>{
                console.log(' root devices res',res)
                commit('SET_ROOT_DEVICES',res.data.data)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    getAllDevices({commit},data){
        console.log('start to get all  devices')
        return new Promise((resolve,reject)=>{
           getAllDevices(data).then(res=>{
                console.log('device list res',res)
                commit('SET_DEVICE_LIST',res.data.data)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
    getChildrenDataSources({commit},data){
        console.log('start to get children  dataSources')
        return new Promise((resolve,reject)=>{
           getChildrenDataSources(data).then(res=>{
                console.log('children dataSources res',res)
                resolve(res)
            }).catch(err=>{
                reject(err)
            })
        })
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
  }