import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:unit2/bloc/passo/bulding/additional_items_edit/additional_items_edit_bloc.dart';
import 'package:unit2/bloc/passo/bulding/general_description/general_description_bloc.dart';
import 'package:unit2/bloc/passo/bulding/property_appraisal_edit/property_appraisal_edit_bloc.dart';

import 'package:unit2/model/passo/additional_items.dart';
import 'package:unit2/model/passo/property_appraisal.dart';
import 'package:unit2/model/passo/property_appraisal_edit.dart';
import 'package:unit2/screens/passo/Building/edit_building/property_owner_info.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import 'package:unit2/widgets/passo/custom_button.dart';

class PropertyAppraisalEditPage extends StatefulWidget {
  final int tempId;
  final VoidCallback NextBtn;
  final VoidCallback PrevBtn;

  PropertyAppraisalEditPage(this.tempId, this.NextBtn, this.PrevBtn);

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

class _PropertyAppraisalEditPage extends State<PropertyAppraisalEditPage> {
  double depRate = 0;
  // int totalAreas = 0;
  // String actualUse = "";

  // @override
  // void initState() {
  //   super.initState();
  //   _loadDataFromSharedPreferences(); // Call the method to load data
  // }

  // Method to load data from SharedPreferences
  // _loadDataFromSharedPreferences() async {
  //   SharedPreferences prefs = await SharedPreferences.getInstance();
  //   setState(() {
  //     actualUse = prefs.getString('actualUse') ?? '';
  //     totalAreas = prefs.getInt('totalArea') ??
  //         0; // Provide a default value if the key doesn't exist
  //   });
  // }

  calculateAdditionalItems(List<AdditionalItems> items) {
    double sum = 0;
    double product = 1;

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

    return sum;
  }

  calculateMarketValue(buildingCost, additionalItems, dep) {
    double sum = 0;
    double depreciation = 0;
    double total = 0;

    sum = buildingCost + calculateAdditionalItems(additionalItems);

    depreciation = sum * dep;

    total = sum - depreciation;

    return total;
  }

  calculateDepCost(buildingCost, additionalItems, dep) {
    double sum = 0;
    double depreciation = 0;
    double total = 0;

    sum = buildingCost + calculateAdditionalItems(additionalItems);

    depreciation = sum * dep;

    total = sum - depreciation;

    return depreciation;
  }

  calculateConstructionCost(constructioncost, addtionalCost) {
    double sum = 0;
    sum = constructioncost + addtionalCost;

    return sum;
  }

