Public Class usrCntlATLAS_EZA_Positionen

    Dim EZA As DAKOSY_Worker.cDakosyEZA = Nothing
    Dim USRCNTL_EZA As usrCntlATLAS_EZA = Nothing

    Dim POS As New List(Of usrCntlATLAS_EZA_Position)
    Dim sectionChangeBLOCK = False
    Dim gesRohmasse = 0

    Event RohmasseChanged(gesRohmasse As Double)
    Event PosAnzChanged(anzahlPos As Double)


    Sub New(EZA As DAKOSY_Worker.cDakosyEZA, USRCNTL_EZA As usrCntlATLAS_EZA)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.EZA = EZA
        Me.USRCNTL_EZA = USRCNTL_EZA
    End Sub
    Private Sub usrCntlATLAS_ncts_Load(sender As Object, e As EventArgs) Handles Me.Load

        addItem()
        'setValues(NCTS.ncts_WARENPOS)
    End Sub

    Public Sub setValues(ncts_POS As DAKOSY_Worker.cDakosyEZA, Optional blockEvents As Boolean = False)
        If ncts_POS Is Nothing Then Exit Sub
        POS.Clear()

        For Each p In ncts_POS.eza_WARENPOS
            addItem(p, blockEvents) 'MyPanel2.Controls.Add()
        Next
        MyPanel2.Controls.Clear()
        If Not blockEvents Then MyListBox1_SelectedIndexChanged(dgvPositionen, New EventArgs)
    End Sub



    Sub initListBox(Optional selectedIndex = -1)
        If dgvPositionen.ColumnCount <= 0 Then Exit Sub
        If selectedIndex >= 0 Then sectionChangeBLOCK = True
        Dim cnt As Integer = 1
        dgvPositionen.Rows.Clear()
        For Each p In POS
            p.lblPosNr.Text = cnt
            dgvPositionen.Rows.Add(POS.IndexOf(p), cnt, p.txtWarenbezeichnung.Text)
            cnt += 1
        Next
        If selectedIndex >= 0 Then
            '   MyListBox1.SetSelected(selectedIndex, True)
            '  sectionChangeBLOCK = False
        End If
    End Sub

    Private Sub dgvPositionen_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvPositionen.KeyDown
        If e.KeyCode = Keys.Delete Then
            removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
        End If
    End Sub


    Private Sub MyListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles dgvPositionen.SelectionChanged
        Try

            If sectionChangeBLOCK Then Exit Sub
            MyPanel2.Controls.Clear()
            If dgvPositionen.SelectedRows.Count > 0 Then
                MyPanel2.Controls.Add(POS(dgvPositionen.SelectedRows(0).Cells("index").Value))
                POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
                '   Try : DirectCast(MyPanel2.Controls(0), usrCntlATLAS_EZA_Position).txtWarenbezeichnung.Focus() : Catch : End Try
                ' initListBox(MyListBox1.SelectedIndex())
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub usrCntlATLAS_EZA_Position_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.Control = True Then
            If e.KeyCode = Keys.Oemplus Then addItem()
            If e.KeyCode = Keys.OemMinus Then
                If dgvPositionen.SelectedRows.Count > 0 Then
                    removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
                End If
            End If

        End If
    End Sub



    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Try
            If keyData = (Keys.Control Or Keys.Add) Then
                'RaiseEvent a() : Return True
                addItem()
                Return True
            ElseIf keyData = (Keys.Control Or Keys.Subtract) Then
                'RaiseEvent REMOVE(ID) : Return True
                removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
                Return True
            ElseIf keyData = Keys.PageUp Then
                'RaiseEvent PREV_POS() : Return True
                PREV_POS()
                Return True
            ElseIf keyData = Keys.PageDown Then
                'RaiseEvent NEXT_POS() : Return True
                NEXT_POS()
                Return True
            End If
        Catch ex As Exception
        End Try
        Return False
    End Function


    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        addItem()
    End Sub


    Function addItem(Optional POSTMP As DAKOSY_Worker.cDakosy_EZA_Warenposition = Nothing, Optional blockEvents As Boolean = False) As usrCntlATLAS_EZA_Position
        Dim u = New usrCntlATLAS_EZA_Position(POS.Count, POSTMP, USRCNTL_EZA)


        AddHandler u.RohmasseChanged, Sub(r)
                                          gesRohmasse = 0
                                          For Each p In POS
                                              gesRohmasse += If(IsNumeric(p.txtRohmasse.Text), p.txtRohmasse.Text, 0)
                                          Next
                                          If Not blockEvents Then RaiseEvent RohmasseChanged(gesRohmasse)
                                      End Sub
        u.Dock = DockStyle.Fill
        '  AddHandler u.ADD, AddressOf addItem
        ' AddHandler u.REMOVE, AddressOf removeItem
        ' AddHandler u.NEXT_POS, AddressOf NEXT_POS
        ' AddHandler u.PREV_POS, AddressOf PREV_POS
        POS.Add(u)
        u.initSrch(blockEvents)
        initListBox()
        If Not blockEvents Then
            dgvPositionen.ClearSelection()
            If dgvPositionen.RowCount > 0 Then dgvPositionen.Rows(dgvPositionen.RowCount - 1).Selected = True
            RaiseEvent PosAnzChanged(POS.Count)
        End If
        Return u
    End Function

    Sub removeItem(index)
        Try
            If index < 0 Then Exit Sub
            'If index < 0 Then Exit Sub
            If dgvPositionen.Rows.Count > index Then
                If vbYes = MsgBox("Möchten Sie die Position wirklich löschen?", vbYesNoCancel) Then
                    dgvPositionen.ClearSelection()
                    POS.RemoveAt(index)
                    initListBox()
                    If dgvPositionen.Rows.Count > index Then
                        dgvPositionen.Rows(index).Selected = True 'SetSelected(index, True)
                    ElseIf dgvPositionen.Rows.Count > 0 Then
                        dgvPositionen.Rows(0).Selected = True
                    End If
                End If
            End If
            RaiseEvent PosAnzChanged(POS.Count)

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

    Sub NEXT_POS()
        If dgvPositionen.SelectedRows.Count > 0 Then
            If dgvPositionen.RowCount > dgvPositionen.SelectedRows(0).Index + 1 Then
                Dim i = dgvPositionen.SelectedRows(0).Index
                dgvPositionen.ClearSelection()
                dgvPositionen.Rows(i + 1).Selected = True
                '  POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
            End If
        End If
    End Sub
    Sub PREV_POS()
        If dgvPositionen.SelectedRows.Count > 0 Then
            If dgvPositionen.SelectedRows(0).Index > 0 Then
                Dim i = dgvPositionen.SelectedRows(0).Index
                dgvPositionen.ClearSelection()
                dgvPositionen.Rows(i - 1).Selected = True
                ' POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If dgvPositionen.SelectedRows.Count > 0 Then
            removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
        End If
    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
        refreshDGV()
    End Sub


    Public Sub refreshDGV()
        With dgvPositionen
            If dgvPositionen.ColumnCount = 0 Then Exit Sub
            For Each r As DataGridViewRow In .Rows
                Dim index = r.Cells("index").Value
                'r.Cells("ID").Value = POS(index)..Text
                If POS.Count > index Then
                    r.Cells("Ware").Value = POS(index).txtWarenbezeichnung.Text
                End If
            Next
        End With
    End Sub


    Public Sub getValues(ByRef EZA As DAKOSY_Worker.cDakosyEZA)
        '   Dim POSITIONEN As List(Of DAKOSY_Worker.cDakosy_NCTSOut_Warenposition)
        EZA.eza_WARENPOS.Clear()

        Dim cnt As Integer = 1
        For Each P In POS
            Dim POSITION As New DAKOSY_Worker.cDakosy_EZA_Warenposition

            POSITION.ezaWP_PositionsNummer = cnt
            POSITION.ezaWP_WarennummerEZT = cProgramFunctions.isLeerNothing(P.txtWarennummer.Text)
            POSITION.ezaWP_WarennummerZusatzCode1 = cProgramFunctions.isLeerNothing(P.txtWTZ1.Text)
            POSITION.ezaWP_WarennummerZusatzCode2 = cProgramFunctions.isLeerNothing(P.txtWTZ2.Text)
            POSITION.ezaWP_Warenbezeichnung = cProgramFunctions.isLeerNothing(P.txtWarenbezeichnung.Text)
            POSITION.ezaWP_Artikelpreis = cProgramFunctions.isLeerNothingDbl(P.txtWarenwert._value)
            POSITION.ezaWP_ArtikelpreisWaehrung = cProgramFunctions.isLeerNothing(P.txtArtikelpreisWaehrung._value)

            'POSITION.ezaWP_WarenwertWaehrung = P.XXXXXXXXXXXX.Text
            'POSITION.ezaWP_SchaetzKz = P.XXXXXXXXXXXX.Text
            'POSITION.ezaWP_WarenwertProzent = P.XXXXXXXXXXXX.Text
            'POSITION.ezaWP_VersendungslandPositionCode = P.XXXXXXXXXXXX.Text
            'POSITION.ezaWP_BestimmungslandPositionCode = P.XXXXXXXXXXXX.Text
            POSITION.ezaWP_Rohmasse = cProgramFunctions.isLeerNothingDbl(P.txtRohmasse.Text)
            POSITION.ezaWP_Eigenmasse = cProgramFunctions.isLeerNothingDbl(P.txtEigenmasse.Text)

            If IsNumeric(P.txtAHStatMenge.Text) Then
                POSITION.ezaWP_AHStatMenge = CInt(P.txtAHStatMenge.Text) 'keine Kommast.
            Else
                POSITION.ezaWP_AHStatMenge = Nothing
            End If

            If P.sbAHStatMasseinheit IsNot Nothing AndAlso P.sbAHStatMasseinheit._value IsNot Nothing AndAlso P.sbAHStatMasseinheit._value.Length > 3 Then
                POSITION.ezaWP_AHStatMengeMasseinheit = cProgramFunctions.isLeerNothing(P.sbAHStatMasseinheit._value.Substring(0, 3))
                POSITION.ezaWP_AHStatMengeQualifier = cProgramFunctions.isLeerNothing(P.sbAHStatMasseinheit._value.Substring(3))
            Else
                POSITION.ezaWP_AHStatMengeMasseinheit = cProgramFunctions.isLeerNothing(P.sbAHStatMasseinheit._value)
                POSITION.ezaWP_AHStatMengeQualifier = Nothing
            End If

            POSITION.ezaWP_PositionsZusatz = cProgramFunctions.isLeerNothing(P.txtPositionszusatz.Text)

            POSITION.ezaWP_PackstueckAnzahl = cProgramFunctions.isLeerNothing(P.txtPkAnzahl.Text)
            POSITION.ezaWP_PackstueckZeichen = cProgramFunctions.isLeerNothing(P.txtPkMarke.Text)
            POSITION.ezaWP_PackstueckArt = cProgramFunctions.isLeerNothing(P.sbPkTyp._value)
            
            ' If P.cboVorangegangenesVerfahren._value <> "" Then
            'POSITION.ezaWP_VerfahrensCodeVorangegangenesVerfahren = P.cboVorangegangenesVerfahren._value.Substring(2, 2)
            ''  Else
            '  POSITION.ezaWP_VerfahrensCodeVorangegangenesVerfahren = Nothing
            ' End If
            POSITION.ezaWP_AnmeldeVErfahren = cProgramFunctions.isLeerNothing(P.cboAngVerfahren._value)
            POSITION.ezaWP_VerfahrensCodeVorangegangenesVerfahren = cProgramFunctions.isLeerNothing(P.cboVorangegangenesVerfahren._value)

            POSITION.ezaWP_BeguenstigungBenatragtCode = cProgramFunctions.isLeerNothing(P.cboBeguenstigung._value)
            POSITION.ezaWP_UrsprungslandCode = cProgramFunctions.isLeerNothing(P.sbUrsprungsland._value)

            POSITION.ezaWP_UNTERLAGEN = New List(Of DAKOSY_Worker.cDakosy_EZA_WarenpositionVorgelegteUnterlagen)
            For Each r As DataGridViewRow In P.dgvUnterlage.Rows
                Dim Ul As New DAKOSY_Worker.cDakosy_EZA_WarenpositionVorgelegteUnterlagen
                Dim split = r.Cells("UlCode").Value.ToString.Split("/")

                If split.Length > 1 Then
                    Ul.ezaWpUl_Bereich = split(0)
                    Ul.ezaWpUl_Art = split(1)

                End If
                Ul.ezaWpUl_Nummer = r.Cells("UlNummer").Value
                Ul.ezaWpUl_DatumAusstellung = r.Cells("UlADat").Value
                Ul.ezaWpUl_VorlageKz = r.Cells("UlVorhanden").Value

                '...?
                POSITION.ezaWP_UNTERLAGEN.Add(Ul)
            Next


            If cProgramFunctions.isLeerNothingDbl(P.txtWarenwert._value, 0) > 0 Then
                'POSITION.kostenpositionen --> Nettopreis '''''AbzugHinzurechnung
                POSITION.ezaWP_Zollwert = CDbl(P.txtWarenwert._value)
                POSITION.ezaWP_AHStatWert = CDbl(P.txtWarenwert._value)
                'POSITION.abzughinzurechnung
            End If


            EZA.eza_WARENPOS.Add(POSITION)
            cnt += 1
        Next
        '    NCTS.ncts_WARENPOS
    End Sub


    Private Sub MyPanel2_ControlAdded(sender As Object, e As ControlEventArgs) Handles MyPanel2.ControlAdded
        Try : DirectCast(e.Control, usrCntlATLAS_EZA_Position).txtWarenbezeichnung.Focus() : Catch : End Try
    End Sub
End Class
