import 'dart:async';

import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:salespro_admin/PDF/pdf_data.dart';
import 'package:salespro_admin/PDF/print_pdf.dart';
import 'package:salespro_admin/commas.dart';
import 'package:salespro_admin/model/personal_information_model.dart';

import '../Screen/tax rates/tax_model.dart';
import '../const.dart';
import '../model/add_to_cart_model.dart';
import '../model/sale_transaction_model.dart';

///___________Quotation_PDF_Formats_______________________________________________________________________________________________________________________________________________________________
FutureOr<Uint8List> generateQuotationDocument({required SaleTransactionModel transactions, required PersonalInformationModel personalInformation}) async {
  final pw.Document doc = pw.Document();

  double totalAmount({required SaleTransactionModel transactions}) {
    double amount = 0;

    for (var element in transactions.productList!) {
      amount = amount + double.parse(element.subTotal.toString()) * double.parse(element.quantity.toString());
    }

    return amount;
  }

  doc.addPage(
    pw.MultiPage(
      // pageFormat: PdfPageFormat.letter.copyWith(marginBottom: 1.5 * PdfPageFormat.cm),
      margin: pw.EdgeInsets.zero,
      crossAxisAlignment: pw.CrossAxisAlignment.start,
      header: (pw.Context context) {
        return pw.Padding(
          padding: const pw.EdgeInsets.only(left: 20.0, right: 20, bottom: 20, top: 5),
          child: pw.Column(
            crossAxisAlignment: pw.CrossAxisAlignment.center,
            children: [
              ///________Company_Name_________________________________________________________
              pw.Container(
                width: double.infinity,
                padding: const pw.EdgeInsets.all(10.0),
                child: pw.Center(
                  child: pw.Text(
                    personalInformation.companyName,
                    style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 22.0, fontWeight: pw.FontWeight.bold),
                  ),
                ),
              ),

              ///______Phone________________________________________________________________
              pw.Container(
                width: double.infinity,
                padding: const pw.EdgeInsets.all(1.0),
                child: pw.Center(
                  child: pw.Text(
                    'Phone: ${personalInformation.phoneNumber}',
                    style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 14.0),
                  ),
                ),
              ),

              ///______Address________________________________________________________________
              pw.Container(
                width: double.infinity,
                padding: const pw.EdgeInsets.all(1.0),
                child: pw.Center(
                  child: pw.Text(
                    'Address: ${personalInformation.countryName}',
                    style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 14.0),
                  ),
                ),
              ),

              ///______Shop_GST________________________________________________________________
              personalInformation.gst.trim().isNotEmpty
                  ? pw.Container(
                      width: double.infinity,
                      padding: const pw.EdgeInsets.all(1.0),
                      child: pw.Center(
                        child: pw.Text(
                          'Shop ${personalInformation.gstOrTinNameType == "" ? 'GST' : personalInformation.gstOrTinNameType}: ${personalInformation.gst}',
                          style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 14.0),
                        ),
                      ),
                    )
                  : pw.Container(),

              ///________Bill/Invoice_________________________________________________________
              pw.Container(
                width: double.infinity,
                padding: const pw.EdgeInsets.all(10.0),
                child: pw.Center(
                    child: pw.Container(
                        decoration: pw.BoxDecoration(
                          border: pw.Border.all(color: PdfColors.black, width: 0.5),
                          borderRadius: const pw.BorderRadius.all(pw.Radius.circular(10)),
                        ),
                        child: pw.Padding(
                          padding: const pw.EdgeInsets.only(top: 2.0, bottom: 2, left: 5, right: 5),
                          child: pw.Text(
                            'Quotation',
                            style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 16.0, fontWeight: pw.FontWeight.bold),
                          ),
                        ))),
              ),

              ///___________price_section_____________________________________________________
              pw.Row(mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, crossAxisAlignment: pw.CrossAxisAlignment.start, children: [
                ///_________Left_Side__________________________________________________________
                pw.Column(children: [
                  ///_____Name_______________________________________
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 60.0,
                      child: pw.Text(
                        'Customer',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 140.0,
                      child: pw.Text(
                        transactions.customerName,
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),

                  ///_____Phone_______________________________________
                  pw.SizedBox(height: 2),
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 60.0,
                      child: pw.Text(
                        'Phone',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 140.0,
                      child: pw.Text(
                        transactions.customerPhone,
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),

                  ///_____Address_______________________________________
                  pw.SizedBox(height: 2),
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 60.0,
                      child: pw.Text(
                        'Address',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 140.0,
                      child: pw.Text(
                        transactions.customerAddress,
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),

                  ///_____Party GST_______________________________________
                  pw.SizedBox(height: transactions.customerGst.trim().isNotEmpty ? 2 : 0),
                  transactions.customerGst.trim().isNotEmpty
                      ? pw.Row(children: [
                          pw.SizedBox(
                            width: 60.0,
                            child: pw.Text(
                              'Party ${personalInformation.gstOrTinNameType == "" ? 'GST' : personalInformation.gstOrTinNameType}',
                              style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                            ),
                          ),
                          pw.SizedBox(
                            width: 10.0,
                            child: pw.Text(
                              ':',
                              style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                            ),
                          ),
                          pw.SizedBox(
                            width: 140.0,
                            child: pw.Text(
                              transactions.customerGst,
                              style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                            ),
                          ),
                        ])
                      : pw.Container(),

                  ///_____Remarks_______________________________________
                  // pw.SizedBox(height: 2),
                  // pw.Row(children: [
                  //   pw.SizedBox(
                  //     width: 60.0,
                  //     child: pw.Text(
                  //       'Remarks',
                  //       style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                  //     ),
                  //   ),
                  //   pw.SizedBox(
                  //     width: 10.0,
                  //     child: pw.Text(
                  //       ':',
                  //       style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                  //     ),
                  //   ),
                  //   pw.SizedBox(
                  //     width: 140.0,
                  //     child: pw.Text(
                  //       '',
                  //       style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                  //     ),
                  //   ),
                  // ]),
                ]),

                ///_________Right_Side___________________________________________________________
                pw.Column(children: [
                  ///______invoice_number_____________________________________________
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 50.0,
                      child: pw.Text(
                        'Invoice',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 125.0,
                      child: pw.Text(
                        '#${transactions.invoiceNumber}',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),
                  pw.SizedBox(height: 2),

                  ///_________Sells By________________________________________________
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 50.0,
                      child: pw.Text(
                        'Sells By',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 125.0,
                      child: pw.Text(
                        'Admin',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),
                  pw.SizedBox(height: 2),

                  ///______Date__________________________________________________________
                  pw.Row(crossAxisAlignment: pw.CrossAxisAlignment.start, children: [
                    pw.SizedBox(
                      width: 50.0,
                      child: pw.Text(
                        'Date',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.Container(
                      width: 125.0,
                      child: pw.Text(
                        '${DateFormat.yMd().format(DateTime.parse(transactions.purchaseDate))}, ${DateFormat.jm().format(DateTime.parse(transactions.purchaseDate))}',
                        // DateTimeFormat.format(DateTime.parse(transactions.purchaseDate), format: AmericanDateTimeFormats.),
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                  ]),
                  pw.SizedBox(height: 2),

                  ///______Status____________________________________________
                  pw.Row(children: [
                    pw.SizedBox(
                      width: 50.0,
                      child: pw.Text(
                        'Status',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 10.0,
                      child: pw.Text(
                        ':',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                      ),
                    ),
                    pw.SizedBox(
                      width: 125.0,
                      child: pw.Text(
                        transactions.isPaid! ? 'Paid' : 'Due',
                        style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontWeight: pw.FontWeight.bold),
                      ),
                    ),
                  ]),
                ]),
              ]),
            ],
          ),
        );
      },
      footer: (pw.Context context) {
        return pw.Column(children: [
          pw.Padding(
            padding: const pw.EdgeInsets.all(10.0),
            child: pw.Row(mainAxisAlignment: pw.MainAxisAlignment.spaceBetween, children: [
              pw.Container(
                alignment: pw.Alignment.centerRight,
                margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
                padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
                child: pw.Column(children: [
                  pw.Container(
                    width: 120.0,
                    height: 1.0,
                    color: PdfColors.black,
                  ),
                  pw.SizedBox(height: 4.0),
                  pw.Text(
                    'Signature of Customer',
                    style: pw.Theme.of(context).defaultTextStyle.copyWith(
                          color: PdfColors.black,
                          fontSize: 11,
                        ),
                  )
                ]),
              ),
              pw.Container(
                alignment: pw.Alignment.centerRight,
                margin: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
                padding: const pw.EdgeInsets.only(bottom: 3.0 * PdfPageFormat.mm),
                child: pw.Column(children: [
                  pw.Container(
                    width: 120.0,
                    height: 1.0,
                    color: PdfColors.black,
                  ),
                  pw.SizedBox(height: 4.0),
                  pw.Text(
                    'Authorized Signature',
                    style: pw.Theme.of(context).defaultTextStyle.copyWith(
                          color: PdfColors.black,
                          fontSize: 11,
                        ),
                  )
                ]),
              ),
            ]),
          ),
          pw.Text('', style: const pw.TextStyle(fontSize: 10, color: PdfColors.black)),
          pw.SizedBox(height: 5),
        ]);
      },
      build: (pw.Context context) => <pw.Widget>[
        pw.Padding(
          padding: const pw.EdgeInsets.only(left: 20.0, right: 20.0, bottom: 20.0),
          child: pw.Column(
            children: [
              ///___________Table__________________________________________________________
              pw.Table.fromTextArray(
                context: context,
                border: const pw.TableBorder(
                  left: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                  right: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                  bottom: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                  top: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                  verticalInside: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                  horizontalInside: pw.BorderSide(
                    color: PdfColors.grey600,
                  ),
                ),
                // headerDecoration: pw.BoxDecoration(color: PdfColor.fromHex('#D5D8DC')),
                columnWidths: <int, pw.TableColumnWidth>{
                  0: const pw.FlexColumnWidth(1),
                  1: const pw.FlexColumnWidth(6),
                  2: const pw.FlexColumnWidth(1.5),
                  3: const pw.FlexColumnWidth(1.5),
                  4: const pw.FlexColumnWidth(1.7),
                  5: const pw.FlexColumnWidth(1.5),
                },
                headerStyle: pw.TextStyle(color: PdfColors.black, fontSize: 11, fontWeight: pw.FontWeight.bold),
                rowDecoration: const pw.BoxDecoration(color: PdfColors.white),
                // oddRowDecoration: const pw.BoxDecoration(color: PdfColors.grey100),
                headerAlignments: <int, pw.Alignment>{
                  0: pw.Alignment.center,
                  1: pw.Alignment.centerLeft,
                  2: pw.Alignment.center,
                  3: pw.Alignment.center,
                  4: pw.Alignment.centerRight,
                  5: pw.Alignment.centerRight,
                },
                cellAlignments: <int, pw.Alignment>{
                  0: pw.Alignment.center,
                  1: pw.Alignment.centerLeft,
                  2: pw.Alignment.center,
                  3: pw.Alignment.center,
                  4: pw.Alignment.centerRight,
                  5: pw.Alignment.centerRight,
                },
                data: <List<String>>[
                  <String>['SL', 'Product Description', 'Warranty', 'Quantity', 'Unit Price', 'Price'],
                  for (int i = 0; i < transactions.productList!.length; i++)
                    <String>[
                      ('${i + 1}'),
                      (transactions.productList!.elementAt(i).productName.toString()),
                      (transactions.productList![i].productWarranty?.toString() ?? ''),
                      (myFormat.format(double.tryParse(transactions.productList!.elementAt(i).quantity.toString()) ?? 0)),
                      (myFormat.format(double.tryParse(transactions.productList!.elementAt(i).subTotal.toString()) ?? 0)),
                      (myFormat.format(double.tryParse(
                              ((double.tryParse(transactions.productList!.elementAt(i).subTotal) ?? 0) * transactions.productList!.elementAt(i).quantity.toInt()).toString()) ??
                          0))
                    ],
                ],
              ),
              // pw.SizedBox(width: 5),
              pw.Paragraph(text: ""),
              pw.Row(
                mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                children: [
                  pw.Column(crossAxisAlignment: pw.CrossAxisAlignment.start, children: [
                    pw.Text(
                      "Payment Method: Just Quotation",
                      style: const pw.TextStyle(
                        color: PdfColors.black,
                        fontSize: 11,
                      ),
                    ),
                    pw.SizedBox(height: 10.0),
                    pw.Container(
                      width: 300,
                      child: pw.Text(
                        amountToWords(transactions.totalAmount!.toInt()),
                        maxLines: 3,
                        style: pw.TextStyle(color: PdfColors.black, fontSize: 11, fontWeight: pw.FontWeight.bold),
                      ),
                    )
                  ]),
                  pw.SizedBox(
                    width: 250.0,
                    child: pw.Column(
                      crossAxisAlignment: pw.CrossAxisAlignment.end,
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Column(children: [
                          ///________Total_Amount_____________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                'Total Amount',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                myFormat.format(double.tryParse(totalAmount(transactions: transactions).toString()) ?? 0),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///________vat_______________________________________________
                          pw.ListView.builder(
                            itemCount: getAllTaxFromCartList(cart: transactions.productList ?? []).length,
                            itemBuilder: (context, index) {
                              return pw.Row(children: [
                                pw.SizedBox(
                                  width: 100.0,
                                  child: pw.Text(
                                    getAllTaxFromCartList(cart: transactions.productList ?? [])[index].name,
                                    style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                          color: PdfColors.black,
                                          fontSize: 11,
                                        ),
                                  ),
                                ),
                                pw.Container(
                                  alignment: pw.Alignment.centerRight,
                                  width: 150.0,
                                  child: pw.Text(
                                    '${getAllTaxFromCartList(cart: transactions.productList ?? [])[index].taxRate}%',
                                    style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                          color: PdfColors.black,
                                          fontSize: 11,
                                        ),
                                  ),
                                ),
                              ]);
                            },
                          ),
                          // pw.Row(children: [
                          //   pw.SizedBox(
                          //     width: 100.0,
                          //     child: pw.Text(
                          //       'Vat',
                          //       style: pw.Theme.of(context).defaultTextStyle.copyWith(
                          //             color: PdfColors.black,
                          //             fontSize: 11,
                          //           ),
                          //     ),
                          //   ),
                          //   pw.Container(
                          //     alignment: pw.Alignment.centerRight,
                          //     width: 150.0,
                          //     child: pw.Text(
                          //       myFormat.format(double.tryParse(transactions.vat.toString()) ?? 0),
                          //       style: pw.Theme.of(context).defaultTextStyle.copyWith(
                          //             color: PdfColors.black,
                          //             fontSize: 11,
                          //           ),
                          //     ),
                          //   ),
                          // ]),
                          pw.SizedBox(height: 2),

                          ///________Service/Shipping__________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                "Service/Shipping",
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                myFormat.format(double.tryParse(transactions.serviceCharge.toString()) ?? 0),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///_________divider__________________________________________
                          pw.Divider(thickness: .5, height: 0.5, color: PdfColors.black),
                          pw.SizedBox(height: 2),

                          ///________Sub Total Amount_______________________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                'Sub-Total',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                myFormat.format(
                                    double.tryParse((transactions.vat!.toDouble() + transactions.serviceCharge!.toDouble() + totalAmount(transactions: transactions)).toString()) ??
                                        0),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///________Discount_______________________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                'Discount',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                '- ${myFormat.format(double.tryParse(transactions.discountAmount.toString()) ?? 0)}',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///_________divider__________________________________________
                          pw.Divider(thickness: .5, height: 0.5, color: PdfColors.black),
                          pw.SizedBox(height: 2),

                          ///________payable_Amount_______________________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 150.0,
                              child: pw.Text(
                                'Net Payable Amount',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 11, fontWeight: pw.FontWeight.bold),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 100.0,
                              child: pw.Text(
                                myFormat.format(double.tryParse(transactions.totalAmount.toString()) ?? 0),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black, fontSize: 11, fontWeight: pw.FontWeight.bold),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///________Received_Amount_______________________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                'Received Amount',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                myFormat.format(transactions.totalAmount! - transactions.dueAmount!),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),

                          ///_________divider__________________________________________
                          pw.Divider(thickness: .5, height: 0.5, color: PdfColors.black),
                          pw.SizedBox(height: 2),

                          ///________Received_Amount_______________________________________________
                          pw.Row(children: [
                            pw.SizedBox(
                              width: 100.0,
                              child: pw.Text(
                                'Due Amount',
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                            // pw.SizedBox(
                            //   width: 10.0,
                            //   child: pw.Text(
                            //     ':',
                            //     style: pw.Theme.of(context).defaultTextStyle.copyWith(color: PdfColors.black),
                            //   ),
                            // ),
                            pw.Container(
                              alignment: pw.Alignment.centerRight,
                              width: 150.0,
                              child: pw.Text(
                                myFormat.format(double.tryParse(transactions.dueAmount!.toString()) ?? 0),
                                style: pw.Theme.of(context).defaultTextStyle.copyWith(
                                      color: PdfColors.black,
                                      fontSize: 11,
                                    ),
                              ),
                            ),
                          ]),
                          pw.SizedBox(height: 2),
                        ]),
                      ],
                    ),
                  ),
                ],
              ),
              pw.Padding(padding: const pw.EdgeInsets.all(10)),
            ],
          ),
        ),
      ],
    ),
  );

  return doc.save();
}

//--------------3 inch thermal printer---------------------
Future<Uint8List> generate3InchQuotationDocument({
  required SaleTransactionModel transactions,
  required PersonalInformationModel personalInformation,
}) async {
  final doc = pw.Document();

  double totalAmount({required SaleTransactionModel transactions}) {
    double amount = 0;
    for (var element in transactions.productList!) {
      amount = amount + double.parse(element.subTotal) * double.parse(element.quantity.toString());
    }
    return double.parse(amount.toStringAsFixed(2));
  }

  final String imageUrl = personalInformation.pictureUrl;
  dynamic imageData = await PDFCommonFunctions().getNetworkImage(imageUrl);
  imageData ??= await PDFCommonFunctions().loadAssetImage(appLogo);

  // Load the NotoSans font
  final font = await rootBundle.load('fonts/NotoSans/NotoSans-Regular.ttf');
  final boldFont = await rootBundle.load('fonts/NotoSans/NotoSans-SemiBold.ttf');
  final notoSansBold = pw.Font.ttf(boldFont);
  final notoSans = pw.Font.ttf(font);

  // Define styles for thermal printer (consistent with invoice)
  final headerStyle = pw.TextStyle(
    fontSize: 12,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );
  //------------store style---------------
  final storeStyle = pw.TextStyle(
    fontSize: 14,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );

  final normalStyle = pw.TextStyle(
    fontSize: 9,
    font: notoSans,
  );
  final smallStyle = pw.TextStyle(
    fontSize: 9,
    font: notoSans,
  );
  final boldStyle = pw.TextStyle(
    fontSize: 9,
    fontWeight: pw.FontWeight.bold,
    font: notoSansBold,
  );

  doc.addPage(
    pw.Page(
      pageFormat: PdfPageFormat(80 * PdfPageFormat.mm, double.infinity, marginAll: 3),
      build: (pw.Context context) {
        return pw.Column(
          crossAxisAlignment: pw.CrossAxisAlignment.start,
          children: [
            /// Header Section (same as invoice)
            if (imageData is Uint8List)
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.Image(
                    pw.MemoryImage(imageData),
                    fit: pw.BoxFit.cover,
                  ),
                ),
              )
            else if (imageData is String)
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.SvgImage(
                    svg: imageData,
                    fit: pw.BoxFit.cover,
                  ),
                ),
              )
            else
              pw.Center(
                child: pw.Container(
                  height: 54.12,
                  width: 52,
                  child: pw.Image(pw.MemoryImage(imageData)),
                ),
              ),
            pw.SizedBox(width: 20.0),
            pw.Center(
              child: pw.Text(
                personalInformation.companyName,
                style: storeStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
            pw.SizedBox(height: 2),
            pw.Center(
              child: pw.Text(
                'Mobile: ${personalInformation.phoneNumber}',
                style: normalStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
            pw.SizedBox(height: 2),
            pw.Center(
              child: pw.Padding(
                padding: pw.EdgeInsets.only(right: 22),
                child: pw.Text(
                  'Address: ${personalInformation.countryName ?? 'n/a'}',
                  style: normalStyle,
                  textAlign: pw.TextAlign.center,
                ),
              ),
            ),
            if (personalInformation.gst.trim().isNotEmpty) pw.SizedBox(height: 2),
            if (personalInformation.gst.trim().isNotEmpty)
              pw.Center(
                child: pw.Text(
                  '${personalInformation.gstOrTinNameType == "" ? 'GST' : personalInformation.gstOrTinNameType}: ${personalInformation.gst}',
                  style: normalStyle,
                  textAlign: pw.TextAlign.center,
                ),
              ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.DecoratedBox(
                  decoration: pw.BoxDecoration(
                    border: pw.Border(
                      bottom: pw.BorderSide(color: PdfColors.black),
                    ),
                  ),
                  child: pw.Text(
                    'QUOTATION',
                    style: headerStyle,
                  )),
            ),
            pw.SizedBox(height: 14),

            // Quotation Info (same layout as invoice)
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Invoice No',
                    value: transactions.invoiceNumber,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.Expanded(
                  child: buildInfoEnd(
                    title: 'Date',
                    value: DateFormat.yMd().format(DateTime.parse(transactions.purchaseDate)),
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 2),
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Name',
                    value: transactions.customerName,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.Expanded(
                  child: buildInfoEnd(
                    title: 'Time',
                    value: DateFormat.jm().format(DateTime.parse(transactions.purchaseDate)),
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 2),
            pw.Row(
              crossAxisAlignment: pw.CrossAxisAlignment.end,
              mainAxisAlignment: pw.MainAxisAlignment.end,
              children: [
                pw.Expanded(
                  child: buildInfo(
                    title: 'Mobile',
                    value: transactions.customerPhone,
                    normalFont: notoSans,
                    boldFont: notoSans,
                  ),
                ),
                pw.Expanded(
                  child: pw.Text('Prepared By: ${'Admin'}', style: normalStyle, textAlign: pw.TextAlign.end),
                ),
                pw.SizedBox(width: 22),
              ],
            ),
            pw.SizedBox(height: 12),

            // Products Table (same style as invoice)
            buildDottedLine(),
            pw.Padding(
              padding: pw.EdgeInsets.symmetric(vertical: 4),
              child: pw.Row(children: [
                pw.Container(
                  margin: pw.EdgeInsets.zero,
                  padding: pw.EdgeInsets.zero,
                  width: 20,
                  alignment: pw.Alignment.centerLeft,
                  child: pw.Text('SL', style: boldStyle),
                ),
                pw.Container(
                  width: 75,
                  alignment: pw.Alignment.centerLeft,
                  child: pw.Text('Item', style: boldStyle),
                ),
                pw.Container(
                  width: 27,
                  alignment: pw.Alignment.center,
                  child: pw.Text('Qty', style: boldStyle),
                ),
                pw.Container(
                  width: 32,
                  alignment: pw.Alignment.center,
                  child: pw.Text('Price', style: boldStyle),
                ),
                pw.Container(
                  width: 45,
                  alignment: pw.Alignment.centerRight,
                  child: pw.Text('Amount', style: boldStyle),
                ),
              ]),
            ),
            buildDottedLine(),
            pw.SizedBox(height: 2),
            // Product List (same style as invoice)
            pw.Column(
              children: List.generate(
                transactions.productList?.length ?? 0,
                (i) {
                  final product = transactions.productList![i];
                  return pw.Padding(
                    padding: const pw.EdgeInsets.symmetric(vertical: 1),
                    child: pw.Row(
                      crossAxisAlignment: pw.CrossAxisAlignment.start,
                      children: [
                        pw.Container(
                          margin: pw.EdgeInsets.zero,
                          padding: pw.EdgeInsets.zero,
                          width: 20,
                          alignment: pw.Alignment.centerLeft,
                          child: pw.Text('${i + 1}', style: smallStyle),
                        ),
                        pw.Container(
                          width: 75,
                          alignment: pw.Alignment.centerLeft,
                          child: pw.Text(
                            product.productName ?? '',
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 27,
                          alignment: pw.Alignment.center,
                          child: pw.Text(
                            '${product.quantity ?? 0}',
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 32,
                          alignment: pw.Alignment.center,
                          child: pw.Text(
                            (myFormat.format(double.tryParse(product.subTotal.toString()) ?? 0)),
                            style: smallStyle,
                          ),
                        ),
                        pw.Container(
                          width: 45,
                          alignment: pw.Alignment.centerRight,
                          child: pw.Text(
                            myFormat.format(double.parse(product.subTotal) * product.quantity.toDouble()),
                            style: smallStyle,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
            pw.SizedBox(height: 2),
            buildDottedLine(),
            pw.SizedBox(height: 6),
            // Summary Section (same style as invoice)
            pw.Align(
              alignment: pw.Alignment.centerRight,
              child: pw.SizedBox(
                  width: 160,
                  child: pw.Column(crossAxisAlignment: pw.CrossAxisAlignment.end, children: [
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Sub-Total : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(totalAmount(transactions: transactions)),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    // '- ${myFormat.format(double.tryParse(transactions.discountAmount.toString()) ?? 0)}',
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Shipping Charge : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse(transactions.serviceCharge.toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Discount : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            '- ${myFormat.format(double.tryParse(transactions.discountAmount.toString()) ?? 0)}',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    buildDottedLine(),
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Total Payable : ',
                            style: boldStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            myFormat.format(double.tryParse(transactions.totalAmount.toString()) ?? 0),
                            style: boldStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Received Amount : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            // myFormat.format(transactions.totalAmount! - transactions.dueAmount!),
                            '0',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                    pw.SizedBox(height: 4),
                    // myFormat.format(transactions.totalAmount! - transactions.dueAmount!),
                    // myFormat.format(double.tryParse(transactions.dueAmount!.toString()) ?? 0),
                    pw.Row(
                      mainAxisAlignment: pw.MainAxisAlignment.end,
                      children: [
                        pw.Expanded(
                          flex: 2,
                          child: pw.Text(
                            'Due Amount : ',
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.Expanded(
                          child: pw.Text(
                            // myFormat.format(double.tryParse(transactions.dueAmount!.toString()) ?? 0),
                            myFormat.format(double.tryParse(transactions.totalAmount.toString()) ?? 0),
                            style: normalStyle,
                            textAlign: pw.TextAlign.end,
                          ),
                        ),
                        pw.SizedBox(width: 22),
                      ],
                    ),
                  ])),
            ),
            pw.SizedBox(height: 4),
            buildDottedLine(),
            pw.SizedBox(height: 4),
            // Footer
            // pw.Row(
            //   mainAxisAlignment: pw.MainAxisAlignment.spaceAround,
            //   children: [
            //     pw.Column(
            //       children: [
            //         pw.Text('Customer Signature', style: smallStyle),
            //         pw.SizedBox(height: 15),
            //         pw.Text('___________________', style: smallStyle),
            //       ],
            //     ),
            //     pw.Column(
            //       children: [
            //         pw.Text('Authorized Signature', style: smallStyle),
            //         pw.SizedBox(height: 15),
            //         pw.Text('___________________', style: smallStyle),
            //       ],
            //     ),
            //   ],
            // ),
            // pw.SizedBox(height: 5),
            // pw.Center(
            //   child: pw.Text(
            //     'Powered By ${generalSetting.companyName.isNotEmpty ? generalSetting.companyName : 'YourAppName'}',
            //     style: pw.TextStyle(fontSize: 7, color: PdfColors.black),
            //   ),
            // ),
            // buildInfo(
            //   title: 'Payment Type',
            //   value: transactions.paymentType.toString(),
            //   normalFont: notoSans,
            //   boldFont: notoSansBold,
            // ),
            // pw.SizedBox(height: 14),
            // pw.Padding(
            //   padding: pw.EdgeInsets.only(right: 22),
            //   child: pw.Text(
            //     'Note : Goods once sold will not be taken back or exchanged',
            //     textAlign: pw.TextAlign.center,
            //     style: normalStyle,
            //   ),
            // ),
            if (personalInformation.invoiceNote.toString().isNotEmpty)
              pw.Padding(
                padding: pw.EdgeInsets.only(right: 22),
                child: buildInfo(
                  title: 'Note',
                  value: personalInformation.invoiceNote.toString(),
                  normalFont: notoSansBold,
                  boldFont: notoSans,
                ),
              ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.BarcodeWidget(
                barcode: pw.Barcode.qrCode(),
                data: 'https://acnoo.com',
                // width: 65,
                // height: 49,
                width: 55,
                height: 39,
              ),
            ),
            pw.SizedBox(height: 6),
            pw.Center(
              child: pw.Text(
                'Developed by acnoo.com',
                style: normalStyle,
                textAlign: pw.TextAlign.center,
              ),
            ),
          ],
        );
      },
    ),
  );

  return doc.save();
}

pw.Widget buildInfo({required String title, required String value, required pw.Font normalFont, required pw.Font boldFont}) {
  final normalStyle = pw.TextStyle(fontSize: 9);
  final boldStyle = pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9);
  return pw.RichText(
    text: pw.TextSpan(
      baseline: 1,
      text: '$title: ',
      style: normalStyle.copyWith(font: normalFont),
      children: [
        pw.TextSpan(text: value, style: boldStyle.copyWith(font: boldFont)),
      ],
    ),
  );
}

double calculateThermalVat({
  required TaxModel tax,
  required List<AddToCartModel> products,
}) {
  double totalVat = 0;

  for (final product in products) {
    // Match product's tax rate and name with the given tax
    if (product.groupTaxRate == tax.taxRate && product.taxType == tax.name) {
      double price = double.tryParse(product.productPurchasePrice.toString()) ?? 0;
      double taxRate = product.groupTaxRate / 100;
      double quantity = product.quantity.toDouble() ?? 1;
      double vat = 0;

      if (product.taxType.toLowerCase() == 'inclusive') {
        double netPrice = price / (1 + taxRate);
        double taxPerUnit = price - netPrice;
        vat = taxPerUnit * quantity;
      } else {
        vat = price * taxRate * quantity;
      }

      totalVat += vat;
    }
  }

  return totalVat;
}

pw.Widget buildInfoEnd({
  required String title,
  required String value,
  required pw.Font normalFont,
  required pw.Font boldFont,
}) {
  final normalStyle = pw.TextStyle(fontSize: 9);
  final boldStyle = pw.TextStyle(fontWeight: pw.FontWeight.bold, fontSize: 9);
  return pw.RichText(
    textAlign: pw.TextAlign.end,
    text: pw.TextSpan(
      baseline: 1,
      text: '$title: ',
      style: normalStyle.copyWith(font: normalFont),
      children: [
        pw.TextSpan(text: value, style: boldStyle.copyWith(font: boldFont)),
      ],
    ),
  );
}

pw.Widget buildDottedLine() {
  return pw.SizedBox(
    width: 250,
    height: 1,
    child: pw.Stack(
      children: [
        pw.Positioned(
          top: 0,
          left: 0,
          child: pw.Container(
            width: 250,
            height: 1,
            child: pw.LayoutBuilder(
              builder: (context, constraints) {
                final totalWidth = constraints!.maxWidth;
                const dotWidth = 2.5;
                const space = 2.0;
                final dotCount = (totalWidth / (dotWidth + space)).floor();

                return pw.Row(
                  children: List.generate(dotCount, (index) {
                    return pw.Container(
                      width: dotWidth,
                      height: 0.5,
                      color: PdfColors.black,
                      margin: const pw.EdgeInsets.only(right: space),
                    );
                  }),
                );
              },
            ),
          ),
        ),
      ],
    ),
  );

// pw.Widget buildDottedLine() {
//   return pw.Row(
//     children: List.generate(80, (index) {
//       return pw.Container(
//         height: 0.5,
//         width: 2.5,
//         color: PdfColor.fromInt(0xff000000), // Corrected color code
//         margin: const pw.EdgeInsets.only(right: 2), // Add spacing between dots
//       );
//     }),
//   );
// }
}
