import ajax from '../../util/ajax'
import api from '../../util/api'

export default {
  namespaced: true,
  state: {
    deviceList: [],
    deviceInfo: {},
  },
  mutations: {
    updateList (state, payload) {
      state.deviceList = payload || []
    },
    updateInfo (state, payload) {
      state.deviceInfo = payload || {}
    },
  },
  actions: {
    GetDeviceList ({ commit }, payload) {
      ajax(api.device.list.url, {
        method: api.device.list.method,
        queryData: { ...payload }
      }).then(res => {
        commit('updateList', res)
      })
    },
    GetDeviceByID ({ commit }, { deviceid }) {
      ajax(api.device.info.url, {
        method: api.device.info.method,
        urlData: {
          id: deviceid,
        }
      }).then(res => {
        commit('updateInfo', res)
      })
    },
    AddDevice ({ commit }, payload) {
      ajax(api.device.add.url, {
        method: api.device.add.method,
        data: {
          ...payload
        }
      }).then(res => {
        commit('updateInfo', res)
      })
    },
    UpdateDevice ({ commit }, payload) {
      ajax(api.device.update.url, {
        method: api.device.update.method,
        data: {
          ...payload
        }
      }).then(res => {
      })
    },
    DelDevice ({ commit }, { id, callback }) {
      ajax(api.device.delete.url, {
        method: api.device.delete.method,
        urlData: {
          id: id,
        }
      }).then(res => {
        callback()
      })
    },
    SetDeviceNull ({ commit }) {
      commit('updateInfo', {})
    },
    UpdateInfo ({ commit }, info) {
      commit('updateInfo', info)
    },
  }
}
