import { serialize } from 'object-to-formdata'
import { IQueryCommon, IRes, IResult } from '@/api/common'
import axios from '@/utils/request'
import { base, IPoint, IPointDismountParams, IPointEnableParams, IPointDetail, IPointDetailResult } from './common'

const uris = {
  base: `${base}/surveysite`,
  page: `${base}/surveysite/list`,
  save: `${base}/surveysite/saveAll`,
  update: `${base}/surveysite/updateAll`,
  del: `${base}/surveysite/deleteByIds`,
  detail: `${base}/surveysite/details`,
  discard: `${base}/surveysite/toVoid`,
  list: `${base}/surveysite/list`,
  enable: `${base}/sitestatus/bachSave`,
  dismount: `${base}/deviceinstall/dismantleDevice`
}

export const addPoint = ({ fileList, ...rest }: Partial<IPointDetail>) =>
  axios.request<IRes<boolean>>({
    url: uris.save,
    method: 'post',
    data: serialize(
      { fileList, dto: new Blob([JSON.stringify(rest)], { type: 'application/json' }) },
      { dotsForObjectNotation: true, noFilesWithArrayNotation: true }
    )
  })

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

export const updatePoint = ({ fileList, ...rest }: Partial<IPointDetail>) =>
  axios.request<IRes<boolean>>({
    url: uris.update,
    method: 'put',
    data: serialize(
      { fileList, dto: new Blob([JSON.stringify(rest)], { type: 'application/json' }) },
      { dotsForObjectNotation: true, noFilesWithArrayNotation: true }
    )
  })

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

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

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

export const getDetail = (id: number) =>
  axios.request<IResult<IPointDetailResult>>({ url: `${uris.detail}/${id}`, method: 'get' })

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

export const getAllPoints = (params: IPoint & IQueryCommon) =>
  axios.request<IRes<IPoint>>({ url: uris.list, method: 'get', params })

export const enablePointBatch = (data: IPointEnableParams) =>
  axios.request<IResult<boolean>>({ url: uris.enable, method: 'post', data })

export const dismountPoint = (data: IPointDismountParams) =>
  axios.request<IResult<boolean>>({ url: uris.dismount, method: 'post', data })
