import { serialize } from 'object-to-formdata'
import { IQueryCommon, IRes, IResult } from '@/api/common'
import axios from '@/utils/request'
import { base, IDevice } from './common'

const uris = {
  base: `${base}/collectdevice`,
  page: `${base}/collectdevice/page`,
  del: `${base}/collectdevice/deleteByIds`
}

export const addDevice = (data: Omit<IDevice, 'id'>) =>
  axios.request<IRes<boolean>>({
    url: uris.base,
    method: 'post',
    data: serialize(data, { dotsForObjectNotation: true, noFilesWithArrayNotation: true })
  })

export const deleteDevice = (id: string) =>
  axios.request<IRes<boolean>>({ url: `${uris.base}/${id}`, method: 'delete' })

export const updateDevice = (data: IDevice) =>
  axios.request<IRes<boolean>>({
    url: uris.base,
    method: 'put',
    data: serialize(data, { dotsForObjectNotation: true, noFilesWithArrayNotation: true })
  })

export const getDevice = (id: string) => axios.request<IResult<IDevice>>({ url: `${uris.base}/${id}`, method: 'get' })

export const devicePage = (params: Partial<IDevice & IQueryCommon>) =>
  axios.request<IRes<IDevice[]>>({ url: uris.page, method: 'get', params })

export const deleteDeviceBatch = (ids: string) =>
  axios.request<IRes<boolean>>({ url: uris.del, method: 'delete', params: { ids } })
