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/passo/land/land_property_owner_info/land_property_owner_info_bloc.dart';
import 'package:unit2/model/passo/land_property_owner.dart';
import 'package:unit2/screens/passo/Land/add_land.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

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

class LandPropertyOwnerInfoEdit extends StatefulWidget {
  Function NextBtn;
  LandPropertyOwner land;
  LandPropertyOwnerInfoEdit(this.NextBtn, this.land);
  @override
  _LandPropertyOwnerInfoEdit createState() => _LandPropertyOwnerInfoEdit();
}

class _LandPropertyOwnerInfoEdit extends State<LandPropertyOwnerInfoEdit> {
  final transaction_codes = ['New', 'Revision'];
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LandPropertyOwnerInfoBloc, LandPropertyOwnerInfoState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        return FormBuilder(
          key: landEditKey,
          initialValue: {
            'transaction_code': widget.land.transCode ?? '',
            'td_no': widget.land.tdn ?? '',
            'owner': widget.land.owner ?? '',
            'pin': widget.land.pin ?? '',
            'tin': widget.land.tin ?? '',
            'cloa_no': widget.land.cloaNo ?? '',
            'dated': widget.land.dated ?? '',
            'survey_no': widget.land.surveyNo ?? '',
            'lot_no': widget.land.lotNo ?? '',
            'blk': widget.land.blkNo ?? '',
            'address': widget.land.address ?? '',
            'tel_no': widget.land.telno ?? '',
            'admin': widget.land.adminUser ?? '',
            'admin_tin': widget.land.adminTin ?? '',
            'admin_address': widget.land.adminAddress ?? '',
            'admin_telno': widget.land.adminTelno ?? '',
          },
          enabled: true,
          onChanged: () {
            landEditKey.currentState!.save();
            debugPrint(landEditKey.currentState!.value.toString());
          },
          autovalidateMode: AutovalidateMode.disabled,
          skipDisabled: true,
          child: SingleChildScrollView(
              child: Padding(
            padding: const EdgeInsets.all(15.0),
            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),
                  customTextField("ARP No./ TD No.", "", "td_no"),
                  customTextField("Owner", "", "owner"),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                            flex: 1, child: customTextField("PIN", "", "pin")),
                        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(
                              "OCT/TCT CLOA No.", "", "cloa_no"),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                            // optional flex property if flex is 1 because the default flex is 1
                            flex: 1,
                            child: customDatTimePicker("Dated", "", "dated"))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField("Survey No.", "", "survey_no"),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                            // optional flex property if flex is 1 because the default flex is 1
                            flex: 1,
                            child: customTextField("Lot No.", "", "lot_no")),
                        const SizedBox(width: 10.0),
                        Expanded(
                            // optional flex property if flex is 1 because the default flex is 1
                            flex: 1,
                            child: customTextField("Blk", "", "blk")),
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField("Address", "", "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.", "", "tel_no"))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField(
                              "Administrator/Beneficial User", "", "admin"),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                            // optional flex property if flex is 1 because the default flex is 1
                            flex: 1,
                            child: customTextField("TIN", "", "admin_tin"))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child:
                              customTextField("Address", "", "admin_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.", "", "admin_telno"))
                      ]),
                  SizedBox(
                    height: 30,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      CustomButton(
                        icon: const Icon(Icons.chevron_right,
                            color: Colors.white),
                        onPressed: () {
                          var land = LandPropertyOwner(
                              id: widget.land.id,
                              transCode: landEditKey
                                  .currentState!.value['transaction_code']
                                  .toString(),
                              tdn: landEditKey.currentState!.value['td_no'],
                              cloaNo:
                                  landEditKey.currentState!.value['cloa_no'],
                              dated: landEditKey.currentState!.value['dated'],
                              assessedById: "1",
                              assessedByName: "cyril",
                              dateCreated:
                                  landEditKey.currentState!.value['dated'],
                              dateModified:
                                  landEditKey.currentState!.value['dated'],
                              pin: landEditKey.currentState!.value['pin'],
                              surveyNo:
                                  landEditKey.currentState!.value['survey_no'],
                              lotNo: landEditKey.currentState!.value['lot_no'],
                              blkNo: landEditKey.currentState!.value['blk'],
                              owner: landEditKey.currentState!.value['owner'],
                              address:
                                  landEditKey.currentState!.value['address'],
                              telno: landEditKey.currentState!.value['tel_no'],
                              tin: landEditKey.currentState!.value['tin'],
                              adminUser:
                                  landEditKey.currentState!.value['admin'],
                              adminAddress: landEditKey
                                  .currentState!.value['admin_address'],
                              adminTin:
                                  landEditKey.currentState!.value['admin_tin'],
                              // faasType: "LAND",
                              adminTelno: landEditKey
                                  .currentState!.value['admin_telno']);

                          context.read<LandPropertyOwnerInfoBloc>().add(
                              UpdateLandPropertyOwner(
                                  land_edit: land, id: widget.land.id!));

                          widget.NextBtn();
                        },
                      )
                    ],
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                ]),
          )),
        );
      },
    );
  }
}
