import 'package:flutter/material.dart'; import 'package:injector/injector.dart'; import 'package:professional_repository/professional_repository.dart'; import 'package:prosappco/components/general_primary_button.dart'; import 'package:prosappco/screens/professional/components/schedule_item.dart'; import 'package:prosappco/utils/schedule_validation.dart'; class ProfessionalScheduleScreen extends StatefulWidget { Schedules schedules; ProfessionalScheduleScreen({ super.key, required this.schedules, }); @override State createState() => _ProfessionalScheduleScreenState(); } class _ProfessionalScheduleScreenState extends State { final _repo = Injector.appInstance.get(); Schedules schedules = Schedules.empty; bool _saving = false; bool _dirty = false; @override void initState() { super.initState(); schedules = widget.schedules; } void _update(Schedules next) { setState(() { schedules = next; _dirty = true; }); } /// Saving now writes to the backend instead of just handing the object back: /// the old flow depended on the profile screen being saved afterwards, so /// leaving without that second save silently discarded everything. Future _save() async { final problems = validateSchedules(schedules); if (problems.isNotEmpty) { ScaffoldMessenger.of(context).clearSnackBars(); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(problems.first)), ); return; } setState(() => _saving = true); try { await _repo.updateSchedules(schedules); if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Horario guardado')), ); Navigator.of(context).pop(schedules); } catch (e) { if (!mounted) return; setState(() => _saving = false); ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text( 'No se pudo guardar el horario. Revisa tu conexión e inténtalo de nuevo.')), ); } } Future _confirmLeave() async { if (!_dirty || _saving) return true; final leave = await showDialog( context: context, builder: (ctx) => AlertDialog( title: const Text('¿Salir sin guardar?'), content: const Text('Perderás los cambios que hiciste en tu horario.'), actions: [ TextButton( onPressed: () => Navigator.pop(ctx, false), child: const Text('Seguir editando')), TextButton( onPressed: () => Navigator.pop(ctx, true), child: const Text('Salir sin guardar')), ], ), ); return leave ?? false; } @override Widget build(BuildContext context) { return PopScope( canPop: false, onPopInvoked: (didPop) async { if (didPop) return; if (await _confirmLeave() && mounted) Navigator.of(context).pop(); }, child: Scaffold( appBar: AppBar( title: const Text('Horario'), ), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.symmetric(horizontal: 15), child: Column( children: [ ScheduleItem( label: "Lunes", schedule: schedules.monday, onChanged: (s) { _update(schedules.copyWith(monday: s)); }, ), ScheduleItem( label: "Martes", schedule: schedules.tuesday, onChanged: (s) { _update(schedules.copyWith(tuesday: s)); }, ), ScheduleItem( label: "Miercoles", schedule: schedules.wednesday, onChanged: (s) { _update(schedules.copyWith(wednesday: s)); }, ), ScheduleItem( label: "Jueves", schedule: schedules.thursday, onChanged: (s) { _update(schedules.copyWith(thursday: s)); }, ), ScheduleItem( label: "Viernes", schedule: schedules.friday, onChanged: (s) { _update(schedules.copyWith(friday: s)); }, ), ScheduleItem( label: "Sabado", schedule: schedules.saturday, onChanged: (s) { _update(schedules.copyWith(saturday: s)); }, ), ScheduleItem( label: "Domingo", schedule: schedules.sunday, onChanged: (s) { _update(schedules.copyWith(sunday: s)); }, ), const SizedBox(height: 20), GeneralPrimaryButton( label: _saving ? "Guardando…" : "Guardar", onPressed: _saving ? () {} : _save, ), const SizedBox(height: 20), ], ), ), ), ), ); } }