import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import 'package:provider/provider.dart'; import '../../providers/app_provider.dart'; import '../../theme/app_theme.dart'; import '../../widgets/signature_pad.dart'; class FirmaScreen extends StatefulWidget { const FirmaScreen({Key? key}) : super(key: key); @override State createState() => _FirmaScreenState(); } class _FirmaScreenState extends State { final _padKey = GlobalKey(); bool _firmado = false; bool _guardando = false; final _fmt = NumberFormat.currency(locale: 'en_US', symbol: '\$', decimalDigits: 2); @override Widget build(BuildContext context) { final provider = context.watch(); return Scaffold( backgroundColor: AppTheme.bgColor, appBar: AppBar( title: const Text('Firma del Cliente', style: TextStyle(fontWeight: FontWeight.bold)), ), body: Column( children: [ Expanded( child: SingleChildScrollView( child: Column( children: [ _buildResumenPedido(provider), const SizedBox(height: 16), _buildSignatureArea(context), ], ), ), ), _buildBottomBar(context, provider), ], ), ); } Widget _buildResumenPedido(AppProvider provider) { final cliente = provider.clienteSeleccionado; return Container( margin: const EdgeInsets.all(16), padding: const EdgeInsets.all(16), decoration: BoxDecoration( gradient: const LinearGradient( colors: [Color(0xFF1E3A8A), Color(0xFF2563EB)], begin: Alignment.topLeft, end: Alignment.bottomRight, ), borderRadius: BorderRadius.circular(16), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: const [ Icon(Icons.receipt_long, color: Colors.white70, size: 16), SizedBox(width: 6), Text('Resumen del pedido', style: TextStyle(color: Colors.white70, fontSize: 13)), ], ), const SizedBox(height: 12), Row( children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Cliente:', style: TextStyle(color: Colors.white60, fontSize: 11)), Text( cliente?.nombre ?? '—', style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 15), ), const SizedBox(height: 4), const Text('Dirección:', style: TextStyle(color: Colors.white60, fontSize: 11)), Text( cliente?.direccion ?? '—', style: const TextStyle(color: Colors.white70, fontSize: 12), maxLines: 1, overflow: TextOverflow.ellipsis, ), ], ), ), const SizedBox(width: 16), Column( crossAxisAlignment: CrossAxisAlignment.end, children: [ const Text('Total:', style: TextStyle(color: Colors.white70, fontSize: 13)), Text( _fmt.format(provider.totalCarrito), style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 24), ), Text( '${provider.cantidadCarrito} unidades', style: const TextStyle(color: Colors.white60, fontSize: 11), ), ], ), ], ), const Divider(color: Colors.white24, height: 20), Text( '${provider.carrito.length} producto(s): ' '${provider.carrito.map((i) => i.producto.nombre.split(' ').first).take(3).join(', ')}' '${provider.carrito.length > 3 ? '...' : ''}', style: const TextStyle(color: Colors.white60, fontSize: 11), ), ], ), ); } Widget _buildSignatureArea(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Column( crossAxisAlignment: CrossAxisAlignment.start, children: const [ Text('Firma de conformidad', style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)), Text('El cliente firma confirmando el pedido', style: TextStyle(fontSize: 12, color: AppTheme.textSecondary)), ], ), TextButton.icon( icon: const Icon(Icons.refresh, size: 16), label: const Text('Limpiar'), onPressed: () { _padKey.currentState?.clear(); setState(() => _firmado = false); }, ), ], ), const SizedBox(height: 10), Container( height: 220, decoration: BoxDecoration( borderRadius: BorderRadius.circular(14), boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.08), blurRadius: 10, offset: const Offset(0, 4))], ), child: Stack( children: [ SignaturePad( key: _padKey, onChanged: (strokes) { setState(() => _firmado = strokes.isNotEmpty); }, ), if (!_firmado) Positioned.fill( child: IgnorePointer( child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.draw_outlined, size: 40, color: Colors.grey.shade300), const SizedBox(height: 8), Text( 'Firmar aquí', style: TextStyle(fontSize: 16, color: Colors.grey.shade400, fontWeight: FontWeight.w500), ), ], ), ), ), ), ], ), ), const SizedBox(height: 10), const Divider(), Padding( padding: const EdgeInsets.symmetric(vertical: 6), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: const [ Text('Nombre del firmante:', style: TextStyle(fontSize: 12, color: AppTheme.textSecondary)), Text('Responsable de recepción', style: TextStyle(fontSize: 12, color: AppTheme.textSecondary)), ], ), ), if (_firmado) Container( margin: const EdgeInsets.only(top: 8), padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8), decoration: BoxDecoration( color: AppTheme.successColor.withOpacity(0.1), borderRadius: BorderRadius.circular(8), border: Border.all(color: AppTheme.successColor.withOpacity(0.3)), ), child: Row( children: const [ Icon(Icons.check_circle_outline, color: AppTheme.successColor, size: 16), SizedBox(width: 8), Text('Firma registrada', style: TextStyle(color: AppTheme.successColor, fontSize: 12, fontWeight: FontWeight.w600)), ], ), ), ], ), ); } Widget _buildBottomBar(BuildContext context, AppProvider provider) { return Container( padding: const EdgeInsets.fromLTRB(16, 12, 16, 28), decoration: const BoxDecoration( color: Colors.white, boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 8, offset: Offset(0, -2))], ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Row( children: [ Expanded( child: OutlinedButton( onPressed: () => Navigator.pop(context), child: const Text('Volver al pedido'), ), ), const SizedBox(width: 12), Expanded( flex: 2, child: ElevatedButton.icon( icon: _guardando ? const SizedBox(width: 18, height: 18, child: CircularProgressIndicator(color: Colors.white, strokeWidth: 2)) : const Icon(Icons.check_circle_outline, size: 22), label: Text(_guardando ? 'Guardando...' : 'Confirmar pedido', style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600)), onPressed: (!_firmado || _guardando) ? null : () async { setState(() => _guardando = true); final pedido = await provider.confirmarPedido(firmaBase64: 'signed'); if (mounted) { _showSuccess(context, pedido.id); } }, style: ElevatedButton.styleFrom( padding: const EdgeInsets.symmetric(vertical: 14), ), ), ), ], ), const SizedBox(height: 8), if (!_firmado) const Text( 'El cliente debe firmar para confirmar el pedido', style: TextStyle(fontSize: 11, color: AppTheme.textSecondary), textAlign: TextAlign.center, ), ], ), ); } void _showSuccess(BuildContext context, String pedidoId) { showDialog( context: context, barrierDismissible: false, builder: (_) => AlertDialog( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)), content: Column( mainAxisSize: MainAxisSize.min, children: [ Container( width: 72, height: 72, decoration: const BoxDecoration( color: Color(0xFFDCFCE7), shape: BoxShape.circle, ), child: const Icon(Icons.check, color: AppTheme.successColor, size: 40), ), const SizedBox(height: 16), const Text('¡Pedido confirmado!', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold, color: AppTheme.textPrimary)), const SizedBox(height: 8), Text( '$pedidoId ha sido registrado correctamente.', style: const TextStyle(color: AppTheme.textSecondary, fontSize: 13), textAlign: TextAlign.center, ), ], ), actions: [ SizedBox( width: double.infinity, child: ElevatedButton( onPressed: () { Navigator.of(context) ..pop() ..pop() ..pop(); }, child: const Text('Volver al inicio'), ), ), ], ), ); } }