public EventStatusChangesWindow(WpfScheduler.Event eventToCheck)
		{
			this.InitializeComponent();

            _eventToCheck = eventToCheck;
            LoadEventInfo();
            UpdateGrid();
		}
        public AddEventModal(WpfScheduler.Scheduler scheduler, DateTime eventStart, Model.User userLoggedIn)
		{
			this.InitializeComponent();

            _scheduler = scheduler;
            _eventStart = eventStart;
            _userLoggedIn = userLoggedIn;

            this.Title = "Agendar cita (" + eventStart.ToString("D") + ")";
            lblEventStartTime.ToolTip = lblEventStartTime.Text = eventStart.ToString("HH:mm") + " hrs";

            FillPatients();
            FillTreatments();
		}
        private void scheduler_OnScheduleContextMenuEvent(object sender, WpfScheduler.Event e)
        {
            string menuEventAction = (sender as MenuItem).Tag.ToString();

            if (menuEventAction == "VIEW_EVENT_STATUS_CHANGES")
            {
                new EventStatusChangesWindow(e).ShowDialog();
                return;
            }

            bool? eventModified = ModifyEventStatus(e, (EventStatus)Enum.Parse(typeof(EventStatus), menuEventAction, true), _userLoggedIn.UserId);
            if (eventModified == true)
            {
                scheduler.RepaintEvents();
            }
        }
        private void LoadEventInfo(WpfScheduler.Event e)
        {
            lblEventId.ToolTip = lblEventId.Text = e.EventInfo.EventId.ToString();
            lblEventStartTime.ToolTip = lblEventStartTime.Text = e.EventInfo.StartEvent.ToString("HH:mm") + " hrs";
            lblEventEndTime.ToolTip = lblEventEndTime.Text = e.EventInfo.EndEvent.ToString("HH:mm") + " hrs";
            lblExpNo.ToolTip = lblExpNo.Text = e.EventInfo.Patient.AssignedId.ToString();
            lblPacientName.ToolTip = lblPacientName.Text = e.EventInfo.Patient.FirstName + " " + e.EventInfo.Patient.LastName;
            lblCellPhone.ToolTip = lblCellPhone.Text = e.EventInfo.Patient.CellPhone;
            lblHomePhone.ToolTip = lblHomePhone.Text = e.EventInfo.Patient.HomePhone;
            lblEmail.ToolTip = lblEmail.Text = e.EventInfo.Patient.Email;
            lblEventStatus.ToolTip = lblEventStatus.Text = e.EventStatusString;
            lblEventCapturer.ToolTip = lblEventCapturer.Text = e.EventInfo.User.FirstName + " " + e.EventInfo.User.LastName;

            lblUpdateClinicHistoryMessage.Visibility = MainWindow.HasPatientToUpdateClinicHistory(e.EventInfo.Patient)
                                                                ? System.Windows.Visibility.Visible
                                                                : System.Windows.Visibility.Hidden;

            lblNextEvent.ToolTip = lblNextEvent.Text = GetNextPendingEvent(e);
        }
        private string GetNextPendingEvent(WpfScheduler.Event e)
        {
            string nextEventDate = string.Empty;

            if (e.EventStatus == EventStatus.CANCELED)
            {
                Model.Event nextPendingEvent = BusinessController.Instance.FindBy<Model.Event>(
                                                            ev =>
                                                                ev.PatientId == e.EventInfo.PatientId
                                                                && !ev.IsCanceled
                                                                && !ev.PatientSkips
                                                                && !ev.IsCompleted
                                                                && e.EventInfo.StartEvent < ev.StartEvent
                                                            )
                                                            .OrderBy(ev => ev.StartEvent)
                                                            .FirstOrDefault();

                nextEventDate = nextPendingEvent == null
                                ? nextEventDate
                                : nextPendingEvent.StartEvent.ToString("dd/MMMM/yyyy");
            }

            return nextEventDate;
        }
 private void scheduler_OnEventMouseLeftButtonDown(object sender, WpfScheduler.Event e)
 {
     LoadEventInfo(e);
 }
        private static void CreateFunctionalityBasedOnEventStatus(WpfScheduler.Event e, EventStatus es)
        {   
            //Creating reminder for recurrent treatments
            if (es == EventStatus.COMPLETED && e.EventInfo.Treatment.Recurrent != null)
            {
                string reminderMessage = "El paciente '" + e.EventInfo.Patient.FirstName + " " + e.EventInfo.Patient.LastName + "'"
                                        + " con Exp. No. " + e.EventInfo.Patient.AssignedId + " tomó el tratamiento de '" + e.EventInfo.Treatment.Name + "' el día "
                                        + e.EventInfo.StartEvent.ToString("D") + " a las " + e.EventInfo.StartEvent.ToString("HH:mm") + " hrs. "
                                        + "\nDado que este tratamiento es recurrente es necesario que llame al paciente para agendar de nuevo una cita.";

                Model.Reminder reminderToAdd = new Model.Reminder()
                {
                    Message = reminderMessage,
                    AppearDate = e.EventInfo.StartEvent.AddDays(e.EventInfo.Treatment.Recurrent.Value),
                    CreatedDate = DateTime.Now,
                    RequireAdmin = true,
                    Seen = false,
                    SeenBy = null,
                    AutoGenerated = true
                };

                if (Controllers.BusinessController.Instance.Add<Model.Reminder>(reminderToAdd) == false)
                {
                    MessageBox.Show("No se pudo generar un recordatorio para esta cita que es de tratammiento recurrente", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }


            if (es == EventStatus.CANCELED)
            {
                //If the event is canceled, then increment UsesLeft to the selected instrument
                //TODO: Now the user is able to make an event regardless if there is instruments available.
                //      So, we can't increment the UsesLeft followint the old logic.
                //if (e.EventInfo.Instrument != null)
                //{
                //    e.EventInfo.Instrument.UsesLeft = e.EventInfo.Instrument.UsesLeft.Value + 1;
                //    if (BusinessController.Instance.Update<Model.Instrument>(e.EventInfo.Instrument) == false)
                //    {
                //        MessageBox.Show("No se pudo incrementar la cantidad de usos al instrumento que iba a ser utilizado en esta cita", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                //    }   
                //}

                //Send email to patient for recurrent canceled events
                List<Model.Event> canceledEventsInARow = GetPatientCanceledEventsInARow(e.EventInfo.Patient.PatientId, 3);
                List<Model.Event> canceledEventsOfSameTreatment = GetPatientCanceledEventsOfSameTreatment(e.EventInfo.Patient.PatientId, e.EventInfo.TreatmentId, 3);

                if (canceledEventsInARow != null || canceledEventsOfSameTreatment != null)
                {
                    new CanceledEventsSendEmailModal(canceledEventsInARow, canceledEventsOfSameTreatment, e.EventInfo.Patient, e.EventInfo.Treatment).ShowDialog();
                }
            }   
        }
        public static bool? ModifyEventStatus(WpfScheduler.Event e, EventStatus es, int userLoggedInId)
        {
            string oldEventStatus = e.EventStatus.ToString();

            switch (es)
            {
                case EventStatus.CANCELED:
                    if (e.EventInfo.IsCanceled && e.EventInfo.IsCompleted == false 
                        && e.EventInfo.PatientSkips == false && e.EventInfo.IsConfirmed == false)
                    {
                        return null;
                    }

                    e.EventInfo.IsCanceled = true;
                    e.EventInfo.IsCompleted = false;
                    e.EventInfo.PatientSkips = false;
                    e.EventInfo.IsConfirmed = false;
                    break;
                case EventStatus.COMPLETED:
                    if (e.EventInfo.IsCanceled == false && e.EventInfo.IsCompleted
                        && e.EventInfo.PatientSkips == false && e.EventInfo.IsConfirmed == false)
                    {
                        return null;
                    }

                    e.EventInfo.IsCanceled = false;
                    e.EventInfo.IsCompleted = true;
                    e.EventInfo.PatientSkips = false;
                    e.EventInfo.IsConfirmed = false;
                    break;
                case EventStatus.PATIENT_SKIPS:
                    if (e.EventInfo.IsCanceled == false && e.EventInfo.IsCompleted
                        && e.EventInfo.PatientSkips && e.EventInfo.IsConfirmed == false)
                    {
                        return null;
                    }

                    e.EventInfo.IsCanceled = false;
                    e.EventInfo.IsCompleted = true;
                    e.EventInfo.PatientSkips = true;
                    e.EventInfo.IsConfirmed = false;
                    break;

                case EventStatus.CONFIRMED:
                    if (e.EventInfo.IsCanceled == false && e.EventInfo.PatientSkips == false
                        && e.EventInfo.IsCompleted == false && e.EventInfo.IsConfirmed)
                    {
                        return null;
                    }

                    e.EventInfo.IsCanceled = false;
                    e.EventInfo.IsCompleted = false;
                    e.EventInfo.PatientSkips = false;
                    e.EventInfo.IsConfirmed = true;
                    break;
                default:
                    return null;
            }

            if (es == EventStatus.PATIENT_SKIPS)
            {
                if (MessageBox.Show
                                (string.Format("¿Está seguro(a) que desea indicar que el paciente '{0}' "
                                    + "no asistió a su cita del día '{1}' que inició a las {2} hrs?",
                                        e.EventInfo.Patient.FirstName + " " + e.EventInfo.Patient.LastName,
                                        e.EventInfo.StartEvent.ToString("D"),
                                        e.EventInfo.StartEvent.ToString("HH:mm")),
                                    "Advertencia",
                                    MessageBoxButton.YesNo,
                                    MessageBoxImage.Warning
                                ) == MessageBoxResult.No)
                {
                    e.EventInfo.IsCanceled = false;
                    e.EventInfo.IsCompleted = false;
                    e.EventInfo.PatientSkips = false;
                    e.EventInfo.IsConfirmed = false;

                    return false;
                }
            }

            if (BusinessController.Instance.Update<Model.Event>(e.EventInfo))
            {
                bool eventStatusChangeRegistered = Utils.AddEventStatusChanges(oldEventStatus, e.EventStatus.ToString(), e.EventInfo.EventId, userLoggedInId);
                if (eventStatusChangeRegistered == false)
                {
                    MessageBox.Show("No se pudo guardar el cambio registrado en el estado de la cita", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return false;
                }

                CreateFunctionalityBasedOnEventStatus(e, es);

                return true;
            }

            MessageBox.Show("No pudo ser modificado el estado de la cita", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            return false;
        }