import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/screens/offline/passo/building/add/add_building.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';
import 'package:intl/intl.dart';
import '../../../../../bloc/offline/offline_passo/building/owner_info_bloc/crud_bloc.dart';

class PropertyInfoOfflinePage extends StatefulWidget {
  final VoidCallback handleButtonPress;
  final OfflineProfile offlineProfile;
  const PropertyInfoOfflinePage(this.handleButtonPress, this.offlineProfile,
      {super.key});

  @override
  _PropertyInfoPage createState() => _PropertyInfoPage();
}

class _PropertyInfoPage extends State<PropertyInfoOfflinePage> {
  int tempId = 0;
  final transaction_codes = ['New', 'Revision'];
  final DateTime now;
  final String formatter;
  bool isOptional = false;

  _PropertyInfoPage()
      : now = DateTime.now(),
        formatter = DateFormat.yMMMMd('en_US').format(DateTime.now());

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(20.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),
              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),
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      SizedBox(
                        width: 50, // Adjust the width as needed
                        height: 50, // Adjust the height as needed
                        child: Checkbox(
                          checkColor: Colors.white,
                          value: isOptional,
                          onChanged: (bool? value) {
                            setState(() {
                              isOptional = value!;
                            });
                          },
                        ),
                      ),
                      Text('Show optional information')
                    ],
                  ),
                  // Other widgets in the column
                ],
              ),
              Visibility(
                visible: isOptional,
                child: Column(
                  children: <Widget>[
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField(
                              "Tel No.", "", 'tel_no', TextInputType.phone),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                          flex: 1,
                          child: customTextField(
                              "TIN", "", 'tin', TextInputType.phone),
                        ),
                      ],
                    ),
                    const SizedBox(height: 10.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField(
                            "Administrator / Beneficial User",
                            "",
                            'benificiary',
                            TextInputType.text,
                          ),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                          flex: 1,
                          child: customTextField("TIN", "", 'benificiary_tin',
                              TextInputType.phone),
                        ),
                      ],
                    ),
                    const SizedBox(height: 10.0),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      children: <Widget>[
                        Expanded(
                          flex: 1,
                          child: customTextField("Address", "",
                              'benificiary_address', TextInputType.text),
                        ),
                        const SizedBox(width: 10.0),
                        Expanded(
                          flex: 1,
                          child: customTextField("Tel No.", "",
                              'benificiary_telno', TextInputType.phone),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 25),
              CustomButton(
                icon: const Icon(Icons.chevron_right, color: Colors.white),
                onPressed: () async {
                  final tempID = await SharedPreferences.getInstance();
                  // Dispatch the event to add the property_info
                  context.read<CrudBloc>().add(
                        AddTodo(
                            id: '1',
                            transCode: offlineBldgKey
                                    .currentState!.value['transaction_code']
                                    ?.toString() ??
                                ' ',
                            tdn: offlineBldgKey.currentState!.value['arp_td'] ??
                                ' ',
                            pin: offlineBldgKey.currentState!.value['pin'] ??
                                ' ',
                            fname:
                                offlineBldgKey.currentState!.value['fname'] ??
                                    ' ',
                            mname:
                                offlineBldgKey.currentState!.value['mname'] ??
                                    ' ',
                            lname:
                                offlineBldgKey.currentState!.value['lname'] ??
                                    ' ',
                            bday: offlineBldgKey.currentState!.value['bday']
                                    .toString() ??
                                ' ',
                            address:
                                offlineBldgKey.currentState!.value['address'] ??
                                    ' ',
                            telno:
                                offlineBldgKey.currentState!.value['tel_no'] ??
                                    ' ',
                            tin: offlineBldgKey.currentState!.value['tin'] ?? ' ',
                            adminUser: offlineBldgKey.currentState!.value['benificiary'] ?? ' ',
                            adminAddress: offlineBldgKey.currentState!.value['benificiary_address'] ?? ' ',
                            adminTin: offlineBldgKey.currentState!.value['benificiary_tin'] ?? ' ',
                            adminTelno: offlineBldgKey.currentState!.value['benificiary_telno'] ?? ' ',
                            faasType: "BUILDING",
                            assessedById: widget.offlineProfile.id.toString(),
                            assessedByName: widget.offlineProfile.firstName!,
                            dateCreated: formatter,
                            dateModified: ' ',
                            genCode: '5th'),
                      );
                  widget.handleButtonPress();
                },
              )
            ]),
      ),
    );
  }
}
