import 'dart:developer';

import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/core/extensions/date_time_ext.dart';
import 'package:enaklo_pos/core/extensions/int_ext.dart';
import 'package:enaklo_pos/core/utils/helper_pdf_service.dart';
import 'package:enaklo_pos/presentation/report/widgets/report_page_title.dart';
import 'package:flutter/material.dart';
import 'package:enaklo_pos/core/utils/permession_handler.dart';
import 'package:enaklo_pos/core/utils/transaction_sales_invoice.dart';
import 'package:enaklo_pos/data/models/response/order_remote_datasource.dart';
import 'package:horizontal_data_table/horizontal_data_table.dart';

class TransactionReportWidget extends StatelessWidget {
  final String title;
  final String searchDateFormatted;
  final List<ItemOrder> transactionReport;
  final List<Widget>? headerWidgets;
  const TransactionReportWidget({
    super.key,
    required this.transactionReport,
    required this.title,
    required this.searchDateFormatted,
    required this.headerWidgets,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ReportPageTitle(
          title: title,
          searchDateFormatted: searchDateFormatted,
          onExport: () async {
            try {
              final status = await PermessionHelper().checkPermission();
              if (status) {
                final pdfFile = await TransactionSalesInvoice.generate(
                    transactionReport, searchDateFormatted);
                log("pdfFile: $pdfFile");
                await HelperPdfService.openFile(pdfFile);
              } else {
                ScaffoldMessenger.of(context).showSnackBar(
                  const SnackBar(
                    content: Text('Storage permission is required to save PDF'),
                    backgroundColor: Colors.red,
                  ),
                );
              }
            } catch (e) {
              log("Error generating PDF: $e");
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text('Failed to generate PDF: $e'),
                  backgroundColor: Colors.red,
                ),
              );
            }
          },
        ),
        const SpaceHeight(16.0),
        Expanded(
          child: Padding(
            padding: const EdgeInsets.all(12),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(12),
              child: HorizontalDataTable(
                leftHandSideColumnWidth: 50,
                rightHandSideColumnWidth: 1020,
                isFixedHeader: true,
                headerWidgets: headerWidgets,
                // isFixedFooter: true,
                // footerWidgets: _getTitleWidget(),
                leftSideItemBuilder: (context, index) {
                  return Container(
                    width: 40,
                    height: 52,
                    alignment: Alignment.centerLeft,
                    child: Center(
                        child: Text(transactionReport[index].id.toString())),
                  );
                },
                rightSideItemBuilder: (context, index) {
                  return Row(
                    children: <Widget>[
                      Container(
                        width: 120,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(
                          transactionReport[index].total!.currencyFormatRp,
                        )),
                      ),
                      Container(
                        width: 120,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(
                          transactionReport[index].subTotal!.currencyFormatRp,
                        )),
                      ),
                      Container(
                        width: 100,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(
                          transactionReport[index].tax!.currencyFormatRp,
                        )),
                      ),
                      Container(
                        width: 100,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                          child: Text(
                            int.parse(transactionReport[index]
                                    .discountAmount!
                                    .replaceAll('.00', ''))
                                .currencyFormatRp,
                          ),
                        ),
                      ),
                      Container(
                        width: 100,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                          child: Text(
                            transactionReport[index]
                                .serviceCharge!
                                .currencyFormatRp,
                          ),
                        ),
                      ),
                      Container(
                        width: 100,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                          child: Text(
                              transactionReport[index].totalItem.toString()),
                        ),
                      ),
                      Container(
                        width: 150,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                          child: Text(transactionReport[index].namaKasir!),
                        ),
                      ),
                      Container(
                        width: 230,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                          child: Text(transactionReport[index]
                              .transactionTime!
                              .toFormattedDate()),
                        ),
                      ),
                    ],
                  );
                },
                itemCount: transactionReport.length,
                rowSeparatorWidget: const Divider(
                  color: Colors.black38,
                  height: 1.0,
                  thickness: 0.0,
                ),
                leftHandSideColBackgroundColor: AppColors.white,
                rightHandSideColBackgroundColor: AppColors.white,

                itemExtent: 55,
              ),
            ),
          ),
        ),
      ],
    );
  }
}
