Public Class usrcntlSessions

    Dim mitarbeiter As VERAG_PROG_ALLGEMEIN.cMitarbeiter


    Public Sub New()

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

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

    End Sub

    Public Sub New(_mitarbeiter As VERAG_PROG_ALLGEMEIN.cMitarbeiter)

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

        Me.mitarbeiter = _mitarbeiter
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub

    Private Sub Button6_Click1(sender As Object, e As EventArgs) Handles btnReduceDay.Click
        srcTAG = srcTAG.AddDays(-1)
        initBtns("Tag")
    End Sub

    Private Sub Button7_Click1(sender As Object, e As EventArgs) Handles btnAddDay.Click
        srcTAG = srcTAG.AddDays(1)
        initBtns("Tag")
    End Sub

    Private Sub Button5_Click1(sender As Object, e As EventArgs) Handles btnToday.Click
        initBtns("Tag")
    End Sub

    Private Sub btnJahr_Click(sender As Object, e As EventArgs) Handles btnJahr.Click
        initBtns("Jahr")
    End Sub

    Private Sub btnBisMonat_Click(sender As Object, e As EventArgs) Handles btnBisMonat.Click
        MONAT = MONAT.AddMonths(1)
        initBtns("Monat")
    End Sub

    Private Sub btnVonMonat_Click(sender As Object, e As EventArgs) Handles btnVonMonat.Click
        MONAT = MONAT.AddMonths(-1)
        initBtns("Monat")
    End Sub

    Private Sub btnBisJahr_Click(sender As Object, e As EventArgs) Handles btnBisJahr.Click
        JAHR = JAHR.AddYears(1)
        initBtns("Jahr")
    End Sub

    Private Sub btnVonJahr_Click(sender As Object, e As EventArgs) Handles btnVonJahr.Click
        JAHR = JAHR.AddYears(-1)
        initBtns("Jahr")
    End Sub

    Private Sub btnMonat_Click(sender As Object, e As EventArgs) Handles btnMonat.Click
        initBtns("Monat")
    End Sub

    Dim MONAT As Date = CDate("01." & Now.Month & "." & Now.Year) '.AddMonths(-1)
    Dim srcTAG As Date = CDate(Now.ToShortDateString)

    Dim JAHR As Date = CDate("01.01." & Now.Year)
    Sub initBtns(MonatJahr)
        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year
        Select Case MonatJahr
            Case "Monat"
                DateTimePicker1.Value = MONAT
                DateTimePicker2.Value = MONAT.AddMonths(1).AddDays(-1)
            Case "Jahr"
                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
                'End If
            Case "Tag"
                DateTimePicker1.Value = srcTAG
                DateTimePicker2.Value = srcTAG
                If srcTAG = Now.ToShortDateString Then
                    btnToday.Text = "Heute"
                ElseIf srcTAG = Now.AddDays(-1).ToShortDateString Then
                    btnToday.Text = "Gestern"
                ElseIf srcTAG = Now.AddDays(-2).ToShortDateString Then
                    btnToday.Text = "Vorgestern"
                Else
                    btnToday.Text = srcTAG.ToString("dd.MM")
                End If

                'End If
            Case ""
                DateTimePicker1.Value = ("01." & Now.Month & "." & Now.Year)
                DateTimePicker2.Value = Now
        End Select


        txtSessDatVon.Text = DateTimePicker1.Value
        txtSessDatBis.Text = DateTimePicker2.Value

        initdgvSessions(CDate(DateTimePicker1.Value).Date, CDate(DateTimePicker2.Value).Date)

    End Sub

    Private Sub initdgvSessions(von As Date, bis As Date)

        Dim SQL_String = ""

        Dim SQL_SESSIONS = "select 'AVISO/SDL' as System,[sess_niederlassung],[sess_ipAddress],[sess_PCName],[sess_PCUserName],[sess_login],[sess_logout]
                            FROM [ADMIN].[dbo].[tblSessions] 
                            where cast(sess_login as Date) between '" & von.ToShortDateString & "' and '" & bis.ToShortDateString & "' and sess_maId = " & mitarbeiter.mit_id


        If mitarbeiter.mit_AliasAD_Username IsNot Nothing AndAlso mitarbeiter.mit_AliasAD_Username <> "" AndAlso mitarbeiter.mit_AliasAD_Domain IsNot Nothing AndAlso mitarbeiter.mit_AliasAD_Domain <> "" Then

            Dim whereLogin As String = "([BenutzerAnmeldeName] = '" & mitarbeiter.mit_AliasAD_Domain & "\" & mitarbeiter.mit_AliasAD_Username & "'"

            If mitarbeiter.mit_AliasAD_Username2 IsNot Nothing AndAlso mitarbeiter.mit_AliasAD_Username2 <> "" AndAlso mitarbeiter.mit_AliasAD_Domain2 IsNot Nothing AndAlso mitarbeiter.mit_AliasAD_Domain2 <> "" Then
                whereLogin &= " OR [BenutzerAnmeldeName] = '" & mitarbeiter.mit_AliasAD_Domain2 & "\" & mitarbeiter.mit_AliasAD_Username2 & "')"
            Else
                whereLogin &= ")"
            End If


            Dim SQL_SESSION_TS = "SELECT  'TS/PC','-','-',[TSServer],[BenutzerAnmeldeName],[LogOn_datetime],[LogOff_datetime]
                              FROM [Doku].[dbo].[TbL_LogOnLogOffLog] 
                              where " & whereLogin & " and cast([LogOn_datetime] as Date)  between '" & von.ToShortDateString & "' and '" & bis.ToShortDateString & "'"

            SQL_String = " select * FROM ( " & SQL_SESSIONS & " UNION ALL " & SQL_SESSION_TS & ")  TEMP order by TEMP.sess_login"
            Else
                SQL_String = " select * FROM ( " & SQL_SESSIONS & ")  TEMP order by TEMP.sess_login"
        End If


        dgvSessions.SET_SQL(SQL_String, "ADMIN")
        dgvSessions.LOAD()

        With dgvSessions

            .Columns("System").Width = 70
            .Columns("sess_niederlassung").Width = 50
            .Columns("sess_niederlassung").HeaderText = "NL"
            .Columns("sess_ipAddress").Width = 100
            .Columns("sess_ipAddress").HeaderText = "IP-Adr"
            .Columns("sess_PCName").Width = 70
            .Columns("sess_PCName").HeaderText = "PC"
            .Columns("sess_PCUserName").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("sess_PCUserName").HeaderText = "Username"
            .Columns("sess_login").Width = 100
            .Columns("sess_login").HeaderText = "Login"
            .Columns("sess_logout").Width = 100
            .Columns("sess_logout").HeaderText = "Logout"

            If dgvSessions.Rows.Count > 0 Then

                Dim changeColor As Boolean = True

                For i As Integer = 0 To .Rows.Count - 1

                    .Rows(i).DefaultCellStyle.BackColor = If(changeColor, Color.LightBlue, Color.White)

                    If i < .Rows.Count - 1 Then

                        Dim currentDate As Date = CDate(.Rows(i).Cells("sess_login").Value).Date
                        Dim nextDate As Date = CDate(.Rows(i + 1).Cells("sess_login").Value).Date

                        If currentDate <> nextDate Then
                            changeColor = Not changeColor
                        End If

                    End If

                Next

            End If


        End With


    End Sub

    Private Sub usrcntlSessions_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Me.DesignMode Then Exit Sub
        initBtns("Tag")
        initdgvSessions(CDate(DateTimePicker1.Value).Date, CDate(DateTimePicker2.Value).Date)
    End Sub

    Private Sub txtSessDatVon_TextChanged(sender As Object, e As EventArgs) Handles txtSessDatVon.TextChanged
        txtSessDatVon.Enabled = sender.text <> ""
        If IsDate(txtSessDatVon.Text) AndAlso IsDate(txtSessDatBis.Text) Then
            initdgvSessions(CDate(txtSessDatVon.Text).Date, CDate(txtSessDatBis.Text).Date)
        End If
    End Sub

    Private Sub txtSessDatBis_TextChanged(sender As Object, e As EventArgs) Handles txtSessDatBis.TextChanged
        txtSessDatBis.Enabled = sender.text <> ""
        If IsDate(txtSessDatVon.Text) AndAlso IsDate(txtSessDatBis.Text) Then
            initdgvSessions(CDate(txtSessDatVon.Text).Date, CDate(txtSessDatBis.Text).Date)
        End If
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtSessDatVon._value = CDate(sender.value).ToShortDateString
        initdgvSessions(CDate(DateTimePicker1.Value).Date, CDate(DateTimePicker2.Value).Date)
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtSessDatBis._value = CDate(sender.value).ToShortDateString
        initdgvSessions(CDate(DateTimePicker1.Value).Date, CDate(DateTimePicker2.Value).Date)
    End Sub
End Class
