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

interface SupplierSelectProps {
  value: number | null;
  onChange: (supplierId: number | null) => void;
}

export default function SupplierSelect({
  value,
  onChange,
}: SupplierSelectProps) {
  const { data } = useQuery(warehouseSupplierListOptions({ perPage: 100 }));
  const suppliers = data?.data ?? [];
  return (
    <select
      className="admin-form-select"
      value={value ?? ""}
      onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}
    >
      <option value="">-- bez dodavatele --</option>
      {suppliers.map((s: WarehouseSupplier) => (
        <option key={s.id} value={s.id}>
          {s.name}
          {s.ico ? ` (${s.ico})` : ""}
        </option>
      ))}
    </select>
  );
}
