Imports System.Windows.Forms

Public Class frmMessenger_NeuerChat

    Dim SQL As New SQL
    Dim CHAT As cMessenger.cMessenger_Chat = Nothing
    Dim loaded = False
    Public chat_id As Integer = -1

    Sub New()

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

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

    End Sub


    Sub New(CHAT As cMessenger.cMessenger_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 initDGV()
        If Not loaded Then Exit Sub
        With dgvMitarbeiter
            Dim FirmaTMP = MyComboBox1._value
            Dim sqlStr = "SELECT TOP (1000) [mit_id],[mit_nname] + ' ' + [mit_vname] + ' (' + mit_niederlassung + ')' as MAname,CASE WHEN mit_firma='ALL' THEN mit_firmaHaupt ELSE mit_firma END as mit_firma  FROM [tblMitarbeiter]"
            sqlStr &= " WHERE  mit_gekuendigt=0 AND /*mit_abteilung IN ('QS','ZOLL','DISPO') AND*/ [mit_TESTonly]=0 AND [mit_pseudoUser]=0 AND [mit_ChatBenutzer]=1 "
            If MyComboBox1._value <> "" Then sqlStr &= " AND  mit_firma IN ('" & FirmaTMP & "','BEIDE','ALLE','ALL')"
            If MyTextBox1.Text <> "" Then sqlStr &= " AND (mit_nname LIKE '" & MyTextBox1.Text & "%' OR mit_vname LIKE '" & MyTextBox1.Text & "%' )"

            sqlStr &= "    order by mit_nname,mit_vname"

            .DataSource = SQL.loadDgvBySql(sqlStr, "ADMIN")

            If .Columns.Count = 0 Then Exit Sub

            .Columns("mit_id").Visible = False
            .AutoSizeColumnsMode = Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells
        End With
    End Sub

    Private Sub frmMessenger_NeuerChat_Load(sender As Object, e As EventArgs) Handles Me.Load
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG", "VERAG"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("ATILLA", "ATILLA"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IMEX", "IMEX"))
        MyComboBox1.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("UNISPED", "UNISPED"))
        MyComboBox1.changeItem("")
        loaded = True
        initDGV()
        If CHAT IsNot Nothing Then
            Me.Text = "Neue Benutzer hinzufügen"
            Me.MyTextBox2.Text = CHAT.chat_name
            Me.MyTextBox2.Enabled = False
            btn.Text = "Hinzufügen"
        End If
    End Sub

    Private Sub MyTextBox1_TextChanged(sender As Object, e As EventArgs) Handles MyTextBox1.TextChanged
        initDGV()
    End Sub

    Private Sub MyTextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyTextBox1.KeyDown
        If dgvMitarbeiter.Rows.Count = 0 Then Exit Sub

        If e.KeyCode = Keys.Down Then
            If dgvMitarbeiter.SelectedRows.Count = 0 Then dgvMitarbeiter.Rows(0).Selected = True
            If dgvMitarbeiter.Rows.Count > dgvMitarbeiter.SelectedRows(0).Index + 1 Then
                Dim newIndex = dgvMitarbeiter.SelectedRows(0).Index + 1
                dgvMitarbeiter.ClearSelection()
                dgvMitarbeiter.Rows(newIndex).Selected = True
                MyTextBox1.Focus()
            End If
        End If

        If e.KeyCode = Keys.Up Then
            If dgvMitarbeiter.SelectedRows.Count = 0 Then dgvMitarbeiter.Rows(0).Selected = True
            If dgvMitarbeiter.SelectedRows(0).Index > 0 Then
                Dim newIndex = dgvMitarbeiter.SelectedRows(0).Index - 1
                dgvMitarbeiter.ClearSelection()
                dgvMitarbeiter.Rows(newIndex).Selected = True
                MyTextBox1.Focus()
            End If
        End If
        If e.KeyCode = Keys.Return Then
            addSelMA()
        End If
    End Sub

    Sub addSelMA()
        If dgvMitarbeiter.SelectedRows.Count > 0 Then
            dgvChatMembers.Rows.Add(dgvMitarbeiter.SelectedRows(0).Cells("mit_id").Value, dgvMitarbeiter.SelectedRows(0).Cells("MAname").Value, dgvMitarbeiter.SelectedRows(0).Cells("mit_firma").Value)
            initAddRemoveDGV()
            MyTextBox1.Text = ""
            MyTextBox1.Focus()
        End If
    End Sub

    Sub initAddRemoveDGV()
        If dgvChatMembers.Rows.Count = 1 Then
            MyTextBox2.Text = dgvChatMembers.Rows(0).Cells("clmnName").Value 'dgvMitarbeiter.SelectedRows(0).Cells("MAname").Value
            MyTextBox2.Enabled = False
        Else
            MyTextBox2.Text = ""
            MyTextBox2.Enabled = True
        End If
    End Sub


    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        If dgvChatMembers.Rows.Count = 0 Then
            MsgBox("Mitglieder eingeben!")
            Exit Sub
        End If

        If CHAT IsNot Nothing Then

            For Each r As DataGridViewRow In dgvChatMembers.Rows
                Dim MB As New VERAG_PROG_ALLGEMEIN.cMessenger.cMessenger_ChatMembers(CHAT.chat_id, r.Cells("clmnMaId").Value)
                MB.chatMb_maId = r.Cells("clmnMaId").Value
                CHAT.CHAT_MEMBERS.Add(MB)
            Next

            If CHAT.SAVE_MEMBERS Then
                Me.Close()
            End If

        Else

            If MyTextBox2.Enabled And MyTextBox2.Text = "" Then
                MsgBox("Chat-Name eingeben!")
                Exit Sub
            End If

            Dim chatName = If(MyTextBox2.Enabled, MyTextBox2.Text, "")

            Dim MB_LIST As New List(Of Integer)
            For Each r As DataGridViewRow In dgvChatMembers.Rows
                MB_LIST.Add(r.Cells("clmnMaId").Value)
            Next
            If chatName = "" And MB_LIST.Count = 1 Then
                If VERAG_PROG_ALLGEMEIN.cMessenger.CHECK_EXISTS(MB_LIST(0), chat_id) Then
                    If vbYes <> MsgBox("Ein Chat mit diesem Mitarbetier existiert bereits. Möchten Sie wirklich einen neuen öffnen?", vbYesNoCancel) Then
                        Me.DialogResult = DialogResult.OK
                        Me.Close()
                        Exit Sub
                    End If
                End If
            End If
            MB_LIST.Add(VERAG_PROG_ALLGEMEIN.cAllgemein.USRID) 'Selbst!

            Dim CHAT As cMessenger.cMessenger_Chat = Nothing
            If VERAG_PROG_ALLGEMEIN.cMessenger.GEN_NEW_CHAT(chatName, MB_LIST, CHAT) Then
                If CHAT IsNot Nothing Then chat_id = CHAT.chat_id
                Me.Close()
            End If
        End If

    End Sub

    Private Sub MyComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox1.SelectedIndexChanged
        initDGV()
    End Sub

    Private Sub dgvMitarbeiter_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMitarbeiter.CellDoubleClick
        addSelMA()
    End Sub


    Private Sub dgvChatMembers_RowsRemoved(sender As Object, e As DataGridViewRowsRemovedEventArgs) Handles dgvChatMembers.RowsRemoved
        initAddRemoveDGV()
    End Sub
End Class