134 lines
3.7 KiB
C#
134 lines
3.7 KiB
C#
using CommunityToolkit.Mvvm.ComponentModel;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
using Asistencia.Models;
|
|
using Asistencia.Services;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Asistencia.ViewModels;
|
|
|
|
public partial class AsistenciaViewModel : ViewModelBase
|
|
{
|
|
private readonly AsistenciaService _asistenciaService;
|
|
private readonly AuthService _authService;
|
|
private readonly MainWindowViewModel _mainVm;
|
|
|
|
[ObservableProperty]
|
|
private string _inputCursoAbierto = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool _enSesion;
|
|
|
|
[ObservableProperty]
|
|
private string _inputRutAlumno = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _mensajeEstado = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private string _mensajeError = string.Empty;
|
|
|
|
[ObservableProperty]
|
|
private bool _hasError;
|
|
|
|
public ObservableCollection<AlumnoPresenteViewModel> AlumnosPresentes { get; } = new();
|
|
|
|
private int _idCursoAbierto;
|
|
|
|
public AsistenciaViewModel(AsistenciaService asistenciaService, AuthService authService, MainWindowViewModel mainVm)
|
|
{
|
|
_asistenciaService = asistenciaService;
|
|
_authService = authService;
|
|
_mainVm = mainVm;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task IniciarSesionAsync()
|
|
{
|
|
HasError = false;
|
|
|
|
if (!int.TryParse(InputCursoAbierto, out var idCurso))
|
|
{
|
|
MensajeError = "Ingrese un ID de curso valido";
|
|
HasError = true;
|
|
return;
|
|
}
|
|
|
|
if (_authService.CurrentUser == null) return;
|
|
|
|
_idCursoAbierto = idCurso;
|
|
await _asistenciaService.IniciarSesionAsync(idCurso, _authService.CurrentUser.RutPasaporte, _authService.TipoAsignacionActual);
|
|
EnSesion = true;
|
|
MensajeEstado = $"Sesion curso {idCurso} — Ingrese RUT de alumnos presentes";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task FinalizarSesionAsync()
|
|
{
|
|
var sesion = await _asistenciaService.BuscarSesionActivaAsync(_idCursoAbierto);
|
|
if (sesion != null)
|
|
{
|
|
await _asistenciaService.FinalizarSesionAsync(sesion.IdAsistenciaProfesor);
|
|
}
|
|
EnSesion = false;
|
|
AlumnosPresentes.Clear();
|
|
InputCursoAbierto = string.Empty;
|
|
InputRutAlumno = string.Empty;
|
|
MensajeEstado = "Sesion finalizada";
|
|
}
|
|
|
|
[RelayCommand]
|
|
private async Task AgregarAlumnoAsync()
|
|
{
|
|
if (!EnSesion) return;
|
|
|
|
HasError = false;
|
|
var rut = InputRutAlumno.Trim();
|
|
|
|
if (string.IsNullOrWhiteSpace(rut))
|
|
{
|
|
MensajeError = "Ingrese el RUT del alumno";
|
|
HasError = true;
|
|
return;
|
|
}
|
|
|
|
if (AlumnosPresentes.Any(a => a.IdAlumno == rut))
|
|
{
|
|
MensajeError = "Este RUT ya fue registrado";
|
|
HasError = true;
|
|
InputRutAlumno = string.Empty;
|
|
return;
|
|
}
|
|
|
|
var alumno = await _asistenciaService.BuscarAlumnoPorRutAsync(_idCursoAbierto, rut);
|
|
if (alumno == null)
|
|
{
|
|
MensajeError = $"RUT {rut} no encontrado en el curso";
|
|
HasError = true;
|
|
InputRutAlumno = string.Empty;
|
|
return;
|
|
}
|
|
|
|
await _asistenciaService.GuardarAsistenciaAlumnoAsync(alumno.IdDetalleContrato, TipoAsistencia.Presente);
|
|
|
|
AlumnosPresentes.Add(new AlumnoPresenteViewModel
|
|
{
|
|
IdAlumno = rut,
|
|
NombreCompleto = alumno.NombreCompleto
|
|
});
|
|
|
|
InputRutAlumno = string.Empty;
|
|
}
|
|
|
|
[RelayCommand]
|
|
private void Volver()
|
|
{
|
|
_mainVm.NavigateToDashboard();
|
|
}
|
|
}
|
|
|
|
public class AlumnoPresenteViewModel
|
|
{
|
|
public string IdAlumno { get; set; } = string.Empty;
|
|
public string NombreCompleto { get; set; } = string.Empty;
|
|
}
|