import { useQuery } from "@tanstack/react-query";
import {
  warehouseLocationListOptions,
  type WarehouseLocation,
} from "../../lib/queries/warehouse";

interface LocationSelectProps {
  value: number | null;
  onChange: (locationId: number | null) => void;
}

export default function LocationSelect({
  value,
  onChange,
}: LocationSelectProps) {
  const { data: locations } = useQuery(warehouseLocationListOptions());
  return (
    <select
      className="admin-form-select"
      value={value ?? ""}
      onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}
    >
      <option value="">-- bez lokace --</option>
      {locations?.map((loc: WarehouseLocation) => (
        <option key={loc.id} value={loc.id}>
          {loc.code} - {loc.name}
        </option>
      ))}
    </select>
  );
}
