import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';

/// A text input for the order name in the one-time order form.
class OneTimeOrderEventNameInput extends StatefulWidget {
  const OneTimeOrderEventNameInput({
    required this.label,
    required this.value,
    required this.onChanged,
    super.key,
  });

  final String label;
  final String value;
  final ValueChanged<String> onChanged;

  @override
  State<OneTimeOrderEventNameInput> createState() =>
      _OneTimeOrderEventNameInputState();
}

class _OneTimeOrderEventNameInputState
    extends State<OneTimeOrderEventNameInput> {
  late final TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController(text: widget.value);
  }

  @override
  void didUpdateWidget(OneTimeOrderEventNameInput oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.value != _controller.text) {
      _controller.text = widget.value;
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return UiTextField(
      label: widget.label,
      controller: _controller,
      onChanged: widget.onChanged,
      hintText: 'Order name',
      prefixIcon: UiIcons.briefcase,
    );
  }
}
