import 'package:intl/intl.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

class CorteCajaTicket {
  static Future<void> imprimirTicket({
    required double fondo,
    required double fondoDiaSig,
    required double ventaEfe,
    required double ventaTarj,
    required double ventaTrans,
    required double deposito,
    required double retiro,
    required double gasto,
    required double corteFinal,
    DateTime? fechaApertura,
  }) async {
    final pdf = pw.Document();
    final numberFormat = NumberFormat('#,##0.00', 'en_US');

    pdf.addPage(pw.Page(
      pageFormat: PdfPageFormat.roll57,
      build: (pw.Context context) {
        return pw.Column(
          crossAxisAlignment: pw.CrossAxisAlignment.start,
          children: [
            pw.Text(
              "Fecha: ${DateFormat('dd/MM/yyyy').format(fechaApertura!)}",
              style: pw.TextStyle(fontSize: 11, fontWeight: pw.FontWeight.bold),
            ),
            _crearLinea("Fondo Inicial:", numberFormat.format(fondo)),
            _crearLinea("Fondo Siguiente:", numberFormat.format(fondoDiaSig)),
            _crearLinea("Ventas Efe:", numberFormat.format(ventaEfe)),
            _crearLinea("Ventas Tarj:", numberFormat.format(ventaTarj)),
            _crearLinea("Venta Trans:", numberFormat.format(ventaTrans)),
            _crearLinea("Depositos:", numberFormat.format(deposito)),
            _crearLinea("Retiros:", numberFormat.format(retiro)),
            _crearLinea("Gastos:", numberFormat.format(gasto)),
            pw.Divider(),
            _crearLinea("Venta Total del día:",
                numberFormat.format(ventaEfe + ventaTarj + ventaTrans)),
            _crearLinea("Efectivo en caja:", numberFormat.format(corteFinal)),
            _crearLinea(
                "Salidas totales día:", numberFormat.format(retiro + gasto)),
          ],
        );
      },
    ));

    await Printing.layoutPdf(
      onLayout: (PdfPageFormat format) async => pdf.save(),
    );
  }

  static pw.Widget _crearLinea(String texto, String valor) {
    return pw.Row(
      mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
      children: [
        pw.Text(texto, style: pw.TextStyle(fontSize: 10)),
        pw.Text("\$$valor", style: pw.TextStyle(fontSize: 10)),
      ],
    );
  }
}
