private void Cal_Button_DeleteServiceTimeSpan_Click(object sender, RoutedEventArgs e)
        {
            NightShift nightShift = (NightShift)Cal_DataGrid.SelectedItems[0];
            uint       selectedId = nightShift.ShiftsId;

            string           question = "Wirklich die Schicht " + selectedId + "\r\n\r\nvon\t" + nightShift.StartTime + "\r\nbis\t" + nightShift.EndTime + "\r\n\r\nLÖSCHEN?";
            MessageBoxResult r        = MessageBox.Show(question, "Wirklich löschen?", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (r == MessageBoxResult.Yes)
            {
                int chandedRows = sql.DeleteShift(selectedId);
                if (chandedRows > 0)
                {
                    Log.Write(Log.Type.Calendar, "Schicht [" + selectedId + "] gelöscht.");
                }
                else
                {
                    MessageBox.Show("Die ausgwählte Schicht konnte nicht gelöscht werden.");
                }

                LoadShiftsToNightShiftClass();
            }

            //LoadShiftsToNightShiftClass();
            BlackOutCalenderDays(true);
        }
        /// <summary>
        /// Liest Bereitschaftsdienste aus der Datenbank in die Klasse NightShift
        /// und übergibt diese als Feld für die Visualisierung.
        /// </summary>
        internal static void LoadShiftsToNightShiftClass()
        {
            ObservableCollection <NightShift> nightShifts = new ObservableCollection <NightShift>();

            Sql       sql = new Sql();
            DataTable dt  = sql.GetShifts();

            foreach (DataRow row in dt.Rows)
            {
                string aName = row["Name"].ToString();

                int sendType = int.Parse(row["SendType"].ToString());

                NightShift nightShift = new NightShift
                {
                    ShiftsId     = uint.Parse(row["ID"].ToString()),
                    EntryTime    = DateTime.Parse(row["EntryTime"].ToString()),
                    GuardName    = aName,
                    StartTime    = DateTime.Parse(row["StartTime"].ToString()),
                    EndTime      = DateTime.Parse(row["EndTime"].ToString()),
                    SendViaSMS   = (sendType == (int)MessageType.SentToSms) || (sendType == (int)MessageType.SentToEmailAndSMS),
                    SendViaEmail = (sendType == (int)MessageType.SentToEmail) || (sendType == (int)MessageType.SentToEmailAndSMS)
                };

                nightShifts.Add(nightShift);
            }

            MainWindow.Cal_ShiftsCollection = nightShifts;
        }
 private void Cal_EndTimeBeam_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
 {
     if (Cal_DataGrid.SelectedItems.Count > 0)
     {
         NightShift nightShift = (NightShift)Cal_DataGrid.SelectedItems[0];
         Cal_TimeTo.SelectedIndex = nightShift.EndTimeHour;
     }
 }
        private void Cal_DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Cal_DataGrid.SelectedItem == null || Cal_DataGrid.SelectedItems.Count < 1)
            {
                Cal_Button_UpdateServiceTimeSpan.IsEnabled = false;
                Cal_Button_UpdateServiceWeek.IsEnabled     = false;
                Cal_Button_DeleteServiceTimeSpan.IsEnabled = false;
                Cal_DataGrid.SelectedIndex = 0;
            }
            else
            {
                Cal_Button_UpdateServiceTimeSpan.IsEnabled = true;
                Cal_Button_UpdateServiceWeek.IsEnabled     = true;
                Cal_Button_DeleteServiceTimeSpan.IsEnabled = true;

                try
                {
                    NightShift nightShift = (NightShift)Cal_DataGrid.SelectedItems[0];

                    Cal_ComboBox_Personal.SelectedValue = nightShift.GuardName;
                    Cal_TimeFrom.SelectedIndex          = nightShift.StartTimeHour;
                    Cal_TimeTo.SelectedIndex            = nightShift.EndTimeHour;
                    Cal_SendViaEmail_CheckBox.IsChecked = nightShift.SendViaEmail;
                    Cal_SendViaSMS_CheckBox.IsChecked   = nightShift.SendViaSMS;

                    //Auswahl gesperrter Daten in DatePicker erzeugt einen Fehler:
                    DateTime DateFromList = (DateTime)nightShift.StartTime.Date;
                    if (!Cal_DatePicker.BlackoutDates.Contains(DateFromList))
                    {
                        try
                        {
                            Cal_DatePicker.SelectedDate = DateFromList;
                        }
                        catch
                        {
                            //nichts unternehmen
                        }

                        Cal_DatePicker.Text = nightShift.StartTime.ToShortDateString();
                    }
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    MessageBox.Show(ex.Message + "\r\n" + Cal_DataGrid.SelectedItems.Count + "\r\n\r\n" + ex.StackTrace);
                }
            }
        }
        private void Cal_Button_UpdateServiceWeek_Click(object sender, RoutedEventArgs e)
        {
            if (Cal_DataGrid.SelectedItem == null || Cal_ComboBox_Personal.SelectedItem == null)
            {
                return;
            }

            string person = Cal_ComboBox_Personal.SelectedItem.ToString();

            uint personId = sql.GetIdFromEntry("Persons", "Name", person);

            NightShift nightShift = (NightShift)Cal_DataGrid.SelectedItems[0];

            DateTime selectedDate = nightShift.StartTime.Date;

            DateTime StartDate = selectedDate.AddDays(DayOfWeek.Monday - selectedDate.DayOfWeek).Date;

            DateTime EndDate = StartDate.AddDays(7);

            string msg = "Bereitschaftswoche \r\nvon " +
                         StartDate.ToShortDateString() +
                         "\r\nbis " +
                         EndDate.ToShortDateString() +
                         "\r\nändern \r\nvon " +
                         nightShift.GuardName +
                         "\r\nauf [" + personId + "] " + person + "?";

            MessageBoxResult r = MessageBox.Show(msg, "Bereitschaftswoche ändern?", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (r == MessageBoxResult.Yes)
            {
                IEnumerable <string> ids = sql.GetListFromColumn("Shifts", "ID", "StartTime BETWEEN " + Sql.ConvertToUnixTime(StartDate) + " AND " + Sql.ConvertToUnixTime(StartDate.AddDays(7)));

                foreach (string strId in ids)
                {
                    if (uint.TryParse(strId, out uint id))
                    {
                        sql.UpdateShift(id, personId, HelperClass.GetMessageType(false, false, (bool)Cal_SendViaEmail_CheckBox.IsChecked, (bool)Cal_SendViaSMS_CheckBox.IsChecked));
                    }
                }

                LoadShiftsToNightShiftClass();
                BlackOutCalenderDays(true);
            }
        }
        /// <summary>
        /// Erstellt die Liste der Stillen Nachrichtenempfänger, die immer informiert werden.
        /// </summary>
        /// <returns>Liste der Stillen Nachrichtenempfänger</returns>
        internal static ObservableCollection <NightShift> GetSilentListeners()
        {
            ObservableCollection <NightShift> silentListeners = new ObservableCollection <NightShift>();

            NightShift nightShift1 = new NightShift
            {
                ShiftsId        = 0,
                GuardName       = "MelBox2Admin",
                SendToEmail     = MainWindow.MelBoxAdmin.Address,
                SendToCellphone = 0, //hier ggf. eine Ausweichnummer eintragen?
                SendViaSMS      = false,
                SendViaEmail    = true
            };

            silentListeners.Add(nightShift1);

            return(silentListeners);
        }
        private void Cal_Button_UpdateServiceTimeSpan_Click(object sender, RoutedEventArgs e)
        {
            NightShift nightShift = (NightShift)Cal_DataGrid.SelectedItems[0];
            uint       selectedId = nightShift.ShiftsId;

            DateTime StartDate = DateTime.Now;

            try
            {
                StartDate = (DateTime)Cal_DatePicker.SelectedDate;
            }
            catch
            {
                //nichts unternehmen
            }

            if (Cal_ComboBox_Personal.SelectedValue == null)
            {
                return;
            }

            string   Personal  = Cal_ComboBox_Personal.SelectedValue.ToString();
            int      indexFrom = Cal_TimeFrom.SelectedIndex;
            int      indexTo   = Cal_TimeTo.SelectedIndex;
            DateTime StartTime = StartDate.Date.AddHours(indexFrom);
            DateTime EndTime   = StartDate.Date.AddDays(1).AddHours(indexTo);

            //Welches Sendemedium?
            MessageType sendType;

            if ((bool)Cal_SendViaSMS_CheckBox.IsChecked)
            {
                sendType = ((bool)Cal_SendViaEmail_CheckBox.IsChecked) ? MessageType.SentToEmailAndSMS : MessageType.SentToSms;
            }
            else
            {
                sendType = ((bool)Cal_SendViaEmail_CheckBox.IsChecked) ? MessageType.SentToEmail : MessageType.NoCategory;
            }

            Sql sql = new Sql();

            uint personId = sql.GetIdFromEntry("Persons", "Name", Personal);

            sql.UpdateShift(selectedId, personId, StartTime, EndTime, sendType);

            string message = "Geänderte Schicht für \r\n\r\n[" +
                             personId + "] " + Personal +
                             "\r\n\r\nSchicht-Nr.:\t" + selectedId +
                             "\r\nvon\t" + StartTime.ToString("dd.MM.yyyy HH:mm") +
                             "\r\nbis\t" + EndTime.ToString("dd.MM.yyyy HH:mm");

            switch (sendType)
            {
            case MessageType.SentToSms:
                message += "\r\n\n Benachrichtigungen per SMS.";
                break;

            case MessageType.SentToEmail:
                message += "\r\n\n Benachrichtigungen per Email.";
                break;

            case MessageType.SentToEmailAndSMS:
                message += "\r\n\n Benachrichtigungen per SMS und Email.";
                break;

            default:
                message += "\r\n\n Benachrichtigungsweg nicht festgelegt.";
                break;
            }

            MessageBox.Show(message, "Geänderte Schicht", MessageBoxButton.OK, MessageBoxImage.Information);
            Log.Write(Log.Type.Calendar, message);

            LoadShiftsToNightShiftClass();
            Timer_CurrentShifts = sql.GetCurrentShifts();
            BlackOutCalenderDays(true);
        }