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: {
    bindKeyByQrCode ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.caseManager.bindKeyByQrCode.url, {
          method: api.caseManager.bindKeyByQrCode.method,
          urlData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    bindKeyByPhone ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.caseManager.bindKeyByPhone.url, {
          method: api.caseManager.bindKeyByPhone.method,
          urlData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    bindKeyer ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.caseManager.bindKey.url, {
          method: api.caseManager.bindKey.method,
          urlData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    getKeyList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.caseManager.getKeyListById.url, {
          method: api.caseManager.getKeyListById.method,
          queryData: { ...payload },
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    GetDeviceList ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.device.list.url, {
          method: api.device.list.method,
          queryData: { ...payload }
        }).then(res => {
          commit('updateList', res)
          resolve(res)
        }).catch(reject)
      })
    },
    GetDeviceByID ({ commit }, { deviceid }) {
      ajax(api.device.info.url, {
        method: api.device.info.method,
        urlData: {
          id: deviceid,
        }
      }).then(res => {
        commit('updateInfo', res)
      })
    },
    AddDevice ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.device.add.url, {
          method: api.device.add.method,
          data: {
            ...payload
          }
        }).then(res => {
          commit('updateInfo', res)
          resolve(res)
        }).catch(reject)
      })
    },
    UpdateDevice ({ commit }, payload) {
      return new Promise((resolve, reject) => {
        ajax(api.device.update.url, {
          method: api.device.update.method,
          data: {
            ...payload
          }
        }).then(res => {
          resolve(res)
        }).catch(reject)
      })
    },
    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)
    },
  }
}
