Imports System.ComponentModel
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel
Imports GrapeCity
Imports VERAG_PROG_ALLGEMEIN

Public Class usrCntlAPI

    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)

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

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

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


        kdtxtKunde.initKdBox(Me.FindForm) '   initKdAbsender()


        cboFiliale.fillWithSQL("SELECT FilialenNr, Grenzstelle FROM Filialen WHERE Fil_Status='A' ORDER BY FilialenNr ", True, "FMZOLL", True)


        cboDatum.Items.Clear()
        cboDatum.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("API-Datum", "ADat"))
        cboDatum.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Durchführungsdatum", "DDat"))
        cboDatum.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Error-Datum", "EDat"))
        cboDatum.changeItem("ADat")

        cboInOut.Items.Clear()
        'cboInOut.fillWithSQL("SELECT distinct([api_InOut]) FROM [tblAPI] ", False, "ADMIN", True)
        cboInOut.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("IN", "IN"))
        cboInOut.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("OUT", "OUT"))

        cboType.Items.Clear()
        'cboType.fillWithSQL("SELECT distinct(api_Type) FROM [tblAPI] ", False, "ADMIN", True)
        cboType.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("FTP", "FTP"))
        cboType.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MAIL", "MAIL"))
        cboType.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("REST", "REST"))

        cboAPIArt.Items.Clear()
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("", ""))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AVISO_OUT_STATUSMAIL", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_OUT_STATUSMAIL))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AVISO_OUT_BELEGMAIL", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_OUT_BELEGMAIL))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG_OUT_EZA", VERAG_PROG_ALLGEMEIN.cAPI_ART.VERAG_OUT_EZA))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAG_OUT_NCTS", VERAG_PROG_ALLGEMEIN.cAPI_ART.VERAG_OUT_NCTS))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MULTISERVICE", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_IN_MSE))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MDM SAMMELRECHNUNG", VERAG_PROG_ALLGEMEIN.cAPI_ART.SDL_OUT_SAMMELRECHNUNG))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("CREDITSAFE", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_IN_CS))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("SAMMELRECHNUNG", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_OUT_SAMMELRECHNUNG))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("EINZELRECHNUNG", VERAG_PROG_ALLGEMEIN.cAPI_ART.AVISO_OUT_EINZELRECHNUNG))
        cboAPIArt.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("MDM-PLOSE AUSWERTUNG", VERAG_PROG_ALLGEMEIN.cAPI_ART.SDL_OUT_MDM_AUSWERTUNG))
        cboAPIArt.changeItem("")


        cboPartnersystem.Items.Clear()
        cboPartnersystem.fillWithSQL("SELECT distinct(api_Partnersystem) from [tblAPI]   where api_Partnersystem is not null ", False, "ADMIN", True)

        cboFirma.Items.Clear()
        cboFirma.fillWithSQL("SELECT distinct([api_Firma]) FROM [tblAPI] where api_Firma is not null ", False, "ADMIN", True)


        cboProgram.Items.Clear()
        cboProgram.fillWithSQL("SELECT distinct([api_progName]) FROM [tblAPI] where api_progName is not null ", False, "ADMIN", True)


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

                DateTimePicker1.Value = JAHR
                DateTimePicker2.Value = JAHR.AddYears(1).AddDays(-1)
            Case "Tag"
                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

    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 btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        Me.Cursor = Cursors.WaitCursor

        Dim sqlstr As String = ""
        Dim TopMax = If(IsNumeric(txtMaxSrch.Text), " TOP " & txtMaxSrch.Text, "")
        sqlstr &= "SELECT " & TopMax & "*  FROM [tblAPI] WHERE 1=1 "
        '" api_id,[api_datetime],[api_progName],[api_Type],[api_Partnersystem],[api_Testsystem], api_apiArt,api_apiUArt,[api_apiZollStatus],[api_InOut],[api_status],[api_fehlversuch],[api_progVersion],[api_EMail],[api_filepathSource],[api_filepathDestination],[api_Firma],[api_Username],[api_Vorsystem],[api_KundenNr],[api_AvisoId],[api_SendungsId],[api_FilialenNr],[api_AbfertigungsNr],[api_BezugsNr],[api_Zollbelegnummer],[api_finTstmp] 

        With DataGridView
            '  .DataSource = myTable
            .SET_SQL(sqlstr & getAPIDatumWhere() & " ORDER BY api_datetime DESC", "ADMIN", getAPIList)
            .LOAD()

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

            .Columns("api_id").Visible = False

            For Each cc As DataGridViewColumn In .Columns
                cc.HeaderText = cc.Name.Replace("api_", "")
            Next

            '.Columns("FilialenNr").Width = 45
            '.Columns("FilialenNr").HeaderText = "Filiale"
            '.Columns("FilialenNr").HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("FilialenNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("AbfertigungsNr").Width = 70
            '.Columns("AbfertigungsNr").HeaderText = "Abf-Nr"
            '.Columns("AbfertigungsNr").HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("AbfertigungsNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("UnterNr").Width = 45
            '.Columns("UnterNr").HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("UnterNr").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("Abfertigungsdatum").Width = 100
            '.Columns("Abfertigungsdatum").HeaderText = "Abf-Dat."
            '.Columns("Abfertigungsdatum").HeaderCell.Style.Alignment = DataGridViewContentAlignment.TopCenter
            '.Columns("Abfertigungsdatum").DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter

            lblErgebnis.Text = "Ergebnisse: " & .RowCount
        End With

        Me.Cursor = Cursors.Default
    End Sub

    Function getAPIDatumWhere() As String
        Dim sqlstr = ""
        If kdtxtKunde.Text <> "" And IsNumeric(kdtxtKunde.Text) Then sqlstr &= " AND [api_KundenNr] LIKE @Kunde "


        If cboDatum._value = "ADat" Then
            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([api_datetime] BETWEEN @Datum_von AND @Datum_bis ) "
                Else
                    sqlstr &= " AND ([api_datetime] BETWEEN @Datum_von AND @Datum_von ) "
                End If
            End If
        ElseIf cboDatum._value = "DDat" Then
            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([api_finTstmp] BETWEEN @Datum_von AND @Datum_bis ) "
                Else
                    sqlstr &= " AND ([api_finTstmp] BETWEEN @Datum_von AND @Datum_von ) "
                End If
            End If
        ElseIf cboDatum._value = "EDat" Then
            If txtAbfertDat.Text <> "" Then
                If txtAbfertDatBis.Text <> "" Then
                    sqlstr &= " AND ([api_errTstmp] BETWEEN @Datum_von AND @Datum_bis ) "
                Else
                    sqlstr &= " AND ([api_errTstmp] BETWEEN @Datum_von AND @Datum_von ) "
                End If
            End If
        End If


        If txtAbfertigungsnummer.Text <> "" Then
            If txtAbfertigungsnummerBis.Text <> "" Then
                sqlstr &= " AND ([api_AbfertigungsNr] BETWEEN @AbfertigungsNr AND @AbfertigungsNr_bis) "
            Else
                sqlstr &= " AND [api_AbfertigungsNr] LIKE @AbfertigungsNr "
            End If
        End If


        If cbxOhneAnhang.Checked Then sqlstr &= " AND isnull([api_EMailAttCnt],-1) =  0 "

        If cboFiliale._value <> "" Then sqlstr &= " AND [api_FilialenNr] LIKE @FilialenNr "

        If cboPartnersystem._value <> "" Then sqlstr &= " AND [api_Partnersystem] = @Partnersystem "

        If cboFirma._value <> "" Then sqlstr &= " AND [api_Firma] = @Firma "

        If cboAPIArt._value <> "" Then sqlstr &= " AND [api_apiArt] = @APIArt "
        If cboType._value <> "" Then sqlstr &= " AND [api_Type] = @APIType "
        If cboProgram._value <> "" Then sqlstr &= " AND [api_progName] = @Program "
        If cboInOut._value <> "" Then sqlstr &= " AND [api_InOut] =  @APIInOut "

        If txtAvisoId.Text <> "" Then sqlstr &= " AND [api_AvisoId] LIKE @AvisoId "
        If txtSendungsId.Text <> "" Then sqlstr &= " AND [api_SendungsId] LIKE @SendungsId "
        If txtZollBelegNr.Text <> "" Then sqlstr &= " AND [api_Zollbelegnummer] LIKE @ZollBelegNr "
        If txtBezugsNr.Text <> "" Then sqlstr &= " AND [api_BezugsNr] LIKE @BezugsNr "
        If txtSuche.Text <> "" Then sqlstr &= " AND ([api_EMail] LIKE @api_EMail OR [api_EMailCC] LIKE @api_EMail OR [api_EMailBCC] LIKE @api_EMail)"


        Return sqlstr
    End Function

    Function getAPIList() As List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)

        Dim list As New List(Of VERAG_PROG_ALLGEMEIN.SQLVariable)
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Kunde", kdtxtKunde.KdNr_value))

        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Datum_von", txtAbfertDat.Text & " 00:00:00"))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Datum_bis", txtAbfertDatBis.Text & " 23:59:59"))
        If txtAbfertigungsnummerBis.Text <> "" Then
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbfertigungsNr", txtAbfertigungsnummer.Text.Replace("*", "")))
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbfertigungsNr_bis", txtAbfertigungsnummerBis.Text.Replace("*", "")))
        Else
            list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AbfertigungsNr", txtAbfertigungsnummer.Text.Replace("*", "%")))
        End If


        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("FilialenNr", cboFiliale._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Partnersystem", cboFiliale._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Program", cboProgram._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("Firma", cboFirma._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("APIArt", cboAPIArt._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("APIType", cboType._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("APIInOut", cboInOut._value))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("AvisoId", txtAvisoId.Text))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("SendungsId", txtSendungsId.Text))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("ZollBelegNr", txtZollBelegNr.Text.Replace("*", "%").ToUpper))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("BezugsNr", txtBezugsNr.Text.Replace("*", "%").ToUpper))
        list.Add(New VERAG_PROG_ALLGEMEIN.SQLVariable("api_EMail", txtSuche.Text.Replace("*", "%")))
        Return list

    End Function

    Private Sub txtAbfertigungsnummer_TextChanged(sender As Object, e As EventArgs) Handles txtAbfertigungsnummer.TextChanged
        txtAbfertigungsnummerBis.Enabled = (txtAbfertigungsnummer.Text <> "")

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f = New frmAPIEinstellungen("tblAPIEinstellungen", "ADMIN")
        f.Show()
    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 DataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellDoubleClick
        Me.Cursor = Cursors.WaitCursor
        Dim err As New frmAPI With {
            .id = sender.CurrentRow.Cells("api_id").Value,
            .progName = sender.CurrentRow.Cells("api_progName").Value,
            .progVersion = sender.CurrentRow.Cells("api_progVersion").Value,
            .datetime = sender.CurrentRow.Cells("api_datetime").Value,
            .testsystem = sender.CurrentRow.Cells("api_Testsystem").Value,
            .type = sender.CurrentRow.Cells("api_Type").Value,
            .mail = sender.CurrentRow.Cells("api_EMail").Value,
            .maid = sender.CurrentRow.Cells("api_MaId").Value,
            .userName = sender.CurrentRow.Cells("api_Username").Value,
            .mailAnh = sender.CurrentRow.Cells("api_EMailAttCnt").Value,
            .firma = sender.CurrentRow.Cells("api_Firma").Value,
            .subject = sender.CurrentRow.Cells("api_EMailSubject").Value,
            .mailcc = sender.CurrentRow.Cells("api_EMailCC").Value,
            .mailbcc = sender.CurrentRow.Cells("api_EMailBCC").Value,
            .errorCode = sender.CurrentRow.Cells("api_status").Value,
            .errStack = IIf(sender.CurrentRow.Cells("api_errTstmp").Value IsNot DBNull.Value, "Datum: " & sender.CurrentRow.Cells("api_errTstmp").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_errMethodName").Value IsNot DBNull.Value, "Methode: " & sender.CurrentRow.Cells("api_errMethodName").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_errMessage").Value IsNot DBNull.Value, "Message: " & sender.CurrentRow.Cells("api_errMessage").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_errStack").Value IsNot DBNull.Value, "Stack: " & sender.CurrentRow.Cells("api_errStack").Value & vbNewLine, ""),
            .errMessage = IIf(sender.CurrentRow.Cells("api_EMailFrom").Value IsNot DBNull.Value, "Mail von : " & sender.CurrentRow.Cells("api_EMailFrom").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_EMail").Value IsNot DBNull.Value, "E-Mail: " & sender.CurrentRow.Cells("api_EMail").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_EMailText").Value IsNot DBNull.Value, "E-MailText: " & sender.CurrentRow.Cells("api_EMailText").Value & vbNewLine, ""),
            .errMethodName = IIf(sender.CurrentRow.Cells("api_BezugsNr").Value IsNot DBNull.Value, "BezugsNr: " & sender.CurrentRow.Cells("api_BezugsNr").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_BezugsNr").Value IsNot DBNull.Value, "AVISO-ID: " & sender.CurrentRow.Cells("api_AvisoId").Value & vbNewLine, "") & IIf(sender.CurrentRow.Cells("api_KundenNr").Value IsNot DBNull.Value, "Kunde: " & sender.CurrentRow.Cells("api_KundenNr").Value & vbNewLine, "")
            }
        err.Show(Me)
        sender.RELOAD()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnExcel_Click(sender As Object, e As EventArgs) Handles btnExcel.Click
        If DataGridView.Rows.Count > 0 Then
            Dim dtNew As New DataTable
            dtNew = TryCast(DataGridView.DataSource, DataTable)

            If dtNew.Rows.Count > 0 Then

                For Each col As DataColumn In dtNew.Columns

                    Dim colName = col.ColumnName.Replace("api_", "")

                    If Not col.ColumnName = (colName) Then col.ColumnName = col.ColumnName.Replace("api_", "")
                Next

                cProgramFunctions.genExcelFromDT_NEW(dtNew)
            End If
        End If
    End Sub
End Class
