Public Class usrCntlDispoliste
    Implements FormualrInterface

    Public Event CHANGED(name, value)
    Public Event KDNR_CHANGED(kdnr)
    Public Event DELETE(c As Control)
    Public Event MOVE_UP(c As Control)
    Public Event MOVE_DOWN(c As Control)
    Dim AVISOID As Integer = 0
    Dim AVISODAL As New VERAG_PROG_ALLGEMEIN.cAvisoDAL
    Public KdNR As Integer = -1
    Dim WithEvents timer As New Timer
    Public Sub ChangeKDNr(kdnr As Integer) _
       Implements FormualrInterface.ChangeKDNr
        initKdNR(kdnr)
    End Sub
    Sub New(AVISOID As Integer)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
        Me.AVISOID = AVISOID
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub
    'Dim KdNR As Integer = -1
    Sub initControlsHandler()
        For Each c In Me.Controls
            Select Case c.GetType.ToString
                Case GetType(VERAG_PROG_ALLGEMEIN.MyTextBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyTextBox).Leave, Sub()
                                                                                        RaiseEvent CHANGED(c.name, c.text)
                                                                                    End Sub
                Case GetType(VERAG_PROG_ALLGEMEIN.MyComboBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyComboBox).SelectedIndexChanged, Sub()
                                                                                                        RaiseEvent CHANGED(c.name, c._value)
                                                                                                    End Sub
                Case GetType(TextBox).ToString
                    AddHandler DirectCast(c, TextBox).Leave, Sub()
                                                                 RaiseEvent CHANGED(c.name, c.text)
                                                             End Sub
            End Select
        Next
    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        Dim kdsrch As New SDL.frmKundenSuche
        'Panel1.Enabled = False
        If kdsrch.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            If IsNumeric(kdsrch.kundenNrSearch.Text) Then
                initKdNR(kdsrch.kundenNrSearch.Text)
                RaiseEvent KDNR_CHANGED(KdNR)
            End If
        End If
        '  Panel1.Enabled = True
    End Sub

    Public Sub initKdNR(KdNR)
        Me.KdNR = KdNR
        Dim KUNDE As New VERAG_PROG_ALLGEMEIN.cKunde(KdNR)
        Dim ADRESSE As New VERAG_PROG_ALLGEMEIN.cAdressen(KdNR)

        txtFirma.Text = If(ADRESSE.Name_1, "") & If(If(ADRESSE.Name_2, "") <> "", " " & ADRESSE.Name_2, "")
        txtAdresse1.Text = If(ADRESSE.Straße, "")
        txtAdresse2.Text = (If(ADRESSE.LandKz, "") & " " & If(ADRESSE.PLZ, "") & " " & If(ADRESSE.Ort, "")).ToString.Trim

        txtAnsprechpartner.Text = If(ADRESSE.Ansprechpartner, "")

    End Sub


    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
        RaiseEvent DELETE(Me)
    End Sub

    Private Sub btnup_Click(sender As Object, e As EventArgs) Handles btnUp.Click
        RaiseEvent MOVE_UP(Me)
    End Sub

    Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        RaiseEvent MOVE_DOWN(Me)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub usrcntlVollmacht_AT_Load(sender As Object, e As EventArgs) Handles Me.Load
        initControlsHandler()
        cboSprache.Items.Clear()
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Türkisch", "TR"))
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Englisch", "EN"))
        'cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Deutsch", "DE"))
        If KdNR > 0 Then initKdNR(KdNR)

        MyFlowLayoutPanel1.Controls.Clear()
        addElement()
        timer.Interval = 500
    End Sub

    Public Sub initAVISOID()
        If AVISOID <= 0 Then Exit Sub

        Dim AVISO As VERAG_PROG_ALLGEMEIN.cAviso = AVISODAL.LesenAviso(AVISOID, "")
        If AVISO IsNot Nothing Then
            txtLKWKennzeichen.Text = AVISO.LKW_Nr
            MyFlowLayoutPanel1.Controls.Clear()
            IMPORT()
            'MyTextBox1.Text = AVISOID
        End If
    End Sub

    Function addElement() As usrCnrlDispolisteElemente
        Dim u As New usrCnrlDispolisteElemente
        AddHandler u.ADD_ELEMENT, Sub()
                                      addElement()
                                      Me.Height += u.Height + 6
                                  End Sub
        AddHandler u.DEL_ELEMENT, Sub(c As usrCnrlDispolisteElemente)
                                      If MyFlowLayoutPanel1.Controls.Count > 1 Then
                                          MyFlowLayoutPanel1.Controls.Remove(c)
                                          Me.Height -= u.Height + 6
                                      End If

                                  End Sub

        MyFlowLayoutPanel1.Controls.Add(u)

        Dim tmp As usrCnrlDispolisteElemente = Nothing
        Dim cnt = 1
        For Each c In MyFlowLayoutPanel1.Controls
            tmp = DirectCast(c, usrCnrlDispolisteElemente)
            tmp.Button1.Visible = False
            tmp.lbl1.Text = cnt : cnt += 1
        Next
        If tmp IsNot Nothing Then tmp.Button1.Visible = True
        Return u
    End Function

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        FormularManagerNEU.genPDF(Me, True)
    End Sub


    Sub IMPORT()
        Me.Cursor = Cursors.WaitCursor
        Try
            If AVISOID <= 0 Then Exit Sub

            For Each r As DataRow In AVISODAL.loadDataTableBySQL("SELECT tblSnd_SendungID FROM tblSendungen WHERE tblSnd_AvisoID=" & AVISOID).Rows
                Dim SENDUNG As New VERAG_PROG_ALLGEMEIN.cSendungen(r("tblSnd_SendungID"))
                ' MsgBox(SENDUNG.FilialenNr)
                If SENDUNG IsNot Nothing Then
                    '  Select Case SENDUNG.FilialenNr
                    '   Case "4805", "4811"
                    '  Case Else
                    Select Case SENDUNG.tblSnd_Abfertigungsart_ID
                        Case "5", "6", "33", "34", "37" ', "28", "26" 
                            '   MsgBox("ADD")
                            Dim u As usrCnrlDispolisteElemente = addElement()
                            Me.Height += u.Height + 6
                            Dim send = ""
                            If If(SENDUNG.tblSnd_Colli, "") <> "" Then send &= SENDUNG.tblSnd_Colli & " Pk. "
                            If If(SENDUNG.tblSnd_Warenbezeichnung, "") <> "" Then send &= SENDUNG.tblSnd_Warenbezeichnung
                            If If(SENDUNG.tblSnd_Gewicht, "") <> "" Then send &= ", " & SENDUNG.tblSnd_Gewicht & " kg"

                            u.RichTextBox.Text = send
                      
                            Dim NCTS_Verzollungsadresse = SENDUNG.tblSnd_NCTSVerzollungsadresse

                            NCTS_Verzollungsadresse = If(SENDUNG.tblSnd_NCTSVerzollungsadresse, "")
                            NCTS_Verzollungsadresse &= vbNewLine & If(SENDUNG.NCTS_Verzollungsadresse_Strasse, "")
                            NCTS_Verzollungsadresse &= vbNewLine & If(SENDUNG.NCTS_Verzollungsadresse_Land, "") & " " & If(SENDUNG.NCTS_Verzollungsadresse_PLZ, "") & " " & If(SENDUNG.NCTS_Verzollungsadresse_Ort, "")
                            NCTS_Verzollungsadresse &= vbNewLine
                            NCTS_Verzollungsadresse = NCTS_Verzollungsadresse.Trim
                            
                            u.RichTextBox2.Text = NCTS_Verzollungsadresse
                            u.RichTextBox3.Text = SENDUNG.NCTS_Bestimmungszollstelle

                    End Select
                    '  End Select
                End If
            Next

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

        If MyFlowLayoutPanel1.Controls.Count = 0 Then addElement()
        Me.Cursor = Cursors.Default
    End Sub

   
End Class
