import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:unit2/bloc/passo/bulding/property_info/property_info_bloc.dart';
import 'package:unit2/model/passo/general_description.dart';
import 'package:unit2/model/passo/unit_construct.dart';
import 'package:unit2/screens/passo/Building/add_building.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/widgets/passo/custom_button.dart';
import 'package:unit2/widgets/passo/custom_formBuilder_fields.dart';

class GeneralDescriptionPage extends StatefulWidget {
  final List<UnitConstruct> unit;
  final VoidCallback onPutGeneralDescription;
  final VoidCallback gendescPrevBtn;

  GeneralDescriptionPage(
      this.unit, this.onPutGeneralDescription, this.gendescPrevBtn);

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

class _GeneralDescriptionPage extends State<GeneralDescriptionPage> {
  final actual_use = [
    "Residential",
    "Agricultural",
    "Commercial",
    "Industrial",
    "Mineral",
    "Timberland",
  ];
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: [
            Container(
              margin:
                  const EdgeInsets.only(left: 0, top: 20, right: 0, bottom: 10),
              child: const Text('GENERAL DESCRIPTION',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                  textAlign: TextAlign.left),
            ),
            Container(
              margin:
                  const EdgeInsets.only(left: 0, top: 10, right: 0, bottom: 0),
              child: FormBuilderDropdown(
                name: 'bldg_type',
                autofocus: false,
                decoration: normalTextFieldStyle("Kind of Building", ""),
                items: widget.unit
                    .map((e) => DropdownMenuItem(
                          value: e,
                          child: Text('${e.bldgType}-${e.building}'),
                        ))
                    .toList(),
              ),
            ),
            customDropDownField("Actual Use", "", 'actual_use', actual_use),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child:
                        customTextField("Bldg. Permit No.", "", 'bldg_permit'),
                  ),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customDatTimePicker(
                          "Certificate of Occupancy Issued ON",
                          "",
                          'date_issued'))
                ]),
            customTextField(
                "Condominium Certificate of Title (CCT)", "", 'cct'),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: customDatTimePicker(
                        "Certificate of Completion Issued ON",
                        "",
                        'coc_issued'),
                  ),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customDatTimePicker(
                          "Certificate of Occupancy Issued ON",
                          "",
                          'coo_issued'))
                ]),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: customDatTimePicker(
                        "Date Constructed /Completed", "", 'date_cnstructed'),
                  ),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customDatTimePicker(
                          "Date Occupied", "", 'date_occupied'))
                ]),
            Row(mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: <
                Widget>[
              Expanded(
                flex: 1,
                child: customTextField("Bldg. Age", "", 'bldg_age'),
              ),
              const SizedBox(width: 10.0),
              Expanded(
                  // optional flex property if flex is 1 because the default flex is 1
                  flex: 1,
                  child: customTextField("No. of storeys", "", 'no_of_storeys'))
            ]),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: customTextField(
                        "Area of 1st Floor", "", 'area_of_1stFl'),
                  ),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customTextField(
                          "Area of 2nd Floor", "", 'area_of_2ndFl'))
                ]),
            Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: customTextField(
                          "Area of 3rd Floor", "", 'area_of_3rdFl')),
                  const SizedBox(width: 10.0),
                  Expanded(
                      // optional flex property if flex is 1 because the default flex is 1
                      flex: 1,
                      child: customTextField(
                          "Area of 4th Floor", "", 'area_of_4thFl'))
                ]),
            customTextField("Total Area", "", 'total_area'),
            SizedBox(
              height: 50,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                CustomButton(
                  icon: const Icon(Icons.chevron_left_rounded,
                      color: Colors.white),
                  onPressed: () {
                    {
                      widget.gendescPrevBtn();
                    }
                    ;
                  },
                ),
                CustomButton(
                  icon: const Icon(Icons.chevron_right_rounded,
                      color: Colors.white),
                  onPressed: () async {
                    {
                      try {
                        final tempID = await SharedPreferences.getInstance();

                        var genDescData = GeneralDesc(
                            id: tempID.getInt('tempid')! - 1,
                            bldgKind: formKey
                                .currentState?.value['bldg_type'].building,
                            strucType: formKey
                                .currentState?.value['bldg_type'].bldgType,
                            bldgPermit:
                                formKey.currentState?.value['bldg_permit'],
                            dateIssued:
                                formKey.currentState?.value['coc_issued'],
                            cct: formKey.currentState?.value['cct'],
                            certCompletionIssued:
                                formKey.currentState?.value['coc_issued'],
                            certOccupancyIssued:
                                formKey.currentState?.value['coo_issued'],
                            dateCompleted:
                                formKey.currentState?.value['date_cnstructed'],
                            dateOccupied:
                                formKey.currentState?.value['date_occupied'],
                            bldgAge: int.tryParse(
                                formKey.currentState?.value['bldg_age']),
                            noStoreys: int.tryParse(
                                formKey.currentState?.value['no_of_storeys']),
                            area1Stfloor: '0',
                            area2Ndfloor: '0',
                            area3Rdfloor: '0',
                            area4Thfloor: '0',
                            totalFloorArea:
                                formKey.currentState?.value['total_area'],
                            floorSketch: null,
                            actualUse:
                                formKey.currentState?.value['actual_use']);

                        context.read<PropertyInfoBloc>()
                          ..add(UpdateGeneralDesc(gen_desc: genDescData));
                      } catch (e) {
                        Fluttertoast.showToast(
                            msg: "Slow internet connection, please try again!");
                      }
                    }
                    ;
                  },
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
