Imports System.Windows.Forms
Imports System.IO
Imports Microsoft.Office.Interop

Public Class usrcntlPDFScan
    Public Property _TEXT_PDF As String
    Public Property _DATENSERVER_KATEGORIE As String = "DOKUMENTE"
    Public Property _DATENSERVER_ORDNER As String = ""
    Public Property _DATENSERVER_UOrdner1 As String = ""
    Public Property _DATENSERVER_UOrdner2 As String = ""
    Public Property _DATENSERVER_UOrdner3 As String = ""
    Public Property _MULTI_FILES As Boolean = False
    Public Property _ARCHIV As Boolean = False

    ' Public pdf_Path As String = ""
    Dim KdNr As Integer = -1

    'Dim DATENSERVER As New cDATENSERVER
    ' Public ScanID As Integer = -1
    Public COLL_ID As Integer = -1
    'Dim DatenserverIDCollection As New cDatenserverIDCollectionList
    Dim DS As VERAG_PROG_ALLGEMEIN.cDATENSERVER


    Public Event FileAdded(id As String, path As String, name As String)

    Private Sub usrcntlPDFScan_Layout(sender As Object, e As LayoutEventArgs) Handles Me.Layout
        Me.txt.Text = _TEXT_PDF
    End Sub


    Public Sub INIT(KdNr)
        Me.KdNr = KdNr
        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", _DATENSERVER_ORDNER, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2, _DATENSERVER_UOrdner3, _TEXT_PDF, KdNr, _MULTI_FILES)
        initPdf()
    End Sub


    Public Sub INIT(KdNr, _DATENSERVER_UOrdner1)
        Me.KdNr = KdNr
        Me._DATENSERVER_UOrdner1 = _DATENSERVER_UOrdner1
        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", _DATENSERVER_ORDNER, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2, _DATENSERVER_UOrdner3, _TEXT_PDF, KdNr, _MULTI_FILES)
        initPdf()
    End Sub

    Public Sub INIT(KdNr, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2)
        Me.KdNr = KdNr
        Me._DATENSERVER_UOrdner1 = _DATENSERVER_UOrdner1
        Me._DATENSERVER_UOrdner2 = _DATENSERVER_UOrdner2
        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", _DATENSERVER_ORDNER, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2, _DATENSERVER_UOrdner3, _TEXT_PDF, KdNr, _MULTI_FILES)
        initPdf()
    End Sub

    Public Sub INIT(KdNr, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2, _DATENSERVER_UOrdner3)
        Me.KdNr = KdNr
        Me._DATENSERVER_UOrdner1 = _DATENSERVER_UOrdner1
        Me._DATENSERVER_UOrdner2 = _DATENSERVER_UOrdner2
        Me._DATENSERVER_UOrdner3 = _DATENSERVER_UOrdner3
        DS = New VERAG_PROG_ALLGEMEIN.cDATENSERVER("DOKUMENTE", _DATENSERVER_ORDNER, _DATENSERVER_UOrdner1, _DATENSERVER_UOrdner2, _DATENSERVER_UOrdner3, _TEXT_PDF, KdNr, _MULTI_FILES)
        initPdf()
    End Sub



    Private Sub usrcntlPDFScan_Load(sender As Object, e As EventArgs) Handles Me.Load
        Me.AllowDrop = True
        Me.txt.Text = _TEXT_PDF
        If _MULTI_FILES Then
            Me.ContextMenuStrip = cntxtMulti
            txt.ContextMenuStrip = cntxtMulti
            picPDF.ContextMenuStrip = cntxtMulti
            ' Else
            '    If DatenserverIDCollection.Count > 0 Then
            'pdf_Path = DATENSERVER.getPathById(ScanID)
            '  End If
        End If


    End Sub

    Public Fill

    ' Sub addId(id)
    '     If id > 0 Then
    '         ScanID = id
    '        pdf_Path = DATENSERVER.getPathById(ScanID)
    '       initPdf()
    '    End If
    '  End Sub
    Sub initPdf()
        If DS Is Nothing Then Exit Sub
        txt.Name = _TEXT_PDF

        If DS.DATA_LIST.LIST.Count > 0 Then
            picPDF.Enabled = True
            txt.Enabled = True
            '  picScanner.Visible = False
            picPDF.BackgroundImage = My.Resources.pdf
            setCursorHand(Me)
            setCursorHand(picPDF)
            setCursorHand(txt)
            '  del.Visible = True
            If Not _MULTI_FILES Then LöschenToolStripMenuItem.Enabled = VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_Datenarchiv_DELETE", "SDL")
            KopierenZwischenablageToolStripMenuItem.Enabled = True
            AlsEMailSendenToolStripMenuItem.Enabled = True
        Else
            picPDF.Enabled = False
            txt.Enabled = False
            picPDF.BackgroundImage = My.Resources.pdf_gray
            'picScanner.Visible = True
            setCursorDefalut(Me)
            setCursorDefalut(picPDF)
            setCursorDefalut(txt)
            ' del.Visible = False
            LöschenToolStripMenuItem.Enabled = False
            KopierenZwischenablageToolStripMenuItem.Enabled = False
            AlsEMailSendenToolStripMenuItem.Enabled = False
        End If

        '  ArchivierenToolStripMenuItem.Visible = If(DS.da_multifiles And DS.DATA_LIST.LIST.Count = 1, True, False)
    End Sub

    Sub setCursorHand(c As Control)
        c.Cursor = Windows.Forms.Cursors.Hand
    End Sub
    Sub setCursorDefalut(c As Control)
        c.Cursor = Windows.Forms.Cursors.Default
    End Sub


    Private Sub txt_Click(sender As Object, e As EventArgs) Handles txt.Click, Me.Click, picPDF.Click

        Try

            If DS.DATA_LIST.LIST.Count > 0 Then
                If _MULTI_FILES Then 'And DS.DATA_LIST.LIST.Count > 1 Then
                    Dim list As New frmPDFScanList

                    showMulti(list)

                    list.Location = System.Windows.Forms.Cursor.Position
                    list.Show() 'Me.FindForm) 'Dialog() 's(Me.FindForm)
                    list.SetDesktopLocation(Windows.Forms.Cursor.Position.X, Windows.Forms.Cursor.Position.Y)
                Else
                    'Process.Start(DS.DATA_LIST.LIST(0).coll_pfad)
                    DS.OPEN_SINGLE(, True)
                End If
            End If

        Catch ex As Exception
            MsgBox("FEHLER: UserControl möglicherweise nicht initialisiert!")
        End Try
    End Sub
    Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter ', txt.DragEnter, Me.DragEnter, picPDF.DragEnter
        e.Effect = DragDropEffects.All
    End Sub
    Private Sub txt_Click(sender As Object, e As DragEventArgs) Handles Me.DragDrop ', txt.DragDrop, Me.DragDrop, picPDF.DragDrop
        If e.Data.GetDataPresent("FileDrop", True) = True Then
            Dim FileList As String() = CType(e.Data.GetData("FileDrop"), Object) 'Hier wird der Variable "Wert" das übergeben, was wir auf die Form gezogen haben. Bei einer Datei wäre es dann der Pfad der Datei.
            If FileList.Count > 0 Then
                If Not _MULTI_FILES And FileList.Count > 1 Then
                    MsgBox("Es können nicht mehrere Dateien hochgeladen werden!")
                    Exit Sub
                End If
                For Each W In FileList
                    If W.ToUpper.EndsWith(".PDF") Then
                        Try

                            Dim bezeichnung = System.IO.Path.GetFileName(W).ToString
                            DS.uploadDataToDATENSERVER(W, bezeichnung, ".pdf")

                            'If DS.DATA_LIST.LIST.Count > 0 Then
                            '    Dim l As VERAG_PROG_ALLGEMEIN.cDatenarchiv_Collection = DS.DATA_LIST.LIST(DS.DATA_LIST.LIST.Count - 1)
                            '    RaiseEvent FileAdded(l.coll_id, l.coll_pfad, l.coll_bezeichnung)
                            'End If
                        Catch ex As Exception
                            MsgBox("FEHLER: UserControl möglicherweise nicht initialisiert!" & ex.Message & ex.StackTrace)
                        End Try
                    End If
                Next
                initPdf()
            End If
        End If
    End Sub


    Sub showMulti(List As frmPDFScanList)
        Try
            List.listlOld.Visible = False
            List.pnlOldHeader.Visible = False
            List.FlowLayoutPanel.Controls.Clear()
            List.listlOld.Controls.Clear()
            Dim tmpHeight = 0
            For Each i In DS.DATA_LIST.LIST
                Dim u As New usrCntLPDFScanSimple(i.coll_id, i.coll_pfad, i.coll_bezeichnung, i.coll_archiv)
                AddHandler u.CLICKED, Sub()
                                          List.Close()
                                      End Sub
                AddHandler u.DELETE, Sub(coll_id, coll_bezeichnung)
                                         If vbYes = MsgBox("Möchten Sie die Datei '" & coll_bezeichnung & "' wirklich löschen?!? Die Daten gehen unwiederruflich verloren.", vbYesNoCancel, "Löschen") Then
                                             'getListItemAT  Dim DS As New VERAG_PROG_ALLGEMEIN.cDATENSERVER(id)
                                             If Not DS.DELETE_LIST_POS(coll_id) Then
                                                 MsgBox("Fehler beim Löschen!")
                                             End If

                                             'DatenserverIDCollection.DELETE(id)
                                             ' For Each c In DatenserverIDCollection.LIST
                                             '   If c.da_id = id Then
                                             '       DatenserverIDCollection.Remove(c)
                                             '        Exit For
                                             '    End If
                                             '        Next
                                             initPdf()

                                             List.Close()
                                         End If
                                     End Sub

                AddHandler u.RENAME, Sub(coll_id, coll_bezeichnung)
                                         Dim bez = InputBox("Bitte geben Sie eine neue Bezeichnung ein:", , coll_bezeichnung)
                                         If bez <> "" Then
                                             For Each ii In DS.DATA_LIST.LIST
                                                 If ii.coll_id = coll_id Then
                                                     ii.coll_bezeichnung = bez
                                                     ii.UPDATE()
                                                     showMulti(List)
                                                     initPdf()
                                                     Exit Sub
                                                 End If
                                             Next
                                         End If

                                     End Sub
                AddHandler u.ARCHIV, Sub(coll_id, coll_bezeichnung)
                                         For Each ii In DS.DATA_LIST.LIST
                                             If ii.coll_id = coll_id Then
                                                 ii.coll_archiv = Not ii.coll_archiv
                                                 ii.UPDATE()
                                                 showMulti(List)
                                                 List.PerformLayout() 'IRGENDWIE NEU LAYOUTEN()
                                                 initPdf()
                                                 Exit Sub
                                             End If
                                         Next
                                     End Sub

                AddHandler u.CLIPBOARD, Sub(coll_id, coll_bezeichnung)
                                            For Each ii In DS.DATA_LIST.LIST
                                                If ii.coll_id = coll_id Then
                                                    'Zwischenablage
                                                    Dim f() As String = {VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.copyToTmp_KeepFilename(ii.coll_pfad, ii.coll_bezeichnung)}
                                                    Dim d As New DataObject(DataFormats.FileDrop, f)
                                                    Clipboard.SetDataObject(d, True)
                                                    Exit Sub
                                                End If
                                            Next
                                            List.Close()
                                        End Sub


                AddHandler u.OPEN_MAIL, Sub(coll_id, coll_bezeichnung)
                                            For Each ii In DS.DATA_LIST.LIST
                                                If ii.coll_id = coll_id Then
                                                    'Mail
                                                    Try
                                                        Dim outl As New Outlook.Application
                                                        Dim Mail As Microsoft.Office.Interop.Outlook.MailItem
                                                        Mail = outl.CreateItem(0)
                                                        Mail.Attachments.Add(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.copyToTmp_KeepFilename(ii.coll_pfad, ii.coll_bezeichnung), Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue,, ii.coll_bezeichnung & ".pdf")
                                                        Mail.Display()
                                                    Catch ex As Exception
                                                        MsgBox("Fehler beim Öffnen der Mail!")
                                                    End Try
                                                End If
                                                List.Close()
                                            Next
                                        End Sub

                If _ARCHIV And i.coll_archiv Then
                    List.listlOld.Controls.Add(u)
                    List.listlOld.Visible = True
                    tmpHeight += 23
                    List.pnlOldHeader.Visible = True
                Else
                    List.FlowLayoutPanel.Controls.Add(u)
                End If
            Next
            List.listlOld.MinimumSize = (New Drawing.Size(List.listlOld.Width, tmpHeight))
        Catch ex As Exception
            MsgBox("FEHLER: UserControl möglicherweise nicht initialisiert!")
        End Try
    End Sub
    '  Private Sub toolScannen_Click(sender As Object, e As EventArgs) Handles toolScannen.Click
    '      Process.Start(pdf_Path)

    ' End Sub


    Private Sub DateiScannenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DateiScannenToolStripMenuItem.Click, ToolStripMenuItem1.Click
        Try
            If KdNr < 0 Then Exit Sub
            Dim frmScan As New VERAG_PROG_ALLGEMEIN.frmScan("DirectScan")
            frmScan.DefaultFileName = Me._TEXT_PDF

            If frmScan.ShowDialog(Me) = DialogResult.OK Then
                If frmScan.ReturnValue IsNot Nothing Then
                    Dim Filename = _TEXT_PDF
                    If frmScan.fileName <> "" Then Filename = frmScan.fileName
                    ' If _MULTI_FILES Then Filename &= Now.ToString("_ddMMyyyy_HHmmss")
                    'Dim path = DATENSERVER.uploadDataToDATENSERVER_fromBytes(frmScan.ReturnValue, "DOKUMENTE", _DATENSERVER_ORDNER, KdNr, Filename, ".pdf")
                    If DS.uploadDataToDATENSERVER_fromBytes(frmScan.ReturnValue, Filename, ".pdf") Then
                        If DS.DATA_LIST.LIST.Count > 0 Then
                            Dim l As VERAG_PROG_ALLGEMEIN.cDatenarchiv_Collection = DS.DATA_LIST.LIST(DS.DATA_LIST.LIST.Count - 1)
                            RaiseEvent FileAdded(l.coll_id, l.coll_pfad, l.coll_bezeichnung)
                        End If
                    End If


                    ' Me.ScanID = DATENSERVER.LAST_ID
                    ' Dim file As New FileInfo(frmScan.ReturnValue)
                    'If file.Exists Then
                    ' Me.pdf_Path = path
                    'DS.DATA_LIST.ADD(Path, VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath( Filename,"")


                    'End If
                End If
            End If
            initPdf()
        Catch ex As Exception
            MsgBox("FEHLER: UserControl möglicherweise nicht initialisiert!")
        End Try
    End Sub

    Private Sub DateiHochladenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DateiHochladenToolStripMenuItem.Click, ToolStripMenuItem2.Click
        Try
            If _MULTI_FILES Then
                If DS.uploadDataToDATENSERVERFileDialog_MULTI(, ".pdf", , "PDF") Then
                    initPdf()
                End If
            Else
                If DS.uploadDataToDATENSERVERFileDialog(, ".pdf", , "PDF") Then
                    If DS.DATA_LIST.LIST.Count > 0 Then
                        Dim l As VERAG_PROG_ALLGEMEIN.cDatenarchiv_Collection = DS.DATA_LIST.LIST(DS.DATA_LIST.LIST.Count - 1)
                        RaiseEvent FileAdded(l.coll_id, l.coll_pfad, l.coll_bezeichnung)
                    End If
                End If
                initPdf()
            End If

        Catch ex As Exception
            MsgBox("FEHLER: UserControl möglicherweise nicht initialisiert!" & ex.Message & ex.StackTrace)
        End Try
    End Sub

    Private Sub LöschenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LöschenToolStripMenuItem.Click 'NUR WENN MULTI = FALSE
        If vbYes = MsgBox("Möchten Sie die Datei wirklich löschen? Die Daten gehen unwiederruflich verloren.", vbYesNoCancel, "Löschen") Then
            DS.DELETE_COMPLETE()
            '''DATENSERVER.deleteFileByDatenarchivId(DatenserverIDCollection(0).da_id)
            'DatenserverIDCollection.DELETE(DatenserverIDCollection.LIST(0).coll_daId)
            '''  DatenserverIDCollection.Clear()
            initPdf()
        End If
    End Sub

    Private Sub usrcntlPDFScan_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseEnter, txt.MouseEnter, picPDF.MouseEnter
        Try
            If DS.DATA_LIST.LIST.Count > 0 Then Me.BackColor = Drawing.Color.LightGray
        Catch ex As Exception

        End Try
    End Sub
    Private Sub usrcntlPDFScan_MouseLeave(sender As Object, e As EventArgs) Handles Me.MouseLeave, txt.MouseLeave, picPDF.MouseLeave
        Try
            If Me.BackColor <> Drawing.Color.White Then Me.BackColor = Drawing.Color.White
        Catch ex As Exception

        End Try

    End Sub

    Private Sub KopierenZwischenablageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KopierenZwischenablageToolStripMenuItem.Click
        Try
            If DS.DATA_LIST.LIST.Count > 0 Then
                Dim f() As String = {VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.copyToTmp_KeepFilename(DS.DATA_LIST.LIST(0).coll_pfad, DS.DATA_LIST.LIST(0).coll_bezeichnung)}
                Dim d As New DataObject(DataFormats.FileDrop, f)
                Clipboard.SetDataObject(d, True)
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Laden der Daten!")
        End Try
    End Sub

    Private Sub AlsEMailSendenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AlsEMailSendenToolStripMenuItem.Click
        Try
            If DS.DATA_LIST.LIST.Count > 0 Then
                Dim outl As New Outlook.Application
                Dim Mail As Microsoft.Office.Interop.Outlook.MailItem
                Mail = outl.CreateItem(0)
                Mail.Attachments.Add(VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.copyToTmp_KeepFilename(DS.DATA_LIST.LIST(0).coll_pfad, DS.DATA_LIST.LIST(0).coll_bezeichnung), Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
                Mail.Display()
            End If
        Catch ex As Exception
            MsgBox("Fehler beim Öffnen der Mail!")
        End Try
    End Sub

    'Private Sub ArchivierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ArchivierenToolStripMenuItem.Click
    '    Try
    '        If DS.DATA_LIST.LIST.Count > 0 Then
    '            DS.DATA_LIST.LIST(0).coll_archiv = Not DS.DATA_LIST.LIST(0).coll_archiv
    '            DS.DATA_LIST.LIST(0).UPDATE()
    '            initPdf()
    '            Exit Sub
    '        End If
    '    Catch ex As Exception
    '        MsgBox("Fehler beim Archivieren!")
    '    End Try
    'End Sub
End Class
