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 '../../models/pedido.dart'; class PerfilScreen extends StatelessWidget { const PerfilScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final provider = context.watch(); final vendedor = provider.vendedor; final fmt = NumberFormat.currency(locale: 'es_BO', symbol: 'Bs.', decimalDigits: 2); return Scaffold( backgroundColor: AppTheme.bgColor, body: CustomScrollView( slivers: [ _buildSliverHeader(context, vendedor, provider), SliverPadding( padding: const EdgeInsets.all(16), sliver: SliverList( delegate: SliverChildListDelegate([ _buildStatsRow(provider, fmt), const SizedBox(height: 20), _buildRutaProgress(provider), const SizedBox(height: 20), _buildPedidosResumen(context, provider, fmt), const SizedBox(height: 20), _buildInfoCard(vendedor), const SizedBox(height: 20), _buildConfigSection(context), const SizedBox(height: 20), _buildLogoutBtn(context, provider), const SizedBox(height: 40), ]), ), ), ], ), ); } Widget _buildSliverHeader(BuildContext context, vendedor, AppProvider provider) { return SliverAppBar( expandedHeight: 200, pinned: true, backgroundColor: AppTheme.primaryColor, flexibleSpace: FlexibleSpaceBar( background: Container( decoration: const BoxDecoration( gradient: LinearGradient( colors: [Color(0xFF1E3A8A), Color(0xFF2563EB)], begin: Alignment.topCenter, end: Alignment.bottomCenter, ), ), child: SafeArea( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ CircleAvatar( radius: 40, backgroundColor: Colors.white.withOpacity(0.2), child: Text( vendedor.avatarInitials, style: const TextStyle(fontSize: 28, fontWeight: FontWeight.bold, color: Colors.white), ), ), const SizedBox(height: 10), Text(vendedor.nombre, style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white)), const SizedBox(height: 4), Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4), decoration: BoxDecoration( color: Colors.white.withOpacity(0.2), borderRadius: BorderRadius.circular(12), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.location_on, color: Colors.white70, size: 14), const SizedBox(width: 4), Text(vendedor.zona, style: const TextStyle(color: Colors.white, fontSize: 12, fontWeight: FontWeight.w500)), ], ), ), ], ), ), ), title: Text(vendedor.nombre, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)), titlePadding: const EdgeInsets.only(left: 16, bottom: 12), ), ); } Widget _buildStatsRow(AppProvider provider, NumberFormat fmt) { final stats = [ {'label': 'Pedidos hoy', 'value': '${provider.pedidosHoy}', 'color': AppTheme.primaryColor}, {'label': 'Ventas hoy', 'value': fmt.format(provider.ventasTotalesHoy), 'color': AppTheme.successColor}, { 'label': 'Clientes', 'value': '${provider.ruta.visitadas}/${provider.ruta.paradas.length}', 'color': AppTheme.accentColor }, ]; return Row( children: stats.map((s) { final color = s['color'] as Color; return Expanded( child: Container( margin: const EdgeInsets.symmetric(horizontal: 4), padding: const EdgeInsets.symmetric(vertical: 14), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(12), boxShadow: [BoxShadow(color: color.withOpacity(0.1), blurRadius: 8)], ), child: Column( children: [ Text(s['value'] as String, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: color)), const SizedBox(height: 4), Text(s['label'] as String, style: const TextStyle(fontSize: 10, color: AppTheme.textSecondary), textAlign: TextAlign.center), ], ), ), ); }).toList(), ); } Widget _buildRutaProgress(AppProvider provider) { final ruta = provider.ruta; final progress = ruta.paradas.isEmpty ? 0.0 : ruta.visitadas / ruta.paradas.length; return _card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text('Progreso de ruta', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14)), Text('${(progress * 100).toInt()}% completado', style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)), ], ), const SizedBox(height: 12), ClipRRect( borderRadius: BorderRadius.circular(6), child: LinearProgressIndicator( value: progress, backgroundColor: AppTheme.bgColor, valueColor: AlwaysStoppedAnimation( progress == 1.0 ? AppTheme.successColor : AppTheme.primaryColor, ), minHeight: 10, ), ), const SizedBox(height: 10), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ _progressChip(Icons.check_circle, '${ruta.visitadas} visitadas', AppTheme.successColor), _progressChip(Icons.pending, '${ruta.pendientes} pendientes', AppTheme.warningColor), _progressChip(Icons.location_on, '${ruta.paradas.length} total', AppTheme.primaryColor), ], ), ], ), ); } Widget _progressChip(IconData icon, String label, Color color) { return Row( mainAxisSize: MainAxisSize.min, children: [ Icon(icon, size: 14, color: color), const SizedBox(width: 4), Text(label, style: TextStyle(fontSize: 11, color: color, fontWeight: FontWeight.w500)), ], ); } Widget _buildPedidosResumen(BuildContext context, AppProvider provider, NumberFormat fmt) { final estados = { EstadoPedido.confirmado: provider.pedidos.where((p) => p.estado == EstadoPedido.confirmado).length, EstadoPedido.entregado: provider.pedidos.where((p) => p.estado == EstadoPedido.entregado).length, EstadoPedido.pendiente: provider.pedidos.where((p) => p.estado == EstadoPedido.pendiente).length, EstadoPedido.cancelado: provider.pedidos.where((p) => p.estado == EstadoPedido.cancelado).length, }; return _card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Resumen de pedidos', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14)), const SizedBox(height: 12), ...estados.entries.map((e) { final color = AppTheme.estadoColor(e.key.label); return Padding( padding: const EdgeInsets.only(bottom: 8), child: Row( children: [ Container(width: 10, height: 10, decoration: BoxDecoration(color: color, shape: BoxShape.circle)), const SizedBox(width: 10), Text(e.key.label, style: const TextStyle(fontSize: 13)), const Spacer(), Text('${e.value}', style: TextStyle(fontWeight: FontWeight.bold, color: color)), ], ), ); }).toList(), ], ), ); } Widget _buildInfoCard(vendedor) { return _card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Información de contacto', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14)), const SizedBox(height: 12), _infoTile(Icons.email_outlined, 'Correo', vendedor.email), _infoTile(Icons.phone_outlined, 'Teléfono', vendedor.telefono), _infoTile(Icons.map_outlined, 'Zona', vendedor.zona), ], ), ); } Widget _infoTile(IconData icon, String label, String value) { return Padding( padding: const EdgeInsets.only(bottom: 10), child: Row( children: [ Icon(icon, size: 18, color: AppTheme.primaryColor), const SizedBox(width: 10), Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(label, style: const TextStyle(fontSize: 10, color: AppTheme.textSecondary)), Text(value, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w500)), ], ), ], ), ); } Widget _buildConfigSection(BuildContext context) { final opciones = [ {'icon': Icons.notifications_outlined, 'label': 'Notificaciones', 'trailing': true}, {'icon': Icons.language, 'label': 'Idioma', 'value': 'Español'}, {'icon': Icons.help_outline, 'label': 'Ayuda y soporte', 'arrow': true}, {'icon': Icons.info_outline, 'label': 'Acerca de la app', 'value': 'v1.0.0'}, ]; return _card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text('Configuración', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14)), const SizedBox(height: 8), ...opciones.map((o) => ListTile( dense: true, contentPadding: EdgeInsets.zero, leading: Icon(o['icon'] as IconData, color: AppTheme.primaryColor, size: 22), title: Text(o['label'] as String, style: const TextStyle(fontSize: 13)), trailing: o['trailing'] == true ? Switch(value: true, onChanged: (_) {}, activeColor: AppTheme.primaryColor) : o['arrow'] == true ? const Icon(Icons.chevron_right, color: AppTheme.textSecondary) : Text(o['value'] as String? ?? '', style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary)), onTap: () {}, )).toList(), ], ), ); } Widget _buildLogoutBtn(BuildContext context, AppProvider provider) { return SizedBox( width: double.infinity, height: 50, child: OutlinedButton.icon( icon: const Icon(Icons.logout, color: AppTheme.dangerColor), label: const Text('Cerrar sesión', style: TextStyle(color: AppTheme.dangerColor, fontWeight: FontWeight.w600)), style: OutlinedButton.styleFrom( side: const BorderSide(color: AppTheme.dangerColor), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), ), onPressed: () { showDialog( context: context, builder: (_) => AlertDialog( title: const Text('Cerrar sesión'), content: const Text('¿Seguro que deseas salir?'), actions: [ TextButton(onPressed: () => Navigator.pop(context), child: const Text('Cancelar')), TextButton( onPressed: () { Navigator.pop(context); provider.logout(); }, child: const Text('Salir', style: TextStyle(color: AppTheme.dangerColor)), ), ], ), ); }, ), ); } Widget _card({required Widget child}) { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(14), boxShadow: [BoxShadow(color: Colors.black.withOpacity(0.04), blurRadius: 6)], ), child: child, ); } }