Imports System.Drawing
Imports System.Windows.Forms
Imports com.sun.javadoc

Public Class frmAuditFlow

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


    Private Sub frmAuditFlow_Load(sender As Object, e As EventArgs) Handles Me.Load
        txtWartungFaelligkeitAbDat._value = Now.ToShortDateString

        txtAbfertDat.Text = Now.AddMonths(-2).ToShortDateString 'CDate("01." & Now.ToString("MM.yyyy")).AddMonths(-1).ToShortDateString
        txtAbfertDatBis.Text = Now.AddMonths(1).ToShortDateString 'Now.ToShortDateString     

        btnMonat.Text = MONAT.ToString("MMMM")
        btnJahr.Text = JAHR.Year

        cboKategorie.Items.Clear()
        cboKategorie.fillWithSQL("SELECT wartK_Kategorie,[wartK_KategorieBezeichnung]  FROM [tblAuditFlow_Kategorien] order by wartK_KategorieBezeichnung", False, "ADMIN", True)


        cboNL.Items.Clear()
        cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        cboNL.SelectedItem = 0

        cboStandort.Items.Clear()
        cboStandort.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        cboStandort.SelectedItem = 0


        cboFirma.Items.Clear()
        cboFirma.fillWithSQL("SELECT distinct(Firma)  FROM  [Filialen] order by Firma", False, "FMZOLL", True)
        cboFirma.changeItem(cAllgemein.FIRMA)


        '        cboSachbearbeiter.fillWithSQL("SELECT [mit_id],[mit_vname] + ' '+ [mit_nname] as Name " &
        '                                      " FROM [ADMIN].[dbo].[tblMitarbeiter] WHERE mit_niederlassung IN ('SUB','Suben')  AND mit_abteilung='ZOLL' AND [mit_gekuendigt]=0   order by name", False, "ADMIN", True)

    End Sub


    Private Sub MyComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboStandort.SelectedIndexChanged, cboKategorie.SelectedIndexChanged
        loadDGVPuefungsobjekte()
    End Sub

    Private Sub cboFirma_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboFirma.SelectedIndexChanged
        cboNL.Items.Clear()
        cboNL.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        If cboFirma._value <> "" Then
            cboNL.fillWithSQL("SELECT  nl_kuerzel,nl_bezeichnung  FROM [tblNiederlassungen] where [nl_firma]='" & cboFirma._value & "' order by nl_bezeichnung", False, "ADMIN", False)
        End If

        loadDGVPuefungsobjekte()
    End Sub

    Private Sub cboNL_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboNL.SelectedIndexChanged
        cboStandort.Items.Clear()
        cboStandort.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))

        If cboFirma._value <> "" AndAlso cboNL._value <> "" Then
            cboStandort.fillWithSQL("SELECT [wartSt_Id],[wartSt_Standort]  FROM [tblAuditFlow_Standorte]  where [wartSt_Aktiv]=1 and [wartSt_Firma]='" & cboFirma._value & "' AND [wartSt_Niederlasung]='" & cboNL._value & "' order by wartSt_Standort", False, "ADMIN", True)
        End If

    End Sub

    '-------------------------------------------------------------------
    '----DGVPuefungsobjekte---------------------------------------------
    '-------------------------------------------------------------------

    Sub loadDGVPuefungsobjekte()
        Try

            Me.Cursor = Cursors.WaitCursor

            Dim TopMax = If(IsNumeric(MyTextBox2.Text), " TOP " & MyTextBox2.Text, "")
            Dim sqlstr As String = ""

            '  MsgBox(VERAG_PROG_ALLGEMEIN.cAllgemein.ERR_OP_GLOBAL)
            sqlstr &= " SELECT " & TopMax & " [wartPO_Id],[wartPO_Bezeichnung],[wartPO_Kennung],[wartPO_Firma],[wartPO_Niederlasung],wartSt_Standort as wartPO_Standort,[wartPO_Kategorie],
                            [wartPO_IntervallTyp],[wartPO_IntervallWert],[wartPO_Erinnerung_VorlaufInTagen] ,[wartPO_zustaendigIntern_Name],[wartPO_Aktiv] 
                        FROM [tblAuditFlow_Pruefobjekte] INNER JOIN tblAuditFlow_Standorte ON wartPO_StandortId=wartSt_Id WHERE 1=1 "

            With dgvPruefungsobjekte
                '  .DataSource = myTable
                '  MsgBox(sqlstr & getWherePruefungsobjekte() & "  order by wartPO_Firma ,wartPO_Niederlasung, wartPO_StandortId")
                .SET_SQL(sqlstr & getWherePruefungsobjekte() & "  order by wartPO_Firma ,wartPO_Niederlasung, wartSt_Standort ", "ADMIN", getListPruefungsobjekte)
                .LOAD()
                setDGVOptionsPruefungsobjekte()
            End With
            showDGVPruefungsobjekte()

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, "")
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Sub showDGVPruefungsobjekte()

        With dgvPruefungsobjekte

            If .ColumnCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False

            .Columns("wartPO_Id").Visible = False


            .Columns("wartPO_Bezeichnung").MinimumWidth = 100
            .Columns("wartPO_Bezeichnung").HeaderText = "Bezeichnung"
            .Columns("wartPO_Bezeichnung").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns("wartPO_Kennung").Width = 60
            .Columns("wartPO_Kennung").HeaderText = "(Kennung)"

            .Columns("wartPO_Kategorie").Width = 100
            .Columns("wartPO_Kategorie").HeaderText = "Kategorie"

            .Columns("wartPO_Firma").Width = 80
            .Columns("wartPO_Firma").HeaderText = "Firma"

            .Columns("wartPO_Niederlasung").Width = 50
            .Columns("wartPO_Niederlasung").HeaderText = "NL"

            .Columns("wartPO_Standort").Width = 100
            .Columns("wartPO_Standort").HeaderText = "Standort"
            .Columns("wartPO_IntervallTyp").Width = 100
            .Columns("wartPO_IntervallTyp").HeaderText = "Intervall"

            .Columns("wartPO_IntervallWert").Width = 60
            .Columns("wartPO_IntervallWert").HeaderText = "Int.-Wert"

            .Columns("wartPO_Erinnerung_VorlaufInTagen").Width = 60
            .Columns("wartPO_Erinnerung_VorlaufInTagen").HeaderText = "Vorl.Tage"

            .Columns("wartPO_zustaendigIntern_Name").Width = 120
            .Columns("wartPO_zustaendigIntern_Name").HeaderText = "Mitarbeiter"

            .Columns("wartPO_Aktiv").Width = 50
            .Columns("wartPO_Aktiv").HeaderText = "Aktiv"



            '  lblErgebnis.Text = "Ergebnise: " & .RowCount
        End With
    End Sub
    Function getWherePruefungsobjekte() As String
        Dim sqlstr = ""


        If cboFirma._value <> "" Then sqlstr &= " AND wartPO_Firma LIKE @wartPO_Firma  "
        If cboNL._value <> "" Then sqlstr &= " AND wartPO_Niederlasung LIKE @wartPO_Niederlasung  "
        If cboStandort._value <> "" Then sqlstr &= " AND wartPO_StandortId LIKE @wartPO_StandortId  "
        If cboKategorie._value <> "" Then sqlstr &= " AND wartPO_Kategorie LIKE @wartPO_Kategorie  "



        Return sqlstr
    End Function
    Function getListPruefungsobjekte() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatVon", txtAbfertDat._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("DatBis", txtAbfertDatBis._value))


        Return list

    End Function


    Sub setDGVOptionsPruefungsobjekte()
        Try
            Exit Sub
            'With dgvPruefungsobjekte
            '    For Each r As DataGridViewRow In .Rows
            '        If r.Cells("sdlAb_Storniert").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Storniert").Value Then
            '            r.DefaultCellStyle.ForeColor = Color.Red
            '            r.DefaultCellStyle.SelectionForeColor = Color.Red
            '            '    r.DefaultCellStyle.SelectionBackColor = Color.White
            '        ElseIf r.Cells("sdlAb_Abgeholt").Value IsNot DBNull.Value AndAlso r.Cells("sdlAb_Abgeholt").Value Then
            '            r.DefaultCellStyle.ForeColor = Color.DarkGreen
            '            r.DefaultCellStyle.SelectionForeColor = Color.DarkGreen
            '            '  r.DefaultCellStyle.SelectionBackColor = Color.White
            '        End If
            '    Next
            'End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub dgvPruefungsobjekte_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPruefungsobjekte.CellDoubleClick
        ' Falls Kopfzeile doppelt geklickt wird -> ignorieren
        If e.RowIndex < 0 Then Exit Sub

        ' ID der ausgewählten Zeile holen (Spalte "wartPO_Id")
        Dim wartPO_Id As Integer = CInt(dgvPruefungsobjekte.Rows(e.RowIndex).Cells("wartPO_Id").Value)

        ' Hier deine Funktion oder Logik aufrufen
        ' Beispiel:
        Dim f As New frmAuditFlow_Pruefungsojekt(wartPO_Id)
        f.ShowDialog(Me)
        dgvPruefungsobjekte.RELOAD()
        setDGVOptionsPruefungsobjekte()
    End Sub

    '-------------------------------------------------------------------



    '-------------------------------------------------------------------
    '-----DGVPrüfungen--------------------------------------------------
    '-------------------------------------------------------------------


    Function getWherePruefungen() As String
        Dim sqlstr = ""

        If txtAbfertDat.Text <> "" Then
            If txtAbfertDatBis.Text <> "" Then
                sqlstr &= " AND ([wartPruef_Faelligkeitsdatum] BETWEEN @DatVon AND @DatBis ) "
            Else
                sqlstr &= " AND ([wartPruef_Faelligkeitsdatum] BETWEEN @DatVon AND @DatVon ) "
            End If
        End If
        If CheckBox1.CheckState = CheckState.Checked Then
            sqlstr &= " AND [wartPruef_Erledigung]=1 "
        ElseIf CheckBox1.CheckState = CheckState.Unchecked Then
            sqlstr &= " AND [wartPruef_Erledigung]=0 "
        Else
            sqlstr &= "  "
        End If


        Return sqlstr
    End Function

    Private Sub dgvPruefungen_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPruefungen.CellDoubleClick
        ' Falls Kopfzeile doppelt geklickt wird -> ignorieren
        If e.RowIndex < 0 Then Exit Sub

        ' ID der ausgewählten Zeile holen (Spalte "wartPO_Id")
        Dim wartPruef_wartPOId As Integer = CInt(dgvPruefungen.Rows(e.RowIndex).Cells("wartPruef_wartPOId").Value)
        Dim wartPruef_Id As Integer = CInt(dgvPruefungen.Rows(e.RowIndex).Cells("wartPruef_Id").Value)

        ' Hier deine Funktion oder Logik aufrufen
        ' Beispiel:
        Dim f As New frmAuditFlow_Pruefungen(wartPruef_wartPOId, wartPruef_Id)
        f.ShowDialog(Me)
        dgvPruefungen.RELOAD()
        setDGVOptionsPruefungen()
    End Sub

    '-------------------------------------------------------------------


    Private Sub btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        loadDGVPuefungen()
    End Sub

    Sub loadDGVPuefungen()

        Try
            With dgvPruefungen

                .Columns.Clear()
                If dgvPruefungsobjekte.SelectedRows.Count = 0 Then
                    Exit Sub
                End If

                Dim wartPO_Id As Integer = CInt(dgvPruefungsobjekte.SelectedRows(0).Cells("wartPO_Id").Value)

                Me.Cursor = Cursors.WaitCursor

                Dim TopMax = If(IsNumeric(MyTextBox2.Text), " TOP " & MyTextBox2.Text, "")
                Dim sqlstr As String = ""

                '  MsgBox(VERAG_PROG_ALLGEMEIN.cAllgemein.ERR_OP_GLOBAL)
                sqlstr &= "SELECT TOP (1000) [wartPruef_Id],wartPruef_wartPOId ,[wartPruef_Faelligkeitsdatum],[wartPruef_Erledigung],[wartPruef_Erledigung_Datum],[wartPruef_Erledigung_BestaetigtMaName]
                        ,wartPruef_Erledigung_Bemerkung,case when [wartPruef_Anhaenge_Count] >0 then 'Ja' else 'Nein' end as [Anhaenge]
                      FROM [tblAuditFlow_Pruefungen] WHERE [wartPruef_wartPOId]='" & wartPO_Id & "'  "


                '  .DataSource = myTable
                'MsgBox(sqlstr & getWherePruefungen() & "  order by wartPruef_Faelligkeitsdatum ")
                .SET_SQL(sqlstr & getWherePruefungen() & "  order by wartPruef_Faelligkeitsdatum ", "ADMIN", getListPruefungsobjekte)
                .LOAD()
                setDGVOptionsPruefungen()
            End With
            showDGVPruefungen()

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, "")
        End Try
        Me.Cursor = Cursors.Default
    End Sub
    Sub showDGVPruefungen()

        With dgvPruefungen

            If .ColumnCount = 0 Then Me.Cursor = Cursors.Default : Exit Sub
            .RowHeadersVisible = False
            .ReadOnly = True
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False

            .Columns("wartPruef_Id").Visible = False
            .Columns("wartPruef_wartPOId").Visible = False


            .Columns("wartPruef_Faelligkeitsdatum").Width = 90
            .Columns("wartPruef_Faelligkeitsdatum").HeaderText = "Fälligkeitsdatum"

            .Columns("wartPruef_Erledigung").Width = 60
            .Columns("wartPruef_Erledigung").HeaderText = "Erledigung"

            .Columns("wartPruef_Erledigung_Datum").Width = 90
            .Columns("wartPruef_Erledigung_Datum").HeaderText = "Erl.Dat"

            .Columns("wartPruef_Erledigung_BestaetigtMaName").Width = 100
            .Columns("wartPruef_Erledigung_BestaetigtMaName").HeaderText = "Erl.MA"


            .Columns("wartPruef_Erledigung_Bemerkung").Width = 60
            .Columns("wartPruef_Erledigung_Bemerkung").HeaderText = "Erl.Bemerkung."
            .Columns("wartPruef_Erledigung_Bemerkung").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

            .Columns("Anhaenge").Width = 60
            .Columns("Anhaenge").HeaderText = "Anh."


            '  lblErgebnis.Text = "Ergebnise: " & .RowCount
        End With
    End Sub

    Sub setDGVOptionsPruefungen()
        Try
            Exit Sub
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

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

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

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

    Private Sub btnMonat_Click(sender As Object, e As EventArgs) Handles btnMonat.Click
        initBtns("Monat")
    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 '

    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"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
                'End If
            Case "Tag"
                '  If cbxGeschaftsjahr.Checked Then
                'DateTimePicker1.Value = JAHR.AddMonths(1)
                ' DateTimePicker2.Value = JAHR.AddYears(1).AddMonths(1).AddDays(-1)
                ' Else
                DateTimePicker1.Value = srcTAG
                DateTimePicker2.Value = srcTAG
                If srcTAG = Now.ToShortDateString Then
                    Button5.Text = "Heute"
                ElseIf srcTAG = Now.AddDays(-1).ToShortDateString Then
                    Button5.Text = "Gestern"
                ElseIf srcTAG = Now.AddDays(-2).ToShortDateString Then
                    Button5.Text = "Vorgestern"
                Else
                    Button5.Text = srcTAG.ToString("dd.MM")
                End If

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

    Private Sub txtAbfertDat_TextChanged(sender As Object, e As EventArgs) Handles txtAbfertDat.Leave
        If sender.Text <> "" AndAlso IsDate(sender.Text) Then
            DateTimePicker1.Value = CDate(sender.text)
        End If
    End Sub
    Private Sub txtAbfertDatBis_TextChanged(sender As Object, e As EventArgs) Handles txtAbfertDatBis.Leave
        If sender.Text <> "" AndAlso IsDate(sender.Text) Then
            DateTimePicker2.Value = CDate(sender.text)
        End If
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
        txtAbfertDat._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub DateTimePicker2_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged
        txtAbfertDatBis._value = CDate(sender.value).ToShortDateString
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim f As New frmAuditFlow_Pruefungsojekt()
        f.ShowDialog(Me)
        dgvPruefungsobjekte.RELOAD()
        setDGVOptionsPruefungsobjekte()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If txtWartungFaelligkeitAbDat._value <> "" Then
            cAuditFlow.ErzeugeFaelligePruefungen(txtWartungFaelligkeitAbDat._value)
        End If
        dgvPruefungsobjekte.RELOAD()
        setDGVOptionsPruefungsobjekte()

        loadDGVPuefungen()
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckStateChanged
        loadDGVPuefungen()
    End Sub

    Private Sub dgvPruefungsobjekte_SelectionChanged(sender As Object, e As EventArgs) Handles dgvPruefungsobjekte.SelectionChanged
        If dgvPruefungsobjekte.SelectedRows.Count > 0 Then
            loadDGVPuefungen()
        Else
            dgvPruefungen.Columns.Clear()
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim frm As New frmManageTables("tblAuditFlow_Standorte", "ADMIN")
        frm.ShowDialog(Me)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim frm As New frmManageTables("tblAuditFlow_Standorte", "ADMIN")
        frm.ShowDialog(Me)
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Dim frm As New frmManageTables("tblAuditFlow_Kategorien", "ADMIN")
        frm.ShowDialog(Me)
    End Sub


End Class