import 'package:date_time_picker/date_time_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter/src/widgets/placeholder.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:intl/intl.dart';
import 'package:unit2/bloc/profile/profile_bloc.dart';
import 'package:unit2/bloc/user/user_bloc.dart';
import 'package:unit2/model/profile/eligibility.dart';
import '../../../../model/location/country.dart' as c;
import '../../../../theme-data.dart/btn-style.dart';
import '../../../../theme-data.dart/colors.dart';
import '../../../../theme-data.dart/form-style.dart';
import '../../../../utils/global.dart';
import '../../../../utils/text_container.dart';

class EditEligibilityScreen extends StatefulWidget {
  final EligibityCert eligibityCert;
  const EditEligibilityScreen({super.key, required this.eligibityCert});

  @override
  State<EditEligibilityScreen> createState() => _EditEligibilityScreenState();
}

class _EditEligibilityScreenState extends State<EditEligibilityScreen> {
  final formKey = GlobalKey<FormBuilderState>();
  bool overseas = false;
  DateFormat dteFormat2 = DateFormat.yMMMMd('en_US');
  c.Country? selectedCountry;
  @override
  Widget build(BuildContext context) {
    return BlocBuilder<UserBloc, UserState>(
      builder: (context, state) {
        if (state is UserLoggedIn) {
          return BlocBuilder<ProfileBloc, ProfileState>(
            builder: (context, state) {
              if (state is EditEligibilityState) {
                return Center(
                  child: Padding(
                    padding: const EdgeInsets.symmetric(
                        vertical: 25, horizontal: 18),
                    child: FormBuilder(
                      key: formKey,
                      child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            FormBuilderTextField(
                              name: "eligibility",
                              initialValue:
                                  widget.eligibityCert.eligibility!.title!,
                              decoration: normalTextFieldStyle(
                                  "Eligibility", "Eligibility"),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            SizedBox(
                              width: screenWidth,
                              child: Row(
                                children: [
                                  Flexible(
                                    flex: 1,
                                    child: FormBuilderTextField(
                                      name: 'license number',
                                      initialValue:
                                          widget.eligibityCert.licenseNumber,
                                      decoration: normalTextFieldStyle(
                                          "license number", "license number"),
                                    ),
                                  ),
                                  const SizedBox(
                                    width: 12,
                                  ),
                                  Flexible(
                                    flex: 1,
                                    child: FormBuilderTextField(
                                      name: 'rating',
                                      initialValue: widget.eligibityCert.rating
                                          .toString(),
                                      decoration: normalTextFieldStyle(
                                          'rating', 'rating'),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            SizedBox(
                              width: screenWidth,
                              child: Row(
                                children: [
                                  Flexible(
                                      flex: 1,
                                      child: DateTimePicker(
                                        firstDate: DateTime(2000),
                                        lastDate: DateTime(2100),
                                        decoration: normalTextFieldStyle(
                                            "Exam date", "Exam date"),
                                        initialValue: widget
                                                    .eligibityCert.examDate ==
                                                null
                                            ? ''
                                            : dteFormat2.format(
                                                widget.eligibityCert.examDate!),
                                      )),
                                  const SizedBox(
                                    width: 12,
                                  ),
                                  Flexible(
                                    flex: 1,
                                    child: DateTimePicker(
                                      firstDate: DateTime(2000),
                                      lastDate: DateTime(2100),
                                      decoration: normalTextFieldStyle(
                                          "Validity date", "Validity date"),
                                      initialValue:
                                          widget.eligibityCert.validityDate ==
                                                  null
                                              ? ''
                                              : dteFormat2.format(widget
                                                  .eligibityCert.validityDate!),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            Text(
                              "Placement of Examination/Confinement",
                              style: Theme.of(context)
                                  .textTheme
                                  .displaySmall!
                                  .copyWith(fontSize: blockSizeVertical * 2),
                            ),
                            const SizedBox(
                              height: 12,
                            ),
                            FormBuilderSwitch(
                              initialValue: overseas,
                              activeColor: second,
                              onChanged: (value) {
                                setState(() {
                                  overseas = value!;
                                });
                              },
                              decoration: normalTextFieldStyle("", ''),
                              name: 'overseas',
                              title: const Text("Overseas Address?"),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                            SizedBox(
                                child: overseas == true
                                    ? FormBuilderDropdown<dynamic>(
                                        items: state.countries
                                            .map<DropdownMenuItem<c.Country>>(
                                                (c.Country country) {
                                
                                          return DropdownMenuItem<c.Country>(
                                              value: country,
                                              child: Text(country.name!));
                                        }).toList(),
                                        name: 'country',
                                        decoration: normalTextFieldStyle(
                                            "Country", "Country"),
                                        onChanged: (value) {
                                          setState(() {
                                            selectedCountry = value;
                                          });
                                        },
                                      )
                                    : Column(
                                        children: [
                                          FormBuilderDropdown(
                                            decoration: normalTextFieldStyle(
                                                "Region", "Region"),
                                            name: 'region',
                                            items: [],
                                            initialValue: widget
                                                        .eligibityCert
                                                        .examAddress
                                                        ?.cityMunicipality
                                                        ?.province
                                                        ?.region
                                                        ?.description ==
                                                    null
                                                ? 'region'
                                                : 'region',
                                          ),
                                          const SizedBox(
                                            height: 20,
                                          ),
                                          FormBuilderDropdown(
                                              decoration: normalTextFieldStyle(
                                                  'Province', "Province"),
                                              name: 'province',
                                              items: [],
                                              initialValue: widget
                                                          .eligibityCert
                                                          .examAddress
                                                          ?.cityMunicipality
                                                          ?.province
                                                          ?.description ==
                                                      null
                                                  ? 'region'
                                                  : 'pprovince'),
                                          const SizedBox(
                                            height: 20,
                                          ),
                                          FormBuilderDropdown(
                                            decoration: normalTextFieldStyle(
                                                "Municipality", "Municipality"),
                                            name: 'municipality',
                                            items: [],
                                            initialValue: widget
                                                        .eligibityCert
                                                        .examAddress
                                                        ?.cityMunicipality
                                                        ?.description ==
                                                    null
                                                ? 'region'
                                                : 'municipality',
                                          ),
                                        ],
                                      )),
                            const SizedBox(
                              height: 20,
                            ),
                            SizedBox(
                              width: screenWidth,
                              height: 60,
                              child: ElevatedButton(
                                  style: mainBtnStyle(
                                      primary, Colors.transparent, second),
                                  onPressed: () {},
                                  child: const Text(submit)),
                            ),
                            const SizedBox(
                              height: 20,
                            ),
                          ]),
                    ),
                  ),
                );
              }
              return Container();
            },
          );
        }
        return Container();
      },
    );
    ;
  }
}
