Public Class frmDiestEintrag

    Dim week() As String = {"SO", "MO", "DI", "MI", "DO", "FR", "SA"}
    Public niederlassung = ""
    Dim SQLDienst As New cDienstplan
    Dim ListMA As New List(Of cDienstMA)
    Dim MA As New cDienstMA
    Public maId As Integer = -1
    Public art As String = "URL"
    Private Sub frmDiestAddMa_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            MessageBox.Show("Enter key pressed")
        End If
    End Sub
    Private Sub frmDiestAddMa_Load(sender As Object, e As EventArgs) Handles Me.Load
        If art = "URL" Then PictureBox1.Image = My.Resources.urlaub : Label1.Text = "Urlaub eintragen:"
        If art = "KS" Then PictureBox1.Image = My.Resources.krank : Label1.Text = "Krankenstand eintragen:"
        If art = "DR" Then PictureBox1.Image = My.Resources.reise : Label1.Text = "Reise eintragen:"

        ListMA = SQLDienst.getAllDienstMA(niederlassung, Now)
        fillCboMA()

        If maId >= 0 Then
            If cboMyItemSelect(cboMA, maId) Then
                loadMA()
            End If
        End If
    End Sub
    Sub loadMA()

    End Sub
    Sub fillCboMA()
        For Each m In ListMA
            cboMA.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem(m.dstma_kuerzel, m.dstma_id))
        Next
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        MsgBox("Funktion deaktiviert")

        Exit Sub

        Try

            If datVon.Value > datBis.Value Then
                MsgBox("'Datum von' muss kleiner als 'Datum bis' sein")
                Exit Sub
            End If

            SQLDienst.insertDienstNA(CType(cboMA.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, datVon.Value, datBis.Value, art, txtInfo.Text, niederlassung, -1)

            'Auch in den aktuellen DP eintragen:
            Dim d As Date = datVon.Value
            While d.ToShortDateString <= CDate(datBis.Value).ToShortDateString
                Dim cDienstNA As New cDienstNA
                cDienstNA.dstna_grund = art
                cDienstNA.dstna_mitId = CType(cboMA.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value
                cDienstNA.dstna_datum = d.ToShortDateString
                ' MsgBox(d.ToShortDateString & " " & CType(cboMA.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value)

                '   SQLDienst.updateDienstEintragArtBemerkungChange(aktMitarbeiterEintrag, TextBox1.Text, TextBox2.Text, TextBox3.Text)
                Dim von As String = getDefaultTimeBySchicht(art, "von", week(d.DayOfWeek))
                Dim bis As String = getDefaultTimeBySchicht(art, "von", week(d.DayOfWeek))
                Dim pause As String = getDefaultTimeBySchicht(art, "pause", week(d.DayOfWeek))

                Select Case week(d.DayOfWeek)
                    Case "SA", "SO" : SQLDienst.updateDienstEintragArtChangeByMitId(CType(cboMA.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, d, "FREI", "00:00", "00:00", "0")
                    Case Else : SQLDienst.updateDienstEintragArtChangeByMitId(CType(cboMA.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, d, art, von, bis, pause)
                End Select
                d = d.AddDays(1)
            End While

            frmDienstplan.initDienstplan()
            Me.Close()
        Catch ex As Exception
            MsgBox("Es ist ein Fehler aufgetreten: " & ex.Message)
        End Try
    End Sub

    Public Function getDefaultTimeBySchicht(abteilung, vonOrBis, woTag)
        abteilung = abteilung.ToString.Replace("ZOLL_VM", "ZOLL1")
        abteilung = abteilung.ToString.Replace("ZOLL_NM", "ZOLL2")
        Dim d As cDienstDetails = SQLDienst.getDienstDetailsWoTagByArtAndWoTag(niederlassung, abteilung, woTag)
        If vonOrBis = "von" Then
            If d.dedet_von <> "" Then Return d.dedet_von Else  : Return "00:00" : End If
        If vonOrBis = "bis" Then
            If d.dedet_bis <> "" Then Return d.dedet_bis Else  : Return "00:00" : End If
        If vonOrBis = "pause" Then
            Return d.dedet_pause : End If
        Return ""
    End Function

    Function cboMyItemSelect(cbo As ComboBox, value As String) As Boolean
        For Each i In cbo.Items
            If i.GetType.Name = "MyListItem" Then
                Try
                    If CType(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value = value Then
                        cbo.SelectedItem = i : Return True
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Next
        Return False
    End Function

End Class