Imports GrapeCity.ActiveReports.Chart.Graphics

Public Class frmUrlKW
    Public KW As Integer
    Public YEAR As Integer
    Public ART As String
    Public KW_ID As Integer = -1

    Public RESULT As String = ""

    Public niederlassung As String = ""
    Public maId As String = ""
    Dim SQLDienst As New cDienstplan
    Dim montag As Date
    Dim loaded = False
    Dim ftString As String = " FT"
    Dim DPAlreadyInit As Boolean = False

    Dim SQL As New VERAG_PROG_ALLGEMEIN.SQL

    Private Sub btnSpeichern_Click(sender As Object, e As EventArgs) Handles btnSpeichern.Click
        ' SQLDienst.delDienstNichtAnwesendKW_ByKwJahr(maId, KW, YEAR)
        ' SQLDienst.delDienstNichtAnwesendIdArtDatum(maId, montag, montag.AddDays(6))
        If KW_ID > 0 Then
            SQLDienst.delDienstNichtAnwesendKW_ByKW_ID(KW_ID)
            SQLDienst.delDienstNichtAnwesend_ByKW_ID(KW_ID)
        Else
            ' SQLDienst.delDienstNichtAnwesendKW_ByKwJahr(maId, KW, YEAR)
            ' SQLDienst.delDienstNichtAnwesendIdArtDatum(maId, montag, montag.AddDays(6))

        End If

        Dim id = SQLDienst.insertDienstNA_KW(maId, KW, YEAR, DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, txtInfo.Text, niederlassung)

        '  Dim ma As cDienstMA = SQLDienst.getDstMA(maId)
        ' Dim wochenstunden = ma.dstma_wochenStunden

        If cbxMo.Checked Then SQLDienst.insertDienstNA(maId, montag, montag, DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxDi.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(1), montag.AddDays(1), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxMi.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(2), montag.AddDays(2), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxDo.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(3), montag.AddDays(3), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxFr.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(4), montag.AddDays(4), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxSa.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(5), montag.AddDays(5), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)
        If cbxSo.Checked Then SQLDienst.insertDienstNA(maId, montag.AddDays(6), montag.AddDays(6), DirectCast(cboArt.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value, "", niederlassung, id)

        RESULT = "OK"

        Me.Close()

    End Sub


    Sub fillCboMA()
        MyComboBox1.fillWithSQL("SELECT dstma_id,dstma_kuerzel FROM tblDienstMitarb WHERE dstma_niederlassung = '" & niederlassung & "' ORDER BY dstma_kuerzel ASC", False, "ADMIN")
        ' Dim ListMA = SQLDienst.getAllDienstMA(niederlassung)

    End Sub

    Private Sub frmUrlKW_Load(sender As Object, e As EventArgs) Handles Me.Load
        cboArt.Items.Clear()
        cboArt.Items.Add((New VERAG_PROG_ALLGEMEIN.MyListItem("Urlaub", "URL")))
        cboArt.Items.Add((New VERAG_PROG_ALLGEMEIN.MyListItem("Krankenstand", "KS")))
        cboArt.Items.Add((New VERAG_PROG_ALLGEMEIN.MyListItem("Dienstreise", "DR")))
        cboArt.Items.Add((New VERAG_PROG_ALLGEMEIN.MyListItem("Berufsschule", "BS")))
        cboArt.Items.Add((New VERAG_PROG_ALLGEMEIN.MyListItem("Zeitausgleich", "ZA")))
        cboArt.SelectedIndex = 0




    End Sub

    Private Sub frmUrlKW_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        init()
        loaded = True
    End Sub

    Sub init()



        lblKW.Text = "KW: " & KW
        fillCboMA()
        For Each i In MyComboBox1.Items
            If DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value = maId Then MyComboBox1.SelectedItem = i
        Next

        Dim FT As New VERAG_PROG_ALLGEMEIN.cFeiertage(YEAR)
        Dim ftString As String = " FT"

        montag = CalendarWeek(KW, YEAR)
        lblMo.Text = montag.ToString("dd.MM.")
        lblDi.Text = montag.AddDays(1).ToString("dd.MM.")
        lblMi.Text = montag.AddDays(2).ToString("dd.MM.")
        lblDo.Text = montag.AddDays(3).ToString("dd.MM.")
        lblFr.Text = montag.AddDays(4).ToString("dd.MM.")
        lblSa.Text = montag.AddDays(5).ToString("dd.MM.")
        lblSo.Text = montag.AddDays(6).ToString("dd.MM.")

        DPAlreadyInit = checkIfDPEntryIsExisting()

        If (FT.isFeiertag(montag, cDienstSettings.getLand(niederlassung))) Then
            lblMo.TextAlign = ContentAlignment.TopLeft
            lblMo.Text &= ftString
            lblMo.ForeColor = Color.Red
        Else
            lblMo.TextAlign = ContentAlignment.TopCenter
            lblMo.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(1), cDienstSettings.getLand(niederlassung))) Then
            lblDi.TextAlign = ContentAlignment.TopLeft
            lblDi.Text &= ftString
            lblDi.ForeColor = Color.Red
        Else
            lblDi.TextAlign = ContentAlignment.TopCenter
            lblDi.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(2), cDienstSettings.getLand(niederlassung))) Then
            lblMi.TextAlign = ContentAlignment.TopLeft
            lblMi.Text &= ftString
            lblMi.ForeColor = Color.Red
        Else
            lblMi.TextAlign = ContentAlignment.TopCenter
            lblMi.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(3), cDienstSettings.getLand(niederlassung))) Then
            lblDo.TextAlign = ContentAlignment.TopLeft
            lblDo.Text &= ftString
            lblDo.ForeColor = Color.Red
        Else
            lblDo.TextAlign = ContentAlignment.TopCenter
            lblDo.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(4), cDienstSettings.getLand(niederlassung))) Then
            lblFr.TextAlign = ContentAlignment.TopLeft
            lblFr.Text &= ftString
            lblFr.ForeColor = Color.Red
        Else
            lblFr.TextAlign = ContentAlignment.TopCenter
            lblFr.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(5), cDienstSettings.getLand(niederlassung))) Then
            lblSa.TextAlign = ContentAlignment.TopLeft
            lblSa.Text &= ftString
            lblSa.ForeColor = Color.Red
        Else
            lblSa.TextAlign = ContentAlignment.TopCenter
            lblSa.ForeColor = Color.Black
        End If

        If (FT.isFeiertag(montag.AddDays(6), cDienstSettings.getLand(niederlassung))) Then
            lblSo.TextAlign = ContentAlignment.TopLeft
            lblSo.Text &= ftString
            lblSo.ForeColor = Color.Red
        Else
            lblSo.TextAlign = ContentAlignment.TopCenter
            lblSo.ForeColor = Color.Black
        End If


        cbxMo.Checked = False
        cbxDi.Checked = False
        cbxMi.Checked = False
        cbxDo.Checked = False
        cbxFr.Checked = False
        cbxSa.Checked = False
        cbxSo.Checked = False



        Dim listKW As cDienstAbwesendheitenKW = SQLDienst.getDienstAbwesendheitenKWByID(KW_ID)
        If listKW IsNot Nothing Then


            For Each i In cboArt.Items
                '    MsgBox(DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value)
                If DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value.Trim = listKW.dstnk_grund.Trim Then cboArt.SelectedItem = i
            Next

            txtInfo.Text = IIf(listKW.dstnk_info Is DBNull.Value, "", listKW.dstnk_info)

            '  Dim ll = Nothing


            '  If KW_ID > 0 Then
            'll = SQLDienst.getDienstAbwesendheitenByKWID(KW_ID)
            '  Else
            '  ll = SQLDienst.getDienstAbwesendheitenByID(montag, montag.AddDays(6), niederlassung, ART, maId)
            ' End If

            For Each l As cDienstAbwesendheiten In SQLDienst.getDienstAbwesendheitenByKWID(KW_ID)

                If l.dstna_datum_von = montag.ToShortDateString Then cbxMo.Checked = True
                If l.dstna_datum_von = montag.AddDays(1).ToShortDateString Then cbxDi.Checked = True
                If l.dstna_datum_von = montag.AddDays(2).ToShortDateString Then cbxMi.Checked = True
                If l.dstna_datum_von = montag.AddDays(3).ToShortDateString Then cbxDo.Checked = True
                If l.dstna_datum_von = montag.AddDays(4).ToShortDateString Then cbxFr.Checked = True
                If l.dstna_datum_von = montag.AddDays(5).ToShortDateString Then cbxSa.Checked = True
                If l.dstna_datum_von = montag.AddDays(6).ToShortDateString Then cbxSo.Checked = True

            Next

        Else
            CheckBox1.Checked = True
            txtInfo.Text = cboArt._value
            If ART <> "" Then
                For Each i In cboArt.Items
                    If DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value = ART Then cboArt.SelectedItem = i
                Next

            End If
        End If


        If DPAlreadyInit Then
            lblUrlaubEintragen.Text = cboArt.SelectedItem.ToString & " eintragen - DP wurde bereits init."
            lblUrlaubEintragen.ForeColor = Color.Red
        Else
            lblUrlaubEintragen.Text = cboArt.SelectedItem.ToString & " eintragen"
            lblUrlaubEintragen.ForeColor = Color.Black
        End If

    End Sub

    Public Function CalendarWeek(ByVal nWeek As Integer, ByVal nYear As Integer) As Date

        ' Wochentag des 4. Januar des Jahres ermitteln
        Dim dStart As New Date(nYear, 1, 4)
        Dim nDay As Integer = (dStart.DayOfWeek + 6) Mod 7 + 1

        ' Beginn der 1. KW des Jahres
        Dim dFirst As Date = dStart.AddDays(1 - nDay)

        ' Gesuchte KW ermitteln
        Return dFirst.AddDays((nWeek - 1) * 7)
    End Function


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
    Public Function WeekCount(ByVal nYear As Integer) As Integer
        ' Ermittelt die Anzahl Wochen in einem Jahr
        Return CalendarWeek(1, nYear + 1).Subtract(CalendarWeek(1, nYear)).Days \ 7
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If KW < WeekCount(YEAR) Then KW += 1 : init()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If KW > 1 Then KW -= 1 : init()
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged

        cbxMo.Checked = IIf(lblMo.Text.Substring(lblMo.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxDi.Checked = IIf(lblDi.Text.Substring(lblDi.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxMi.Checked = IIf(lblMi.Text.Substring(lblMi.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxDo.Checked = IIf(lblDo.Text.Substring(lblDo.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxFr.Checked = IIf(lblFr.Text.Substring(lblFr.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxSa.Checked = IIf(lblSa.Text.Substring(lblSa.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
        cbxSo.Checked = IIf(lblSo.Text.Substring(lblSo.Text.Length - ftString.Length) = ftString AndAlso Not cboArt._value = "DR", False, sender.checked)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        SQLDienst.delDienstNichtAnwesendKW_ByKW_ID(KW_ID)
        SQLDienst.delDienstNichtAnwesend_ByKW_ID(KW_ID)
        RESULT = "DEL"
        Me.Close()
    End Sub

    Private Sub cboArt_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboArt.SelectedIndexChanged
        If loaded Then
            txtInfo.Text = cboArt._value
            CheckBox1_CheckedChanged(CheckBox1, e)
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        KW_ID = -1
        init()
    End Sub

    Private Function checkIfDPEntryIsExisting()

        If SQL.DLookup("count([dstetr_id])", "[tblDienstplanEintraege]", " [dstetr_niederlassung]='" & niederlassung & "' and [dstetr_dstmaId]='" & maId & "' and [dstetr_datum] BETWEEN'" & montag.ToShortDateString & "' AND '" & montag.AddDays(6).ToShortDateString & "'", "ADMIN", 0) > 0 Then
            Return True
        Else
            Return False
        End If



    End Function
End Class