Public Class frmChangeLKW
    Public OldLkW As String = ""
    Public SDLNr As String = ""
    Public History As String = ""
    Public kundenNr As String = ""
    Public newKFZ As String = ""
    Dim kundenSQL As New kundenSQL

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click

        'cboLKW.Text = cboLKW.Text.ToUpper
        'Label1.ForeColor = Color.Black

        If cboLKW.Text <> "" Then
            If OldLkW <> "" And OldLkW <> cboLKW.Text Then
                Try
                    Dim SQL As New SQL
                    If cbxFurAlleLeistungenUbernehmen.Checked Then
                        VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.REMANE_FOLDER_KDNR(New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, OldLkW, Nothing, Nothing),
                                                                           New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, cboLKW.Text, Nothing, Nothing))
                        SQL.doSQL("UPDATE [LKW] SET KfzKennzeichen= '" & cboLKW.Text & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & OldLkW & "'", "SDL", True)
                        SQL.doSQL("UPDATE SDL SET KfzKennzeichen= '" & cboLKW.Text & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & OldLkW & "'", "SDL", True)
                        SQL.doSQL("UPDATE [Kartenpool] SET KfzKennzeichen= '" & cboLKW.Text & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & OldLkW & "'", "SDL", True)
                        
                    Else
                        Dim newHist = SQL.getValueTxtBySql("SELECT isnull(max([History]),0)+1 as History FROM SDL where KundenNr = '" & kundenNr & "' AND KfzKennzeichen='" & cboLKW.Text & "' And SDLNr ='" & SDLNr & "' ", "SDL")
                        VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.REMANE_FOLDER_KDNR(New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, OldLkW, "SDL_" & SDLNr, "HIST_" & History),
                                                                          New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, cboLKW.Text, "SDL_" & SDLNr, "HIST_" & newHist))
                        SQL.doSQL("UPDATE SDL SET KfzKennzeichen= '" & cboLKW.Text & "', History='" & newHist & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & OldLkW & "' AND  SDLNr='" & SDLNr & "'  AND  History='" & History & "'", "SDL", True)
                        
                    End If

                    newKFZ = cboLKW.Text
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            Else
                ' Label1.ForeColor = Color.Red
                cboLKW.Focus()
                Me.DialogResult = Windows.Forms.DialogResult.None
            End If
        End If
    End Sub

    Private Sub MyTextBox2_KeyDown(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Return Then
            btnOK.PerformClick()
        End If
    End Sub

    Private Sub frmAddLKW_Load(sender As Object, e As EventArgs) Handles Me.Load
        initCboLkw()
        If OldLkW <> "" Then
            cboLKW.Text = OldLkW
        End If
        cboLKW.Focus()
    End Sub

    Private Sub txtKennzeichen_LostFocus(sender As Object, e As EventArgs)
        '   txtKennzeichen.Text = txtKennzeichen.Text.ToUpper
    End Sub

    Private Sub FlatButton2_Click(sender As Object, e As EventArgs) Handles FlatButton2.Click
        Dim f As New frmAddLKW
            If f.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                If f.txtKennzeichen.Text <> "" Then
                kundenSQL.insertLKW(kundenNr, f.txtKennzeichen.Text, f.MyTextBox2.Text)
                    initCboLkw(f.txtKennzeichen.Text)
                Else
                    MsgBox("Kein LKW-Kz angegeben!")
                End If
            End If
    End Sub
    Sub initCboLkw(Optional cbolkwsearch = "")
        cboLKW.Text = ""
        cboLKW.Items.Clear()
        For Each s In kundenSQL.getLKWKfzByKdNr(kundenNr)
            cboLKW.Items.Add(s)
        Next
        If cbolkwsearch <> "" Then
            For Each i In cboLKW.Items
                If i.ToString = cbolkwsearch Then cboLKW.SelectedItem = i : cbolkwsearch = "" : Exit For
            Next
        End If
    End Sub

End Class