Imports System.Drawing
Imports System.Windows.Forms

Public Class frminterneEmailBenachrichtigungen

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL


    Private Sub frminterneEmailBenachrichtigungen_Load(sender As Object, e As EventArgs) Handles Me.Load


        cboKategorie.Items.Clear()
        cboKategorie.fillWithSQL("SELECT distinct([mb_object]) FROM [tblEMailbenachrichtigungen] order by mb_object", False, "ADMIN", True)

        cboFirma.Items.Clear()
        cboFirma.fillWithSQL("SELECT distinct(Firma_Bez) FROM [tblFirma] order by Firma_Bez", False, "FMZOLL", True)
        cboFirma.changeItem(cAllgemein.FIRMA)

        loadDGVMailbenachrichtigungen()

    End Sub

    Private Sub cboKategorie_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboKategorie.SelectedIndexChanged
        loadDGVMailbenachrichtigungen()
    End Sub

    Private Sub cboFirma_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFirma.SelectedIndexChanged
        loadDGVMailbenachrichtigungen()
    End Sub


    Sub loadDGVMailbenachrichtigungen()
        Try

            Me.Cursor = Cursors.WaitCursor

            Dim TopMax = If(IsNumeric(MyTextBox2.Text), " TOP " & MyTextBox2.Text, "")
            Dim sqlstr As String = ""


            sqlstr &= " SELECT  e.mb_id,e.mb_aktiv,e.mb_object,e.mb_beschreibung,STRING_AGG(m.mit_email, ', ') AS Emails,STRING_AGG(m.mit_nname, ', ') as Mitarbeiter,COUNT(DISTINCT m.mit_email) AS Anzahl,e.mb_firmaID,ISNULL(f.Firma_Bez,'ALLE') AS Firma_Bez, MAX(CASE WHEN ISNULL(m.mit_gekuendigt,0) <> 0 THEN 1 ELSE 0 END) AS inaktivMA
                        FROM [tblEMailbenachrichtigungen] e
                        
                        LEFT JOIN [VERAG].[dbo].[tblFirma] f ON e.mb_firmaID = f.Firma_ID
                        LEFT JOIN ADMIN.dbo.tblMitarbeiter m
                            CROSS APPLY dbo.SplitString(m.mit_EMailbenachrichtigungen, ',') s
                            ON TRY_CAST(s.value AS INT) = e.mb_id
                        
                        WHERE 1 = 1 "

            With dgvBenachrichtigungen
                Dim SQLHAVING As String = ""
                If cboOhneEmpf.Checked Then
                    SQLHAVING &= " HAVING COUNT(DISTINCT m.mit_email) = 0 "
                End If


                .SET_SQL(sqlstr & getWherePruefungsobjekte() & " GROUP BY e.mb_id,e.mb_aktiv,e.mb_object,mb_beschreibung,e.mb_firmaID,f.Firma_Bez " & SQLHAVING & " ORDER BY mb_object,f.Firma_Bez ", "ADMIN", getListBenachrichtigungen)
                .LOAD()
                showDGVPruefungsobjekte()
            End With

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, "")
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Sub showDGVPruefungsobjekte()

        With dgvBenachrichtigungen

            If .ColumnCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False

            .Columns("mb_id").Visible = False
            .Columns("mb_firmaID").Visible = False
            .Columns("inaktivMA").Visible = False

            .Columns("mb_object").Width = 200
            .Columns("mb_object").HeaderText = "Benachrichtigung"


            .Columns("Mitarbeiter").MinimumWidth = 100
            .Columns("Mitarbeiter").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns("mb_beschreibung").MinimumWidth = 100
            .Columns("mb_beschreibung").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("mb_beschreibung").HeaderText = "Info"

            .Columns("Emails").MinimumWidth = 100
            .Columns("Emails").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("Emails").HeaderText = "EMail an"

            .Columns("Anzahl").Width = 50
            .Columns("Anzahl").HeaderText = "# Empf."

            .Columns("Firma_Bez").Width = 150
            .Columns("Firma_Bez").HeaderText = "Firma"

            .Columns("mb_aktiv").Width = 40
            .Columns("mb_aktiv").HeaderText = "Aktiv"

            For Each r As DataGridViewRow In dgvBenachrichtigungen.Rows
                If CInt(r.Cells("inaktivMA").Value) > 0 Then
                    r.Cells("Mitarbeiter").Style.BackColor = Color.IndianRed
                End If
            Next


        End With
    End Sub
    Function getWherePruefungsobjekte() As String
        Dim sqlstr = ""

        If cboFirma._value <> "" Then sqlstr &= " AND Firma_Bez LIKE @Firma_Bez  "
        If cboKategorie._value <> "" Then sqlstr &= " AND mb_object LIKE @mb_object "


        Return sqlstr
    End Function

    Function getListBenachrichtigungen() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma_Bez", cboFirma._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("mb_object", cboKategorie._value))


        Return list

    End Function

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

    End Sub

    Private Sub cboOhneEmpf_CheckedChanged(sender As Object, e As EventArgs) Handles cboOhneEmpf.CheckedChanged
        loadDGVMailbenachrichtigungen()
    End Sub
End Class