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

// ignore: must_be_immutable
class DatePickerWidget extends StatefulWidget {
  final String label;
  final TextEditingController controller;
  late DateTime value;

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

  @override
  State<DatePickerWidget> createState() => _DatePickerWidgetState();
}

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

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text('${widget.label}:',
            style: GoogleFonts.inter(
              textStyle: const TextStyle(color: Colors.white, fontSize: 12, fontWeight: FontWeight.w500),
            )),
        const Gap(8),
        GestureDetector(
          onTap: () => {_selectDate(context)},
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 14),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.white),
              borderRadius: BorderRadius.circular(4),
              color: Colors.transparent,
            ),
            alignment: Alignment.centerLeft,
            child: Text(widget.controller.text.isNotEmpty ? widget.controller.text : 'Select Date',
                style: GoogleFonts.inter(
                    textStyle: const TextStyle(color: Color.fromRGBO(255, 255, 255, 1), fontSize: 16),
                    fontWeight: FontWeight.w500)),
          ),
        ),
      ],
    );
  }
}
