Imports System.Windows.Forms

Public Class frmMessenger_SonstigeNEW

    Dim SQL As New SQL
    Dim CHAT As New cMessenger.cMessenger_Chat

    Dim loaded = False
    Public chat_id As Integer = -1

    Sub New(CHAT)

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

    End Sub
    Sub New()
        InitializeComponent()

    End Sub
    'Sub initDGV()
    '    With dgvChatMembers
    '        .Rows.Clear()

    '        For Each MB In CHAT.CHAT_MEMBERS
    '            Dim MA As New cMitarbeiter(MB.chatMb_maId)
    '            .Rows.Add({MB.chatMb_maId, MA.Fullname, If(MA.mit_firma = "ALL", MA.mit_FirmaHaupt, MA.mit_firma)})
    '        Next
    '        .ClearSelection()
    '    End With
    'End Sub

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

        cboArt.Items.Clear()
        cboArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UMFRAGE", "UMFRAGE"))
        cboArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MITTEILUNG", "MITTEILUNG"))
        cboArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ARBEITSANWEISUNG", "ARBEITSANWEISUNG"))

        If CHAT.chat_id > 0 Then
            MyTextBox1.Text = CHAT.chat_name
            Button7.Enabled = False
            cboArt.Enabled = False

        Else


            Button3.Enabled = False
        End If
    End Sub


    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        lblErrArt.Visible = False
        lblErrName.Visible = False
        If cboArt._value = "" Then lblErrArt.Visible = True : Exit Sub
        If MyTextBox1.Text = "" Then lblErrName.Visible = True : Exit Sub


        CHAT.chat_art = cboArt._value
        CHAT.chat_name = MyTextBox1.Text
        CHAT.chat_freigegeben = False
        CHAT.chat_erstelltMaId = VERAG_PROG_ALLGEMEIN.cAllgemein.USRID


        If CHAT.SAVE(False) Then
            initMain()
        End If

    End Sub

    Sub initMain()
        If CHAT Is Nothing Then Exit Sub
        initDGVMembers()

        Select Case CHAT.chat_art
            Case "UMFRAGE"
                Dim usrcntl As New usrCntlMessenger_Umfrage

                usrcntl.setValues(CHAT)


                pnlMain.Controls.Clear()
                usrcntl.Dock = DockStyle.Fill
                pnlMain.Controls.Add(usrcntl)
            Case "MITTEILUNG"
                Dim usrcntl As New usrCntlMessenger_Mitteilung

                usrcntl.setValues(CHAT)


                pnlMain.Controls.Clear()
                usrcntl.Dock = DockStyle.Fill
                pnlMain.Controls.Add(usrcntl)

        End Select
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        SAVE_ME
    End Sub
    Sub SAVE_ME()
        If CHAT Is Nothing Then Exit Sub
        If pnlMain.Controls.Count = 0 Then Exit Sub

        Select Case CHAT.chat_art
            Case "UMFRAGE"
                Dim usrcntl As usrCntlMessenger_Umfrage = DirectCast(pnlMain.Controls(0), usrCntlMessenger_Umfrage)

                CHAT.chat_name = MyTextBox1.Text
                CHAT = usrcntl.getValues(CHAT)

                CHAT.SAVE(True)

            Case "MITTEILUNG"
                Dim usrcntl As usrCntlMessenger_Mitteilung = DirectCast(pnlMain.Controls(0), usrCntlMessenger_Mitteilung)

                CHAT.chat_name = MyTextBox1.Text
                CHAT = usrcntl.getValues(CHAT)

                CHAT.SAVE(True)

        End Select
    End Sub
    Sub initDGVMembers()
        With dgvChatMembers
            .Rows.Clear()

            For Each MB In CHAT.CHAT_MEMBERS
                Dim MA As New cMitarbeiter(MB.chatMb_maId)
                .Rows.Add({MB.chatMb_maId, MA.Fullname, If(MA.mit_firma = "ALL", MA.mit_FirmaHaupt, MA.mit_firma)})
            Next
            .ClearSelection()
        End With
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        Dim f As New frmMessenger_NeuerChat(CHAT)
        f.ShowDialog()
        initDGVMembers()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If dgvChatMembers.SelectedRows.Count > 1 Then
            Dim ol As New List(Of Object)
            For Each r As DataGridViewRow In dgvChatMembers.SelectedRows
                If r.Cells("clmnMaId").Value <> VERAG_PROG_ALLGEMEIN.cAllgemein.USRID Then
                    For Each MB In CHAT.CHAT_MEMBERS
                        If r.Cells("clmnMaId").Value = MB.chatMb_maId Then
                            ol.Add(MB)
                        End If
                    Next
                End If
            Next
            For Each o In ol
                CHAT.CHAT_MEMBERS.Remove(o)
                If CHAT.SAVE_MEMBERS() Then
                    initDGVMembers()
                End If
            Next
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        CHAT.chat_freigegeben = 1
        CHAT.chat_aktiv = 1


        If Not CHAT.chat_aktiv Then MsgBox("Chat inaktiv!") : Exit Sub

        If CHAT.SAVE(True) Then

            Dim MSG As New VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_ChatMessages(CHAT.chat_id, VERAG_PROG_ALLGEMEIN.cAllgemein.USRID)
            MSG.chatMg_text = ""
            MSG.chatMg_type = CHAT.chat_art
            MSG.chatMg_maName = VERAG_PROG_ALLGEMEIN.cAllgemein.MITARBEITER.Fullname
            MSG.chatMg_maFirma = VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA

            If MSG.SAVE(True, CHAT.CHAT_MEMBERS) Then

                Me.Close()
            End If

        End If


    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        CHAT.chat_freigegeben = False
        CHAT.chat_aktiv = False
        If CHAT.SAVE(True) Then
            Me.Close()
        End If
    End Sub

End Class