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:unit2/bloc/offline/offline_passo/land/value_adjustment/value_adjustment_bloc.dart';
import 'package:unit2/screens/offline/passo/land/add/AddValueAdjustmentModal.dart';

import '../../../../../utils/alerts.dart';
import '../../../../../widgets/passo/custom_button.dart';

class ValueAdjustmentOfflinePage extends StatefulWidget {
  Function PrevBtn;
  Function NextBtn;
  ValueAdjustmentOfflinePage(this.PrevBtn, this.NextBtn);
  @override
  _ValueAdjustmentOfflinePage createState() => _ValueAdjustmentOfflinePage();
}

class _ValueAdjustmentOfflinePage extends State<ValueAdjustmentOfflinePage> {
  void deleteItem(int itemId) {
    context.read<ValueAdjustmentBloc>().add(DeleteValueAdjustment(id: itemId));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ProgressHUD(
        padding: const EdgeInsets.all(24),
        backgroundColor: Colors.black87,
        indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
        child: BlocConsumer<ValueAdjustmentBloc, ValueAdjustmentState>(
            listener: (context, state) {
          // if (state is LandValueAdjustmentsLoading) {
          //   final progress = ProgressHUD.of(context);
          //   progress!.showWithText("Please wait...");
          // }
          // if (state is LandValueAdjustmentsLoaded) {
          //   final progress = ProgressHUD.of(context);
          //   progress?.dismiss();
          // }
          // if (state is LandValueAdjustmentsErrorState) {
          //   final progress = ProgressHUD.of(context);
          //   progress?.dismiss();
          // }
        }, builder: (context, state) {
          final state = context.watch<ValueAdjustmentBloc>().state;
          if (state is ValueAdjustmentLoaded) {
            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('VALUE ADJUSTMENTS',
                              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<ValueAdjustmentBloc>()
                                  .add(ShowValueAdjustment());
                            },
                            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('Base Market Value'),
                                  ),
                                  const DataColumn(
                                    label: Text('Adjustment Factors'),
                                  ),
                                  const DataColumn(
                                    label: Text('% Adjustment'),
                                  ),
                                  const DataColumn(
                                    label: Text('Value Adjustment'),
                                  ),
                                  const DataColumn(
                                    label: Text('Market Value'),
                                  ),
                                  const DataColumn(
                                    label: Text('Action'),
                                  )
                                ],
                                rows: state.valueAdjustment.map((dataRow) {
                                  return DataRow(
                                    cells: [
                                      DataCell(Text(dataRow.baseMarketval!)),
                                      DataCell(
                                          Text(dataRow.adjustmentFactors!)),
                                      DataCell(Text(dataRow.adjustment!)),
                                      DataCell(Text(dataRow.valueAdjustment!)),
                                      DataCell(Text(dataRow.marketValue!)),
                                      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: () {
                                              confirmAlertWithCancel(
                                                  context,
                                                  () => deleteItem(dataRow.id!),
                                                  () => null,
                                                  'Delete Item?',
                                                  "Are you sure you want to delete this item?");
                                            },
                                          ),
                                        ],
                                      ))
                                    ],
                                  );
                                }).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 ValueAdjustmentDeletedState) {
            if (state.success) {
              WidgetsBinding.instance.addPostFrameCallback((_) {
                successAlert(context, "Deletion Successful",
                    "Extra item has been deleted successfully", () {
                  Navigator.of(context).pop();
                  context
                      .read<ValueAdjustmentBloc>()
                      .add(const LoadValueAdjustment());
                });
              });
            }
          }
          if (state is ShowValueAdjustmentcreen) {
            return ConstrainedBox(
              constraints: BoxConstraints(maxHeight: 1000.0),
              child: AlertDialog(
                insetPadding: EdgeInsets.symmetric(
                  horizontal: 20.0,
                  vertical: 10.0,
                ),
                title: Text(
                  'ADD VALUE ADJUSTMENTS',
                  textAlign: TextAlign.center,
                ),
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(child: AddLandValueAdjustmentOfflineModal())
                  ],
                ),
              ),
            );
          }
          return Container();
        }),
      ),
    );
  }
}
