import { request } from "@/api/request";

/** 获取机柜列表（分页） */
export const fetchRackList = (data?: {
  page?: number;
  page_size?: number;
  keyword?: string;
  datacenter_id?: number;
  floor_id?: number;
  rack_type?: string;
  status?: string;
  sort?: string;
  order?: string;
}) => {
  return request.post("/Assets/v1/rack/list", data || {});
};

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

/** 根据楼层获取机柜列表（下拉，不分页） */
export const fetchRackListByFloor = (
  floorId: number,
  params?: { name?: string }
) => {
  return request.get(`/Assets/v1/rack/floor/${floorId}`, { params });
};

/** 获取机柜详情 */
export const fetchRackDetail = (id: number) => {
  return request.get(`/Assets/v1/rack/detail/${id}`);
};

/** 创建机柜 */
export const createRack = (data: any) => {
  return request.post("/Assets/v1/rack/create", data);
};

/** 更新机柜 */
export const updateRack = (data: any) => {
  return request.put("/Assets/v1/rack/update", data);
};

/** 删除机柜 */
export const deleteRack = (id: number) => {
  return request.delete(`/Assets/v1/rack/delete/${id}`);
};

/** 获取供应商列表（用于下拉选择） */
export const fetchSupplierList = () => {
  return request.get("/Assets/v1/supplier/all");
};

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

/** 获取楼层列表（用于下拉选择） */
export const fetchFloorListForSelect = (params?: { datacenter_id?: number; keyword?: string; name?: string }) => {
  const normalizedParams = params?.keyword
    ? { ...params, name: params.name ?? params.keyword }
    : params
  return request.get("/Assets/v1/floor/all", { params: normalizedParams });
};
