Public Class frmNacherfassungEndEmpfaenger
    Public ENDEMPFAENGER As List(Of VERAG_PROG_ALLGEMEIN.cEndEmpfaener)
    Public FilialenNr As Integer = -1
    Public AbfertigungsNr As Integer = -1
    Public UnterNr As Integer = -1



    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        Try
            ENDEMPFAENGER.Clear()
            For Each r As DataGridViewRow In dgvEndEmpfaenger.Rows
                'If Not IsNumeric(r.Cells("LeistungsNr").Value) Then MsgBox("Leistungsnummer muss eine Zahl sein!") : Me.DialogResult = Windows.Forms.DialogResult.None : Exit Sub
                'If Not IsNumeric(r.Cells("Preis").Value) Then MsgBox("Preis muss eine Zahl sein!") : Me.DialogResult = Windows.Forms.DialogResult.None : Exit Sub
                ENDEMPFAENGER.Add(New VERAG_PROG_ALLGEMEIN.cEndEmpfaener(FilialenNr, AbfertigungsNr, UnterNr, CInt(r.Cells("clmnKdNr").Value), CStr(r.Cells("clmnEndEmpfaenger").Value), r.Cells("clmnUIDLand").Value, r.Cells("clmnUID").Value, r.Cells("clmnRechnungsbetrag").Value, r.Cells("clmnRechnungsNr").Value))
            Next
            Me.Close()

        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
            Me.DialogResult = Windows.Forms.DialogResult.None
        End Try
    End Sub

    Private Sub frmNacherfassungVorkosten_Load(sender As Object, e As EventArgs) Handles Me.Load
        For Each v In ENDEMPFAENGER
            dgvEndEmpfaenger.Rows.Add(v.EndEmpfaengerKdNr, v.EndEmpfaenger, If(v.UstIdKz, ""), If(v.UstIdNr, ""), v.Rechnungsbetrag, v.RechnungsNr)
        Next


        cboUSt_UstIdNrLand.fillWithSQL(" SELECT [Länderverzeichnis für die Außenhandelsstatistik].LandKz,[Länderverzeichnis für die Außenhandelsstatistik].LandBez  " &
                                      " FROM [Länderverzeichnis für die Außenhandelsstatistik] LEFT JOIN [Währungstabelle] ON [Länderverzeichnis für die Außenhandelsstatistik].LandNr = [Währungstabelle].[Währungsschlüssel] " &
                                      " WHERE [Währungstabelle].[MitgliedslandEU] = 1 " &
                                      " ORDER BY [Länderverzeichnis für die Außenhandelsstatistik].LandKz ", , "FMZOLL", True)
        kdEndEmpfaenger.initKdBox(Me, txtKdNr)
        kdEndEmpfaenger.Focus()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        lblErrKD.Visible = False
        lblErrUID.Visible = False
        'lblErrRG.Visible = False

        kdEndEmpfaenger.Focus() 'Rg_value
        If kdEndEmpfaenger.KdNr_value <= 0 Then lblErrKD.Visible = True : Exit Sub
        If cboUSt_UstIdNr.Text = "" Then lblErrUID.Visible = True : Exit Sub
        If cboUSt_UstIdNrLand._value = "" Then lblErrUID.Visible = True : Exit Sub

        Dim rg = Nothing
        If IsNumeric(txtRechnugnsbetrag._value) AndAlso txtRechnugnsbetrag._value > 0 Then
            rg = CDbl(txtRechnugnsbetrag._value)
        End If

        If UL_EDIT < 0 Then
            dgvEndEmpfaenger.Rows.Add(kdEndEmpfaenger.KdNr_value, kdEndEmpfaenger.Text, cboUSt_UstIdNrLand._value, cboUSt_UstIdNr.Text, rg, txtRechnugnsNr.Text)
        Else
            dgvEndEmpfaenger.Rows.RemoveAt(UL_EDIT)
            dgvEndEmpfaenger.Rows.Insert(UL_EDIT, kdEndEmpfaenger.KdNr_value, kdEndEmpfaenger.Text, cboUSt_UstIdNrLand._value, cboUSt_UstIdNr.Text, rg, txtRechnugnsNr.Text)
            UL_EDIT = -1
            Button2.BackgroundImage = My.Resources.plus
            lklBearbeitungAbbrechenUl.Visible = False
        End If

        kdEndEmpfaenger.KdNr = ""
        cboUSt_UstIdNr.Text = ""
        cboUSt_UstIdNrLand.changeItem("")
        txtRechnugnsbetrag.Text = ""
        txtRechnugnsNr.Text = ""

        kdEndEmpfaenger.Focus()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If dgvEndEmpfaenger.SelectedRows.Count > 0 Then
            dgvEndEmpfaenger.Rows.Remove(dgvEndEmpfaenger.SelectedRows(0))
        End If
    End Sub


    Private Sub txtRechnugnsbetrag_KeyDown(sender As Object, e As KeyEventArgs) Handles txtRechnugnsbetrag.KeyDown, txtRechnugnsNr.KeyDown, cboUSt_UstIdNr.KeyDown
        If e.KeyCode = Keys.Return Then
            Button2.PerformClick()
        End If
    End Sub

    Dim UL_EDIT = -1
    Private Sub dgvUnterlage_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEndEmpfaenger.CellDoubleClick
        If e.RowIndex < 0 Then Exit Sub
        editableRowUL(e.RowIndex)
    End Sub

    Sub editableRowUL(index As Integer)
        kdEndEmpfaenger.KdNr = dgvEndEmpfaenger.Rows(index).Cells("clmnKdNr").Value
        cboUSt_UstIdNr.Text = dgvEndEmpfaenger.Rows(index).Cells("clmnUID").Value
        txtRechnugnsbetrag.Text = dgvEndEmpfaenger.Rows(index).Cells("clmnRechnungsbetrag").Value
        txtRechnugnsNr.Text = dgvEndEmpfaenger.Rows(index).Cells("clmnRechnungsNr").Value
        cboUSt_UstIdNrLand.changeItem(dgvEndEmpfaenger.Rows(index).Cells("clmnUIDLand").Value)

        UL_EDIT = index
        Button2.BackgroundImage = My.Resources.stift
        lklBearbeitungAbbrechenUl.Visible = True
    End Sub

    Private Sub lklBearbeitungAbbrechenUl_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles lklBearbeitungAbbrechenUl.LinkClicked
        UL_EDIT = -1
        kdEndEmpfaenger.KdNr = ""
        cboUSt_UstIdNr.Text = ""
        cboUSt_UstIdNrLand.changeItem("")
        txtRechnugnsbetrag.Text = ""
        txtRechnugnsNr.Text = ""
        Button1.BackgroundImage = My.Resources.plus
        lklBearbeitungAbbrechenUl.Visible = False
        kdEndEmpfaenger.Focus()
    End Sub

    Private Sub frmNacherfassungEndEmpfaenger_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        kdEndEmpfaenger.Focus()
    End Sub

    Private Sub kdEndEmpfaenger_TextChanged(sender As Object, e As EventArgs) Handles kdEndEmpfaenger.PropertyChanged
        If kdEndEmpfaenger.KdData_ADRESSEN IsNot Nothing Then
            cboUSt_UstIdNrLand.changeItem(If(kdEndEmpfaenger.KdData_ADRESSEN.UstIdKz, ""))
            cboUSt_UstIdNr.Text = If(kdEndEmpfaenger.KdData_ADRESSEN.UstIdNr, "")
        End If
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        dgvEndEmpfaenger.ClearSelection()

        For Each r As DataGridViewRow In dgvEndEmpfaenger.Rows
            'If r.Cells("clmnUIDLand").Value <> "" AndAlso r.Cells("clmnUIDNr").Value <> "" Then
            If r.Cells("clmnKdNr").Value IsNot Nothing AndAlso r.Cells("clmnKdNr").Value.ToString <> "" Then
                r.Cells("clmnUIDLand").Style.BackColor = Color.LightGray
                r.Cells("clmnUIDLand").Style.ForeColor = Color.Black


                Dim UID = VERAG_PROG_ALLGEMEIN.cUIDPruefung.doUIDPruefung(r.Cells("clmnKdNr").Value, True)
                If UID IsNot Nothing Then
                    If UID.uid_valid Then
                        r.Cells("clmnUIDLand").Style.BackColor = Color.Green
                        r.Cells("clmnUIDLand").Style.ForeColor = Color.White
                    Else
                        r.Cells("clmnUIDLand").Style.BackColor = Color.DarkRed
                        r.Cells("clmnUIDLand").Style.ForeColor = Color.White
                    End If
                End If
                r.Cells("clmnUID").Style.BackColor = r.Cells("clmnUIDLand").Style.BackColor
                r.Cells("clmnUID").Style.ForeColor = r.Cells("clmnUIDLand").Style.ForeColor
            End If
        Next
        Me.DialogResult = DialogResult.None
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        dgvEndEmpfaenger.ClearSelection()

        For Each r As DataGridViewRow In dgvEndEmpfaenger.Rows
            'If r.Cells("clmnUIDLand").Value <> "" AndAlso r.Cells("clmnUIDNr").Value <> "" Then
            If r.Cells("clmnUIDLand").Value IsNot Nothing AndAlso r.Cells("clmnUIDLand").Value.ToString <> "" AndAlso r.Cells("clmnUID").Value IsNot Nothing AndAlso r.Cells("clmnUID").Value.ToString <> "" Then
                r.Cells("clmnUIDLand").Style.BackColor = Color.LightGray
                r.Cells("clmnUIDLand").Style.ForeColor = Color.Black

                Dim UID_ERG = SDL.cFinanzOnlineWebService.Call_Web_Service_MethodUID_Abfrage_NEU2020(If(r.Cells("clmnUIDLand").Value, "") & If(r.Cells("clmnUID").Value, ""), "", "", "")
                Select Case UID_ERG
                    Case 0
                        r.Cells("clmnUIDLand").Style.BackColor = Color.Green
                        r.Cells("clmnUIDLand").Style.ForeColor = Color.White
                    Case 1
                        r.Cells("clmnUIDLand").Style.BackColor = Color.DarkRed
                        r.Cells("clmnUIDLand").Style.ForeColor = Color.White
                End Select
            End If
            r.Cells("clmnUID").Style.BackColor = r.Cells("clmnUIDLand").Style.BackColor
            r.Cells("clmnUID").Style.ForeColor = r.Cells("clmnUIDLand").Style.ForeColor
        Next
        Me.DialogResult = DialogResult.None
    End Sub
End Class