import 'package:flutter/material.dart'; import '../services/dio_client.dart'; import '../services/order_service.dart'; class OrderInvoiceScreen extends StatefulWidget { final String orderId; const OrderInvoiceScreen({super.key, required this.orderId}); @override State createState() => _OrderInvoiceScreenState(); } class _OrderInvoiceScreenState extends State { bool loading = true; Map invoice = {}; @override void initState() { super.initState(); load(); } Future load() async { final service = OrderService(DioClient.getInstance(context)); final res = await service.getInvoice(widget.orderId); if (res['success'] == true) { invoice = res['invoice'] ?? {}; } loading = false; setState(() {}); } Widget _row(String label, dynamic value) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(label, style: const TextStyle(fontSize: 14, color: Colors.grey)), Text(value?.toString() ?? "N/A", style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600)), ], ), ); } @override Widget build(BuildContext context) { final items = invoice['items'] ?? []; return Scaffold( appBar: AppBar(title: const Text("Invoice")), body: loading ? const Center(child: CircularProgressIndicator()) : Padding( padding: const EdgeInsets.all(16), child: ListView( children: [ const Text("Invoice Summary", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), const SizedBox(height: 10), _row("Invoice No", invoice['invoice_number']), _row("Invoice Date", invoice['invoice_date']), _row("Due Date", invoice['due_date']), _row("Payment Method", invoice['payment_method']), _row("Reference No", invoice['reference_no']), _row("Status", invoice['status']), const Divider(height: 30), const Text("Amount Details", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)), const SizedBox(height: 10), _row("Amount (without GST)", invoice['final_amount']), _row("GST Amount", invoice['gst_amount']), _row("Final Amount With GST", invoice['final_amount_with_gst']), _row("Tax Type", invoice['tax_type']), _row("CGST %", invoice['cgst_percent']), _row("SGST %", invoice['sgst_percent']), _row("IGST %", invoice['igst_percent']), const Divider(height: 30), _row("Customer Name", invoice['customer_name']), _row("Company Name", invoice['company_name']), _row("Email", invoice['customer_email']), _row("Mobile", invoice['customer_mobile']), _row("Address", invoice['customer_address']), _row("Pincode", invoice['pincode']), _row("Notes", invoice['notes']), const Divider(height: 30), // PDF DOWNLOAD if (invoice['pdf_path'] != null) TextButton.icon( onPressed: () { // open pdf }, icon: const Icon(Icons.picture_as_pdf, color: Colors.red), label: const Text("Download PDF"), ), const SizedBox(height: 20), const Text("Invoice Items", style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold)), const SizedBox(height: 10), ...List.generate(items.length, (i) { final item = items[i]; return Card( margin: const EdgeInsets.only(bottom: 12), child: ListTile( title: Text(item['description'] ?? "Item"), subtitle: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text("Qty: ${item['qty'] ?? 0}"), Text("Price: ₹${item['price'] ?? 0}"), ], ), trailing: Text( "₹${item['ttl_amount'] ?? 0}", style: const TextStyle( fontWeight: FontWeight.bold, color: Colors.indigo), ), ), ); }), ], ), ), ); } }