import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:searchfield/searchfield.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/memoranda/memoranda_admin_bloc.dart';
import 'package:unit2/bloc/offline/offline_passo/admin/signatories/signatories_admin_bloc.dart';
import 'package:unit2/model/offline/offline_profile.dart';
import 'package:unit2/screens/offline/passo/building/add/add_building.dart';
import 'package:accordion/accordion.dart';
import 'package:accordion/controllers.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

import '../../../../../bloc/offline/offline_passo/building/additional_items_offline/additional_items_offline_bloc.dart';
import '../../../../../bloc/offline/offline_passo/building/assessment_offline/bldg_assessment_offline_bloc.dart';
import '../../../../../bloc/offline/offline_passo/building/building_and_structure/building_and_structure_bloc.dart';
import '../../../../../model/passo/additional_items.dart';
import '../../../../../model/passo/building_and_structure.dart';
import '../../../../../model/passo/memoranda.dart';
import '../../../../../model/passo/signatories.dart';
import '../../../../../theme-data.dart/colors.dart';
import '../../../../../theme-data.dart/form-style.dart';
import 'package:intl/intl.dart'; // Import the intl package

class PropertyAssessmentOfflinePage extends StatefulWidget {
  Function function;
  final OfflineProfile offlineProfile;

  PropertyAssessmentOfflinePage(this.function, this.offlineProfile);

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

class _PropertyAssessmentOfflinePage
    extends State<PropertyAssessmentOfflinePage> {
  double assessment_level = 0;
  bool isTaxable = false;
  bool isExempt = false;
  String _memoranda = "";
  String _notes = "";
  String appraised_by = "";
  String rec_by = "";
  String approved_by = "";
  String appraised_by_designation = "";
  String rec_by_designation = "";
  String approved_by_designation = "";
  final focus = FocusNode();
  final focuss = FocusNode();
  final appraisedByFocus = FocusNode();
  final recByFocus = FocusNode();
  final apprvdByFocus = FocusNode();

  final quarter = ['1st', '2nd', '3rd', '4th'];

  TextEditingController memorandaController = TextEditingController();
  TextEditingController noteController = TextEditingController();

  String assessmentLevel(marketValues, property_class) {
    final marketValue = marketValues;
    switch (property_class) {
      case 'Residential':
        if (marketValue < 175000) {
          // setState(() {
          //   assessment_level = 0;
          // });
          return '0 ';
        } else if (marketValue < 300000 && marketValue > 175000) {
          // setState(() {
          //   assessment_level = 0.10;
          // });
          return '10 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.20;
          // });
          return '20 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.25;
          // });
          return '25 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        }
        break;
      case 'Agricultural':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return '45 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 750000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return '55 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return '65 ';
        } else if (marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        }
        break;
      case 'Commercial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return '75 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
        }
        break;
      case 'Industrial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return '30 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return '35 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return '40 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return '75 ';
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
          return '80 ';
        }
        break;
      case 'Mineral':
        break;
      case 'Timberland':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return '45 ';
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return '50 ';
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return '55 ';
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return '60 ';
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return '65 ';
        } else if (marketValue < 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return '70 ';
        }
        break;
      default:
    }
    return '';
  }

  double assessmentValue(marketValues, property_class) {
    final marketValue = marketValues;
    switch (property_class) {
      case 'Residential':
        if (marketValue < 175000) {
          // setState(() {
          //   assessment_level = 0;
          // });
          return marketValue * 0;
        } else if (marketValue < 300000 && marketValue > 175000) {
          // setState(() {
          //   assessment_level = 0.10;
          // });
          return marketValue * 0.10;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.20;
          // });
          return marketValue * 0.20;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.25;
          // });
          return marketValue * 0.25;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        }
        break;
      case 'Agricultural':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return marketValue * 0.45;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 750000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return marketValue * 0.55;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return marketValue * 0.65;
        } else if (marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        }
        break;
      case 'Commercial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return marketValue * 0.75;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
        }
        break;
      case 'Industrial':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.30;
          // });
          return marketValue * 0.30;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.35;
          // });
          return marketValue * 0.35;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.40;
          // });
          return marketValue * 0.40;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 5000000 && marketValue > 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        } else if (marketValue < 10000000 && marketValue > 5000000) {
          // setState(() {
          //   assessment_level = 0.75;
          // });
          return marketValue * 0.75;
        } else if (marketValue > 10000000) {
          // setState(() {
          //   assessment_level = 0.80;
          // });
          return marketValue * 0.80;
        }
        break;
      case 'Mineral':
        break;
      case 'Timberland':
        if (marketValue < 300000) {
          // setState(() {
          //   assessment_level = 0.45;
          // });
          return marketValue * 0.45;
        } else if (marketValue < 500000 && marketValue > 300000) {
          // setState(() {
          //   assessment_level = 0.50;
          // });
          return marketValue * 0.50;
        } else if (marketValue < 750000 && marketValue > 500000) {
          // setState(() {
          //   assessment_level = 0.55;
          // });
          return marketValue * 0.55;
        } else if (marketValue < 1000000 && marketValue > 750000) {
          // setState(() {
          //   assessment_level = 0.60;
          // });
          return marketValue * 0.60;
        } else if (marketValue < 2000000 && marketValue > 1000000) {
          // setState(() {
          //   assessment_level = 0.65;
          // });
          return marketValue * 0.65;
        } else if (marketValue < 2000000) {
          // setState(() {
          //   assessment_level = 0.70;
          // });
          return marketValue * 0.70;
        }
        break;
      default:
    }
    return 0;
  }

  _calculateMarketValue(
      List<AdditionalItems> items, List<BldgAndStructure> bldg) {
    double add_sum = 0;
    double product = 1;
    double bldg_sum = 0;

    for (AdditionalItems value in items) {
      add_sum += double.parse(value.adjustedMarketVal.toString());
    }

    for (BldgAndStructure value in bldg) {
      bldg_sum += double.parse(value.adjustedMarketValue.toString());
    }

    return add_sum + bldg_sum;
  }

  @override
  Widget build(BuildContext context) {
    var width = MediaQuery.of(context).size.width;
    return BlocConsumer<MemorandaAdminBloc, MemorandaAdminState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        if (state is MemorandaLoaded) {
          final memoranda = state.memo;
          final note = state.memo;
          return BlocConsumer<SignatoriesAdminBloc, SignatoriesAdminState>(
            listener: (context, state) {
              // TODO: implement listener
            },
            builder: (context, state) {
              if (state is SignatoriesLoaded) {
                final signatories = state.signatories;
                return BlocConsumer<AdditionalItemsOfflineBloc,
                    AdditionalItemsOfflineState>(
                  listener: (context, state) {
                    // TODO: implement listener
                  },
                  builder: (context, state) {
                    if (state is AdditionalItemsLoaded) {
                      final addItem = state.addItem;
                      return BlocConsumer<BuildingAndStructureBloc,
                          BuildingAndStructureState>(
                        listener: (context, state) {
                          // TODO: implement listener
                        },
                        builder: (context, state) {
                          if (state is BuildingAndStructureLoaded) {
                            return Column(
                              children: [
                                Align(
                                  alignment: Alignment.center,
                                  child: Container(
                                    margin:
                                        const EdgeInsets.fromLTRB(0, 20, 0, 20),
                                    child: const Text(
                                      'PROPERTY ASSESSMENT cont..',
                                      style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 18,
                                      ),
                                      textAlign: TextAlign.left,
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: Container(
                                    margin: const EdgeInsets.symmetric(
                                        horizontal: 20), // Adjust margins here
                                    child: SingleChildScrollView(
                                      child: Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment:
                                                MainAxisAlignment.spaceAround,
                                            children: [
                                              Row(
                                                children: [
                                                  const Text('Taxable'),
                                                  Checkbox(
                                                    checkColor: Colors.white,
                                                    value: isTaxable,
                                                    onChanged: (bool? value) {
                                                      setState(() {
                                                        isTaxable = value!;
                                                      });
                                                    },
                                                  )
                                                ],
                                              ),
                                              Row(
                                                children: [
                                                  const Text('Exempt'),
                                                  Checkbox(
                                                    checkColor: Colors.white,
                                                    value: isExempt,
                                                    onChanged: (bool? value) {
                                                      setState(() {
                                                        isExempt = value!;
                                                      });
                                                    },
                                                  )
                                                ],
                                              ),
                                            ],
                                          ),
                                          Column(
                                            children: [
                                              const SizedBox(
                                                height: 20,
                                              ),
                                              const Text(
                                                'EFFECTIVITY OF ASSESSMENT / REASSESSMENT / SWORN STATEMENT:',
                                                style: TextStyle(
                                                    fontWeight:
                                                        FontWeight.bold),
                                              ),
                                              const SizedBox(
                                                height: 20,
                                              ),
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment
                                                        .spaceAround,
                                                children: [
                                                  SizedBox(
                                                      width: width / 3 - 20,
                                                      height: 50,
                                                      child:
                                                          customDropDownField(
                                                              'Qtr',
                                                              '',
                                                              'qtr',
                                                              quarter)),
                                                  SizedBox(
                                                      width: width / 3 - 20,
                                                      height: 50,
                                                      child: customTextField(
                                                          'Sworn Statement No.',
                                                          '',
                                                          'sworn_statement',
                                                          TextInputType
                                                              .number)),
                                                  SizedBox(
                                                      width: width / 3 - 20,
                                                      height: 50,
                                                      child: customTextField(
                                                          'Year',
                                                          '',
                                                          'yr',
                                                          TextInputType
                                                              .number)),
                                                ],
                                              ),
                                            ],
                                          ),
                                          const SizedBox(
                                            height: 10,
                                          ),
                                          const Divider(
                                            thickness: 2,
                                          ),
                                          Row(
                                            children: [
                                              SizedBox(
                                                width: 10,
                                              ),
                                              Expanded(
                                                child: SizedBox(
                                                  width: width / 2 - 100,
                                                  height: 50,
                                                  child: customDatTimePicker(
                                                    'Date Received',
                                                    '',
                                                    'date_received',
                                                  ),
                                                ),
                                              ),
                                              SizedBox(
                                                width: 20,
                                              ),
                                              Expanded(
                                                child: SizedBox(
                                                  width: width / 2 - 100,
                                                  height: 50,
                                                  child: customDatTimePicker(
                                                    'Date of Entry',
                                                    '',
                                                    'date_of_entry',
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          const Align(
                                            alignment: Alignment.centerLeft,
                                            child: Text(
                                              'APPRAISED/ASSESSED BY:',
                                              style: TextStyle(
                                                  fontWeight: FontWeight.bold),
                                              textAlign: TextAlign.start,
                                            ),
                                          ),
                                          const SizedBox(
                                            height: 15,
                                          ),
                                          SingleChildScrollView(
                                            scrollDirection: Axis.horizontal,
                                            child: Row(children: [
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  const Text('Name'),
                                                  Container(
                                                    padding:
                                                        EdgeInsets.fromLTRB(
                                                            20, 0, 0, 0),
                                                    margin: const EdgeInsets
                                                        .fromLTRB(0, 10, 0, 0),
                                                    width: 250,
                                                    decoration: BoxDecoration(
                                                      border: Border.all(
                                                        color: Colors
                                                            .grey, // You can set the color here
                                                        width:
                                                            0.9, // You can set the width here
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5), // Set the border radius here
                                                    ),
                                                    child: SearchField(
                                                      itemHeight: 65,

                                                      suggestions: signatories
                                                          .map((Signatories
                                                                  signatories) =>
                                                              SearchFieldListItem(
                                                                  '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname}',
                                                                  item:
                                                                      signatories,
                                                                  child:
                                                                      ListTile(
                                                                    title: Text(
                                                                      '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname!}',
                                                                      overflow:
                                                                          TextOverflow
                                                                              .ellipsis,
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  )))
                                                          .toList(),

                                                      validator:
                                                          FormBuilderValidators
                                                              .required(
                                                                  errorText:
                                                                      "This field is required"),
                                                      searchStyle: TextStyle(),

                                                      ////agency suggestion tap
                                                      focusNode:
                                                          appraisedByFocus,
                                                      suggestionState:
                                                          Suggestion.expand,
                                                      onSuggestionTap:
                                                          (appraised) {
                                                        setState(() {
                                                          appraised_by =
                                                              "${appraised.item?.firstname} ${appraised.item?.middlename} ${appraised.item?.lastname}";
                                                          appraised_by_designation =
                                                              appraised.item!
                                                                  .designation;
                                                        });
                                                        appraisedByFocus
                                                            .unfocus();
                                                      },
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              const SizedBox(
                                                width: 10,
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Designation'),
                                                  Container(
                                                      padding:
                                                          EdgeInsets.fromLTRB(
                                                              10, 15, 10, 10),
                                                      margin: const EdgeInsets
                                                          .fromLTRB(
                                                          0, 10, 0, 0),
                                                      width: 250,
                                                      height: 50,
                                                      decoration: BoxDecoration(
                                                        border: Border.all(
                                                          color: Colors
                                                              .grey, // You can set the color here
                                                          width:
                                                              0.9, // You can set the width here
                                                        ),
                                                        borderRadius:
                                                            BorderRadius.circular(
                                                                5), // Set the border radius here
                                                      ),
                                                      child: Text(
                                                        appraised_by_designation
                                                            .toUpperCase(),
                                                        style: TextStyle(
                                                            fontSize: 15),
                                                        textAlign:
                                                            TextAlign.center,
                                                      )),
                                                ],
                                              ),
                                              const SizedBox(
                                                width: 10,
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Date'),
                                                  Container(
                                                      width: 100,
                                                      child:
                                                          customDatTimePicker(
                                                              '',
                                                              '',
                                                              'app_date')),
                                                ],
                                              ),
                                            ]),
                                          ),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          const Align(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                'RECOMMENDING APPROVAL:',
                                                style: TextStyle(
                                                    fontWeight:
                                                        FontWeight.bold),
                                              )),
                                          const SizedBox(
                                            height: 15,
                                          ),
                                          SingleChildScrollView(
                                            scrollDirection: Axis.horizontal,
                                            child: Row(children: [
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  const Text('Name'),
                                                  Container(
                                                    padding:
                                                        EdgeInsets.fromLTRB(
                                                            20, 0, 0, 0),
                                                    margin: const EdgeInsets
                                                        .fromLTRB(0, 10, 0, 0),
                                                    width: 250,
                                                    decoration: BoxDecoration(
                                                      border: Border.all(
                                                        color: Colors
                                                            .grey, // You can set the color here
                                                        width:
                                                            0.9, // You can set the width here
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5), // Set the border radius here
                                                    ),
                                                    child: SearchField(
                                                      itemHeight: 65,

                                                      suggestions: signatories
                                                          .map((Signatories
                                                                  signatories) =>
                                                              SearchFieldListItem(
                                                                  '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname}',
                                                                  item:
                                                                      signatories,
                                                                  child:
                                                                      ListTile(
                                                                    title: Text(
                                                                      '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname!}',
                                                                      overflow:
                                                                          TextOverflow
                                                                              .ellipsis,
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  )))
                                                          .toList(),

                                                      validator:
                                                          FormBuilderValidators
                                                              .required(
                                                                  errorText:
                                                                      "This field is required"),
                                                      searchStyle: TextStyle(),

                                                      ////agency suggestion tap
                                                      focusNode: recByFocus,
                                                      suggestionState:
                                                          Suggestion.expand,
                                                      onSuggestionTap:
                                                          (appraised) {
                                                        setState(() {
                                                          rec_by =
                                                              "${appraised.item?.firstname} ${appraised.item?.middlename} ${appraised.item?.lastname}";
                                                          rec_by_designation =
                                                              appraised.item!
                                                                  .designation;
                                                        });
                                                        recByFocus.unfocus();
                                                      },
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              const SizedBox(
                                                width: 10,
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Designation'),
                                                  Container(
                                                      padding:
                                                          EdgeInsets.fromLTRB(
                                                              10, 15, 10, 10),
                                                      margin: const EdgeInsets
                                                          .fromLTRB(
                                                          0, 10, 0, 0),
                                                      width: 250,
                                                      height: 50,
                                                      decoration: BoxDecoration(
                                                        border: Border.all(
                                                          color: Colors
                                                              .grey, // You can set the color here
                                                          width:
                                                              0.9, // You can set the width here
                                                        ),
                                                        borderRadius:
                                                            BorderRadius.circular(
                                                                5), // Set the border radius here
                                                      ),
                                                      child: Text(
                                                        rec_by_designation
                                                            .toUpperCase(),
                                                        style: TextStyle(
                                                            fontSize: 15),
                                                        textAlign:
                                                            TextAlign.center,
                                                      )),
                                                ],
                                              ),
                                              const SizedBox(
                                                width: 10,
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Date'),
                                                  Container(
                                                      width: 100,
                                                      child:
                                                          customDatTimePicker(
                                                              '',
                                                              '',
                                                              'rec_date')),
                                                ],
                                              ),
                                            ]),
                                          ),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          const Align(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                'APPROVED BY:',
                                                style: TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              )),
                                          const SizedBox(
                                            height: 15,
                                          ),
                                          SingleChildScrollView(
                                            scrollDirection: Axis.horizontal,
                                            child: Row(children: [
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  const Text('Name'),
                                                  Container(
                                                    padding:
                                                        EdgeInsets.fromLTRB(
                                                            20, 0, 0, 0),
                                                    margin: const EdgeInsets
                                                        .fromLTRB(0, 10, 0, 0),
                                                    width: 250,
                                                    decoration: BoxDecoration(
                                                      border: Border.all(
                                                        color: Colors
                                                            .grey, // You can set the color here
                                                        width:
                                                            0.9, // You can set the width here
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5), // Set the border radius here
                                                    ),
                                                    child: SearchField(
                                                      itemHeight: 65,

                                                      suggestions: signatories
                                                          .map((Signatories
                                                                  signatories) =>
                                                              SearchFieldListItem(
                                                                  '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname}',
                                                                  item:
                                                                      signatories,
                                                                  child:
                                                                      ListTile(
                                                                    title: Text(
                                                                      '${signatories.firstname}  ${signatories.middlename}  ${signatories.lastname!}',
                                                                      overflow:
                                                                          TextOverflow
                                                                              .ellipsis,
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  )))
                                                          .toList(),

                                                      validator:
                                                          FormBuilderValidators
                                                              .required(
                                                                  errorText:
                                                                      "This field is required"),
                                                      searchStyle: TextStyle(),

                                                      ////agency suggestion tap
                                                      focusNode: apprvdByFocus,
                                                      suggestionState:
                                                          Suggestion.expand,
                                                      onSuggestionTap:
                                                          (appraised) {
                                                        setState(() {
                                                          approved_by =
                                                              "${appraised.item?.firstname} ${appraised.item?.middlename} ${appraised.item?.lastname}";
                                                          approved_by_designation =
                                                              appraised.item!
                                                                  .designation;
                                                        });
                                                        apprvdByFocus.unfocus();
                                                      },
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              const SizedBox(
                                                width: 10,
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Designation'),
                                                  Container(
                                                      padding:
                                                          EdgeInsets.fromLTRB(
                                                              10, 15, 10, 10),
                                                      margin: const EdgeInsets
                                                          .fromLTRB(
                                                          0, 10, 0, 0),
                                                      width: 250,
                                                      height: 50,
                                                      decoration: BoxDecoration(
                                                        border: Border.all(
                                                          color: Colors
                                                              .grey, // You can set the color here
                                                          width:
                                                              0.9, // You can set the width here
                                                        ),
                                                        borderRadius:
                                                            BorderRadius.circular(
                                                                5), // Set the border radius here
                                                      ),
                                                      child: Text(
                                                        approved_by_designation
                                                            .toUpperCase(),
                                                        style: TextStyle(
                                                            fontSize: 15),
                                                        textAlign:
                                                            TextAlign.center,
                                                      )),
                                                ],
                                              ),
                                              Column(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.start,
                                                children: [
                                                  Text('Date'),
                                                  Container(
                                                      width: 100,
                                                      child:
                                                          customDatTimePicker(
                                                              '',
                                                              '',
                                                              'approve_date')),
                                                ],
                                              ),
                                            ]),
                                          ),
                                          const SizedBox(
                                            height: 50,
                                          ),
                                          const Align(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                'MEMORANDA: ',
                                                style: TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              )),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          SizedBox(
                                              width: 500,
                                              height: 50,
                                              child: SearchField(
                                                itemHeight: 200,
                                                controller: memorandaController,
                                                suggestions: memoranda
                                                    .map(
                                                        (Memoranda memoranda) =>
                                                            SearchFieldListItem(
                                                              '${memoranda.memoranda}',
                                                              item:
                                                                  memoranda, // Change: Use individual Memoranda object
                                                              child: ListTile(
                                                                title: Text(
                                                                  '${memoranda.memoranda}',
                                                                  overflow:
                                                                      TextOverflow
                                                                          .visible,
                                                                ),
                                                              ),
                                                            ))
                                                    .toList(),
                                                validator: FormBuilderValidators
                                                    .required(
                                                        errorText:
                                                            "This field is required"),
                                                // searchInputDecoration:
                                                //     normalTextFieldStyle("Memoranda", "")
                                                //         .copyWith(
                                                //   suffixIcon:
                                                //       const Icon(Icons.arrow_drop_down),
                                                //   contentPadding: EdgeInsets.symmetric(
                                                //       vertical: 15.0, horizontal: 16.0),
                                                //   border: OutlineInputBorder(
                                                //     borderRadius: BorderRadius.circular(10.0),
                                                //     borderSide: BorderSide(
                                                //       color:
                                                //           Colors.grey, // Adjust border color
                                                //     ),
                                                //   ),
                                                //   focusedBorder: OutlineInputBorder(
                                                //     borderRadius: BorderRadius.circular(10.0),
                                                //     borderSide: BorderSide(
                                                //       color: Colors
                                                //           .blue, // Adjust focused border color
                                                //     ),
                                                //   ),
                                                // ),
                                                focusNode: focuss,
                                                suggestionState:
                                                    Suggestion.expand,
                                                suggestionDirection:
                                                    SuggestionDirection.up,
                                                onSuggestionTap: (memoranda) {
                                                  setState(() {
                                                    _memoranda =
                                                        memorandaController
                                                            .text;
                                                  });
                                                  focuss.unfocus();
                                                },
                                              )),
                                          Container(
                                            alignment: Alignment.center,
                                            padding: EdgeInsets.all(10),
                                            child: Column(
                                              children: [
                                                TextField(
                                                  controller:
                                                      memorandaController,
                                                  keyboardType:
                                                      TextInputType.multiline,
                                                  maxLines: 7,
                                                  decoration: InputDecoration(
                                                    focusedBorder:
                                                        OutlineInputBorder(
                                                            borderSide: BorderSide(
                                                                width: 1,
                                                                color: Colors
                                                                    .redAccent)),
                                                    disabledBorder:
                                                        OutlineInputBorder(
                                                      borderSide:
                                                          const BorderSide(
                                                        width: 1,
                                                        color: Colors.grey,
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5),
                                                    ),
                                                    enabledBorder:
                                                        OutlineInputBorder(
                                                      borderSide:
                                                          const BorderSide(
                                                        color: Colors.grey,
                                                        width: 1,
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          const Align(
                                              alignment: Alignment.centerLeft,
                                              child: Text(
                                                'NOTE: ',
                                                style: TextStyle(
                                                  fontWeight: FontWeight.bold,
                                                ),
                                              )),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          SizedBox(
                                              width: 400,
                                              height: 50,
                                              child: SearchField(
                                                itemHeight: 200,
                                                controller: noteController,
                                                suggestions: note
                                                    .map((Memoranda note) =>
                                                        SearchFieldListItem(
                                                          '${note.memoranda}',
                                                          item:
                                                              note, // Change: Use individual Memoranda object
                                                          child: ListTile(
                                                            title: Text(
                                                              '${note.memoranda}',
                                                              overflow:
                                                                  TextOverflow
                                                                      .visible,
                                                            ),
                                                          ),
                                                        ))
                                                    .toList(),
                                                validator: FormBuilderValidators
                                                    .required(
                                                        errorText:
                                                            "This field is required"),
                                                searchInputDecoration:
                                                    InputDecoration().copyWith(
                                                  suffixIcon: const Icon(
                                                      Icons.arrow_drop_down),
                                                ),
                                                focusNode: focus,
                                                suggestionState:
                                                    Suggestion.expand,
                                                suggestionDirection:
                                                    SuggestionDirection.up,
                                                onSuggestionTap: (memoranda) {
                                                  setState(() {
                                                    _notes =
                                                        noteController.text;
                                                  });
                                                  focus.unfocus();
                                                },
                                              )),
                                          Container(
                                            alignment: Alignment.center,
                                            padding: EdgeInsets.all(10),
                                            child: Column(
                                              children: [
                                                TextField(
                                                  controller: noteController,
                                                  keyboardType:
                                                      TextInputType.multiline,
                                                  maxLines: 7,
                                                  decoration: InputDecoration(
                                                    focusedBorder:
                                                        OutlineInputBorder(
                                                            borderSide: BorderSide(
                                                                width: 1,
                                                                color: Colors
                                                                    .redAccent)),
                                                    disabledBorder:
                                                        OutlineInputBorder(
                                                      borderSide:
                                                          const BorderSide(
                                                        width: 1,
                                                        color: Colors.grey,
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5),
                                                    ),
                                                    enabledBorder:
                                                        OutlineInputBorder(
                                                      borderSide:
                                                          const BorderSide(
                                                        color: Colors.grey,
                                                        width: 1,
                                                      ),
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                              5),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                          const SizedBox(
                                            height: 30,
                                          ),
                                          ElevatedButton(
                                            onPressed: () async {
                                              final tempID =
                                                  await SharedPreferences
                                                      .getInstance();
                                              print(
                                                  tempID.getInt('tempid')! - 1);
                                              // final List<PropertyAssessment>
                                              //     propertyAssessments = [];
                                              DateTime? appDate = offlineBldgKey
                                                  .currentState!
                                                  .value['app_date'];
                                              DateTime? recDate = offlineBldgKey
                                                  .currentState!
                                                  .value['rec_date'];
                                              DateTime? approveDate =
                                                  offlineBldgKey.currentState!
                                                      .value['approve_date'];
                                              DateTime? dateReceived =
                                                  offlineBldgKey.currentState!
                                                      .value['date_received'];
                                              DateTime? entryDate =
                                                  offlineBldgKey.currentState!
                                                      .value['date_of_entry'];
                                              String appDateString =
                                                  appDate != null
                                                      ? appDate.toString()
                                                      : '';
                                              String recDateString =
                                                  recDate != null
                                                      ? recDate.toString()
                                                      : '';
                                              String approveDateString =
                                                  approveDate != null
                                                      ? approveDate.toString()
                                                      : '';
                                              String receivedDateString =
                                                  recDate != null
                                                      ? recDate.toString()
                                                      : '';
                                              String entryDateString =
                                                  recDate != null
                                                      ? recDate.toString()
                                                      : '';
                                              context.read<BldgAssessmentOfflineBloc>().add(
                                                  AddBldgAssessment(
                                                      id: 1,
                                                      bldgapprDetailsId: tempID
                                                          .getInt('tempid')!,
                                                      assessedById: widget
                                                          .offlineProfile.id
                                                          .toString(),
                                                      assessedByName: widget
                                                          .offlineProfile
                                                          .firstName!,
                                                      dateCreated: '',
                                                      dateModified: '',
                                                      actualUse: offlineBldgKey.currentState!.value['actual_use'] ??
                                                          '', // Replace null with an empty string
                                                      marketValue:
                                                          _calculateMarketValue(addItem, state.bldgAndStructure)
                                                              .toString(),
                                                      assessmentLevel: assessmentLevel(
                                                          _calculateMarketValue(
                                                              addItem,
                                                              state
                                                                  .bldgAndStructure),
                                                          offlineBldgKey
                                                              .currentState
                                                              ?.value['actual_use']),
                                                      assessedValue: assessmentValue(_calculateMarketValue(addItem, state.bldgAndStructure), offlineBldgKey.currentState?.value['actual_use']).toString(),
                                                      taxable: isTaxable == true ? '1' : '0',
                                                      exempt: isExempt == true ? '1' : '0',
                                                      qtr: offlineBldgKey.currentState!.value['qtr'] ?? '0', // Replace null with '0'
                                                      yr: int.parse(offlineBldgKey.currentState!.value['yr'] ?? '0'), // Replace null with '0'
                                                      appraisedbyName: appraised_by,
                                                      appraisedbyDate: appDateString, // Replace null with current date
                                                      recommendapprName: rec_by,
                                                      recommendapprDate: recDateString, // Replace null with current date
                                                      approvedbyName: approved_by,
                                                      approvedbyDate: approveDateString,
                                                      memoranda: _memoranda,
                                                      note: _notes,
                                                      swornstatementNo: offlineBldgKey.currentState!.value['sworn_statement'] ?? " ", // Replace null with an empty string
                                                      dateReceived: receivedDateString,
                                                      // Replace null with current date
                                                      entryDateAssessment: entryDateString,
                                                      // Replace null with current date
                                                      entryDateBy: widget.offlineProfile.firstName!,
                                                      genCode: '5th',
                                                      appraisedbyDesignation: appraised_by_designation,
                                                      approvedbyDesignation: approved_by_designation,
                                                      recommendapprDesignation: rec_by_designation
                                                      // Replace null with an empty string
                                                      ));
                                              // print('assess');
                                              // print((asses));

                                              widget.function();
                                            },
                                            style: ElevatedButton.styleFrom(
                                                backgroundColor: primary,
                                                foregroundColor: Colors.red),
                                            child: const Padding(
                                              padding: EdgeInsets.all(15.0),
                                              child: Align(
                                                alignment: Alignment.center,
                                                child: Text(
                                                  'Save',
                                                  style: TextStyle(
                                                    color: Colors.white,
                                                  ),
                                                  textAlign: TextAlign.center,
                                                ),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            );
                          }
                          return Container();
                        },
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            },
          );
        }
        return Container();
      },
    );
  }
}

class MyInputForm extends StatelessWidget //__
{
  const MyInputForm({super.key});

  @override
  Widget build(context) //__
  {
    return Column(
      children: [
        TextFormField(
          maxLines: 5,
          decoration: InputDecoration(
            label: const Text('Some text goes here ...'),
            border: OutlineInputBorder(
              borderRadius: BorderRadius.circular(10),
            ),
          ),
        ),
      ],
    );
  }
}
