Imports System.ComponentModel

Public Class frmMitarbSuche
    Dim ConnStr As String
    Private ADMIN As New cMitarbeiter
    Private cOptionenDAL As New cOptionenDAL
    Private Grid_aktiv As Boolean = False
    Private Anzahl As Integer
    Private locZeilen As Integer = -1
    Public mid As Integer = -1
    Public searchUse As String = ""

    Private Sub frmMitarbSuche_Load(sender As Object, e As EventArgs) Handles Me.Load
        ConnStr = frmMain.ConnStr

        Try
            '  Me.Size = My.Settings.frmMainSize
            ' Me.Location = My.Settings.frmMainPosition
        Catch ex As Exception
            'nix tun - Standardgröße wird somit automatisch gesetzt
        End Try

        Grid_aktiv = True
        Tabelle_anzeigen()

    End Sub

    Public Sub Tabelle_anzeigen()
        Dim start As Long = (DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds
        'lblMs.Image = My.Resources.sanduhr
        'Paint wieder reaktivieren
        locZeilen = -1
        If Not Grid_aktiv Then Exit Sub
        Dim top As String = "" ' "top 100"

        'je nach Auswahl SQL anpassen
        Dim hSQL = "SELECT mit_id, mit_username, mit_vname, mit_nname, mit_niederlassung, mit_abteilung, mit_gekuendigt,mit_geschlecht FROM tblMitarbeiter "
        Dim search As String = txtSuche.Text
        Dim where As String = ""
        If txtSuche.Text <> "" Then

            Dim arr() As String = Split(txtSuche.Text.Replace(" ", ","), ",")

            Dim cnt As Integer = 1
            If arr.Count() > 0 Then cnt = arr.Count
            Dim or_txt As String = ""
            For i As Integer = 0 To cnt - 1 Step 1
                If arr(i) <> "" Then search = arr(i).Trim
                If i > 0 Then where += " AND "
                where += " ( mit_username LIKE '%" & search & "%' OR  mit_vname LIKE '%" & search & "%' OR  mit_nname LIKE '%" & search & "%' OR  mit_niederlassung LIKE '%" & search & "%' OR  mit_abteilung LIKE '%" & search & "%' ) "
            Next
            where = " WHERE " & where
        End If


        '  MsgBox(hSQL & where)
        dgvMitarb.DataSource = cOptionenDAL.AnzeigeTabelle(hSQL & where)


        'Tabelle Kommt/Geht bereinigen
        If dgvMitarb.RowCount = 0 Then
            dgvMitarb.DataSource = Nothing
        Else
            Spalten_festlegen()
            '  Eintraege_festlegen()
            'AL: Sortierung nach Ausfahrt (Zeit)
            dgvMitarb.Sort(dgvMitarb.Columns(3), ListSortDirection.Ascending)
        End If

        'Anzeige der Anzahl gewählter/aller Datensätze
        If (dgvMitarb.RowCount = 100) Then
            'lblAnzahl.Text = Format(dgvMitarb.RowCount, "#,##0") & " ausgewählt (Die Suche wurde auf 100 Ergebnisse beschränkt.)"
        Else
            ' lblAnzahl.Text = Format(dgvMitarb.RowCount, "#,##0") & " ausgewählt"
        End If
        ' lblAnzahl.Refresh()
        Dim ende As Long = (DateTime.Now - New DateTime(1970, 1, 1)).TotalMilliseconds
        ' lblMs.Text = "Ladezeit: " & (ende - start) & " ms"
        'lblMs.Image = Nothing
    End Sub


    Private Sub Spalten_festlegen()
        With dgvMitarb
            .RowTemplate.Height = 20
            .RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopLeft
            .AllowUserToAddRows = False
            .AllowUserToDeleteRows = False
            .AllowUserToOrderColumns = False
            .AllowUserToResizeColumns = False
            .AllowUserToResizeRows = False
            .RowTemplate.ReadOnly = True
            .RowHeadersVisible = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            If searchUse = "vortragender" Then
                .MultiSelect = False
            End If
            .AllowUserToOrderColumns = True
            
            .Columns(0).Visible = False     'ID nicht anzeigen
            .Columns(6).Visible = False     'ID nicht anzeigen

            .Columns(1).Width = 80
            .Columns(1).HeaderText = "Username"
            .Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

            .Columns(2).Width = 150
            .Columns(2).HeaderText = "Vorname"

            .Columns(3).Width = 150
            .Columns(3).HeaderText = "Nachname"

            .Columns(4).Width = 85
            .Columns(4).HeaderText = "Niederlassung"

            .Columns(5).Width = 85
            .Columns(5).HeaderText = "Abteilung"


        End With
    End Sub


    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        frmMitarbDetails.mid = dgvMitarb.CurrentRow.Cells(0).Value
        frmMitarbDetails.Show()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        frmMitarbDetails.Show()
    End Sub

    Private Sub txtSuche_TextChanged(sender As Object, e As EventArgs) Handles txtSuche.TextChanged
        Tabelle_anzeigen()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click, dgvMitarb.CellDoubleClick
        If searchUse = "addTeilnehmer" Then
            Dim SEMINARE As New cSeminare
            Dim ctrlList As New List(Of Integer)
            For i As Integer = 0 To frmSeminarDetails.dgvTeilnehmer.RowCount - 1 Step 1
                ctrlList.Add(frmSeminarDetails.dgvTeilnehmer.Rows(i).Cells(1).Value)
            Next
            With dgvMitarb
                For i As Integer = 0 To .RowCount - 1 Step 1
                    Dim mit_id As Integer = .Rows(i).Cells(0).Value
                    If .Rows(i).Selected = True And checkList(ctrlList, mit_id) Then
                        SEMINARE.insertSemMitarbeiter(frmSeminarDetails.semId, mit_id)
                    End If
                Next
            End With
            frmSeminarDetails.Tabelle_anzeigen()
        ElseIf searchUse = "vortragender" Then
            frmSeminarDetails.vortragenderId = dgvMitarb.SelectedRows(0).Cells(0).Value
            Dim anrede As String = ""

            '   If dgvMitarb.SelectedRows(0).Cells(7).Value = "m" Then anrede = "Herr "
            ' If dgvMitarb.SelectedRows(0).Cells(7).Value = "w" Then anrede = "Frau "
            frmSeminarDetails.txtVortragender.Text = anrede & dgvMitarb.SelectedRows(0).Cells(2).Value & " " & dgvMitarb.SelectedRows(0).Cells(3).Value
        End If
        Me.Close()
    End Sub

    Private Function checkList(ByVal ctrlList As List(Of Integer), ByVal id As Integer) As Boolean
        For Each i As Integer In ctrlList
            If i = id Then Return False
        Next
        Return True
    End Function
    Private Sub dgvMitarb_CellContentClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMitarb.CellContentClick

    End Sub
End Class