Imports System.Web.UI.WebControls.Expressions
Imports System.Windows
Imports System.Windows.Interop
Imports com.sun.org.apache.xpath.internal.functions
Imports DAKOSY_Worker.TR100
Imports DocumentFormat.OpenXml.Drawing
Imports DocumentFormat.OpenXml.Drawing.Charts
'Imports DocumentFormat.OpenXml.Wordprocessing
Imports GrapeCity.ActiveReports
Imports GrapeCity.ActiveReports.Design.DdrDesigner.Services
Imports GrapeCity.ActiveReports.Design.QueryDesignerImpl.SqlParser.Irony.Parsing
Imports javax.validation.constraints
Imports sun.misc
Imports TAlex.WPF.Helpers
Imports SDL.EORI
Imports System.Drawing
Imports System.Runtime.DesignerServices

Public Class frmKundenZOLLVollmachten
    Public kdnr As Integer = -1
    Public filePath As String
    Public erhalten As Boolean = False
    Public archiviert As Boolean = False
    Public geloescht As Boolean = False
    Dim id As Integer = -1
    'Dim cDATENSERVER As New VERAG_PROG_ALLGEMEIN.cDATENSERVER
    Dim ksql As New kundenSQL
    Dim vollmachtartId As Integer
    Dim emailId As Integer
    Dim vermerk As String = ""
    Dim vmId As Integer = -1
    Dim currentVM As New VERAG_PROG_ALLGEMEIN.cKundenVollmachten

    Sub New(kdnr, vollmachtartId)
        Me.InitializeComponent()
        Me.kdnr = kdnr
        Me.vollmachtartId = vollmachtartId
    End Sub

    Sub New(kdnr, vmaNr, filePath)
        Me.InitializeComponent()
        Me.erhalten = True
        Me.kdnr = kdnr
        Me.filePath = filePath
        Me.vollmachtartId = vollmachtartId
        'UI-Elements
        MyComboBox1.Visible = True
        Me.txtGueltigVon.Text = Now.ToShortDateString
        Me.txtGueltigBis.Text = Nothing

    End Sub

    Private Sub frmKundenZOLLVollmachten_Load(sender As Object, e As EventArgs) Handles Me.Load
        MyComboBox1.fillWithSQL("SELECT [kdvma_Id],[kdvma_Bezeichnung] + ' - ' + kdvma_Firma FROM [tblKundenVollmachtenArt]", False, "FMZOLL", False)
        Me.MyComboBox1._value = vollmachtartId
        initDGVVollmachtenEmbedded(False)

        Dim FirmaTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("OFFERTE_FREMD_VERAGIMEX", "SDL") Or VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("FAKTURIERUNG_VERAGIMEX", "SDL") Then
            FirmaTmp = "VERAGIMEX"
        End If

        Select Case FirmaTmp
            Case "UNISPED", "IMEX", "VERAGIMEX", "FRONTOFFICE"
                Panel1.Visible = True
            Case Else
                Panel1.Visible = True
                'Me.Height = Me.Height - Panel1.Height
        End Select


    End Sub




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

    End Sub

    Sub saveVM(VM As VERAG_PROG_ALLGEMEIN.cKundenVollmachten)

        lblWarning.Visible = False
        If MyComboBox1._value = "" Then Me.DialogResult = Windows.Forms.DialogResult.None : Exit Sub
        VM.kdvm_vermerk = txtVermerk.Text
        VM.kdvm_kdvmaId = vollmachtartId
        VM.kdvm_KundenNr = kdnr
        VM.kdvm_archiviert = cbxArchiviert.Checked

        If txtGueltigVon.Text = "" Or Not IsDate(txtGueltigVon.Text) Then
            txtGueltigVon.ForeColor = Color.Red
            lblWarning.Visible = True
            Me.DialogResult = Windows.Forms.DialogResult.None
            Exit Sub
        Else
            VM.kdvm_erhalten_Datum = txtGueltigVon.Text
            VM.kdvm_gueltig_ab = txtGueltigVon.Text
            VM.kdvm_erhalten = True
        End If

        If txtGueltigBis.Text IsNot "" And Not IsDate(txtGueltigBis.Text) Then
            txtGueltigBis.ForeColor = Color.Red
            Me.DialogResult = Windows.Forms.DialogResult.None
            Exit Sub

        ElseIf txtGueltigBis.Text = "" Then
            VM.kdvm_gueltig_bis = Nothing
        Else
            VM.kdvm_gueltig_bis = txtGueltigBis.Text
        End If

        vollmachtenArchivieren()

        If Not VM.SAVE() Then
            Me.DialogResult = Windows.Forms.DialogResult.None
        End If

        If dgvVollmachtenEmbedded.Enabled = False Then
            dgvVollmachtenEmbedded.Enabled = True
        End If


        dgvVollmachtenEmbedded.GetOrder()
        initDGVVollmachtenEmbedded(True)
        dgvVollmachtenEmbedded.SetOrder()

    End Sub

    Private Sub lblNoPDFavailable_Click(sender As Object, e As EventArgs) Handles lblPDFavailable.Click
        If currentVM.kdvm_datenarchivId > 0 Then
            ' cDATENSERVER.openDataTMPByDatenarchivId(id)
            Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(currentVM.kdvm_datenarchivId)
            DS.OPEN_SINGLE()
        End If
    End Sub

    'Private Sub cbxErhaltenAm_CheckedChanged(sender As Object, e As EventArgs) Handles cbxErhaltenAm.CheckedChanged
    '    ' MyTextBox1.Enabled = sender.checked
    'End Sub
    'Private Sub cbxErhaltenAm_CheckedChanged(sender As Object, e As EventArgs) Handles cbxErhaltenAm.Click
    '    'wird nicht mehr verwendet
    '    If Not sender.checked Then
    '        txtGueltigVon.Text = ""
    '    Else
    '        txtGueltigVon.Text = Now.ToShortDateString
    '    End If
    'End Sub

    Private Sub MyTextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtGueltigVon.TextChanged
        txtGueltigVon.ForeColor = Color.Black
        cbxErhaltenAm.Checked = (txtGueltigVon.Text <> "")
    End Sub

    Private Sub MyTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles txtGueltigVon.KeyDown
        If e.KeyCode = Keys.Return Then
            btnOK.PerformClick()
        End If
        If e.KeyCode = Keys.Escape Then
            Button1.PerformClick()
        End If
    End Sub

    Private Sub MyComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox1.SelectedIndexChanged
        lbl.Text = MyComboBox1.Text
    End Sub

    Private Sub frmKundenZOLLVollmachten_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        txtGueltigVon.Focus()
        If txtGueltigVon.Text <> "" Then
            cbxErhaltenAm.Checked = True
            txtGueltigVon.SelectAll()
        End If

    End Sub


    Sub initDGVVollmachtenEmbedded(enalblePanel As Boolean)


        Dim FirmaTmp = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
        Dim where = " And art.kdvma_Firma='" & FirmaTmp & "'"

        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("OFFERTE_FREMD_VERAGIMEX", "SDL") Or VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("FAKTURIERUNG_VERAGIMEX", "SDL") Then
            FirmaTmp = "VERAGIMEX"
        End If

        Select Case FirmaTmp
            Case "VERAGIMEX"
                where = " AND  art.kdvma_Firma IN ('VERAG', 'IMEX') "
        End Select



        If dgvVollmachtenEmbedded.Enabled = False Then
            dgvVollmachtenEmbedded.Enabled = True
        End If

        pnlDetails.Enabled = enalblePanel

        With dgvVollmachtenEmbedded
            .Columns.Clear()


            .DataSource = ksql.loadDgvBySql("SELECT abf.kdvm_Id,art.kdvma_Id,  art.kdvma_Bezeichnung, abf.[kdvm_KundenNr], abf.[kdvm_erhalten],abf.[kdvm_erhalten_Datum], abf.kdvm_datenarchivId,abf.kdvm_Info, abf.[kdvm_archiviert], abf.[kdvm_gueltig_bis], abf.[kdvm_vermerk], abf.[kdvm_geloescht]" &
                                                    " FROM [tblKundenVollmachtenArt] as art " &
                                                    " INNER  JOIN (select * from [tblKundenVollmachten]) as abf on art.[kdvma_Id]=abf.[kdvm_kdvmaId]" &
                                                    " WHERE art.kdvma_visible=1 AND abf.kdvm_erhalten = 1 AND ISNULL(abf.[kdvm_geloescht],0) != 1 AND [kdvm_KundenNr]='" & kdnr & "' AND [kdvm_kdvmaId]='" & vollmachtartId & "'" & where &
                                                    " ORDER BY kdvm_archiviert asc, abf.kdvm_gueltig_bis asc, abf.kdvm_erhalten_Datum desc ", "FMZOLL")

            '  .RowTemplate.Height = 30
            .Columns("kdvm_Id").Visible = False
            .Columns("kdvma_Id").Visible = False
            .Columns("kdvm_KundenNr").Visible = False
            .Columns("kdvm_datenarchivId").Visible = False
            .Columns("kdvma_Bezeichnung").Visible = False
            .Columns("kdvma_Bezeichnung").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("kdvma_Bezeichnung").HeaderText = "Bezeichnung"
            .Columns("kdvm_erhalten").Width = 50
            .Columns("kdvm_erhalten").HeaderText = "Erhalten"
            .Columns("kdvm_erhalten").Visible = False
            .Columns("kdvm_erhalten").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("kdvm_erhalten_Datum").Width = 100
            .Columns("kdvm_erhalten_Datum").HeaderText = "Gültig von"
            .Columns("kdvm_erhalten_Datum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("kdvm_Info").Width = 200
            .Columns("kdvm_Info").HeaderText = "Info"
            .Columns("kdvm_Info").Visible = False
            .Columns("kdvm_gueltig_bis").Visible = True
            .Columns("kdvm_gueltig_bis").Width = 100
            .Columns("kdvm_gueltig_bis").HeaderText = "Gültig bis"
            .Columns("kdvm_gueltig_bis").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("kdvm_archiviert").Visible = True
            .Columns("kdvm_archiviert").Width = 60
            .Columns("kdvm_archiviert").HeaderText = "Archiviert"
            .Columns("kdvm_archiviert").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            .Columns("kdvm_vermerk").MinimumWidth = 100
            .Columns("kdvm_vermerk").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("kdvm_vermerk").HeaderText = "Vermerk"
            .Columns("kdvm_vermerk").Visible = True
            .Columns("kdvm_geloescht").Visible = False


            If True Then

                Dim c As New DataGridViewImageColumn
                c.Name = "clmnPDF" : c.HeaderText = "PDF"
                c.Image = My.Resources.pdf_gray : c.ImageLayout = DataGridViewImageCellLayout.Zoom
                c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
                c.DefaultCellStyle.Tag = "PDF anzeigen"
                .Columns.Add(c)
            End If


            Select Case VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA
                Case "UNISPED", "IMEX"
                    '.Columns("clmnTherefore").Visible = False
                    .Columns("clmnPDF").Visible = True
                Case "FRONTOFFICE"
                    '.Columns("clmnTherefore").Visible = True
                    .Columns("clmnPDF").Visible = True
                Case Else
                    '.Columns("clmnTherefore").Visible = True
                    .Columns("clmnPDF").Visible = True
            End Select
            For Each r As DataGridViewRow In .Rows
                If r.Cells("kdvm_erhalten").Value Is DBNull.Value Then
                    r.DefaultCellStyle.ForeColor = Color.Gray
                ElseIf CBool(r.Cells("kdvm_erhalten").Value) = False Then
                    r.DefaultCellStyle.ForeColor = Color.Gray
                ElseIf IsDate(r.Cells("kdvm_erhalten_Datum").Value) AndAlso CDate(r.Cells("kdvm_erhalten_Datum").Value) < Now.AddYears(-2) Then
                    r.DefaultCellStyle.ForeColor = Color.Red
                End If

                If True Then
                    If r.Cells("kdvm_datenarchivId").Value IsNot DBNull.Value AndAlso CInt(r.Cells("kdvm_datenarchivId").Value) > 0 Then
                        DirectCast(r.Cells("clmnPDF"), DataGridViewImageCell).Value = My.Resources.pdf
                    End If
                End If
            Next
        End With

    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles cbxVMarchivieren.CheckedChanged


        'If vbYes = MsgBox("Sollen die bestehenden Vollmachten archiviert werden?", vbYesNoCancel) Then
        '    Dim succeeded = ksql.setVollmachtarchivieren(kdnr, MyComboBox1._value)
        '    If succeeded = True Then
        '        MsgBox("Bestehende Vollmachten wurden archiviert.")
        '        cbxVMarchivieren.Enabled = False
        '    End If
        'End If


    End Sub

    Private Sub dgvvollmachtenembedded_cellmouseclick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvVollmachtenEmbedded.CellMouseClick
        'für die Ermittlung des Contextmenüs -> VM löschen
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If dgvVollmachtenEmbedded.SelectedRows.Count > 0 Then
                ContextMenuStrip1.Show(Cursor.Position)
            End If
        End If

    End Sub

    Private Sub ToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ToolStripMenuItem1.Click
        'Contextmenü -> Datensatz ändern -> wird nicht verwendet
        If dgvVollmachtenEmbedded.SelectedRows.Count = 1 Then

            Dim VM As New VERAG_PROG_ALLGEMEIN.cKundenVollmachten(dgvVollmachtenEmbedded.SelectedRows(0).Cells("kdvm_Id").Value)
            loadChangedVMToPanel(True, VM)
            setSaveButton()

        End If
    End Sub

    Private Sub loadChangedVMToPanel(enablePanel As Boolean, VM As VERAG_PROG_ALLGEMEIN.cKundenVollmachten)
        pnlDetails.Enabled = enablePanel

        cbxArchiviert.Checked = VM.kdvm_archiviert
        txtVermerk.Text = VM.kdvm_vermerk
        txtGueltigVon.Text = VM.kdvm_erhalten_Datum
        txtGueltigBis.Text = VM.kdvm_gueltig_bis

        checkEmailIsPresent()
        checkPDFisPresent()
        setOKButton()

        'Listener für Änderungen im Detail-Panel
        For Each c As Control In pnlDetails.Controls
            If TypeOf (c) Is CheckBox Then
                AddHandler CType(c, CheckBox).CheckedChanged, AddressOf SomethingChanged
            ElseIf TypeOf (c) Is TextBox Then
                AddHandler CType(c, TextBox).TextChanged, AddressOf SomethingChanged
            End If
        Next
    End Sub


    Private Sub SomethingChanged(sender As Object, e As EventArgs)
        setSaveButton()
    End Sub

    Sub checkPDFisPresent()

        If currentVM.kdvm_datenarchivId > 0 Then
            picDeletePDF.Visible = True
            picPDF.Visible = True
            picPDF.Image = My.Resources.pdf
            picPDF.Cursor = Cursors.Default
        Else
            picDeletePDF.Visible = False
            picPDF.Visible = True
            picPDF.Image = My.Resources.pdf_gray
            picPDF.Cursor = Cursors.Hand

        End If
    End Sub

    Sub checkEmailIsPresent()
        If currentVM.kdvm_emailId > 0 Then
            picDeleteEmail.Visible = True
            picEmail.Visible = True
            picEmail.Image = My.Resources.email_big1
            picEmail.Cursor = Cursors.Default
        Else
            picDeleteEmail.Visible = False
            picEmail.Visible = True
            picEmail.Image = My.Resources.email_big_gray
            picEmail.Cursor = Cursors.Hand
        End If
    End Sub


    Private Sub dgvVollmachtenEmbedded_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvVollmachtenEmbedded.CellDoubleClick

        If dgvVollmachtenEmbedded.Columns(e.ColumnIndex).Name = "clmnPDF" Then

            If dgvVollmachtenEmbedded.Rows(e.RowIndex).Cells("kdvm_datenarchivId").Value IsNot DBNull.Value AndAlso CInt(dgvVollmachtenEmbedded.Rows(e.RowIndex).Cells("kdvm_datenarchivId").Value) > 0 Then

                Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(dgvVollmachtenEmbedded.Rows(e.RowIndex).Cells("kdvm_datenarchivId").Value)
                DS.OPEN_SINGLE(True)
            End If
        End If
    End Sub

    Private Sub LöschenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LöschenToolStripMenuItem.Click
        'Contextmenü -> Löschen der VM
        If dgvVollmachtenEmbedded.SelectedRows.Count = 1 Then

            If vbYes = MsgBox("Soll diese Vollmacht gelöscht werden?", vbYesNoCancel) Then
                Dim vollmacht = New VERAG_PROG_ALLGEMEIN.cKundenVollmachten(dgvVollmachtenEmbedded.SelectedRows(0).Cells("kdvm_Id").Value)
                vollmacht.kdvm_geloescht = True

                If vollmacht.SAVE = True Then
                    MsgBox("Bestehende Vollmacht wurde gelöscht.")
                    initDGVVollmachtenEmbedded(False)

                End If
            End If

        End If

    End Sub
    Private Sub btnNeu_Click(sender As Object, e As EventArgs) Handles btnNeu.Click
        'Neuanlage einer VM
        dgvVollmachtenEmbedded.ClearSelection()

        If dgvVollmachtenEmbedded.Rows.Count > 0 Then
            cbxVMarchivieren.Visible = True
            cbxVMarchivieren.Checked = True
        End If

        dgvVollmachtenEmbedded.Enabled = False

        currentVM = New VERAG_PROG_ALLGEMEIN.cKundenVollmachten With {
            .kdvm_KundenNr = kdnr,
            .kdvm_kdvmaId = vollmachtartId,
            .kdvm_Info = "",
            .kdvm_erhalten = False,
            .kdvm_archiviert = False,
            .kdvm_datenarchivId = -1,
            .kdvm_erhalten_Datum = Now.ToShortDateString,
            .kdvm_emailId = -1
        }

        loadChangedVMToPanel(True, currentVM)
        hideAttachementControl()
        setSaveButton()

    End Sub

    Private Sub vollmachtenArchivieren()

        If cbxVMarchivieren.Checked = False Then Exit Sub
        Dim count As Integer = 0
        With dgvVollmachtenEmbedded

            For Each r As DataGridViewRow In .Rows
                Dim VMindex = r.Cells("kdvm_Id").Value
                If r.Cells("kdvm_archiviert").Value <> True Then
                    count += 1
                    Dim currentVollmacht = New VERAG_PROG_ALLGEMEIN.cKundenVollmachten(VMindex)
                    currentVollmacht.kdvm_archiviert = True
                    currentVollmacht.SAVE()
                End If
            Next

            If count > 0 Then
                MsgBox("Bestehende Vollmachten wurden archiviert.")
            End If
        End With

        cbxVMarchivieren.Visible = False
        cbxVMarchivieren.Checked = False


    End Sub

    Private Sub setSaveButton()

        btnOK.Visible = False
        btnSave.Visible = True

    End Sub

    Private Sub dgvVollmachtenEmbedded_SelectionChanged(sender As Object, e As EventArgs) Handles dgvVollmachtenEmbedded.SelectionChanged


        If dgvVollmachtenEmbedded.SelectedRows.Count > 0 Then
            Dim rowindex = dgvVollmachtenEmbedded.SelectedRows(0).Index
            currentVM = New VERAG_PROG_ALLGEMEIN.cKundenVollmachten(dgvVollmachtenEmbedded.SelectedRows(0).Cells("kdvm_id").Value)

            loadChangedVMToPanel(True, currentVM)

            If dgvVollmachtenEmbedded.Enabled = False Then Exit Sub
            vmId = dgvVollmachtenEmbedded.SelectedRows(0).Cells("kdvm_id").Value

        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        saveVM(currentVM)
        setOKButton()
    End Sub

    Private Sub setOKButton()
        btnOK.Visible = True
        btnSave.Visible = False
    End Sub


    Private Sub picPDF_Click(sender As Object, e As EventArgs) Handles picPDF.Click

        If Me.MyComboBox1._value < 0 Then Exit Sub

        Dim DS As Object

        If currentVM.kdvm_datenarchivId > 0 Then
            DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER(currentVM.kdvm_datenarchivId)
            DS.OPEN_SINGLE()
            'MsgBox("Open PDF")
        Else
            DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VOLLMACHTEN", "VMID_" & vmId, "", "", "PDF_" & MyComboBox1.Text, kdnr)
            Dim path = DS.uploadDataToDATENSERVERFileDialog()

            If path <> "" Then
                currentVM.kdvm_datenarchivId = DS.da_id
            End If

            picDeletePDF.Visible = True = (currentVM.kdvm_datenarchivId > 0)
            saveVM(currentVM)
        End If

        dgvVollmachtenEmbedded.GetOrder()
        initDGVVollmachtenEmbedded(True)
        dgvVollmachtenEmbedded.SetOrder()

    End Sub

    Private Sub picDeletePDF_Click(sender As Object, e As EventArgs) Handles picDeletePDF.Click

        lblPDFavailable.Cursor = Cursors.WaitCursor
        If (currentVM.kdvm_datenarchivId > 0) Then
            If vbYes = MsgBox("Möchten Sie das PDF wirklich löschen?", vbYesNoCancel) Then

                Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(currentVM.kdvm_datenarchivId)
                DS.DELETE_COMPLETE()

                currentVM.kdvm_datenarchivId = -1
                If currentVM.SAVE Then
                    currentVM.kdvm_datenarchivId = -1 : picDeletePDF.Visible = False
                    picPDF.Image = My.Resources.pdf_gray
                End If
            End If
        End If
        lblPDFavailable.Cursor = Cursors.Default

        dgvVollmachtenEmbedded.GetOrder()
        initDGVVollmachtenEmbedded(True)
        dgvVollmachtenEmbedded.SetOrder()

    End Sub


    Private Sub picEmail_Click(sender As Object, e As EventArgs) Handles picEmail.Click
        If Me.MyComboBox1._value < 0 Then Exit Sub

        Dim DS As Object

        If currentVM.kdvm_emailId > 0 Then

            DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER(currentVM.kdvm_emailId)
            DS.OPEN_SINGLE()
            'MsgBox("Open EMail")

        Else

            DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", "VOLLMACHTEN", "VMID_" & vmId, "", "", "Email_" & MyComboBox1.Text, kdnr)
            Dim path = DS.uploadDataToDATENSERVERFileDialog()

            If path <> "" Then
                currentVM.kdvm_emailId = DS.da_id
            End If

            picDeleteEmail.Visible = True = (currentVM.kdvm_emailId > 0)

            saveVM(currentVM)
        End If

        dgvVollmachtenEmbedded.GetOrder()
        initDGVVollmachtenEmbedded(True)
        dgvVollmachtenEmbedded.SetOrder()
    End Sub

    Private Sub picDeleteEmail_Click(sender As Object, e As EventArgs) Handles picDeleteEmail.Click

        lblPDFavailable.Cursor = Cursors.WaitCursor
        If (currentVM.kdvm_emailId > 0) Then
            If vbYes = MsgBox("Möchten Sie die Email wirklich löschen?", vbYesNoCancel) Then

                Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(currentVM.kdvm_emailId)
                DS.DELETE_COMPLETE()

                currentVM.kdvm_emailId = -1
                If currentVM.SAVE Then
                    currentVM.kdvm_emailId = -1 : picDeleteEmail.Visible = False

                    picEmail.Image = My.Resources.email_big_gray

                End If
            End If
        End If
        lblPDFavailable.Cursor = Cursors.Default

        dgvVollmachtenEmbedded.GetOrder()
        initDGVVollmachtenEmbedded(True)
        dgvVollmachtenEmbedded.SetOrder()

    End Sub

    Sub hideAttachementControl()

        picDeletePDF.Visible = False
        picPDF.Visible = False
        picDeleteEmail.Visible = False
        picEmail.Visible = False
    End Sub

End Class