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/land/land_property_owner_bloc/land_property_owner_bloc.dart';
import 'package:unit2/screens/offline/passo/land/edit/edit_land.dart';

import '../../../../../model/passo/land_property_owner.dart';
import '../../../../../widgets/passo/custom_button.dart';
import '../../../../../widgets/passo/custom_formBuilder_fields.dart';

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

class _LandPropertyOwnerInfoOfflineEdit
    extends State<LandPropertyOwnerInfoOfflineEdit> {
  final transaction_codes = ['New', 'Revision'];
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<LandPropertyOwnerBloc, LandPropertyOwnerState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        return FormBuilder(
          key: landOfflineKeyEdit,
          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: () {
            landOfflineKeyEdit.currentState!.save();
            debugPrint(landOfflineKeyEdit.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", TextInputType.number),
                  customTextField("Owner", "", "owner", TextInputType.text),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                            flex: 1,
                            child: customTextField(
                                "PIN", "", "pin", TextInputType.number)),
                        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.number))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField("OCT/TCT CLOA No.", "",
                              "cloa_no", TextInputType.number),
                        ),
                        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",
                              TextInputType.number),
                        ),
                        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", TextInputType.number)),
                        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", TextInputType.number)),
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField(
                              "Address", "", "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.", "", "tel_no", TextInputType.number))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField(
                              "Administrator/Beneficial User",
                              "",
                              "admin",
                              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", "", "admin_tin", TextInputType.number))
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField("Address", "", "admin_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.", "", "admin_telno",
                                TextInputType.number))
                      ]),
                  SizedBox(
                    height: 30,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      CustomButton(
                        icon: const Icon(Icons.chevron_right,
                            color: Colors.white),
                        onPressed: () {
                          var landOwner = LandPropertyOwner(
                              id: widget.land.id!,
                              transCode: landOfflineKeyEdit
                                  .currentState!.value['transaction_code']
                                  .toString(),
                              tdn: landOfflineKeyEdit
                                  .currentState!.value['td_no'],
                              cloaNo: landOfflineKeyEdit
                                  .currentState!.value['cloa_no'],
                              dated: landOfflineKeyEdit
                                  .currentState!.value['dated']
                                  .toString(),
                              assessedById: "1",
                              assessedByName: "cyril",
                              dateCreated: 'none',
                              dateModified: 'none',
                              pin:
                                  landOfflineKeyEdit.currentState!.value['pin'],
                              surveyNo: landOfflineKeyEdit
                                  .currentState!.value['survey_no'],
                              lotNo: landOfflineKeyEdit
                                  .currentState!.value['lot_no'],
                              blkNo:
                                  landOfflineKeyEdit.currentState!.value['blk'],
                              owner: landOfflineKeyEdit
                                  .currentState!.value['owner'],
                              address: landOfflineKeyEdit
                                  .currentState!.value['address'],
                              telno: landOfflineKeyEdit.currentState!.value['tel_no'],
                              tin: landOfflineKeyEdit.currentState!.value['tin'],
                              adminUser: landOfflineKeyEdit.currentState!.value['admin'],
                              adminAddress: landOfflineKeyEdit.currentState!.value['admin_address'],
                              adminTin: landOfflineKeyEdit.currentState!.value['admin_tin'],
                              faasType: "LAND",
                              adminTelno: landOfflineKeyEdit.currentState!.value['admin_telno']);
                          context.read<LandPropertyOwnerBloc>().add(
                              UpdateLandPropertyOwnerInfo(
                                  landPropertyOwner: landOwner,
                                  id: widget.land.id!));

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