import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:unit2/bloc/offline/offline_passo/building/owner_info_bloc/crud_bloc.dart';
import 'package:unit2/model/passo/property_info.dart';
import 'package:unit2/screens/offline/passo/building/edit/edit_building.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

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

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

  @override
  State<PropertyOwnerInfoEditOffline> createState() =>
      _PropertyOwnerInfoEditOffline();
}

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 _PropertyOwnerInfoEditOffline
    extends State<PropertyOwnerInfoEditOffline> {
  Map<String, dynamic> myMap = {'zero': 0, 'one': 1, 'two': 2};

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

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(15.0),
        child: Column(
          children: <Widget>[
            FormBuilder(
                key: offlineBldgEditKey,
                initialValue: {
                  'transaction_code': widget.faas.transCode,
                  'arp_td': widget.faas.tdn,
                  'pin': widget.faas.pin,
                  'fname': widget.faas.fname,
                  'mname': widget.faas.mname,
                  'lname': widget.faas.lname,
                  'bday': widget.faas.bday,
                  '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,
                  'benificiary_tin': widget.faas.adminTin,
                },
                enabled: true,
                onChanged: () {
                  offlineBldgEditKey.currentState!.save();
                  debugPrint(offlineBldgEditKey.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: Row(
                          children: [
                            Expanded(
                              child: const Text(
                                'PROPERTY OWNER INFO',
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 18),
                                textAlign: TextAlign.center,
                              ),
                            ),
                          ],
                        ),
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.red,
                          ),
                          onPressed: () {},
                          child: const Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Text('SAVE'), // <-- Text
                              SizedBox(
                                width: 5,
                              ),
                              Icon(
                                // <-- Icon
                                Icons.save,
                                size: 24.0,
                              ),
                            ],
                          ),
                        ),
                      ),
                      const SizedBox(height: 15),
                      customDropDownField(
                          widget.faas.transCode ?? "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', TextInputType.phone)),
                          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', TextInputType.phone)),
                        ],
                      ),
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: customTextField("First Name", "", 'fname',
                                  TextInputType.text),
                            ),
                            const SizedBox(width: 10.0),
                            Expanded(
                              // optional flex property if flex is 1 because the default flex is 1
                              flex: 1,
                              child: customTextField("Middle Name", "", 'mname',
                                  TextInputType.text),
                            ),
                            const SizedBox(width: 10.0),
                            Expanded(
                              // optional flex property if flex is 1 because the default flex is 1
                              flex: 1,
                              child: customTextField(
                                  "Last Name", "", 'lname', TextInputType.text),
                            )
                          ]),
                      customDatTimePicker("Birthday", "", "bday"),
                      customTextField(
                          "Address", "", 'address', TextInputType.text),
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: customTextField(
                                  "Tel No.", "", 'tel_no', TextInputType.phone),
                            ),
                            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', TextInputType.phone))
                          ]),
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: customTextField(
                                  "Administrator / Benificial User",
                                  "",
                                  'benificiary',
                                  TextInputType.text),
                            ),
                            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', TextInputType.phone))
                          ]),
                      Row(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Expanded(
                              flex: 1,
                              child: customTextField("Address", "",
                                  'benificiary_address', TextInputType.text),
                            ),
                            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', TextInputType.phone))
                          ]),
                      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: offlineBldgEditKey
                                        .currentState?.value['transaction_code']
                                        .toString() ??
                                    widget.faas.transCode,
                                assessedById: '1',
                                assessedByName: 'cyril',
                                tdn: offlineBldgEditKey.currentState!.value['arp_td'] ??
                                    widget.faas.tdn,
                                pin: offlineBldgEditKey.currentState!.value['pin'] ??
                                    widget.faas.pin,
                                fname: offlineBldgEditKey.currentState!.value['fname'] ??
                                    widget.faas.fname,
                                mname: offlineBldgEditKey.currentState!.value['mname'] ??
                                    widget.faas.mname,
                                bday: offlineBldgEditKey.currentState!.value['bday']
                                    .toString(),
                                lname: offlineBldgEditKey.currentState!.value['lname'] ??
                                    widget.faas.lname,
                                address: offlineBldgEditKey.currentState!.value['address'] ?? widget.faas.address,
                                telno: offlineBldgEditKey.currentState!.value['tel_no'] ?? widget.faas.telno,
                                tin: offlineBldgEditKey.currentState!.value['tin'] ?? widget.faas.tin,
                                adminUser: offlineBldgEditKey.currentState!.value['benificiary'] ?? widget.faas.adminUser,
                                adminAddress: offlineBldgEditKey.currentState!.value['benificiary_address'] ?? widget.faas.adminAddress,
                                adminTin: offlineBldgEditKey.currentState!.value['benificiary_tin'] ?? widget.faas.adminTin,
                                adminTelno: offlineBldgEditKey.currentState!.value['benificiary_telno'] ?? widget.faas.adminTelno,
                                faasType: "Building");

                            context.read<CrudBloc>().add(
                                UpdatePropertyOwnerInfo(
                                    id: widget.faas.id!,
                                    propertyInfo: property_info));

                            widget.NextBtn();
                          },
                        ),
                      ),
                    ])),
          ],
        ),
      ),
    );
  }
}
