import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
import 'package:unit2/bloc/passo/bulding/additional_item/additional_item_bloc.dart';
import 'package:unit2/bloc/passo/bulding/additional_items_edit/additional_items_edit_bloc.dart';
import 'package:unit2/model/passo/additional_items.dart';
import 'package:unit2/model/passo/class_components.dart';
import 'package:unit2/model/passo/unit_construct.dart';
import 'package:unit2/screens/passo/Building/edit_building/AddExtraItems.dart';
import 'package:unit2/utils/alerts.dart';
import 'package:unit2/widgets/passo/custom_button.dart';

class AdditionalItemEditPage extends StatefulWidget {
  final List<UnitConstruct> unit;
  final List<ClassComponents> options;
  final int tempId;
  final VoidCallback NextBtn;
  final VoidCallback PrevBtn;

  AdditionalItemEditPage(
      this.unit, this.options, this.tempId, this.NextBtn, this.PrevBtn);

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

class _AdditionalItemEditPage extends State<AdditionalItemEditPage> {
  void deleteItem(int itemId) {
    context
        .read<AdditionalItemsEditBloc>()
        .add(DeleteAdditionalItemsEdit(id: itemId));
  }

  // double _totalMarketValue(items) {
  //   double total = 0;
  //   items.forEach((row) {
  //     total += double.parse(row.adjustedMarketVal);
  //   });
  //   return total;
  // }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<AdditionalItemsEditBloc, AdditionalItemsEditState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      builder: (context, state) {
        final state = context.watch<AdditionalItemsEditBloc>().state;
        if (state is AdditionalItemsEditLoaded) {
          return Column(
            children: [
              Container(
                height: 500,
                child: 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('ADDITIONAL ITEMS',
                                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<AdditionalItemsEditBloc>()
                                    .add(ShowAdditionalItemsEdit());
                              },
                              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('Items'),
                                ),
                                const DataColumn(
                                  label: Text('Unit Value'),
                                ),
                                const DataColumn(
                                  label: Text('% of BUCC'),
                                ),
                                const DataColumn(
                                  label: Text('Market Value'),
                                ),
                                const DataColumn(
                                  label: Text('Action'),
                                )
                              ],
                              rows: state.items.map((dataRow) {
                                return DataRow(
                                  cells: [
                                    DataCell(Text(dataRow.className)),
                                    DataCell(Text(dataRow.baseUnitValue)),
                                    DataCell(Text(dataRow.unitValue)),
                                    DataCell(Text(((double.parse(
                                            dataRow.adjustedMarketVal)))
                                        .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(_totalMarketValue(state.items)),
              //         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 AdditionalItemsEditDeletedState) {
          if (state.success) {
            WidgetsBinding.instance.addPostFrameCallback((_) {
              successAlert(context, "Deletion Successful",
                  "Extra item has been deleted successfully", () {
                Navigator.of(context).pop();
                context.read<AdditionalItemsEditBloc>().add(
                    LoadAdditionalItemsEdit(
                        items: const <AdditionalItems>[], id: widget.tempId));
              });
            });
          }
        }
        if (state is ShowAddItemsScreenEdit) {
          return ConstrainedBox(
            constraints: BoxConstraints(maxHeight: 1000.0),
            child: AlertDialog(
              insetPadding: EdgeInsets.symmetric(
                horizontal: 20.0,
                vertical: 10.0,
              ),
              title: Text(
                'ADD EXTRA ITEMS',
                textAlign: TextAlign.center,
              ),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Expanded(
                      child: AddExtraItemsEdit(
                          widget.unit, widget.options, widget.tempId))
                ],
              ),
            ),
          );
        }
        return Container(
          child: Column(
            children: [
              Container(
                margin: const EdgeInsets.only(
                    left: 0, top: 20, right: 0, bottom: 10),
                child: const Text('ADDITIONAL MATERIALS',
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                    textAlign: TextAlign.left),
              ),
              Align(
                alignment: Alignment.topRight,
                child: ElevatedButton(
                  onPressed: () {
                    context
                        .read<AdditionalItemBloc>()
                        .add(ShowAdditionalItems());
                  },
                  child: Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      const Text('ADD ITEM'), // <-- Text
                      const SizedBox(
                        width: 5,
                      ),
                      const Icon(
                        // <-- Icon
                        Icons.add,
                        size: 24.0,
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      },
    );
  }
}
