import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:gap/gap.dart';
import 'package:intl/intl.dart';

class DatePickerWidget extends StatelessWidget {
  final String label;
  final TextEditingController controller;
  // final VoidCallback onPressed;
  late DateTime value = DateTime.now();

  DatePickerWidget({super.key, required this.label, required this.controller, required this.value});

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
        context: context, initialDate: value, firstDate: DateTime(2015, 8), lastDate: DateTime(2101));
    if (picked != null && picked != value) {
      value = picked;
      final DateFormat formatter = DateFormat('MMMM dd, yyyy');
      controller.text = formatter.format(value);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('$label:',
            style: GoogleFonts.outfit(
              textStyle: const TextStyle(color: Colors.white, fontSize: 16),
            )),
        const Gap(8),
        TextField(
          controller: controller,
          decoration: InputDecoration(
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(10)),
          ),
          readOnly: true,
          style: GoogleFonts.outfit(textStyle: const TextStyle(color: Color.fromRGBO(255, 255, 255, 1), fontSize: 16)),
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 8),
              child: Container(
                decoration: BoxDecoration(
                  border: Border.all(color: const Color.fromRGBO(255, 255, 255, 1), width: 1),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: TextButton(
                  onPressed: () => {_selectDate(context)},
                  child: Text('Select Date',
                      style: GoogleFonts.outfit(
                        textStyle: const TextStyle(color: Colors.white, fontSize: 16),
                      )),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }
}
