import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:intl/intl.dart';
import 'package:unit2/bloc/passo/land/land_appraisal/land_appraisal_bloc.dart';
import 'package:unit2/model/passo/land_appr.dart';
import 'package:unit2/screens/passo/Land/edit_land/AddLandAppraisalEdit.dart';

import 'package:unit2/utils/alerts.dart';
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import 'package:unit2/widgets/passo/custom_button.dart';

class LandAppraisalEdit extends StatefulWidget {
  Function PrevBtn;
  Function NextBtn;
  final int faasId;
  LandAppraisalEdit(this.PrevBtn, this.NextBtn, this.faasId);
  @override
  _LandAppraisalEdit createState() => _LandAppraisalEdit();
}

class _LandAppraisalEdit extends State<LandAppraisalEdit> {
  //  double _totalMarketValue(items) {
  //   double total = 0;
  //   items.forEach((row) {
  //     total += double.parse(row);
  //   });
  //   return total;
  // }

  void deleteItem(int itemId) {
    context.read<LandAppraisalBloc>().add(DeleteLandAppraisal(id: itemId));
  }

  @override
  Widget build(BuildContext context) {
    return ProgressHUD(
      padding: const EdgeInsets.all(24),
      backgroundColor: Colors.black87,
      indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
      child: BlocConsumer<LandAppraisalBloc, LandAppraisalState>(
          listener: (context, state) {
        if (state is LandAppraisalLoading) {
          final progress = ProgressHUD.of(context);
          progress!.showWithText("Please wait...");
        }
        if (state is LandAppraisalLoaded) {
          final progress = ProgressHUD.of(context);
          progress?.dismiss();
        }
        if (state is LandAppraisalErrorState) {
          final progress = ProgressHUD.of(context);
          progress?.dismiss();
        }
      }, builder: (context, state) {
        final state = context.watch<LandAppraisalBloc>().state;
        if (state is LandAppraisalLoaded) {
          return Column(
            children: [
              Expanded(
                  child: SingleChildScrollView(
                child: Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Column(
                    children: [
                      Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 20, right: 0, bottom: 10),
                        child: const Text('LAND APPRAISAL',
                            style: TextStyle(
                                fontWeight: FontWeight.bold, fontSize: 18),
                            textAlign: TextAlign.left),
                      ),
                      Align(
                        alignment: Alignment.topRight,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.red,
                          ),
                          onPressed: () {
                            context
                                .read<LandAppraisalBloc>()
                                .add(ShowLandAppraisal());
                          },
                          child: Row(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              const Text('ADD ITEM'), // <-- Text
                              const SizedBox(
                                width: 5,
                              ),
                              const Icon(
                                // <-- Icon
                                Icons.add,
                                size: 24.0,
                              ),
                            ],
                          ),
                        ),
                      ),
                      SingleChildScrollView(
                          scrollDirection: Axis.horizontal,
                          child: DataTable(
                              // ignore: prefer_const_literals_to_create_immutables
                              columns: [
                                const DataColumn(
                                  label: Text('Classification'),
                                ),
                                const DataColumn(
                                  label: Text('Sub-Classification'),
                                ),
                                const DataColumn(
                                  label: Text('Area'),
                                ),
                                const DataColumn(
                                  label: Text('Unit Value'),
                                ),
                                const DataColumn(
                                  label: Text('Base MArket Value'),
                                ),
                                const DataColumn(
                                  label: Text('Action'),
                                )
                              ],
                              rows: state.land_appr.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.classification!)),
                                    DataCell(Text(dataRow.subClass!)),
                                    DataCell(Text(dataRow.area!)),
                                    DataCell(Text(
                                        ((double.parse(dataRow.unitValue!)))
                                            .toString())),
                                    DataCell(Text(
                                        ((double.parse(dataRow.baseMarketval!)))
                                            .toString())),
                                    DataCell(Row(
                                      children: [
                                        InkWell(
                                          child: Container(
                                            height: 30,
                                            width: 30,
                                            decoration: BoxDecoration(
                                              shape: BoxShape.circle,
                                              color: Colors.red,
                                            ),
                                            child: Icon(
                                              Icons.delete,
                                              color: Colors.white,
                                              size: 20.0,
                                            ),
                                          ),
                                          onTap: () {
                                            deleteItem(dataRow.id!);
                                          },
                                        ),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        InkWell(
                                          child: Container(
                                            height: 30,
                                            width: 30,
                                            decoration: BoxDecoration(
                                              shape: BoxShape.circle,
                                              color: Colors.red,
                                            ),
                                            child: Icon(
                                              Icons.edit,
                                              color: Colors.white,
                                              size: 20.0,
                                            ),
                                          ),
                                          onTap: () {},
                                        ),
                                      ],
                                    ))
                                  ],
                                );
                              }).toList()))
                    ],
                  ),
                ),
              )),
              // Padding(
              //   padding: const EdgeInsets.only(left: 20.0, right: 20.0),
              //   child: Row(
              //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
              //     children: [
              //       Text(
              //         'Total',
              //         style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
              //       ),
              //       Text(
              //         NumberFormat.currency(locale: 'en-PH', symbol: "₱")
              //             .format('1.0'),
              //         style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15),
              //       )
              //     ],
              //   ),
              // ),
              Padding(
                padding: const EdgeInsets.all(15.0),
                child: 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: () {
                        {
                          widget.NextBtn();
                        }
                        ;
                      },
                    )
                  ],
                ),
              ),
            ],
          );
        }
        if (state is LandAppraisalDeletedState) {
          if (state.success) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              successAlert(context, "Deletion Successful",
                  "Extra item has been deleted successfully", () {
                Navigator.of(context).pop();
                context.read<LandAppraisalBloc>().add(LoadLandAppraisalEdit(
                    land_appr: const <LandAppr>[], id: widget.faasId!));
              });
            });
          }
        }
        if (state is ShowAddLandAppraisalScreen) {
          return ConstrainedBox(
            constraints: const BoxConstraints(
              maxHeight: 700.0,
            ),
            child: AlertDialog(
              insetPadding: const EdgeInsets.symmetric(
                horizontal: 20.0,
                vertical: 10.0,
              ),
              title: const Text(
                'ADD LAND APPRAISAL',
                textAlign: TextAlign.center,
              ),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Align(
                    alignment: Alignment.topCenter,
                    child: Container(
                      child: ConstrainedBox(
                        constraints: BoxConstraints(maxHeight: 500),
                        child: Container(
                          child: AddLandAppraisalEditModal(widget.faasId),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          );
        }
        if (state is LandAppraisalErrorState) {
          return SomethingWentWrong(
            message: onError,
            onpressed: () {
              context.read<LandAppraisalBloc>().add(LoadLandAppraisalEdit(
                  land_appr: <LandAppr>[], id: widget.faasId!));
            },
          );
        }
        return Container();
      }),
    );
  }
}
