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

const uris = {
  base: `${base}/devicetypep`,
  page: `${base}/devicetypep/page`,
  del: `${base}/devicetypep/deleteByIds`,
  param: {
    base: `${base}/devicetargetp`,
    page: `${base}/devicetargetp/page`,
    del: `${base}/devicetargetp/deleteByIds`
  },
  dictonary: {
    type: `${base}/devicedictionaryp/page`,
    param: `${base}/targetdictionaryp/page`
  }
}

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

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

export const updateDeviceType = (data: IDeviceType) =>
  axios.request<IRes<boolean>>({ url: uris.base, method: 'put', data })

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

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

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

export const addDeviceTypeParam = (data: Omit<IDeviceTypeParam, 'id'>) =>
  axios.request<IRes<boolean>>({ url: uris.param.base, method: 'post', data })

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

export const updateDeviceTypeParam = (data: IDeviceTypeParam) =>
  axios.request<IRes<boolean>>({ url: uris.param.base, method: 'put', data })

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

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

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

export const getTypeDictionary = () =>
  axios.request<IRes<IDeviceTypeDictionary[]>>({
    url: uris.dictonary.type,
    method: 'get',
    params: { current: 1, size: 9999 }
  })

export const getTypeParamDictionary = (typeId: number) =>
  axios.request<IRes<IDeviceTypeParamDictionary[]>>({
    url: uris.dictonary.param,
    method: 'get',
    params: { ulevel: 2, typeId, current: 1, size: 9999 }
  })
