import AdminModal from "./AdminModal";
import AdminButton from "./AdminButton";

interface AdminFormModalProps {
  isOpen: boolean;
  onClose: () => void;
  title: string;
  children: React.ReactNode;
  onSubmit?: () => void | Promise<void>;
  submitLabel?: string;
  cancelLabel?: string;
  isLoading?: boolean;
  size?: "sm" | "md" | "lg" | "xl";
}

export default function AdminFormModal({
  isOpen,
  onClose,
  title,
  children,
  onSubmit,
  submitLabel = "Save",
  cancelLabel = "Cancel",
  isLoading = false,
  size = "md",
}: AdminFormModalProps) {
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    if (onSubmit) {
      await onSubmit();
    }
  };

  return (
    <AdminModal
      isOpen={isOpen}
      onClose={onClose}
      title={title}
      size={size}
      footer={
        <>
          <AdminButton variant="secondary" onClick={onClose} disabled={isLoading}>
            {cancelLabel}
          </AdminButton>
          {onSubmit && (
            <AdminButton variant="primary" onClick={handleSubmit} isLoading={isLoading}>
              {submitLabel}
            </AdminButton>
          )}
        </>
      }
    >
      <form onSubmit={handleSubmit} className="space-y-5">
        {children}
      </form>
    </AdminModal>
  );
}
