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/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/item_sales_invoice.dart';
import 'package:enaklo_pos/core/utils/permession_handler.dart';
import 'package:enaklo_pos/data/models/response/item_sales_response_model.dart';
import 'package:horizontal_data_table/horizontal_data_table.dart';

class ItemSalesReportWidget extends StatelessWidget {
  final String title;
  final String searchDateFormatted;
  final List<ItemSales> itemSales;
  final List<Widget>? headerWidgets;
  const ItemSalesReportWidget({
    super.key,
    required this.itemSales,
    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 ItemSalesInvoice.generate(
                    itemSales, 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: 80,
                rightHandSideColumnWidth: 670,
                isFixedHeader: true,
                headerWidgets: headerWidgets,

                // isFixedFooter: true,
                // footerWidgets: _getTitleWidget(),
                leftSideItemBuilder: (context, index) {
                  return Container(
                    width: 80,
                    height: 52,
                    alignment: Alignment.centerLeft,
                    child: Center(child: Text(itemSales[index].id.toString())),
                  );
                },
                rightSideItemBuilder: (context, index) {
                  return Row(
                    children: <Widget>[
                      Container(
                        width: 100,
                        height: 52,
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(itemSales[index].orderId.toString())),
                      ),
                      Container(
                        width: 200,
                        height: 52,
                        alignment: Alignment.centerLeft,
                        child:
                            Center(child: Text(itemSales[index].productName!)),
                      ),
                      Container(
                        width: 60,
                        height: 52,
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(itemSales[index].quantity.toString())),
                      ),
                      Container(
                        width: 150,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(
                          itemSales[index].price!.currencyFormatRp,
                        )),
                      ),
                      Container(
                        width: 160,
                        height: 52,
                        padding: const EdgeInsets.fromLTRB(5, 0, 0, 0),
                        alignment: Alignment.centerLeft,
                        child: Center(
                            child: Text(
                          (itemSales[index].price! * itemSales[index].quantity!)
                              .currencyFormatRp,
                        )),
                      ),
                    ],
                  );
                },
                itemCount: itemSales.length,
                rowSeparatorWidget: const Divider(
                  color: Colors.black38,
                  height: 1.0,
                  thickness: 0.0,
                ),
                leftHandSideColBackgroundColor: AppColors.white,
                rightHandSideColBackgroundColor: AppColors.white,

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