Imports Microsoft.Office.Interop

Public Class frmLKW_AnhaengeDatenarchiv

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL
    Public KdNr As Integer
    'Public Property _DATENSERVER_KATEGORIE As List(Of String) ' As String = "DOKUMENTE"
    'Public Property _DATENSERVER_ORDNER As List(Of String)
    'Public Property _DATENSERVER_UOrdner1 As List(Of String)
    'Public Property _DATENSERVER_UOrdner2 As List(Of String)
    'Public Property _DATENSERVER_UOrdner3 As List(Of String)

    Public Property LIST_LKW As List(Of String)


    Sub New(KdNr As Integer, LIST_LKW As List(Of String))

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

    End Sub

    Private Sub cbx_CheckedChanged(sender As Object, e As EventArgs) Handles cbx.CheckedChanged
        For Each c In MyPanel1.Controls
            If c IsNot sender Then
                c.checked = sender.checked
            End If
        Next
    End Sub

    Private Sub Form4_Load(sender As Object, e As EventArgs) Handles Me.Load
        '_DATENSERVER_KATEGORIE.Add("")
        '_DATENSERVER_ORDNER.Add("")
        '_DATENSERVER_UOrdner1.Add("")
        '_DATENSERVER_UOrdner2.Add("")
        '_DATENSERVER_UOrdner3.Add("")
        initList()

        cbx.Checked = True

    End Sub



    Sub initList()
        Try

            For Each l In LIST_LKW
                Dim dt As DataTable = SQL.loadDgvBySql("Select da_name,  (Select TOP 1 isnull(coll_pfad,'') as coll_pfad    FROM [tblDatenarchiv_Collection]  where coll_daid=da_Id And coll_archiv=0 order by coll_date desc) as coll_pfad
                                                                From [tblDatenarchiv] 
                                                      Where da_KundenNr ='" & KdNr & "' AND  da_kategorie='DOKUMENTE' and da_ordner='MDM' and da_uordner1='" & l & "' ", "FMZOLL")

                Dim Zulassungsschein = ""
                Dim CEMT = ""
                Dim Leasing = ""
                Dim Miete = ""
                Dim CIF = ""
                Dim COC = ""
                For Each r In dt.Rows
                    If r("coll_pfad") IsNot DBNull.Value AndAlso r("coll_pfad") <> "" Then
                        If r("da_name") = "Zulassungsschein" Then Zulassungsschein = r("coll_pfad")
                        If r("da_name") = "CEMT" Then CEMT = r("coll_pfad")
                        If r("da_name") = "Leasing-Vertrag" Then Leasing = r("coll_pfad")
                        If r("da_name") = "Miet-Vertrag" Then Miete = r("coll_pfad")
                        If r("da_name") = "CIF-Dokument" Then CIF = r("coll_pfad")
                        If r("da_name") = "COC-Dokument" Then COC = r("coll_pfad")
                    End If
                Next

                MyDatagridview1.Rows.Add(l, Zulassungsschein, CEMT, Leasing, Miete, CIF, COC, (Zulassungsschein <> ""), (CEMT <> ""), (Leasing <> ""), (Miete <> ""), (CIF <> ""), (COC <> ""))
            Next
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim myList = getPfade()

        If myList.Count > 0 Then
            Clipboard.Clear()
            Dim myArray As String() = myList.ToArray()
            Dim d As New DataObject(DataFormats.FileDrop, myArray)
            Clipboard.SetDataObject(d, True)
        End If
    End Sub

    Function getPfade(Optional zielPfad = "")
        Dim myList As New List(Of String)
        Dim zielPfadZusammenfassung = "" ' VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.TMP_PATH 'getTMPPath("", "", , False)
        VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.clearTMPPath("LKW_Anhaenge") 'TMP Löschen

        Dim extension = ".pdf"
        If CheckBox4.Checked Then
            If zielPfad = "" Then zielPfad = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.TMP_PATH
            zielPfadZusammenfassung = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPAbsolutePath(zielPfad & KdNr & "_Anhänge" & extension, extension,, True)
            zielPfad = ""
        End If
        For Each r As DataGridViewRow In MyDatagridview1.Rows
            If cbxZulassung.Checked Then If r.Cells("clmnZulassung_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnZulassung_docID").Value, r.Cells("clmnLKW").Value, "Zulassung", zielPfad)) '
            If cbxCEMT.Checked Then If r.Cells("clmnCEMT_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnCEMT_docID").Value, r.Cells("clmnLKW").Value, "CEMT", zielPfad)) 'r.Cells("clmnCEMT_docID").Value)
            If cbxLeasing.Checked Then If r.Cells("clmnLeasing_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnLeasing_docID").Value, r.Cells("clmnLKW").Value, "Leasing", zielPfad)) 'r.Cells("clmnLeasing_docID").Value)
            If cbxMiete.Checked Then If r.Cells("clmnMiete_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnMiete_docID").Value, r.Cells("clmnLKW").Value, "Miete", zielPfad)) 'r.Cells("clmnLeasing_docID").Value)
            If cbxCIF.Checked Then If r.Cells("clmnCIF_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnCIF_docID").Value, r.Cells("clmnLKW").Value, "CIF", zielPfad)) 'r.Cells("clmnLeasing_docID").Value)
            If cbxCOC.Checked Then If r.Cells("clmnCOC_docID").Value <> "" Then myList.Add(copyFileToTMP(r.Cells("clmnCOC_docID").Value, r.Cells("clmnLKW").Value, "COC", zielPfad)) 'r.Cells("clmnLeasing_docID").Value)
        Next
        'zusammenfassen
        If CheckBox4.Checked Then
            If FormularManagerNEU.MergePdfFiles(myList, zielPfadZusammenfassung) Then
                myList.Clear() : myList.Add(zielPfadZusammenfassung) 'mylist überscheiben
            End If
        End If
        Return myList
    End Function

    Function copyFileToTMP(pfad, kz, art, Optional zielPfad = "") As String
        Try
            If zielPfad = "" Then
                zielPfad = VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.getTMPPath(kz & "_" & art & ".pdf", ".pdf", , False, "LKW_Anhaenge")
            Else
                zielPfad &= kz & "_" & art & ".pdf"
            End If

            System.IO.File.Copy(pfad, zielPfad, True)
            Return zielPfad
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try
        Return ""
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim outl As New Outlook.Application
            Dim Mail As Microsoft.Office.Interop.Outlook.MailItem
            Mail = outl.CreateItem(0)
            Dim myList = getPfade()

            For Each l In myList
                Mail.Attachments.Add(l, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
            Next

            Mail.Display()
        Catch ex As Exception
            MsgBox("Fehler beim Öffnen der Mail!" & ex.Message & ex.StackTrace)
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            Dim myList = getPfade()

            If myList.Count > 0 Then

                Dim fd As New FolderBrowserDialog

                If fd.ShowDialog() = DialogResult.OK Then

                    getPfade(fd.SelectedPath & "\")
                    'For Each l In myList
                    '    copyFile(l, fd.SelectedPath)
                    'Next
                End If

            End If
        Catch ex As Exception
            MsgBox(ex.Message & ex.StackTrace)
        End Try

    End Sub

    'Public Function copyFile(file, toDirectory, Optional ByRef dest = "")
    '    If Not IO.File.Exists(file) Then Return False
    '    If Not IO.Directory.Exists(toDirectory) Then IO.Directory.CreateDirectory(toDirectory)
    '    dest = toDirectory & cut_file(file)
    '    If IO.File.Exists(dest) Then
    '        dest = toDirectory & Now.ToString("yyMMdd_HHmmss.fff_") & cut_file(file)
    '    End If
    '    IO.File.Copy(file, dest) ' Verschiebt die Datei
    '    Return True

    'End Function
    'Public Function cut_file(ByVal file As String) As String ' Funktion zum Entfernen der Backslashs / Ordner While file.Contains("\") file = file.Remove(0, 1) End While Return file End Function 
    '    While file.Contains("\")
    '        file = file.Remove(0, 1)
    '    End While
    '    Return file
    'End Function
End Class