Files
hidalyx-drluiscastro/lib/features/dashboard/presentation/views/dashboard_view.dart
T

358 lines
15 KiB
Dart
Raw Normal View History

2026-09-21 16:33:51 -04:00
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:go_router/go_router.dart';
import '../../../../core/theme/app_colors.dart';
import '../../../auth/data/services/auth_service.dart';
import '../store/dashboard_store.dart';
import '../widgets/dashboard_app_bar.dart';
import '../widgets/dashboard_metrics_cards.dart';
import '../widgets/quick_actions_dropdown.dart';
import '../widgets/recent_patients_card.dart';
import '../widgets/today_appointments_card.dart';
import '../../../patient_search/presentation/views/patient_search_modal_view.dart';
import '../../../patient/presentation/views/patient_modal_view.dart';
import '../../../config/presentation/views/config_modal_view.dart';
import 'appointments_modal_view.dart';
import '../../../../core/widgets/medical_modal.dart';
class DashboardView extends StatefulWidget {
final DashboardStore? store;
const DashboardView({super.key, this.store});
@override
State<DashboardView> createState() => _DashboardViewState();
}
class _DashboardViewState extends State<DashboardView> {
late final DashboardStore _store;
bool _isSecretary = false;
@override
void initState() {
super.initState();
_store = widget.store ?? DashboardStore();
_store.loadDashboardData();
_loadUserRole();
}
Future<void> _loadUserRole() async {
final session = await AuthService().loadSavedSession();
if (mounted && session != null) {
setState(() {
_isSecretary = session.isSecretary;
});
}
}
void _handleLogout() {
showDialog(
context: context,
barrierDismissible: false,
builder: (dialogContext) => AlertDialog(
backgroundColor: Colors.white,
surfaceTintColor: Colors.transparent,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: const BorderSide(color: AppColors.border),
),
titlePadding: const EdgeInsets.fromLTRB(24, 24, 24, 12),
contentPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
actionsPadding: const EdgeInsets.fromLTRB(24, 12, 24, 20),
title: Row(
2026-09-21 16:33:51 -04:00
children: [
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: AppColors.error.withValues(alpha: 0.12),
shape: BoxShape.circle,
),
child: const Icon(Icons.logout_rounded, color: AppColors.error, size: 22),
),
const SizedBox(width: 12),
const Text('Cerrar Sesión', style: TextStyle(fontSize: 17, fontWeight: FontWeight.w700, color: AppColors.textPrimary)),
2026-09-21 16:33:51 -04:00
],
),
content: Container(
padding: const EdgeInsets.all(14),
decoration: BoxDecoration(
color: AppColors.primaryContainer.withValues(alpha: 0.45),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: AppColors.primary.withValues(alpha: 0.2)),
),
child: const Row(
children: [
Icon(Icons.info_outline_rounded, color: AppColors.primary, size: 20),
SizedBox(width: 10),
Expanded(
child: Text(
'¿Está seguro de que desea salir del sistema Hidalyx Med?',
style: TextStyle(fontSize: 13, color: AppColors.textPrimary, fontWeight: FontWeight.w500),
),
),
],
),
),
2026-09-21 16:33:51 -04:00
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
child: const Text('Cancelar', style: TextStyle(color: AppColors.textSecondary, fontWeight: FontWeight.w600)),
2026-09-21 16:33:51 -04:00
),
ElevatedButton.icon(
2026-09-21 16:33:51 -04:00
style: ElevatedButton.styleFrom(
backgroundColor: AppColors.error,
foregroundColor: Colors.white,
elevation: 0,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
2026-09-21 16:33:51 -04:00
),
icon: const Icon(Icons.logout_rounded, size: 16),
label: const Text('Salir del Sistema', style: TextStyle(fontWeight: FontWeight.w700)),
2026-09-21 16:33:51 -04:00
onPressed: () async {
Navigator.of(dialogContext).pop();
await AuthService().logout();
if (mounted) {
context.go('/login');
}
},
),
],
),
);
}
void _handleQuickAction(QuickActionType action) {
switch (action) {
case QuickActionType.nuevoPaciente:
showMedicalModal(
context: context,
builder: (_) => PatientModalView(
isSecretary: _isSecretary,
onSaved: (_) {
_store.loadDashboardData();
},
),
);
break;
case QuickActionType.buscarPaciente:
showMedicalModal(
context: context,
builder: (_) => PatientSearchModalView(isSecretary: _isSecretary),
);
break;
case QuickActionType.configuracion:
showMedicalModal(
context: context,
builder: (_) => ConfigModalView(
onSaved: (updatedConfig) {
_store.doctorInfo = updatedConfig;
},
),
);
break;
}
}
@override
Widget build(BuildContext context) {
return Observer(
builder: (_) {
return Scaffold(
backgroundColor: AppColors.background,
appBar: DashboardAppBar(
doctorName: _store.doctorInfo.nombreDoctor,
specialty: _store.doctorInfo.especialidad,
isSecretary: _isSecretary,
onLogoutPressed: _handleLogout,
onQuickActionSelected: _handleQuickAction,
),
body: _store.isLoading
? const Center(
child: CircularProgressIndicator(color: AppColors.primary),
)
: SingleChildScrollView(
padding: const EdgeInsets.all(24),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1400),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Encabezado de Bienvenida
Row(
children: [
Container(
width: 4,
height: 28,
decoration: BoxDecoration(
color: AppColors.primary,
borderRadius: BorderRadius.circular(2),
),
),
const SizedBox(width: 12),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
_isSecretary ? 'Recepción de Pacientes' : 'Panel Central - Consultorio',
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
const SizedBox(height: 2),
Text(
_isSecretary
? 'Módulo de recepción, registro y sala de espera.'
: 'Bienvenido, ${_store.doctorInfo.nombreDoctor}. Sistema clínico listo.',
style: const TextStyle(
color: AppColors.textSecondary,
fontSize: 14,
),
),
],
),
),
// Indicador en Vivo de Sala de Espera
if (_store.enEsperaCount > 0)
Container(
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 8),
decoration: BoxDecoration(
color: const Color(0xFFDCFCE7),
borderRadius: BorderRadius.circular(10),
border: Border.all(color: const Color(0xFF86EFAC)),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(
Icons.chair_rounded,
size: 18,
color: Color(0xFF15803D),
),
const SizedBox(width: 8),
Text(
'${_store.enEsperaCount} en Sala de Espera',
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w700,
color: Color(0xFF15803D),
),
),
],
),
),
],
),
const SizedBox(height: 24),
// Mensaje de Error / Reintento si aplica
if (_store.errorMessage != null) ...[
Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
decoration: BoxDecoration(
color: AppColors.warning.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(12),
border: Border.all(color: AppColors.warning.withValues(alpha: 0.3)),
),
child: Row(
children: [
const Icon(Icons.info_outline_rounded, color: AppColors.warning),
const SizedBox(width: 12),
Expanded(
child: Text(
'Aviso de sincronización: ${_store.errorMessage}',
style: const TextStyle(color: AppColors.textPrimary, fontSize: 13),
),
),
TextButton(
onPressed: () => _store.loadDashboardData(),
child: const Text('Reintentar'),
),
],
),
),
const SizedBox(height: 16),
],
// Las 4 Tarjetas de Métricas Solicitadas
DashboardMetricsCards(metrics: _store.metrics),
const SizedBox(height: 24),
// Paneles de Detalle: Pacientes Nuevos y Citas para Hoy
LayoutBuilder(
builder: (context, constraints) {
final recentCard = RecentPatientsCard(
patients: _store.recentPatients,
);
final appointmentsCard = TodayAppointmentsCard(
appointments: _store.todayAppointments,
isSecretary: _isSecretary,
onStatusChange: (patient, newStatus) async {
await _store.changeWaitingStatus(patient.id, newStatus);
},
onSchedulePressed: () {
showMedicalModal(
context: context,
builder: (_) => AppointmentsModalView(
isSecretary: _isSecretary,
onStatusChanged: (patientId, newStatus) {
_store.changeWaitingStatus(patientId, newStatus);
},
),
);
},
onPatientTap: (patient) {
if (patient.id > 0) {
showMedicalModal(
context: context,
builder: (_) => PatientModalView(
patientId: patient.id,
isSecretary: _isSecretary,
onSaved: (_) => _store.loadDashboardData(),
onDeleted: (_) => _store.loadDashboardData(),
2026-09-21 16:33:51 -04:00
),
);
}
},
);
if (constraints.maxWidth >= 1000) {
return IntrinsicHeight(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: recentCard),
const SizedBox(width: 16),
Expanded(child: appointmentsCard),
],
),
);
}
return Column(
children: [
recentCard,
const SizedBox(height: 16),
appointmentsCard,
],
);
},
),
],
),
),
),
),
);
},
);
}
}