Imports Microsoft.Office.Interop

Public Class usrCntlKundenlogin

    Public kdNr As Integer
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL


    Sub New()

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

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
    End Sub

    Sub New(kdNr)

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

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.kdNr = kdNr
    End Sub


    Sub init(kdNr)
        'dgvKundenlogins.Rows.Clear()
        Me.kdNr = kdNr
        initDGV()
    End Sub

    Sub initDGV()

        If kdNr > 0 Then
            With dgvKundenlogins
                .DataSource = SQL.loadDgvBySql("SELECT KundenNr, Username, Password as Passwort, Lieferant, Portal FROM [tblKundenlogin]  where [KundenNr]=" & kdNr, "FMZOLL")

                .Columns("KundenNr").Visible = False
                .Columns("Username").Width = 120
                .Columns("Passwort").Width = 100
                .Columns("Portal").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("Lieferant").Width = 100
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .ClearSelection()
            End With


        End If

    End Sub


    Private Sub dgvAufschub_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvKundenlogins.CellDoubleClick, dgvKundenlogins.CellContentDoubleClick
        If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("Kundendaten_bearbeiten", Me.FindForm) Then

            If dgvKundenlogins.SelectedRows.Count > 0 Then
                Dim f As New frmKundenlogin(kdNr, IIf(Not IsDBNull(dgvKundenlogins.SelectedRows(0).Cells("Portal").Value), dgvKundenlogins.SelectedRows(0).Cells("Portal").Value, ""))
                f.ShowDialog(Me)
                initDGV()
            End If
        End If
    End Sub

    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        Dim f As New frmKundenlogin(kdNr)
        f.ShowDialog(Me)
        initDGV()
    End Sub


    Private Sub dgvAufschub_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvKundenlogins.KeyDown
        If e.KeyCode = Keys.Delete Then
            If VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("Kundendaten_bearbeiten", Me.FindForm) Then
                If dgvKundenlogins.SelectedRows.Count > 0 Then
                    If vbYes = MsgBox("Möchten Sie den Kundenportaleintrag wirklich löschen?", vbYesNoCancel) Then
                        Dim portal As New VERAG_PROG_ALLGEMEIN.cKundenportal(dgvKundenlogins.SelectedRows(0).Cells("KundenNr").Value, dgvKundenlogins.SelectedRows(0).Cells("Portal").Value)
                        portal.DELETE(dgvKundenlogins.SelectedRows(0).Cells("KundenNr").Value, dgvKundenlogins.SelectedRows(0).Cells("Portal").Value)
                        initDGV()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub dgvAufschub_CellClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvKundenlogins.CellMouseClick
        If e.RowIndex < 0 Then Exit Sub
        DirectCast(sender, DataGridView).Rows(e.RowIndex).Selected = True
        If e.Button = Windows.Forms.MouseButtons.Right Then
            If dgvKundenlogins.SelectedRows.Count > 0 Then
                ContextMenuStrip1.Show(Cursor.Position)
            End If
        End If
    End Sub

    Private Sub LöschenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LöschenToolStripMenuItem.Click

        Dim delKey As New KeyEventArgs(Keys.Delete)
        dgvAufschub_KeyDown(sender, delKey)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnExcel.Click, btnMail.Click

        Try



            Select Case sender.name

                Case "btnExcel"

                    Me.Cursor = Cursors.WaitCursor
                    cProgramFunctions.genExcelFromDGV_NEW(dgvKundenlogins, True)
                    Me.Cursor = Cursors.Default

                Case "btnMail"

                    Dim outl As New Outlook.Application
                    Dim Mail As Microsoft.Office.Interop.Outlook.MailItem
                    Mail = outl.CreateItem(0)

                    Dim kunednSQL As New kundenSQL
                    Dim kundeMail As New cKunden()
                    kundeMail = kunednSQL.getKundeByKdNr(kdNr)

                    Mail.Subject = kundeMail.Kurzname & " - CUSTOMER PORTAL LOGIN DATA"

                    Dim TextHTMLtable As String = ""
                    TextHTMLtable &= "<table border=1>"
                    TextHTMLtable &= "<tr><td>Supplier</td><td>Portal</td><td>Username</td><td>Password</td><td>Link</td></tr>"

                    For Each r As DataGridViewRow In dgvKundenlogins.SelectedRows

                        If r.Cells("Portal").Value <> "UTA-INTERN" Then ' überspringen!

                            TextHTMLtable &= "<tr>"
                            TextHTMLtable &= "<td><b>" & r.Cells("Lieferant").Value & "</b></td>"
                            TextHTMLtable &= "<td><b>" & r.Cells("Portal").Value & "</b></td>"
                            TextHTMLtable &= "<td><b>" & r.Cells("Username").Value & "</b></td>"
                            TextHTMLtable &= "<td><b>" & r.Cells("Passwort").Value & "</b></td>"

                            Dim Link As String = getLink(r.Cells("Portal").Value)

                            TextHTMLtable &= "<td><b>" & Link & "</b></td>"

                            TextHTMLtable &= "</tr>"

                        End If


                    Next

                    TextHTMLtable &= "</table>"

                    Mail.HTMLBody = "Dear Sir or Madam,<br><br>attached we send you the customer portal login data.<br><br>"

                    Mail.HTMLBody &= TextHTMLtable

                    Mail.HTMLBody &= "<br><br>"
                    Mail.HTMLBody &= VERAG_PROG_ALLGEMEIN.cAllgemein.USRNAME & "<br>"
                    Mail.HTMLBody &= "<br>"
                    Mail.HTMLBody &= cFakturierung.getSignature("DE", 19, True, True)


                    Mail.To &= kundeMail.EMail
                    Mail.CC &= kundeMail.EMail2

                    Mail.Display()


            End Select


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



    End Sub

    Private Function getLink(Portal As String) As String

        Dim link As String = ""
        Dim hyperlink As String = ""

        Select Case Portal
            Case "Q8-TRUCK" : link = "https://iaccount.q8.com/"
            Case "UTA PORTAL" : link = "https://meinedenred.at/login"
            Case "PLOSE ARP" : link = "https://www.apr.plose.it/login"
            Case "MSTSTTOLLS" : link = "https://iaccount.q8.com/"
            Case "HUB4TRUCKS" : link = "https://truck.telepass.com/api/uaa/login"
        End Select

        If link <> "" Then

            hyperlink = $"<a href=""{link}"">{Portal}</a>"

        End If


        Return IIf(hyperlink <> "", hyperlink, "-")

    End Function

End Class
