import http from './http'
import type { SimpleFieldBatchPayload, SimpleFieldPayload, SimpleFieldRecord } from '@/types/simpleField'

function q(params?: Record<string, unknown>) {
  return { params }
}

export async function listSimpleFields(fieldType?: string, carType?: string, keyword?: string) {
  const { data } = await http.get<SimpleFieldRecord[]>('/simple-fields', q({
    fieldType,
    carType,
    q: keyword
  }))
  return data
}

export async function saveSimpleField(payload: SimpleFieldPayload) {
  const { data } = payload.id
    ? await http.put<SimpleFieldRecord>(`/simple-fields/${payload.id}`, payload)
    : await http.post<SimpleFieldRecord>('/simple-fields', payload)
  return data
}

export async function deleteSimpleField(id: string) {
  await http.delete(`/simple-fields/${id}`)
}

export async function saveSimpleFieldsBatch(payload: SimpleFieldBatchPayload) {
  const { data } = await http.post<{ count: number }>('/simple-fields/batch', payload)
  return data
}
