561 lines
26 KiB
Dart
561 lines
26 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../theme.dart';
|
|
import '../db/dao/cache_clientes_dao.dart';
|
|
|
|
// Comentario explicativo: Pantalla de Clientes conectada a CacheClientesDao.
|
|
// Permite listar, filtrar por tipo de servicio y registrar nuevos clientes con su tipo de servicio asociado
|
|
// (Alarmas, CCTV, Rastreo GPS, Control de Acceso, Servicio Integral 360) garantizando funcionamiento offline.
|
|
class ClientesPage extends StatefulWidget {
|
|
const ClientesPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<ClientesPage> createState() => _ClientesPageState();
|
|
}
|
|
|
|
class _ClientesPageState extends State<ClientesPage> {
|
|
final _clientesDao = CacheClientesDao();
|
|
List<Map<String, dynamic>> _clientes = [];
|
|
bool _cargando = true;
|
|
String _filtroServicio = 'todos';
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_cargarClientes();
|
|
}
|
|
|
|
// Comentario explicativo: Carga los clientes desde la base de datos local SQLite
|
|
Future<void> _cargarClientes() async {
|
|
final lista = await _clientesDao.getTodos();
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_clientes = lista;
|
|
_cargando = false;
|
|
});
|
|
}
|
|
|
|
Color _colorEstado(String estado) {
|
|
switch (estado.toLowerCase()) {
|
|
case 'critica':
|
|
return AppColors.alarmaActiva;
|
|
case 'alerta':
|
|
return AppColors.clienteAlerta;
|
|
default:
|
|
return AppColors.clienteOk;
|
|
}
|
|
}
|
|
|
|
// Comentario explicativo: Devuelve el ícono correspondiente según el tipo de servicio asociado
|
|
IconData _iconoServicio(String servicio) {
|
|
final s = servicio.toLowerCase();
|
|
if (s.contains('cctv') || s.contains('video') || s.contains('camara')) {
|
|
return Icons.videocam_outlined;
|
|
} else if (s.contains('gps') || s.contains('satelital') || s.contains('rastreo')) {
|
|
return Icons.gps_fixed;
|
|
} else if (s.contains('acceso')) {
|
|
return Icons.badge_outlined;
|
|
} else if (s.contains('integral') || s.contains('360')) {
|
|
return Icons.verified_user_outlined;
|
|
} else {
|
|
return Icons.notifications_active_outlined;
|
|
}
|
|
}
|
|
|
|
// Comentario explicativo: Devuelve el color temático para el tipo de servicio asociado
|
|
Color _colorServicio(String servicio) {
|
|
final s = servicio.toLowerCase();
|
|
if (s.contains('cctv') || s.contains('video') || s.contains('camara')) {
|
|
return const Color(0xFF26A69A); // Verde Teal
|
|
} else if (s.contains('gps') || s.contains('satelital') || s.contains('rastreo')) {
|
|
return const Color(0xFF42A5F5); // Azul satelital
|
|
} else if (s.contains('acceso')) {
|
|
return const Color(0xFFAB47BC); // Púrpura
|
|
} else if (s.contains('integral') || s.contains('360')) {
|
|
return const Color(0xFFFFB300); // Ámbar dorado
|
|
} else {
|
|
return AppColors.primary; // Primario para Alarmas
|
|
}
|
|
}
|
|
|
|
// Comentario explicativo: Chip interactivo para filtrar clientes por su tipo de servicio
|
|
Widget _chipFiltro(String key, String etiqueta, IconData icono) {
|
|
final activo = _filtroServicio == key;
|
|
return Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: FilterChip(
|
|
selected: activo,
|
|
showCheckmark: false,
|
|
avatar: Icon(icono, size: 14, color: activo ? const Color(0xFF002F6C) : AppColors.outline),
|
|
label: Text(etiqueta),
|
|
labelStyle: appLabel(
|
|
size: 11,
|
|
weight: activo ? FontWeight.bold : FontWeight.normal,
|
|
color: activo ? const Color(0xFF002F6C) : AppColors.onSurface,
|
|
),
|
|
backgroundColor: AppColors.surfaceLow,
|
|
selectedColor: AppColors.primary,
|
|
side: BorderSide(color: activo ? AppColors.primary : AppColors.outlineVariant),
|
|
onSelected: (_) {
|
|
setState(() => _filtroServicio = key);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
// Comentario explicativo: Abre el diálogo modal para dar de alta un nuevo cliente y asociar su tipo de servicio
|
|
Future<void> _mostrarDialogoNuevoCliente() async {
|
|
final nombreCtrl = TextEditingController();
|
|
final alarmasActivasCtrl = TextEditingController(text: '0');
|
|
final alarmasCriticasCtrl = TextEditingController(text: '0');
|
|
String servicioSeleccionado = 'Monitoreo de Alarmas';
|
|
String estadoSeleccionado = 'normal';
|
|
|
|
await showDialog(
|
|
context: context,
|
|
builder: (dialogCtx) {
|
|
return StatefulBuilder(
|
|
builder: (ctx, setDialogState) {
|
|
return AlertDialog(
|
|
backgroundColor: AppColors.surface,
|
|
shape: RoundedRectangleBorder(
|
|
borderRadius: BorderRadius.circular(16),
|
|
side: const BorderSide(color: AppColors.outlineVariant),
|
|
),
|
|
title: Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(8),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.primaryContainer,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: const Icon(Icons.person_add_alt_1, color: AppColors.primary, size: 20),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Text('Agregar Cliente',
|
|
style: appHeadline(size: 16, weight: FontWeight.bold, color: AppColors.onSurface)),
|
|
],
|
|
),
|
|
content: SizedBox(
|
|
width: 480,
|
|
child: SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Nombre de la Empresa / Cliente *',
|
|
style: appLabel(size: 12, weight: FontWeight.w600, color: AppColors.onSurface)),
|
|
const SizedBox(height: 6),
|
|
TextField(
|
|
controller: nombreCtrl,
|
|
style: appLabel(color: AppColors.onSurface),
|
|
decoration: InputDecoration(
|
|
hintText: 'Ej. Hospital San Rafael / Corporación Norte',
|
|
hintStyle: appLabel(color: AppColors.outline),
|
|
filled: true,
|
|
fillColor: AppColors.surfaceLow,
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(color: AppColors.outlineVariant),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
Text('Tipo de Servicio Asociado *',
|
|
style: appLabel(size: 12, weight: FontWeight.w600, color: AppColors.onSurface)),
|
|
const SizedBox(height: 6),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: AppColors.outlineVariant),
|
|
),
|
|
child: DropdownButtonHideUnderline(
|
|
child: DropdownButton<String>(
|
|
value: servicioSeleccionado,
|
|
isExpanded: true,
|
|
dropdownColor: AppColors.surfaceHigh,
|
|
style: appLabel(size: 13, color: AppColors.onSurface),
|
|
items: const [
|
|
DropdownMenuItem(value: 'Monitoreo de Alarmas', child: Text('Monitoreo de Alarmas')),
|
|
DropdownMenuItem(value: 'CCTV / Video-vigilancia', child: Text('CCTV / Video-vigilancia')),
|
|
DropdownMenuItem(value: 'Rastreo Satelital GPS', child: Text('Rastreo Satelital GPS')),
|
|
DropdownMenuItem(value: 'Control de Acceso', child: Text('Control de Acceso')),
|
|
DropdownMenuItem(value: 'Servicio Integral 360', child: Text('Servicio Integral 360')),
|
|
],
|
|
onChanged: (v) {
|
|
if (v != null) setDialogState(() => servicioSeleccionado = v);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(height: 14),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Estado Inicial',
|
|
style: appLabel(size: 12, weight: FontWeight.w600, color: AppColors.onSurface)),
|
|
const SizedBox(height: 6),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(8),
|
|
border: Border.all(color: AppColors.outlineVariant),
|
|
),
|
|
child: DropdownButtonHideUnderline(
|
|
child: DropdownButton<String>(
|
|
value: estadoSeleccionado,
|
|
isExpanded: true,
|
|
dropdownColor: AppColors.surfaceHigh,
|
|
style: appLabel(size: 12, color: AppColors.onSurface),
|
|
items: const [
|
|
DropdownMenuItem(value: 'normal', child: Text('Normal (Verde)')),
|
|
DropdownMenuItem(value: 'alerta', child: Text('Alerta (Ámbar)')),
|
|
DropdownMenuItem(value: 'critica', child: Text('Crítica (Rojo)')),
|
|
],
|
|
onChanged: (v) {
|
|
if (v != null) setDialogState(() => estadoSeleccionado = v);
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 12),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text('Alarmas Activas',
|
|
style: appLabel(size: 12, weight: FontWeight.w600, color: AppColors.onSurface)),
|
|
const SizedBox(height: 6),
|
|
TextField(
|
|
controller: alarmasActivasCtrl,
|
|
keyboardType: TextInputType.number,
|
|
style: appLabel(color: AppColors.onSurface),
|
|
decoration: InputDecoration(
|
|
filled: true,
|
|
fillColor: AppColors.surfaceLow,
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(8),
|
|
borderSide: const BorderSide(color: AppColors.outlineVariant),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(dialogCtx),
|
|
child: Text('Cancelar', style: appLabel(color: AppColors.outline)),
|
|
),
|
|
ElevatedButton.icon(
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.primary,
|
|
foregroundColor: const Color(0xFF002F6C),
|
|
),
|
|
icon: const Icon(Icons.check, size: 16),
|
|
label: const Text('Registrar'),
|
|
onPressed: () async {
|
|
final nombre = nombreCtrl.text.trim();
|
|
if (nombre.isEmpty) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('Por favor ingresa el nombre de la empresa o cliente.')),
|
|
);
|
|
return;
|
|
}
|
|
|
|
final activas = int.tryParse(alarmasActivasCtrl.text.trim()) ?? 0;
|
|
final criticas = int.tryParse(alarmasCriticasCtrl.text.trim()) ?? 0;
|
|
|
|
// Comentario explicativo: Registrar el nuevo cliente con su tipo de servicio asociado en SQLite
|
|
final nuevoId = await _clientesDao.obtenerSiguienteId();
|
|
await _clientesDao.insertar({
|
|
'id': nuevoId,
|
|
'nombre': nombre,
|
|
'estado': estadoSeleccionado,
|
|
'alarmas_activas': activas,
|
|
'alarmas_criticas': criticas,
|
|
'tipo_servicio': servicioSeleccionado,
|
|
'ultima_actualizacion': DateTime.now().toIso8601String(),
|
|
});
|
|
|
|
await _cargarClientes();
|
|
if (mounted) {
|
|
Navigator.pop(dialogCtx);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
backgroundColor: AppColors.surfaceHigh,
|
|
content: Text(
|
|
'Cliente "$nombre" registrado con servicio "$servicioSeleccionado".',
|
|
style: appLabel(color: AppColors.clienteOk),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Filtrado reactivo por tipo de servicio
|
|
final clientesFiltrados = _clientes.where((c) {
|
|
if (_filtroServicio == 'todos') return true;
|
|
final serv = ((c['tipo_servicio'] as String?) ?? 'Monitoreo de Alarmas').toLowerCase();
|
|
if (_filtroServicio == 'alarma') return serv.contains('alarma');
|
|
if (_filtroServicio == 'cctv') return serv.contains('cctv') || serv.contains('video');
|
|
if (_filtroServicio == 'gps') return serv.contains('gps') || serv.contains('satelital') || serv.contains('rastreo');
|
|
if (_filtroServicio == 'acceso') return serv.contains('acceso');
|
|
if (_filtroServicio == 'integral') return serv.contains('integral') || serv.contains('360');
|
|
return true;
|
|
}).toList();
|
|
|
|
return SingleChildScrollView(
|
|
primary: false,
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
// Comentario explicativo: Cabecera con título descriptivo y botón de refrescar; la creación de clientes se centraliza en la fila interactiva para evitar duplicidad visual
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Text('Clientes Registrados',
|
|
style: appHeadline(size: 22, weight: FontWeight.w800, color: AppColors.onSurface)),
|
|
const SizedBox(height: 4),
|
|
Text('Monitoreo del estado perimetral y central de cada sede con su tipo de servicio',
|
|
style: appLabel(size: 13, color: AppColors.outline)),
|
|
]),
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh, color: AppColors.primary),
|
|
onPressed: _cargarClientes,
|
|
tooltip: 'Refrescar clientes',
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Filtro horizontal por tipo de servicio
|
|
SingleChildScrollView(
|
|
scrollDirection: Axis.horizontal,
|
|
child: Row(
|
|
children: [
|
|
_chipFiltro('todos', 'Todos los servicios', Icons.apps),
|
|
_chipFiltro('alarma', 'Monitoreo Alarmas', Icons.notifications_active_outlined),
|
|
_chipFiltro('cctv', 'CCTV / Video-vigilancia', Icons.videocam_outlined),
|
|
_chipFiltro('gps', 'Rastreo GPS', Icons.gps_fixed),
|
|
_chipFiltro('acceso', 'Control de Acceso', Icons.badge_outlined),
|
|
_chipFiltro('integral', 'Integral 360', Icons.verified_user_outlined),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
|
|
// Comentario explicativo: Fila interactiva para agregar cliente y su tipo de servicio asociado
|
|
Container(
|
|
margin: const EdgeInsets.only(bottom: 16),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: AppColors.primary.withValues(alpha: 0.35), width: 1.2),
|
|
),
|
|
child: Material(
|
|
color: Colors.transparent,
|
|
child: InkWell(
|
|
onTap: _mostrarDialogoNuevoCliente,
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.primaryContainer,
|
|
borderRadius: BorderRadius.circular(10),
|
|
),
|
|
child: const Icon(Icons.person_add_alt_1, color: AppColors.primary, size: 22),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Text(
|
|
'Agregar Cliente',
|
|
style: appHeadline(size: 15, weight: FontWeight.w800, color: AppColors.primary),
|
|
),
|
|
const SizedBox(width: 8),
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.primary.withValues(alpha: 0.15),
|
|
borderRadius: BorderRadius.circular(4),
|
|
),
|
|
child: Text(
|
|
'+ REGISTRO',
|
|
style: appLabel(size: 9, weight: FontWeight.w800, color: AppColors.primary),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'Alta de cuenta con su tipo de servicio asociado (Alarmas, CCTV, Rastreo GPS, Control de Acceso o Integral 360)',
|
|
style: appLabel(size: 12, color: AppColors.outline),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const SizedBox(width: 10),
|
|
ElevatedButton.icon(
|
|
onPressed: _mostrarDialogoNuevoCliente,
|
|
style: ElevatedButton.styleFrom(
|
|
backgroundColor: AppColors.primary,
|
|
foregroundColor: const Color(0xFF002F6C),
|
|
elevation: 0,
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
|
),
|
|
icon: const Icon(Icons.add, size: 16),
|
|
label: Text('Agregar Cliente', style: appLabel(size: 12, weight: FontWeight.bold, color: const Color(0xFF002F6C))),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
|
|
if (_cargando)
|
|
const Center(child: CircularProgressIndicator())
|
|
else if (clientesFiltrados.isEmpty)
|
|
Container(
|
|
padding: const EdgeInsets.all(32),
|
|
alignment: Alignment.center,
|
|
child: Text(
|
|
_filtroServicio == 'todos'
|
|
? 'No hay clientes en caché local.'
|
|
: 'No hay clientes registrados con este tipo de servicio.',
|
|
style: appLabel(color: AppColors.outline),
|
|
),
|
|
)
|
|
else
|
|
ListView.separated(
|
|
shrinkWrap: true,
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
itemCount: clientesFiltrados.length,
|
|
separatorBuilder: (_, __) => const SizedBox(height: 12),
|
|
itemBuilder: (_, i) {
|
|
final c = clientesFiltrados[i];
|
|
final estado = (c['estado'] as String?) ?? 'normal';
|
|
final color = _colorEstado(estado);
|
|
final activas = (c['alarmas_activas'] as int?) ?? 0;
|
|
final criticas = (c['alarmas_criticas'] as int?) ?? 0;
|
|
final servicio = (c['tipo_servicio'] as String?) ?? 'Monitoreo de Alarmas';
|
|
final iconoServicio = _iconoServicio(servicio);
|
|
final colorServicio = _colorServicio(servicio);
|
|
|
|
return Container(
|
|
padding: const EdgeInsets.all(16),
|
|
decoration: BoxDecoration(
|
|
color: AppColors.surfaceLow,
|
|
borderRadius: BorderRadius.circular(12),
|
|
border: Border.all(color: color.withValues(alpha: 0.4), width: 1.2),
|
|
),
|
|
child: Row(
|
|
children: [
|
|
Container(
|
|
width: 12,
|
|
height: 12,
|
|
decoration: BoxDecoration(shape: BoxShape.circle, color: color),
|
|
),
|
|
const SizedBox(width: 14),
|
|
Expanded(
|
|
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
|
Row(
|
|
children: [
|
|
Text(
|
|
c['nombre'] as String? ?? 'Cliente',
|
|
style: appHeadline(size: 14, weight: FontWeight.w700, color: AppColors.onSurface),
|
|
),
|
|
const SizedBox(width: 10),
|
|
// Comentario explicativo: Badge que visualiza el tipo de servicio asociado al cliente
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
|
|
decoration: BoxDecoration(
|
|
color: colorServicio.withValues(alpha: 0.15),
|
|
borderRadius: BorderRadius.circular(6),
|
|
border: Border.all(color: colorServicio.withValues(alpha: 0.35)),
|
|
),
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(iconoServicio, size: 12, color: colorServicio),
|
|
const SizedBox(width: 4),
|
|
Text(
|
|
servicio,
|
|
style: appLabel(size: 11, weight: FontWeight.w600, color: colorServicio),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'ID: #${c['id']} • Estado: ${estado.toUpperCase()}',
|
|
style: appLabel(size: 12, color: AppColors.outline),
|
|
),
|
|
]),
|
|
),
|
|
Column(crossAxisAlignment: CrossAxisAlignment.end, children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
|
decoration: BoxDecoration(
|
|
color: activas > 0 ? AppColors.surfaceHighest : AppColors.surfaceLowest,
|
|
borderRadius: BorderRadius.circular(8),
|
|
),
|
|
child: Text(
|
|
'$activas activas ${criticas > 0 ? "($criticas críticas)" : ""}',
|
|
style: appLabel(
|
|
size: 11,
|
|
weight: FontWeight.bold,
|
|
color: criticas > 0 ? AppColors.alarmaActiva : AppColors.primary,
|
|
),
|
|
),
|
|
),
|
|
]),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
]),
|
|
);
|
|
}
|
|
}
|