import 'package:flutter/material.dart';

import '../../utils/date_time_utils.dart';

class DatePickerButton extends StatefulWidget {
  final DateTime? date;
  final void Function(DateTime?) onSave;

  const DatePickerButton({super.key, required this.date, required this.onSave});

  @override
  State<DatePickerButton> createState() => _DatePickerButtonState();
}

class _DatePickerButtonState extends State<DatePickerButton> {
  late DateTime? selectedDate = widget.date;

  @override
  Widget build(BuildContext context) {
    return TextButton(
        onPressed: () => showDatePicker(
                    context: context,
                    initialDate: selectedDate,
                    firstDate: DateTime(2000),
                    lastDate: DateTime(2100))
                .then((value) {
              setState(() => selectedDate = value);
              widget.onSave(value);
            }),
        child: Text(
          selectedDate != null ? getPrettyDate(selectedDate) : "Vyber datum",
        ));
  }
}
