Imports System.Reflection
Imports System.ComponentModel


Public Class frmNewData
    Public addnew = True
    Public SDLLeistung As String = ""
    Public history As String = ""
    Public kdnr As String = ""
    Public KfzKennzeichen As String = ""
    Public newData = False
    ' Dim valueChanged As Boolean = False
    Dim kundenSQL As New kundenSQL
    Public LeistungVordefiniert As Integer = -1
    Dim PF As New cProgramFunctions

    Public showDetailForm As Boolean = False
    
   

    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 RemoveClickEvent(b As ToolStripButton)
        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 frmDatenDetails_Load(sender As Object, e As EventArgs) Handles Me.Load
        KdSearchBox1.initKdBox(Me, txtKundenNr)
        LeistungVordefiniert = If(IsNumeric(SDLLeistung), SDLLeistung, -1)


        MyFlowLayoutPanel1.HorizontalScroll.Visible = False
        MyFlowLayoutPanel1.HorizontalScroll.Enabled = False

        addCntlLKW()



        AddHandler KdSearchBox1.PropertyChanged, Sub()
                                                     Try
                                                         checkKD()
                                                         If KdSearchBox1.KdNr > 0 Then
                                                             For Each c In MyFlowLayoutPanel1.Controls
                                                                 DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).KdNr = KdSearchBox1.KdNr
                                                                 DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).initCBO()
                                                                 DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).cboLKW.Focus()
                                                             Next
                                                         End If
                                                     Catch ex As Exception
                                                         MsgBox(ex.Message & vbNewLine & ex.StackTrace)
                                                     End Try
                                                 End Sub
        If IsNumeric(kdnr) Then
            KdSearchBox1.KdNr = kdnr
            'If SDLLeistung <> "" Then init()
            initSDL()

            If KfzKennzeichen <> "" Then
                If MyFlowLayoutPanel1.Controls.Count > 0 Then
                    DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).cboLKW.Text = KfzKennzeichen
                End If
            End If
        End If

    End Sub


    Sub addCntlLKW()
        Dim u As New usrcntlNewDataLKW(kdnr)
        AddHandler u.LKW_ADDED, Sub(kfz)
                                    For Each c In MyFlowLayoutPanel1.Controls
                                        DirectCast(c, usrcntlNewDataLKW).initCBO()
                                    Next
                                End Sub
        AddHandler u.LKW_DEL, Sub(kfz)
                                  If MyFlowLayoutPanel1.Controls(MyFlowLayoutPanel1.Controls.Count - 1) IsNot u Then
                                      MyFlowLayoutPanel1.Controls.Remove(u)
                                      If Not (MyFlowLayoutPanel1.VerticalScroll.Visible Or MyFlowLayoutPanel1.HorizontalScroll.Visible) Then Me.Height -= u.Height
                                  End If
                              End Sub
        AddHandler u.cboLKW.SelectedIndexChanged, Sub()
                                                      If u.cboLKW.Text = "" Then
                                                          If MyFlowLayoutPanel1.Controls(MyFlowLayoutPanel1.Controls.Count - 1) IsNot u Then
                                                              MyFlowLayoutPanel1.Controls.Remove(u)
                                                              If Not (MyFlowLayoutPanel1.VerticalScroll.Visible Or MyFlowLayoutPanel1.HorizontalScroll.Visible) Then Me.Height -= u.Height
                                                          End If
                                                      Else
                                                          If MyFlowLayoutPanel1.Controls(MyFlowLayoutPanel1.Controls.Count - 1) Is u Then
                                                              addCntlLKW()
                                                          End If
                                                      End If
                                                  End Sub

        MyFlowLayoutPanel1.Controls.Add(u)
        If (Me.Top + Me.Height + u.Height) < My.Computer.Screen.WorkingArea.Height Then
            Me.Height += u.Height
        End If

        u.cboLKW.Focus()
    End Sub
    Sub initSDL()
        Dim Speditionsdienstleistungen As List(Of cSpeditionsdienstleistungen) = kundenSQL.getSpeditionsdienstleistungenByKdNr(kdnr)
        'cboLeistungen.Items.Clear()
        For Each c As Control In FlowLayoutPanel.Controls
            If c.GetType.ToString = "System.Windows.Forms.Button" Then c.Enabled = False : DirectCast(c, Button).FlatAppearance.BorderColor = Color.LightGray
        Next
        For Each s In Speditionsdienstleistungen
            For Each c As Control In FlowLayoutPanel.Controls
                If c.GetType.ToString = "System.Windows.Forms.Button" Then
                    If DirectCast(c, Button).Tag = s.SDLNr Then

                        If s.Sperre Then
                            DirectCast(c, Button).FlatAppearance.BorderColor = Color.Red
                        Else
                            c.Enabled = True
                            DirectCast(c, Button).FlatAppearance.BorderColor = Color.Black
                        End If

                    End If
                End If
            Next
        Next

    End Sub


    Sub checkKD()
        '  MsgBox(KdSearchBox1.KdNr_value)
        If IsNumeric(KdSearchBox1.KdNr_value) AndAlso kundenSQL.existByKdNr(KdSearchBox1.KdNr_value) Then
            '  MsgBox(KdSearchBox1.KdNr_value)
            kdnr = KdSearchBox1.KdNr_value
            txtKundenNr.ForeColor = Color.Black
            Dim kundenSQL As New kundenSQL
            Dim cKunden As cKunden = kundenSQL.getKundeByKdNr(kdnr)
            '   lblFirma.Text = cKunden.Kurzname
            ' If cKunden.Abfertigungsverbot Then lblFirma.ForeColor = Color.Red : lblFirma.Text &= " (Abfertigungsverbot)"
            '  Panel1.BackColor = IIf(cKunden.Abfertigungsverbot, Color.IndianRed, Color.FromArgb(0, 54, 128))
            Label4.Visible = cKunden.Abfertigungsverbot
            initSDL()
            ' init()
        Else
            txtKundenNr.ForeColor = Color.Red
            'lblFirma.Text = ""
        End If
    End Sub


    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs)
        's.row_kdNr = kdnr
        'FlatButton1.PerformClick()
    End Sub

    Private Sub FlatButton3_Click(sender As Object, e As EventArgs)
        Dim frmKundenSuche As New frmKundenSuche
        If frmKundenSuche.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
            txtKundenNr.Text = frmKundenSuche.kundenNrSearch.Text
        End If
    End Sub



    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        Me.DialogResult = Windows.Forms.DialogResult.None
        Me.Close()
    End Sub

    Private Sub frmNewData_Scroll(sender As Object, e As ScrollEventArgs) Handles Me.Scroll

    End Sub





    Private Sub frmNewData_Shown(sender As Object, e As EventArgs) Handles Me.Shown
       

        If LeistungVordefiniert > 0 Then
            'FlowLayoutPanel.Visible = False
            Me.Height = Me.Height - (FlowLayoutPanel.Height)

            FlowLayoutPanel.Height = 0
            FlowLayoutPanel.Enabled = False


            With btnAddLeistungVordefiniert
                Select Case LeistungVordefiniert
                    Case 100 : btnAddLeistungVordefiniert.Image = My.Resources.ids : btnAddLeistungVordefiniert.Text = "IDS" & vbNewLine & "hinzufügen"
                    Case 101 : btnAddLeistungVordefiniert.Image = My.Resources.woelfl : btnAddLeistungVordefiniert.Text = "Wölfl" & vbNewLine & "hinzufügen"
                    Case 212 : btnAddLeistungVordefiniert.Image = My.Resources.uta : btnAddLeistungVordefiniert.Text = "UTA" & vbNewLine & "hinzufügen"
                    Case 200 : btnAddLeistungVordefiniert.Image = My.Resources.go : btnAddLeistungVordefiniert.Text = "Maut AT" & vbNewLine & "hinzufügen"
                    Case 201 : btnAddLeistungVordefiniert.Image = My.Resources.toll_collect : btnAddLeistungVordefiniert.Text = "Maut DE" & vbNewLine & "hinzufügen"
                    Case 202 : btnAddLeistungVordefiniert.Image = My.Resources.telepass : btnAddLeistungVordefiniert.Text = "Maut IT" & vbNewLine & "hinzufügen"
                    Case 203 : btnAddLeistungVordefiniert.Image = My.Resources.premid : btnAddLeistungVordefiniert.Text = "Maut CZ" & vbNewLine & "hinzufügen"
                    Case 213 : btnAddLeistungVordefiniert.Image = My.Resources.hugo : btnAddLeistungVordefiniert.Text = "Hugo" & vbNewLine & "hinzufügen"
                    Case 214 : btnAddLeistungVordefiniert.Image = My.Resources.plose : btnAddLeistungVordefiniert.Text = "PLOSE" & vbNewLine & "hinzufügen"
                    Case 215 : btnAddLeistungVordefiniert.Image = My.Resources.plose_it : btnAddLeistungVordefiniert.Text = "PLOSE IT" & vbNewLine & "hinzufügen"
                    Case 216 : btnAddLeistungVordefiniert.Image = My.Resources.plose_cz : btnAddLeistungVordefiniert.Text = "PLOSE CZ" & vbNewLine & "hinzufügen"
                    Case 209 : btnAddLeistungVordefiniert.Image = My.Resources.viatoll : btnAddLeistungVordefiniert.Text = "Maut PL" & vbNewLine & "hinzufügen"
                    Case 210 : btnAddLeistungVordefiniert.Image = My.Resources.abc_obu : btnAddLeistungVordefiniert.Text = "Maut SI" & vbNewLine & "hinzufügen"
                    Case 208 : btnAddLeistungVordefiniert.Image = My.Resources.mse : btnAddLeistungVordefiniert.Text = "MSE" & vbNewLine & "hinzufügen"
                    Case 205 : btnAddLeistungVordefiniert.Image = Nothing : btnAddLeistungVordefiniert.Text = "MotorWay" & vbNewLine & "hinzufügen"
                    Case 211 : btnAddLeistungVordefiniert.Image = My.Resources.axxes : btnAddLeistungVordefiniert.Text = "MULTIBOX" & vbNewLine & "hinzufügen"
                    Case 207 : btnAddLeistungVordefiniert.Image = My.Resources.frejus : btnAddLeistungVordefiniert.Text = "FREJUS" & vbNewLine & "hinzufügen"
                    Case 206 : btnAddLeistungVordefiniert.Image = Nothing : btnAddLeistungVordefiniert.Text = "DouBox" & vbNewLine & "hinzufügen"
                    Case 500 : btnAddLeistungVordefiniert.Image = My.Resources.Plakette1 : btnAddLeistungVordefiniert.Text = "MAN Abgas" & vbNewLine & "hinzufügen"
                    Case 501 : btnAddLeistungVordefiniert.Image = My.Resources.verag_Card1 : btnAddLeistungVordefiniert.Text = "Parkplatz" & vbNewLine & "hinzufügen"
                End Select
                .Visible = True
                .Tag = LeistungVordefiniert
            End With
          
        End If
        MyFlowLayoutPanel1.Height = Me.Height - MyFlowLayoutPanel1.Top - FlowLayoutPanel.Height
        MyFlowLayoutPanel1.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Bottom

        If KdSearchBox1.KdNr > 0 Then
            If MyFlowLayoutPanel1.Controls.Count > 0 Then
                DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).cboLKW.Text = KfzKennzeichen
            End If
        Else
            KdSearchBox1.Focus()
        End If

        MyFlowLayoutPanel1.HorizontalScroll.Maximum = 0
        MyFlowLayoutPanel1.AutoScroll = False
        MyFlowLayoutPanel1.VerticalScroll.Visible = False
        MyFlowLayoutPanel1.AutoScroll = True
    End Sub






    Private Sub KdSearchBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles KdSearchBox1.KeyDown
        If e.KeyCode = Keys.Return Then
            e.SuppressKeyPress = True
            If MyFlowLayoutPanel1.Controls.Count > 0 Then
                DirectCast(MyFlowLayoutPanel1.Controls(0), usrcntlNewDataLKW).cboLKW.Text = KfzKennzeichen
            End If
        End If
    End Sub


    Private Sub btnSDL_Woelfl_Click(sender As Object, e As EventArgs) Handles btnSDL_Woelfl.Click, btnSDL_IDS.Click, btnSDL_MautAT.Click, btnSDL_MautDE.Click, btnSDL_MautMSE.Click, btnSDL_UTA.Click, btnSDL_MautPL.Click,
        btnSDL_MautIT.Click, btnSDL_MautSI.Click, btnSDL_MautCZ.Click, btnSDL_ECOTAX.Click, btnSDL_FREJUS.Click, btnSDL_Hugo.Click, btnSDL_DouBox.Click, btnSDL_MotorWay.Click, btnSDL_MautFR.Click, btnAddLeistungVordefiniert.Click, btnSDL_MANAbgas.Click,
        btnVeragParkplatz.Click, btnSDL_PLOSE.Click, btnSDL_PLOSE_IT.Click, btnSDL_PLOSE_CZ.Click

        If KdSearchBox1.KdNr <= 0 Then MsgBox("Kunde auswählen!") : Exit Sub

        Dim min1LKW = False
        For Each c In MyFlowLayoutPanel1.Controls
            If DirectCast(c, usrcntlNewDataLKW).cboLKW._value <> "" Then min1LKW = True
        Next
        If Not min1LKW Then MsgBox("LKW auswählen!") : Exit Sub

        For Each c In MyFlowLayoutPanel1.Controls
            Dim cbo As VERAG_PROG_ALLGEMEIN.MyComboBox = DirectCast(c, usrcntlNewDataLKW).cboLKW

            If cbo._value <> "" Then



                If sender.tag = "501" Then
                    Dim kdnr_tmp = KdSearchBox1.KdNr
                    Dim kfz_tmp = cbo.Text
                    Dim history = PF.newEntryVERAG_Card(KdSearchBox1.KdNr, cbo.Text, sender.tag)

                    Me.SDLLeistung = sender.tag
                    Me.kdnr = kdnr_tmp
                    Me.KfzKennzeichen = kfz_tmp
                    Me.history = history

                Else
                    Dim history = PF.newEntry(KdSearchBox1.KdNr, cbo.Text, sender.tag)

                    Me.SDLLeistung = sender.tag
                    Me.kdnr = KdSearchBox1.KdNr
                    Me.KfzKennzeichen = cbo.Text
                    Me.history = history

                End If



            End If
        Next

        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub


    Private Sub FlatButton1_Click_1(sender As Object, e As EventArgs) Handles FlatButton1.Click
        Me.DialogResult = Windows.Forms.DialogResult.None
        Me.Close()
    End Sub

End Class


' If SDLNr = 501 Then

'            If KundenNr <= 0 Then Return "CANCEL"
'            If KfzKennzeichen = "" Then KfzKennzeichen = InputBox("KfzKennzeichen eingeben:")
'            If KfzKennzeichen = "" Then Return "CANCEL"

'Dim f As New frmFindNewVeragCard
'            If f.ShowDialog(control) = DialogResult.OK Then
'Dim SDLTMP As New cSDL(f.kdNr, f.KfzKennzeichen, f.SDLNr, f.History)

'                SDLTMP.KundenNr = KundenNr
'                SDLTMP.KfzKennzeichen = KfzKennzeichen
'                If SDLTMP.SAVE() Then

'                    KfzKennzeichen = SDLTMP.KfzKennzeichen
'                    History = SDLTMP.History
'                    SDLNr = 501
'                    Return "OK"
'                Else
'                    Return "CANCEL"
'                End If
'            Else
'                Return "CANCEL"
'            End If
'        Else