Public Class frmDienstplan

    Dim DP_ART = "SUB"
    Dim datum As Date = Now
    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

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

        Me.Height = Screen.FromRectangle(Me.Bounds).WorkingArea.Size.Height
        Me.Top = 0

        MyComboBox1.fillWithSQL("SELECT  [dpset_niederlassung],dpset_niederlassung as nl FROM [ADMIN].[dbo].[tblDienstSettings] order by dpset_niederlassung", False, "ADMIN", False)
        If VERAG_PROG_ALLGEMEIN.cAllgemein.FIRMA = "ATILLA" Then
            MyComboBox1.changeItem("ATILLA")
        Else
            MyComboBox1.changeItem(VERAG_PROG_ALLGEMEIN.cAllgemein.ABTEILUNG)
        End If
        If MyComboBox1._value = "" Then MyComboBox1.changeItem("SUB")

        initDP()
    End Sub


    Sub initDP()
        If DP_ART = "" Then Exit Sub
        Dim NAHeight = 150
        pnlDP.Controls.Clear()

        Dim dt As DataTable = SQL.loadDgvBySql("SELECT *  FROM [ADMIN].[dbo].[tblDienstplanEintraegeDetails]  where dedet_niederlassung='" & DP_ART & "'  and dedet_benutzerdefinierteSchicht=1  and [dedet_aktiv]=1 order by dedet_reihenfolge", "ADMIN")

        For Each r In dt.Rows
            Dim ustCntl As New usrCntlDienstWoche
            ustCntl.lblBezeichnung.Text = r("dedet_info")
            ustCntl.Height = (pnlDP.Height - NAHeight) * (r("dedet_ProzentGrafik") / 100)
            If r("dedet_bgAlternativeFarbe") IsNot DBNull.Value AndAlso r("dedet_bgAlternativeFarbe") <> "" Then
                ustCntl.Panel3.BackColor = ColorTranslator.FromHtml(r("dedet_bgAlternativeFarbe"))
            Else
                '    ustCntl.Panel3.BackColor = Color.Firebrick
            End If

            With ustCntl.dgv_Mo
                Dim sqlSchicht = ""

                sqlSchicht &= " SELECT mit_nname + ' ' + mit_vname as MA ,dstetr_von, dstetr_bis,dstetr_info,'DW: ' + isnull(mit_durchwahl,'') as dw,isnull(mit_email,'') as email"
                sqlSchicht &= " FROM [ADMIN].[dbo].[tblDienstplanEintraege] inner join tblDienstMitarb on dstma_id=dstetr_dstmaId"
                sqlSchicht &= "  inner join tblMitarbeiter on dstma_mitid=mit_id "
                sqlSchicht &= " where dstetr_datum='" & datum.ToShortDateString & "' and dstetr_niederlassung='" & DP_ART & "'  and dstetr_art='" & r("dedet_bezeichnungDP") & "' order by dstetr_von, dstma_reihenfolge"

                .DataSource = SQL.loadDgvBySql(sqlSchicht, "ADMIN")
                '    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
                .Columns("MA").Width = 200
                .Columns("dstetr_von").Width = 80
                .Columns("dstetr_bis").Width = 80
                .Columns("dstetr_info").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("dw").Width = 150
                .Columns("email").Width = 150
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .ClearSelection()
                AddHandler ustCntl.dgv_Mo.SelectionChanged, Sub()
                                                                ustCntl.dgv_Mo.ClearSelection()
                                                            End Sub

            End With
            ustCntl.Width = pnlDP.Width - 25
            pnlDP.Controls.Add(ustCntl)
            ustCntl.initAnzahl()
        Next


        Dim ustCntlNA As New usrCntlDienstWoche
        ustCntlNA.lblBezeichnung.Text = "NA"
        ustCntlNA.Height = NAHeight 'pnlDP.Height * (r("dedet_ProzentGrafik") / 100) '- 10
        ustCntlNA.Panel3.BackColor = Color.Gray

        checkAnwesend()
        With ustCntlNA.dgv_Mo
            Dim sqlSchicht = ""

            sqlSchicht &= " SELECT mit_nname + ' ' + mit_vname as MA ,dstetr_von, dstetr_bis,dstetr_info,'DW: ' + isnull(mit_durchwahl,'') as dw,isnull(mit_email,'') as email"
            sqlSchicht &= " FROM [ADMIN].[dbo].[tblDienstplanEintraege] inner join tblDienstMitarb on dstma_id=dstetr_dstmaId"
            sqlSchicht &= "  inner join tblMitarbeiter on dstma_mitid=mit_id "
            sqlSchicht &= " where dstetr_datum='" & datum.ToShortDateString & "' and dstetr_niederlassung='" & DP_ART & "'  and dstetr_art IN('URL','SO', 'DR','BS','ZA','FREI') order by dstetr_von, dstma_reihenfolge"

            .DataSource = SQL.loadDgvBySql(sqlSchicht, "ADMIN")
            '    .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .Columns("MA").Width = 200
            .Columns("dstetr_von").Width = 80
            .Columns("dstetr_bis").Width = 80
            .Columns("dstetr_info").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("dw").Width = 150
            .Columns("email").Width = 150
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ClearSelection()
            AddHandler ustCntlNA.dgv_Mo.SelectionChanged, Sub()
                                                              ustCntlNA.dgv_Mo.ClearSelection()
                                                          End Sub
            .DefaultCellStyle.ForeColor = Color.Gray
            .DefaultCellStyle.Font = New Font(.Font.FontFamily, .Font.Size, FontStyle.Regular)

        End With
        ustCntlNA.Width = pnlDP.Width - 25
        pnlDP.Controls.Add(ustCntlNA)
        ustCntlNA.initAnzahl()


    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
        lbl0000.Text = Now.ToString("HH:mm:ss")
       
    End Sub

    Private Sub TimerDGV_Tick(sender As Object, e As EventArgs) Handles TimerDGV.Tick
        checkAnwesend()
    End Sub
    Sub checkAnwesend()
        If datum.ToShortDateString = Now.ToShortDateString Then
            For Each c In pnlDP.Controls
                '  If c.GetType.ToString = GetType(DataGridView).ToString Then
                Dim u As usrCntlDienstWoche = c
                For Each r As DataGridViewRow In u.dgv_Mo.Rows
                    If r.Cells("dstetr_von") IsNot Nothing Then
                        If r.Cells("dstetr_von").Value < Now.ToString("HH:mm:ss") And r.Cells("dstetr_bis").Value > Now.ToString("HH:mm:ss") Then
                            r.DefaultCellStyle.ForeColor = Color.Black
                            r.DefaultCellStyle.Font = New Font(u.dgv_Mo.Font.FontFamily, u.dgv_Mo.Font.Size, FontStyle.Bold)
                        Else
                            r.DefaultCellStyle.ForeColor = Color.Gray
                            r.DefaultCellStyle.Font = New Font(u.dgv_Mo.Font.FontFamily, u.dgv_Mo.Font.Size, FontStyle.Regular)
                        End If
                    End If
                Next
                ' End If
            Next
        End If
    End Sub

    Private Sub frmDienstplan_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        checkAnwesend()
        initBtns()
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        datum = datum.AddDays(-1)
        initBtns()
    End Sub

    Sub initBtns()
        If datum.ToShortDateString = Now.ToShortDateString Then
            Button5.Text = "Heute"
        ElseIf datum.ToShortDateString = Now.AddDays(-1).ToShortDateString Then
            Button5.Text = "Gestern"
        ElseIf datum.ToShortDateString = Now.AddDays(1).ToShortDateString Then
            Button5.Text = "Morgen"
        Else
            Button5.Text = datum.ToString("ddd, dd.MM")
        End If

        lblMO.BackColor = Color.White : lblMO.ForeColor = Color.Black
        lblDI.BackColor = Color.White : lblDI.ForeColor = Color.Black
        lblMI.BackColor = Color.White : lblMI.ForeColor = Color.Black
        lblDO.BackColor = Color.White : lblDO.ForeColor = Color.Black
        LBLFR.BackColor = Color.White : LBLFR.ForeColor = Color.Black
        lblSA.BackColor = Color.White : lblSA.ForeColor = Color.Black
        lblSO.BackColor = Color.White : lblSO.ForeColor = Color.Black

        If datum.ToString("ddd").ToUpper = "MO" Then lblMO.BackColor = Color.FromArgb(0, 54, 128) : lblMO.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "DI" Then lblDI.BackColor = Color.FromArgb(0, 54, 128) : lblDI.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "MI" Then lblMI.BackColor = Color.FromArgb(0, 54, 128) : lblMI.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "DO" Then lblDO.BackColor = Color.FromArgb(0, 54, 128) : lblDO.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "FR" Then LBLFR.BackColor = Color.FromArgb(0, 54, 128) : LBLFR.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "SA" Then lblSA.BackColor = Color.FromArgb(0, 54, 128) : lblSA.ForeColor = Color.White
        If datum.ToString("ddd").ToUpper = "SO" Then lblSO.BackColor = Color.FromArgb(0, 54, 128) : lblSO.ForeColor = Color.White
        initDP()
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        datum = datum.AddDays(1)
        initBtns()
    End Sub

    Private Sub MyComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MyComboBox1.SelectedIndexChanged
        DP_ART = MyComboBox1._value
        initDP()
    End Sub

    Private Sub Label6_Click(sender As Object, e As EventArgs) Handles lblSA.Click

    End Sub

    Private Sub lblMO_Click(sender As Object, e As EventArgs) Handles lblMO.Click, lblDI.Click, lblMI.Click, lblDO.Click, LBLFR.Click, lblSA.Click, lblSO.Click
        Dim dtmp As Date = datum
        Do 'zurück zum montag
            If dtmp.ToString("ddd").ToUpper = "MO" Then
                Exit Do
            End If
            dtmp = dtmp.AddDays(-1)
        Loop
        Do
            If dtmp.ToString("ddd").ToUpper = sender.text Then
                datum = dtmp
                initBtns()
                Exit Sub
            End If
            dtmp = dtmp.AddDays(1)
        Loop

    End Sub

    Private Sub frmDienstplan_SizeChanged(sender As Object, e As EventArgs) Handles Me.SizeChanged
        initDP()
    End Sub
End Class