import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:multiselect/multiselect.dart';
import 'package:unit2/bloc/passo/bulding/property_info/property_info_bloc.dart';
import 'package:unit2/model/passo/property_info.dart';
import 'package:unit2/theme-data.dart/btn-style.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

GlobalKey<FormBuilderState> keys = GlobalKey<FormBuilderState>();

class PropertyOwnerInfoEdit extends StatefulWidget {
  final int index;
  final PropertyInfo faas;
  final String title;
  final VoidCallback NextBtn;
  final VoidCallback PrevBtn;

  const PropertyOwnerInfoEdit(
      this.index, this.faas, this.title, this.NextBtn, this.PrevBtn);

  @override
  State<PropertyOwnerInfoEdit> createState() => _PropertyOwnerInfoEdit();
}

ButtonStyle secondaryBtnStyle(
    Color background, Color borderColor, Color overlay) {
  return ButtonStyle(
      elevation: MaterialStateProperty.all<double>(0),
      backgroundColor: MaterialStateProperty.all<Color>(background),
      overlayColor: MaterialStateProperty.all<Color>(overlay),
      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(8.0),
              side: BorderSide(
                width: 2,
                color: borderColor,
              ))));
}

class _PropertyOwnerInfoEdit extends State<PropertyOwnerInfoEdit> {
  // late List<String> selectedFoundation = widget.faas.foundations;
  // late List<String> selectedColumns = widget.faas.columns;
  // late List<String> selectedBeams = widget.faas.beams;
  // late List<String> selectedTFraming = widget.faas.truss_framing;
  // late List<String> selectedRoof = widget.faas.roof;
  // late List<String> selectedFlooring = widget.faas.flooring;
  // late List<String> selectedWallPartition = widget.faas.walls_and_partition;

  Map<String, dynamic> myMap = {'zero': 0, 'one': 1, 'two': 2};

  final transaction_codes = ['New', 'Revision'];

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<PropertyInfoBloc, PropertyInfoState>(
        listener: (context, state) {
      if (state is PropertyInfoLoaded) {
        final progress = ProgressHUD.of(context);
        progress?.dismiss();
      }
      if (state is PropertyInfoLoading) {
        final progress = ProgressHUD.of(context);
        progress!.showWithText("Please wait...");
      }

      if (state is PropertyInfoErrorState) {
        final progress = ProgressHUD.of(context);
        progress?.dismiss();
      }
    }, builder: (context, state) {
      if (state is PropertyInfoLoaded) {
        return SingleChildScrollView(
          scrollDirection: Axis.vertical,
          child: Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: Expanded(
                  child: Column(
                    children: <Widget>[
                      FormBuilder(
                          key: keys,
                          initialValue: {
                            'transaction_code': widget.faas.transCode,
                            'arp_td': widget.faas.tdn,
                            'pin': widget.faas.pin,
                            'owner': widget.faas.owner,
                            'address': widget.faas.address,
                            'tel_no': widget.faas.telno,
                            'tin': widget.faas.tin,
                            'benificiary': widget.faas.adminUser,
                            'benificiary_telno': widget.faas.adminTelno,
                            'benificiary_address': widget.faas.adminAddress,
                            'benificaiary_tin': widget.faas.adminTin,
                          },
                          enabled: true,
                          onChanged: () {
                            keys.currentState!.save();
                            debugPrint(keys.currentState!.value.toString());
                          },
                          autovalidateMode: AutovalidateMode.disabled,
                          skipDisabled: true,
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: <Widget>[
                                Container(
                                  margin: const EdgeInsets.only(
                                      left: 0, top: 20, right: 0, bottom: 10),
                                  child: const Text('PROPERTY OWNER INFO',
                                      style: TextStyle(
                                          fontWeight: FontWeight.bold,
                                          fontSize: 18),
                                      textAlign: TextAlign.left),
                                ),
                                const SizedBox(height: 15),
                                customDropDownField("Transaction Code", "",
                                    "transaction_code", transaction_codes),
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceEvenly,
                                  children: <Widget>[
                                    Expanded(
                                        // optional flex property if flex is 1 because the default flex is 1
                                        flex: 1,
                                        child: customTextField(
                                            "ARP No. / TD No.", "", 'arp_td')),
                                    const SizedBox(width: 10.0),
                                    Expanded(
                                        // optional flex property if flex is 1 because the default flex is 1
                                        flex: 1,
                                        child:
                                            customTextField("Pin", "", 'pin')),
                                  ],
                                ),
                                customTextField("Owner", "", 'owner'),
                                customTextField("Address", "", 'address'),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "Tel No.", "", 'tel_no'),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child:
                                              customTextField("TIN", "", 'tin'))
                                    ]),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField(
                                            "Administrator / Benificial User",
                                            "",
                                            'benificiary'),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child: customTextField(
                                              "TIN", "", 'benificiary_tin'))
                                    ]),
                                Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceEvenly,
                                    children: <Widget>[
                                      Expanded(
                                        flex: 1,
                                        child: customTextField("Address", "",
                                            'benificiary_address'),
                                      ),
                                      const SizedBox(width: 10.0),
                                      Expanded(
                                          // optional flex property if flex is 1 because the default flex is 1
                                          flex: 1,
                                          child: customTextField("Tel No.", "",
                                              'benificiary_telno'))
                                    ]),
                                const SizedBox(height: 25),
                                SizedBox(
                                  width: MediaQuery.of(context).size.width,
                                  child: CustomButton(
                                    icon: const Icon(Icons.chevron_right,
                                        color: Colors.white),
                                    onPressed: () {
                                      var property_info = PropertyInfo(
                                          id: widget.faas.id,
                                          transCode: keys.currentState!
                                              .value['transaction_code']
                                              .toString(),
                                          tdn: keys
                                              .currentState!.value['arp_td'],
                                          pin: keys.currentState!.value['pin'],
                                          owner:
                                              keys.currentState!.value['owner'],
                                          address: keys
                                              .currentState!.value['address'],
                                          telno: keys
                                              .currentState!.value['tel_no'],
                                          tin: keys.currentState!.value['tin'],
                                          adminUser: keys.currentState!
                                              .value['benificiary'],
                                          adminAddress: keys.currentState!
                                              .value['benificiary_address'],
                                          adminTin: keys.currentState!
                                              .value['benificiary_tin'],
                                          adminTelno: keys.currentState!
                                              .value['benificiary_telno'],
                                          assessedById: '1',
                                          assessedByName: 'Cyril',
                                          dateModified: DateTime.now(),
                                          dateCreated: DateTime.now());

                                      context.read<PropertyInfoBloc>().add(
                                          UpdatPropertyInfo(
                                              property_info: property_info));

                                      widget.NextBtn();
                                    },
                                  ),
                                ),
                              ])),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      }
      if (state is PropertyInfoErrorState) {
        return SomethingWentWrong(
          message: onError,
          onpressed: () {
            context.read<PropertyInfoBloc>().add(LoadPropertyInfo());
          },
        );
      }
      return Container();
    });
  }
}
