import { request } from '@/api/request'

export interface Floor {
  id?: number
  name?: string
  code?: string
  datacenter_id?: number
  floor_number?: number
  status?: string
  area?: number
  height?: number
  load_bearing?: number
  enabled?: boolean
  layout_plan?: string
  layout_plan_file_id?: string
  description?: string
}

export type SaveFloorPayload = Omit<Floor, 'layout_plan'>

interface ApiResponse<T> {
  code: number
  message: string
  details: T
}

/** 获取楼层列表（分页） */
export const fetchFloorList = (data?: { page?: number; page_size?: number; keyword?: string; datacenter_id?: number; status?: string }) => {
  return request.post('/Assets/v1/floor/list', data || {})
}

/** 获取楼层详情 */
export const fetchFloorDetail = (id: number) => {
  return request.get(`/Assets/v1/floor/detail/${id}`)
}

/** 根据数据中心获取楼层列表（下拉，不分页） */
export const fetchFloorListByDatacenter = (datacenterId: number, params?: { name?: string }) => {
  return request.get(`/Assets/v1/floor/datacenter/${datacenterId}`, { params })
}

/** 创建楼层 */
export const createFloor = (data: SaveFloorPayload) => {
  return request.post<ApiResponse<Floor>>('/Assets/v1/floor/create', data)
}

/** 更新楼层 */
export const updateFloor = (data: SaveFloorPayload) => {
  return request.put<ApiResponse<Floor>>('/Assets/v1/floor/update', data)
}

/** 删除楼层 */
export const deleteFloor = (id: number) => {
  return request.delete(`/Assets/v1/floor/delete/${id}`)
}

/** 获取数据中心列表（用于下拉选择） */
export const fetchDatacenterList = (params?: { keyword?: string }) => {
  return request.get('/Assets/v1/datacenter/all', { params })
}
