Imports System.ComponentModel

Public Class usrCntlSeminare

    Dim ConnStr As String
    Private SEMINARE As New cSeminare
    Private cOptionenDAL As New cOptionenDAL
    Private Grid_aktiv As Boolean = False
    Private Anzahl As Integer
    Private locZeilen As Integer = -1
    Public semId As Integer = -1

    Private Sub uctrlMitarbeiter_Load(sender As Object, e As EventArgs) Handles MyBase.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()
        If TabControl1.SelectedIndex = 0 Then showDgvSchulungen()
        If TabControl1.SelectedIndex = 1 Then showDgvAusbildungen()
    End Sub
    Public Sub showDgvSchulungen()
        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 sem_id,sem_name,sem_von,sem_bis,sem_dauer,sem_vortragender,sem_ort,sem_isVeragSeminar FROM tblSeminare WHERE sem_type='SCHULUNG' "
        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 += " ( sem_name LIKE '%" & search & "%' OR  sem_vortragender LIKE '%" & search & "%' OR  sem_ort LIKE '%" & search & "%' ) "
            Next
            where = " WHERE " & where
        End If


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


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

        'Anzeige der Anzahl gewählter/aller Datensätze
        If (dgvUID.RowCount = 100) Then
            'lblAnzahl.Text = Format(dgvUID.RowCount, "#,##0") & " ausgewählt (Die Suche wurde auf 100 Ergebnisse beschränkt.)"
        Else
            ' lblAnzahl.Text = Format(dgvUID.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

    Public Sub showDgvAusbildungen()
        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 sem_id,sem_name,sem_von,sem_bis,sem_dauer,sem_vortragender,sem_ort,sem_isVeragSeminar FROM tblSeminare WHERE sem_type='AUSBILDUNG' "
        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 += " ( sem_name LIKE '%" & search & "%' OR  sem_vortragender LIKE '%" & search & "%' OR  sem_ort LIKE '%" & search & "%' ) "
            Next
            where = " WHERE " & where
        End If


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


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

        'Anzeige der Anzahl gewählter/aller Datensätze
        If (dgvAusbildungen.RowCount = 100) Then
            'lblAnzahl.Text = Format(dgvUID.RowCount, "#,##0") & " ausgewählt (Die Suche wurde auf 100 Ergebnisse beschränkt.)"
        Else
            ' lblAnzahl.Text = Format(dgvUID.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 setColumsDgvUID()
        With dgvUID
            .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

            ' .AllowUserToOrderColumns = True

            .Columns(0).Visible = False     'ID nicht anzeigen

            .Columns(1).Width = 240
            .Columns(1).HeaderText = "Seminar"

            .Columns(2).Width = 95
            .Columns(2).HeaderText = "Von"

            .Columns(3).Width = 95
            .Columns(3).HeaderText = "Bis"

            .Columns(4).Width = 85
            .Columns(4).HeaderText = "Dauer"
            .Columns(4).DefaultCellStyle.Format = "HH:mm"

            .Columns(5).Width = 180
            .Columns(5).HeaderText = "Vortragender"

            .Columns(6).Width = 140
            .Columns(6).HeaderText = "Ort"

            .Columns(7).Width = 90
            .Columns(7).HeaderText = "VERAG-Seminar"
            .Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter


        End With
    End Sub
    Private Sub setColumsDgvAusbildungen()
        With dgvAusbildungen
            .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

            ' .AllowUserToOrderColumns = True

            .Columns(0).Visible = False     'ID nicht anzeigen

            .Columns(1).Width = 240
            .Columns(1).HeaderText = "Seminar"

            .Columns(2).Width = 95
            .Columns(2).HeaderText = "Von"

            .Columns(3).Width = 95
            .Columns(3).HeaderText = "Bis"

            .Columns(4).Width = 85
            .Columns(4).HeaderText = "Dauer"
            .Columns(4).DefaultCellStyle.Format = "HH:mm"

            .Columns(5).Width = 180
            .Columns(5).HeaderText = "Vortragender"

            .Columns(6).Width = 140
            .Columns(6).HeaderText = "Ort"

            .Columns(7).Width = 90
            .Columns(7).HeaderText = "VERAG-Seminar"
            .Columns(7).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter


        End With
    End Sub

    Private Sub dgvUID_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvUID.CellDoubleClick
        frmSeminarDetails.semId = dgvUID.CurrentRow.Cells(0).Value
        frmSeminarDetails.Show()
    End Sub

    Private Sub dgvAusbildungen_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvAusbildungen.CellDoubleClick
        frmSeminarDetails.semId = dgvAusbildungen.CurrentRow.Cells(0).Value
        frmSeminarDetails.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
        frmSeminarDetails.Show()
    End Sub

    Private Sub TabControl1_QueryContinueDrag(sender As Object, e As QueryContinueDragEventArgs) Handles TabControl1.QueryContinueDrag
        Tabelle_anzeigen()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        frmSeminarDetails.semId = dgvUID.CurrentRow.Cells(0).Value
        frmSeminarDetails.Show()
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    End Sub

  
End Class