  String assessmentLevel(marketValues, property_class) {
    final marketValue = double.parse(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 = double.parse(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;
  }

  @override
  Widget build(BuildContext context) {
    return ProgressHUD(
      padding: const EdgeInsets.all(24),
      backgroundColor: Colors.black87,
      indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
      child:
          BlocConsumer<PropertyAppraisalEditBloc, PropertyAppraisalEditState>(
        listener: (context, state) {
          if (state is PropertyAppraisalEditLoading) {
            final progress = ProgressHUD.of(context);
            progress!.showWithText("Please wait...");
          }

          if (state is PropertyAppraisalEditErrorState) {
            final progress = ProgressHUD.of(context);
            progress?.dismiss();
          }
        },
        builder: (context, state) {
          if (state is PropertyAppraisalEditLoaded) {
            final appraisal = state.appraisalEdit;
            return BlocConsumer<AdditionalItemsEditBloc,
                AdditionalItemsEditState>(listener: (context, state) {
              if (state is PropertyAppraisalEditLoading) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is AdditionalItemsEditLoading) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is AdditionalItemsEditErrorState) {
                final progress = ProgressHUD.of(context);
                progress?.dismiss();
              }
            }, builder: (context, state) {
              if (state is AdditionalItemsEditLoaded) {
                final item = state.items;

                return BlocConsumer<GeneralDescriptionBloc,
                    GeneralDescriptionState>(
                  listener: (context, state) {
                    if (state is GenDescLoading) {
                      final progress = ProgressHUD.of(context);
                      progress!.showWithText("Please wait...");
                    }
                    if (state is GenDescLoaded) {
                      final progress = ProgressHUD.of(context);
                      progress?.dismiss();
                    }
                    if (state is GenDescErrorState) {
                      final progress = ProgressHUD.of(context);
                      progress?.dismiss();
                    }
                  },
                  builder: (context, state) {
                    if (state is GenDescLoaded) {
                      double totalArea = double.tryParse(
                              (state.gendesc.totalFloorArea ??
                                          appraisal.totalArea)
                                      ?.toString() ??
                                  '0.0') ??
                          0.0;

                      double bldgUnitValue = double.tryParse(keys.currentState
                                  ?.value['bldg_type']?.unitValue ??
                              appraisal.unitconstructCost) ??
                          0.0;
                      return Column(
                        children: [
                          Expanded(
                            child: SingleChildScrollView(
                              child: Container(
                                margin: const EdgeInsets.only(
                                    left: 20.0, right: 20.0),
                                child: Column(
                                  children: [
                                    Container(
                                      margin: const EdgeInsets.only(
                                          left: 0,
                                          top: 20,
                                          right: 0,
                                          bottom: 20),
                                      child: const Text('PROPERTY APPRAISAL',
                                          style: TextStyle(
                                              fontWeight: FontWeight.bold,
                                              fontSize: 18),
                                          textAlign: TextAlign.left),
                                    ),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Unit Construction Cost",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            bldgUnitValue.toString() + ' sq.m',
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Building Core",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: const Text(
                                            '',
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 40),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Sub-total",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            bldgUnitValue.toString(),
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 40),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Cost of Additional Items",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: const Text(
                                            '',
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Sub-total",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            appraisal.addItemsSubtotal ??
                                                '0.00',
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Total Construction Cost",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            calculateConstructionCost(
                                                    double.parse(appraisal
                                                        .unitconstructSubtotal!),
                                                    double.parse(appraisal
                                                        .addItemsSubtotal!))
                                                .toString(),
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 40),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Depreciation Rate",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        SizedBox(
                                          width: 90,
                                          height: 25,
                                          child: FormBuilderTextField(
                                            name: 'depRate',
                                            decoration: normalTextFieldStyle(
                                                "0.00", ""),
                                            validator:
                                                FormBuilderValidators.compose(
                                                    []),
                                            onChanged: (value) {
                                              setState(() {
                                                depRate = double.parse(value!);
                                              });
                                            },
                                          ),
                                        ),
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Depreciation Cost",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            calculateDepCost(
                                                    (totalArea * bldgUnitValue),
                                                    item,
                                                    double.parse(keys
                                                                .currentState
                                                                ?.value[
                                                            'depRate'] ??
                                                        appraisal
                                                            .depreciationRate))
                                                .toString(),
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Total % Depreciation",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            '${(double.parse(keys.currentState?.value['depRate'] ?? appraisal.depreciationRate) * 100).toStringAsFixed(2)}%',
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    const SizedBox(height: 15),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Container(
                                          child: const Text(
                                            "Market Value",
                                            style: TextStyle(
                                                fontWeight: FontWeight.bold,
                                                fontSize: 13),
                                            textAlign: TextAlign.left,
                                          ),
                                        ),
                                        Container(
                                          child: Text(
                                            calculateMarketValue(
                                                    (totalArea * bldgUnitValue),
                                                    item,
                                                    double.parse(keys
                                                                .currentState
                                                                ?.value[
                                                            'depRate'] ??
                                                        appraisal
                                                            .depreciationRate))
                                                .toString(),
                                            textAlign: TextAlign.right,
                                          ),
                                        )
                                      ],
                                    ),
                                    Row(
                                      children: [
                                        Expanded(
                                          flex: 1,
                                          child: SingleChildScrollView(
                                            scrollDirection: Axis.horizontal,
                                            child: Container(
                                              margin:
                                                  const EdgeInsets.symmetric(
                                                      horizontal: 20.0),
                                              child: Column(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.start,
                                                children: [
                                                  Container(
                                                    margin: const EdgeInsets
                                                        .fromLTRB(0, 20, 0, 20),
                                                    child: const Text(
                                                      'PROPERTY ASSESSMENT',
                                                      style: TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: 18,
                                                      ),
                                                      textAlign: TextAlign.left,
                                                    ),
                                                  ),
                                                  Column(
                                                    children: [
                                                      Row(
                                                        children: [
                                                          Container(
                                                            width: 100,
                                                            margin:
                                                                const EdgeInsets
                                                                        .only(
                                                                    top: 15,
                                                                    left: 15),
                                                            padding:
                                                                const EdgeInsets
                                                                    .all(5.0),
                                                            child: const Text(
                                                              'Actual Use',
                                                              style: TextStyle(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold,
                                                                fontSize: 13,
                                                              ),
                                                              textAlign:
                                                                  TextAlign
                                                                      .center,
                                                            ),
                                                          ),
                                                          Container(
                                                            width: 150,
                                                            margin:
                                                                const EdgeInsets
                                                                        .only(
                                                                    top: 15,
                                                                    left: 15),
                                                            padding:
                                                                const EdgeInsets
                                                                    .all(5.0),
                                                            child: const Text(
                                                              'Market Value',
                                                              style: TextStyle(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold,
                                                                fontSize: 13,
                                                              ),
                                                              textAlign:
                                                                  TextAlign
                                                                      .center,
                                                            ),
                                                          ),
                                                          Container(
                                                            width: 100,
                                                            margin:
                                                                const EdgeInsets
                                                                        .only(
                                                                    top: 15,
                                                                    left: 15),
                                                            padding:
                                                                const EdgeInsets
                                                                    .all(5.0),
                                                            child: const Text(
                                                              'Ass. Level',
                                                              style: TextStyle(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold,
                                                                fontSize: 13,
                                                              ),
                                                              textAlign:
                                                                  TextAlign
                                                                      .center,
                                                            ),
                                                          ),
                                                          Container(
                                                            width: 150,
                                                            margin:
                                                                const EdgeInsets
                                                                        .only(
                                                                    top: 15,
                                                                    left: 15),
                                                            padding:
                                                                const EdgeInsets
                                                                    .all(5.0),
                                                            child: const Text(
                                                              'Ass. Value',
                                                              style: TextStyle(
                                                                fontWeight:
                                                                    FontWeight
                                                                        .bold,
                                                                fontSize: 13,
                                                              ),
                                                              textAlign:
                                                                  TextAlign
                                                                      .center,
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                      SizedBox(
                                                        height: 50,
                                                        child:
                                                            SingleChildScrollView(
                                                          scrollDirection:
                                                              Axis.horizontal,
                                                          child: Row(children: [
                                                            Container(
                                                              height: 100,
                                                              child: Row(
                                                                children: [
                                                                  Container(
                                                                    width: 100,
                                                                    margin: const EdgeInsets
                                                                            .only(
                                                                        top: 15,
                                                                        left:
                                                                            15),
                                                                    padding:
                                                                        const EdgeInsets.all(
                                                                            5.0),
                                                                    child: Text(
                                                                      state.gendesc
                                                                              .actualUse ??
                                                                          "",
                                                                      style:
                                                                          const TextStyle(
                                                                        fontWeight:
                                                                            FontWeight.bold,
                                                                        fontSize:
                                                                            13,
                                                                      ),
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  ),
                                                                  Container(
                                                                    width: 150,
                                                                    margin: const EdgeInsets
                                                                            .only(
                                                                        top: 15,
                                                                        left:
                                                                            15),
                                                                    padding:
                                                                        const EdgeInsets.all(
                                                                            5.0),
                                                                    child: Text(
                                                                      NumberFormat
                                                                          .currency(
                                                                        locale:
                                                                            'en-PH',
                                                                        symbol:
                                                                            "₱",
                                                                      ).format(
                                                                        calculateMarketValue(
                                                                            (totalArea *
                                                                                bldgUnitValue),
                                                                            item,
                                                                            double.parse(keys.currentState?.value['depRate'] ??
                                                                                appraisal.depreciationRate)),
                                                                      ),
                                                                      style:
                                                                          const TextStyle(
                                                                        fontWeight:
                                                                            FontWeight.bold,
                                                                        fontSize:
                                                                            13,
                                                                      ),
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  ),
                                                                  Container(
                                                                    width: 100,
                                                                    margin: const EdgeInsets
                                                                            .only(
                                                                        top: 15,
                                                                        left:
                                                                            15),
                                                                    padding:
                                                                        const EdgeInsets.all(
                                                                            5.0),
                                                                    child: Text(
                                                                      '${assessmentLevel(calculateMarketValue((totalArea * bldgUnitValue), item, double.parse(keys.currentState?.value['depRate'] ?? appraisal.depreciationRate)).toString(), state.gendesc.actualUse)}%',
                                                                      style:
                                                                          const TextStyle(
                                                                        fontWeight:
                                                                            FontWeight.bold,
                                                                        fontSize:
                                                                            13,
                                                                      ),
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  ),
                                                                  Container(
                                                                    width: 150,
                                                                    margin: const EdgeInsets
                                                                            .only(
                                                                        top: 15,
                                                                        left:
                                                                            15),
                                                                    padding:
                                                                        const EdgeInsets.all(
                                                                            5.0),
                                                                    child: Text(
                                                                      NumberFormat
                                                                          .currency(
                                                                        locale:
                                                                            'en-PH',
                                                                        symbol:
                                                                            "₱",
                                                                      ).format(assessmentValue(
                                                                          calculateMarketValue((totalArea * bldgUnitValue), item, double.parse(keys.currentState?.value['depRate'] ?? appraisal.depreciationRate))
                                                                              .toString(),
                                                                          state
                                                                              .gendesc
                                                                              .actualUse)),
                                                                      style:
                                                                          const TextStyle(
                                                                        fontWeight:
                                                                            FontWeight.bold,
                                                                        fontSize:
                                                                            13,
                                                                      ),
                                                                      textAlign:
                                                                          TextAlign
                                                                              .center,
                                                                    ),
                                                                  ),
                                                                  const SizedBox(
                                                                    height: 80,
                                                                  ),
                                                                ],
                                                              ),
                                                            )
                                                          ]),
                                                        ),
                                                      )
                                                    ],
                                                  )
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                    Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceEvenly,
                                      children: [
                                        CustomButton(
                                          icon: const Icon(
                                              Icons.chevron_left_rounded,
                                              color: Colors.white),
                                          onPressed: () {
                                            {
                                              widget.PrevBtn();
                                            }
                                            ;
                                          },
                                        ),
                                        CustomButton(
                                          icon: const Icon(
                                              Icons.chevron_right_rounded,
                                              color: Colors.white),
                                          onPressed: () async {
                                            final tempID =
                                                await SharedPreferences
                                                    .getInstance();

                                            final id =
                                                tempID.getInt('tempid')! - 1;
                                            {
                                              var appraisals = PropertyAppraisalEdit(
                                                  id: 1,
                                                  bldgapprDetailsId: id,
                                                  unitconstructCost:
                                                      bldgUnitValue.toString(),
                                                  buildingCore: 'test',
                                                  unitconstructSubtotal:
                                                      (totalArea * bldgUnitValue)
                                                          .toString(),
                                                  depreciationRate:
                                                      depRate.toString(),
                                                  depreciationCost:
                                                      calculateDepCost((totalArea * bldgUnitValue), item, depRate)
                                                          .toString(),
                                                  costAddItems:
                                                      calculateAdditionalItems(item)
                                                          .toString(),
                                                  addItemsSubtotal:
                                                      calculateAdditionalItems(item)
                                                          .toString(),
                                                  totalpercentDepreciation:
                                                      (depRate * 100)
                                                          .toStringAsFixed(2),
                                                  marketValue: calculateMarketValue(
                                                          (totalArea * bldgUnitValue),
                                                          item,
                                                          depRate)
                                                      .toString(),
                                                  totalArea: totalArea.toString());
                                              context
                                                  .read<
                                                      PropertyAppraisalEditBloc>()
                                                  .add(
                                                      UpdatePropertyAppraisalEdit(
                                                          appraisalEdit:
                                                              appraisals,
                                                          id: widget.tempId));

                                              widget.NextBtn();
                                            }
                                            ;
                                          },
                                        )
                                      ],
                                    )
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      );
                    }
                    return Container();
                  },
                );
              }
              return Container();
            });
          }
          if (state is PropertyAppraisalEditErrorState) {
            return SomethingWentWrong(
              message: onError,
              onpressed: () {
                context
                    .read<PropertyAppraisalEditBloc>()
                    .add(LoadPropertyAppraisalEdit(
                      appraisalEdit: PropertyAppraisalEdit(),
                      id: widget.tempId,
                    ));
              },
            );
          }
          return Container();
        },
      ),
    );
  }
}
