Imports System.Data.SqlClient
Imports System.Reflection
Imports System.ComponentModel
Imports System.Windows.Media
Imports sun.reflect

Public Class usrCntlLKW
    Dim kdnr As String
    Dim kz As String
    Dim lkwNationalitaet As String

    Dim erfassungsdatum As String
    Dim aenderungsdatum As String
    Dim sachbearbeiter As String
    'Private bindingSDL As New BindingSource
    'Private dataAdapterLKW As New SqlDataAdapter
    Dim SQL As New SQL
    Dim kundenSQL As New kundenSQL
    Dim FUNC As New cProgramFunctions
    Dim SDLLeistunenLoaded As Boolean = False
    Public Event LKWChanged(kdNr, newKfz, SDLNr, History)
    Public Event nextLKW_Clicked()
    Public Event prevLKW_Clicked()

    Dim valueChanged As Boolean = False
    Dim loaded = False

    Private Sub usrCntlLKW_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.PageDown Then
            btnNextLKW.PerformClick()
        End If
        If e.KeyCode = Keys.PageUp Then
            btnPrevLKW.PerformClick()
        End If
    End Sub
    'Dim usrCntlBinding As cBinding = Nothing



    '    Private Sub usrCntlLKW_Changed(kdNr As Object, newKfz As Object, SDLNr As Object, History As Object) Handles Me.Changed
    '      init(kdNr, kz, lkwNationalitaet, erfassungsdatum, aenderungsdatum, sachbearbeiter)
    '  End Sub


    Private Sub usrCntlLKW_Load(sender As Object, e As EventArgs) Handles Me.Load
        init(kdnr, kz, lkwNationalitaet, erfassungsdatum, aenderungsdatum, sachbearbeiter)

        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", Me.FindForm) Then
            btnAddLeistung.Enabled = False
        End If
        loaded = True
    End Sub

    Public Sub init(kdnr, kz, lkwNationalitaet, erfassungsdatum, aenderungsdatum, sachbearbeiter)
        'dgvSDLLeistungen.Columns.Clear()
        Me.kdnr = kdnr
        Me.kz = IIf(kz Is DBNull.Value, "", kz)
        Me.lkwNationalitaet = IIf(lkwNationalitaet Is DBNull.Value, "", lkwNationalitaet)
        Me.erfassungsdatum = IIf(erfassungsdatum Is DBNull.Value, "", erfassungsdatum)
        Me.aenderungsdatum = IIf(aenderungsdatum Is DBNull.Value, "", aenderungsdatum)
        Me.sachbearbeiter = IIf(sachbearbeiter Is DBNull.Value, "", sachbearbeiter)

        lblLKW.Text = Me.kz
        lblLKWLand.Text = Me.lkwNationalitaet
        lblErfassung.Text = Me.erfassungsdatum
        lblAenderung.Text = Me.aenderungsdatum
        lblSachbearbeiter.Text = Me.sachbearbeiter

        If IsNumeric(kdnr) AndAlso kdnr > 0 Then
            Try
                '   Me.dgvSDLLeistungen.DataSource = Me.bindingSDL
                initSDLBinding()
                showDgvSDLLeistungen()
                SDLLeistunenLoaded = True
                If dgvSDLLeistungen.Rows.Count > 0 Then
                    'dgvSDLLeistungen.CurrentCell = dgvSDLLeistungen.Item(1, 0)
                    dgvSDLLeistungen.Rows(0).Selected = True
                End If

                'Scan-Elemente initialisieren:
                scanZulassungsschein.INIT(kdnr, Me.kz)
                scanCEMT.INIT(kdnr, Me.kz)
                scanLeasingVertrag.INIT(kdnr, Me.kz)
                scanMietVertrag.INIT(kdnr, Me.kz)
                scanSonstiges.INIT(kdnr, Me.kz)
                scanCOCDokument.INIT(kdnr, Me.kz)
                scanCIFDokument.INIT(kdnr, Me.kz)

                scanZulassungsschein.Enabled = True
                scanCEMT.Enabled = True
                scanLeasingVertrag.Enabled = True
                scanMietVertrag.Enabled = True
                scanSonstiges.Enabled = True

                scanCOCDokument.Enabled = True
                scanCIFDokument.Enabled = True


                '   AddHandler btnNextSDL.Click, AddressOf nextLKW
                '  AddHandler btnPrevSDL.Click, AddressOf prevLKW

                'FUNC.initChangeEvent(Me)
            Catch ex As SqlException
                MessageBox.Show("Problem mit dem Laden der Daten. Wenden Sie sich an den Programmbetreuer. " & ex.Message)
            End Try
        End If
        dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
    End Sub

    Sub showDgvSDLLeistungen()
        Try
            With dgvSDLLeistungen
                If .ColumnCount > 0 Then

                    .RowTemplate.Height = 20
                    .AllowUserToAddRows = False
                    .AllowUserToDeleteRows = False
                    .AllowUserToOrderColumns = False
                    .AllowUserToResizeColumns = False
                    .AllowUserToResizeRows = False
                    .RowTemplate.ReadOnly = True
                    .RowHeadersVisible = False
                    .SelectionMode = DataGridViewSelectionMode.FullRowSelect

                    .AllowUserToOrderColumns = True


                    .Columns("KundenNr").Visible = False
                    .Columns("KfzKennzeichen").Visible = False
                    .Columns("History").Visible = False
                    .Columns("Archiviert").Visible = False


                    .Columns("SDLNr").Width = 40
                    .Columns("SDLNr").HeaderText = "SDLNr"
                    .Columns("SDLNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("SDLBez").Width = 220
                    .Columns("SDLBez").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                    .Columns("SDLBez").HeaderText = "Bezeichnung"



                    .Columns("Bestelldatum").Width = 65
                    .Columns("Bestelldatum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("Bestelldatum").HeaderText = "Bestelldat."

                    .Columns("Lieferdatum").Width = 65
                    .Columns("Lieferdatum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("GültigBis").Width = 65
                    .Columns("GültigBis").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("KartenNr").Width = 110

                    .Columns("PIN").Width = 45
                    .Columns("PIN").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("Ersatzkarte").Width = 50
                    .Columns("Ersatzkarte").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("Ersatzkarte").HeaderText = "Ersatzk."

                    .Columns("Retourniert").Width = 50
                    .Columns("Retourniert").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("Retourniert").HeaderText = "Retour."

                    .Columns("Gesperrt").Width = 50
                    .Columns("Gesperrt").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("Defekt").Width = 50
                    .Columns("Defekt").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

                    .Columns("Verloren").Width = 50
                    .Columns("Verloren").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("Verloren").HeaderText = "Verloren"

                    .Columns("Gestohlen").Width = 50
                    .Columns("Gestohlen").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("Gestohlen").HeaderText = "Gestohl."

                    .Columns("LKWAbgemeldet").Width = 50
                    .Columns("LKWAbgemeldet").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
                    .Columns("LKWAbgemeldet").HeaderText = "Abgem."

                    redRows()
                End If
            End With
        Catch ex As Exception

        End Try

    End Sub


    Sub redRows()
        For Each r As DataGridViewRow In dgvSDLLeistungen.Rows
            Dim bgred = False
            'If r.Cells("Ersatzkarte").Value IsNot DBNull.Value AndAlso r.Cells("Ersatzkarte").Value Then bgred = True
            If r.Cells("Retourniert").Value IsNot DBNull.Value AndAlso r.Cells("Retourniert").Value Then bgred = True
            If r.Cells("Gesperrt").Value IsNot DBNull.Value AndAlso r.Cells("Gesperrt").Value Then bgred = True
            If r.Cells("Defekt").Value IsNot DBNull.Value AndAlso r.Cells("Defekt").Value Then bgred = True
            If r.Cells("Verloren").Value IsNot DBNull.Value AndAlso r.Cells("Verloren").Value Then bgred = True
            If r.Cells("LKWAbgemeldet").Value IsNot DBNull.Value AndAlso r.Cells("LKWAbgemeldet").Value Then bgred = True
            If r.Cells("Gestohlen").Value IsNot DBNull.Value AndAlso r.Cells("Gestohlen").Value Then bgred = True
            If r.Cells("GültigBis").Value IsNot DBNull.Value AndAlso CDate(r.Cells("GültigBis").Value) < Now.ToShortDateString Then bgred = True
            If r.Cells("Archiviert").Value IsNot DBNull.Value AndAlso r.Cells("Archiviert").Value Then bgred = True
            ' If r.Cells("TelepassErsatzkarte").Value IsNot DBNull.Value AndAlso CDate(r.Cells("TelepassErsatzkarte").Value) < Now.ToShortDateString Then bgred = True

            If bgred Then r.DefaultCellStyle.ForeColor = System.Drawing.Color.Red
        Next

    End Sub
    Private Sub initSDLBinding(Optional srchSdlNr = Nothing, Optional srchHistory = Nothing)

        Dim sqlstr As String = " SELECT SDL.KundenNr,SDL.KfzKennzeichen, SDL.SDLNr, SDL.History,Speditionsdienstleistungen.SDLBez,SDL.Bestelldatum,SDL.Lieferdatum,SDL.GültigBis,SDL.KartenNr,SDL.PIN, " &
                                          " sdl.[Ersatzkarte],sdl.[Retourniert],sdl.[Gesperrt],sdl.[Defekt],sdl.[Verloren],sdl.[Gestohlen],sdl.LKWAbgemeldet /*, sdl.Erfassungsdatum,sdl.Änderungsdatum,sdl.Sachbearbeiter*/ , sdl.Archiviert " &
                                     " FROM SDL, Speditionsdienstleistungen WHERE KundenNr=" & kdnr & " AND  KfzKennzeichen='" & kz & "' AND SDL.SDLNr=Speditionsdienstleistungen.SDLNr AND SDLTypNr IN (1,2) ORDER BY Speditionsdienstleistungen.SDLBez, SDL.History desc"
        dgvSDLLeistungen.SET_SQL(sqlstr, "SDL")
        dgvSDLLeistungen.LOAD()

        If dgvSDLLeistungen.RowCount > 0 Then
            Try
                If srchSdlNr IsNot Nothing And srchHistory IsNot Nothing Then
                    For Each r As DataGridViewRow In dgvSDLLeistungen.Rows
                        If r.Cells("SDLNr") IsNot Nothing And r.Cells("History") IsNot Nothing Then
                            If r.Cells("SDLNr").Value = srchSdlNr And r.Cells("History").Value = srchHistory Then
                                r.Selected = True
                                dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
                                Exit Sub
                            End If
                        End If
                    Next
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            redRows()
        End If
    End Sub

    Private Sub dgvSDLLeistungen_SelectionChanged(sender As Object, e As EventArgs) Handles dgvSDLLeistungen.Click 'currentCellChanged ' dgvSDLLeistungen.CurrentCellChanged
        'MsgBox(dgvSDLLeistungen.CurrentRow.Cells(2).Value)
        ' MsgBox("SEL")
        If loaded = False Then Exit Sub
        pnlSDLLeistungenDetails.Controls.Clear()
        Try

            If dgvSDLLeistungen.RowCount > 0 And dgvSDLLeistungen.SelectedRows.Count > 0 AndAlso SDLLeistunenLoaded Then

                If dgvSDLLeistungen.SelectedRows.Count = 0 Then dgvSDLLeistungen.Rows(0).Selected = True 'Exit Sub


                If dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 100 Then ' IDS
                    Dim usrcntl As New usrCntlSDLLeistungenIDS(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub


                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 101 Then ' WÖLFL
                    Dim usrcntl As New usrCntlSDLLeistungenWOELFL(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 200 Then ' AT-MAUT
                    Dim usrcntl As New usrCntlSDLLeistungMautAT(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 201 Then ' DE-MAUT
                    Dim usrcntl As New usrCntlSDLLeistungMautDE(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 202 Then ' IT-MAUT
                    Dim usrcntl As New usrCntlSDLLeistungenIT(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 203 Then ' CZ MAUT
                    Dim usrcntl As New usrCntlSDLLeistungenCZ(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 204 Then ' FR-MAUT
                    Dim usrcntl As New usrCntlSDLLeistungenFR(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 205 Then ' MotorWayMaut
                    Dim usrcntl As New usrCntlSDLLeistungenMotorWayMaut(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 206 Then ' DUOBOX FR / ES
                    Dim usrcntl As New usrCntlSDLLeistungenDUOBOX(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 207 Then ' FREJUS
                    Dim usrcntl As New usrCntlSDLLeistungenFREJUS(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 208 Then ' MSE Card
                    Dim usrcntl As New usrCntlSDLLeistungenMSE(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 209 Then ' PL MAUT
                    Dim usrcntl As New usrCntlSDLLeistungenPL(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 210 Then ' SI-MAUT
                    Dim usrcntl As New usrCntlSDLLeistungenSI(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 211 Then ' ECOTAX
                    Dim usrcntl As New usrCntlSDLLeistungenECOTAX(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 212 Then ' UTA
                    Dim usrcntl As New usrCntlSDLLeistungenUTA(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 213 Then ' HUGO
                    Dim usrcntl As New usrCntlSDLLeistungHUGO(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub
                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 214 Then ' PLOSE 
                    Dim usrcntl As New usrCntlSDLLeistungPLOSE(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 215 Then ' PLOSE IT
                    Dim usrcntl As New usrCntlSDLLeistungenPLOSE_IT(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 216 Then ' PLOSE CZ
                    Dim usrcntl As New usrCntlSDLLeistungenPLOSE_CZ(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub


                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 217 Then ' PLOSE CZ
                    Dim usrcntl As New usrCntlSDLLeistungenPLOSE_MEDPASS(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub


                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 500 Then ' MAN
                    Dim usrcntl As New usrCntlSDLLeistungenMANAbgas(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 501 Then ' parkplatz
                    Dim usrcntl As New usrCntlSDLLeistungVERAGCard(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                ElseIf dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = 502 Then ' parkplatz
                    Dim usrcntl As New usrCntlSDLLeistungVERAGCardNEU(dgvSDLLeistungen.SelectedRows(0).Cells(0).Value, dgvSDLLeistungen.SelectedRows(0).Cells(1).Value, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value)
                    pnlSDLLeistungenDetails.Controls.Add(usrcntl)
                    AddHandler usrcntl.LKWChanged, Sub(kdNr, newKfz, SDLNr, History)
                                                       RaiseEvent LKWChanged(kdNr, newKfz, SDLNr, History)
                                                   End Sub
                    AddHandler usrcntl.CHANGED, Sub(kdNr, newKfz, SDLNr, History)
                                                    doChange(kdNr, newKfz, SDLNr, History)
                                                End Sub

                End If

                ' If Not usrCntlBinding Is Nothing Then
                'AddHandler usrCntlBinding.bindingdataTable.ColumnChanged, Sub()
                ' setChange()
                'End Sub
                '   AddHandler usrCntlBinding.bindingdataTable.RowDeleted, Sub()
                'setChange()
                'End Sub
                'End If
            Else


            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Sub doChange(kdNr, newKfz, SDLNr, History)
        dgvSDLLeistungen.VALUE_CHANGED = True
    End Sub
    Private Sub RemoveClickEvent(b As Button)
        Dim f1 As FieldInfo = GetType(Control).GetField("EventClick", BindingFlags.Static Or BindingFlags.NonPublic)
        Dim obj As Object = f1.GetValue(b)
        Dim pi As PropertyInfo = b.GetType().GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim list As EventHandlerList = DirectCast(pi.GetValue(b, Nothing), EventHandlerList)
        list.RemoveHandler(obj, list(obj))
    End Sub



    'Private Sub setChange()
    '     valueChanged = True
    '      btnSave.Enabled = True
    ' End Sub



    Sub nextLKW() '(sender As Object, e As EventArgs)
        If dgvSDLLeistungen.SelectedCells.Count > 0 Then
            Dim i As Integer = dgvSDLLeistungen.SelectedRows(0).Index
            If (i + 1 < dgvSDLLeistungen.RowCount) Then
                dgvSDLLeistungen.CurrentCell = dgvSDLLeistungen.Item(2, i + 1)
            End If
        End If
        dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
    End Sub
    Sub prevLKW() '(sender As Object, e As EventArgs)
        If dgvSDLLeistungen.SelectedCells.Count > 0 Then
            Dim i As Integer = dgvSDLLeistungen.SelectedRows(0).Index
            If (i > 0) Then
                dgvSDLLeistungen.CurrentCell = dgvSDLLeistungen.Item(2, i - 1)
            End If
        End If
        dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
    End Sub


    Private Sub btnNextSDL_Click(sender As Object, e As EventArgs) Handles btnNextSDL.Click
        nextLKW()
    End Sub
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnPrevSDL.Click
        prevLKW()
    End Sub


    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnAddLeistung.Click
        Dim Speditionsdienstleistungen As List(Of cSpeditionsdienstleistungen) = kundenSQL.getSpeditionsdienstleistungenByKdNr(kdnr)
        cntxtAddSdl.Items.Clear()
        For Each s In Speditionsdienstleistungen
            'Select Case s.SDLNr
            '    Case 501, 502 ' Parkplatzkarten sollten hier nicht hinzugefügt werden können....


            'Case Else
            Dim i As New ToolStripMenuItem
                    i.Text = s.SDLNr & " - " & s.SDLBez
                    i.Name = "tsmiSDLNr_" & s.SDLNr
                    cntxtAddSdl.Items.Add(i)
                    If s.Sperre <> 0 Then i.Enabled = False : i.Text = i.Text & " (gesperrt)"
                    AddHandler i.Click, AddressOf DToolStripMenuItem_Click

            ' End Select
        Next
        btnAddLeistung.ContextMenuStrip = cntxtAddSdl
        ' cntxtAddSdl
        Dim ptLowerLeft As New Point(sender.width, sender.Height)
        ptLowerLeft = sender.PointToScreen(ptLowerLeft)
        ptLowerLeft.X -= sender.width
        cntxtAddSdl.Show(ptLowerLeft)
    End Sub



    Private Sub DToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DToolStripMenuItem.Click
        If sender.name.contains("tsmiSDLNr_") Then
            ' MsgBox(sender.name.ToString.Replace("tsmiSDLNr_", ""))
            Dim sdlNr As String = sender.name.ToString.Replace("tsmiSDLNr_", "")

            Dim cpf As New cProgramFunctions

            Dim hist = -1
            Select Case sdlNr
                Case 501, 502
                    hist = cpf.newEntryVERAG_Card(kdnr, kz, sdlNr)
                Case Else
                    hist = cpf.newEntry(kdnr, kz, sdlNr)
            End Select


            If hist > 0 Then
                initSDLBinding(sdlNr, hist)
            Else
                initSDLBinding()
            End If
            showDgvSDLLeistungen()

            Select Case sdlNr
                Case 213, 210, 207
                    MsgBox("Nicht vergessen: Abrechnungsblatt erstellen!")
            End Select
        End If

    End Sub



    Private Sub DgvDelays_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvSDLLeistungen.CellMouseDown
        Try
            If e.Button = Windows.Forms.MouseButtons.Right Then
                If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", Me.FindForm) Then Exit Sub
                dgvSDLLeistungen.ClearSelection()
                dgvSDLLeistungen.Rows(e.RowIndex).Selected = True
                cntxtSDLLeistungenOptions.Show(dgvSDLLeistungen, dgvSDLLeistungen.PointToClient(Cursor.Position))
                'AddHandler cntxtSDLLeistungenOptions.Items(0).Click, AddressOf delSDLOptions
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


   

    Private Sub dgvSDLLeistungen_KeyUp(sender As Object, e As KeyEventArgs) Handles dgvSDLLeistungen.KeyUp
        If e.KeyCode = Keys.Delete Then
            deletedgvSDLLeistungenRow()
        End If
        If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
            dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
        End If

    End Sub
    Private Sub LöschenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LöschenToolStripMenuItem.Click
        deletedgvSDLLeistungenRow()
    End Sub

    Sub deletedgvSDLLeistungenRow()
        If dgvSDLLeistungen.SelectedRows.Count > 0 Then
            If dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = "501" Or dgvSDLLeistungen.SelectedRows(0).Cells(2).Value = "502" Then
                MsgBox("VERAG-Parkplatzkarten können nicht gelöscht werden. Bitte auf gesperrt/verloren/gestohlen/defekt setzen!")
                Exit Sub
            End If
            If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Leistungen_bearbeiten", Me.FindForm) Then Exit Sub
            If vbYes = MsgBox("Möchten Sie die Leistung '" & dgvSDLLeistungen.SelectedRows(0).Cells(2).Value & " - " & dgvSDLLeistungen.SelectedRows(0).Cells(4).Value & "' wirklich löschen?", vbYesNoCancel) Then
                If kundenSQL.delSDLBykdNrKz(kdnr, kz, dgvSDLLeistungen.SelectedRows(0).Cells(2).Value, dgvSDLLeistungen.SelectedRows(0).Cells(3).Value) Then
                    initSDLBinding()
                    pnlSDLLeistungenDetails.Controls.Clear()
                    If dgvSDLLeistungen.SelectedRows.Count > 0 Then
                        dgvSDLLeistungen.Rows(0).Selected = True
                        dgvSDLLeistungen_SelectionChanged(dgvSDLLeistungen, New EventArgs)
                    End If
                End If
            End If
        End If
    End Sub




    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnPrevLKW.Click
        RaiseEvent prevLKW_Clicked()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnNextLKW.Click
        RaiseEvent nextLKW_Clicked()
    End Sub

    Private Sub dgvSDLLeistungen_RELOADED() Handles dgvSDLLeistungen.RELOADED
        showDgvSDLLeistungen()
    End Sub
End Class
