import 'package:flutter/material.dart';
import 'package:telemednet/screens/patientDashboard/registrationScreens/patient_family_members_screen.dart';

class FamilyMembersEditScreen extends StatefulWidget {
  final FamilyMember? familyMember;

  const FamilyMembersEditScreen({super.key, this.familyMember});

  @override
  State<FamilyMembersEditScreen> createState() =>
      _FamilyMembersEditScreenState();
}

class _FamilyMembersEditScreenState extends State<FamilyMembersEditScreen> {
  late TextEditingController nameController;
  late TextEditingController relationController;
  late TextEditingController genderController;
  late TextEditingController dobController;

  @override
  void initState() {
    super.initState();
    nameController =
        TextEditingController(text: widget.familyMember?.name ?? '');
    relationController =
        TextEditingController(text: widget.familyMember?.relation ?? '');
    genderController =
        TextEditingController(text: widget.familyMember?.gender ?? '');
    dobController =
        TextEditingController(text: widget.familyMember?.dateOfBirth ?? '');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Family member'),
        actions: [
          TextButton(
            onPressed: () {
              FamilyMember newMember = FamilyMember(
                name: nameController.text,
                relation: relationController.text,
                gender: genderController.text,
                dateOfBirth: dobController.text,
              );
              Navigator.pop(context, newMember);
            },
            child: const Text('Done', style: TextStyle(color: Colors.blue)),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildTextField(nameController, 'Name'),
            _buildDropdownField('Relation', relationController.text,
                (String? newValue) {
              setState(() {
                relationController.text = newValue ?? '';
              });
            }),
            _buildDropdownField('Gender', genderController.text,
                (String? newValue) {
              setState(() {
                genderController.text = newValue ?? '';
              });
            }),
            _buildDateField(context),
            const SizedBox(height: 16),
            const Text('Address', style: TextStyle(fontSize: 16)),
            const SizedBox(height: 8),
            Card(
              child: ListTile(
                title: const Text('Enter address details'),
                trailing: const Icon(Icons.chevron_right),
                onTap: () {
                  // Navigate to address entry screen
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildTextField(TextEditingController controller, String label) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: TextField(
        controller: controller,
        decoration: InputDecoration(
          labelText: label,
          border: const OutlineInputBorder(),
        ),
      ),
    );
  }

  Widget _buildDropdownField(
      String label, String value, Function(String?) onChanged) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: DropdownButtonFormField<String>(
        decoration: InputDecoration(
          labelText: label,
          border: const OutlineInputBorder(),
        ),
        value: value.isEmpty ? null : value,
        onChanged: onChanged,
        items: <String>['Father', 'Mother', 'Son', 'Daughter']
            .map<DropdownMenuItem<String>>((String value) {
          return DropdownMenuItem<String>(
            value: value,
            child: Text(value),
          );
        }).toList(),
      ),
    );
  }

  Widget _buildDateField(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16),
      child: TextField(
        controller: dobController,
        decoration: const InputDecoration(
          labelText: 'Date of Birth',
          border: OutlineInputBorder(),
          suffixIcon: Icon(Icons.calendar_today),
        ),
        readOnly: true,
        onTap: () async {
          DateTime? pickedDate = await showDatePicker(
            context: context,
            initialDate: DateTime.now(),
            firstDate: DateTime(1900),
            lastDate: DateTime.now(),
          );
          if (pickedDate != null) {
            setState(() {
              dobController.text =
                  "${pickedDate.day}/${pickedDate.month}/${pickedDate.year}";
            });
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    nameController.dispose();
    relationController.dispose();
    genderController.dispose();
    dobController.dispose();
    super.dispose();
  }
}
