Imports System.ComponentModel
Imports System.IO
Imports System.Windows.Documents
Imports VERAG_PROG_ALLGEMEIN

Public Class usrCntlMailsender


    Dim DatenarchivBind As cEasyBinding
    Public table
    Public BIND_DB
    Private EditRow As Integer = -1
    Sub New(table, BIND_DB)
        Me.table = table
        Me.BIND_DB = BIND_DB
        InitializeComponent()
    End Sub


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

        loaddgv()


        'Dim distinctDT As DataTable = DatenarchivBind.bindingdataTable.DefaultView.ToTable(True, "firma")

        'For Each d As DataRow In distinctDT.Rows
        '    If Not cboPartnersystem.Items.Contains(d.Item(0)) Then cboPartnersystem.Items.Add(d.Item(0))
        'Next


    End Sub

    Private Sub btnSuche_Click(sender As Object, e As EventArgs) Handles btnSuche.Click
        Me.Cursor = Cursors.WaitCursor

        loaddgv()

        Me.Cursor = Cursors.Default
    End Sub

    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("param", cboPartnersystem._value))

        Return list

    End Function

    Function getDatenarchivwhere() As String

        Dim sqlstr = ""
        If cboPartnersystem._value <> "" Then sqlstr &= " AND [param] = '" & cboPartnersystem._value & "'"

        Return sqlstr
    End Function


    Private Sub DataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView.CellDoubleClick
        Me.Cursor = Cursors.WaitCursor
        Dim tmprowindex As Integer = e.RowIndex
        If DataGridView.Columns(e.ColumnIndex).Name = "open" Then

            If DataGridView.CurrentRow.Cells("param").Value IsNot DBNull.Value AndAlso DataGridView.CurrentRow.Cells("param").Value.ToString.StartsWith("\\") Then
                Process.Start(DataGridView.CurrentRow.Cells("param").Value)
            End If
        Else

        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub cbxProduktivsystem_CheckedChanged(sender As Object, e As EventArgs) Handles cbxTestsystem.CheckedChanged

        VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = cbxTestsystem.Checked

        DatenarchivBind = New cEasyBinding(BIND_DB)


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


        DatenarchivBind.initBinding("SELECT " & TopMax & " *  FROM " & table & " WHERE 1 = 1 " & getDatenarchivwhere(), table)

        DataGridView.DataSource = DatenarchivBind.bindingdataTable

        Dim distinctDT As DataTable = DatenarchivBind.bindingdataTable.DefaultView.ToTable(True, "param")

        For Each d As DataRow In distinctDT.Rows
            If Not cboPartnersystem.Items.Contains(d.Item(0)) Then cboPartnersystem.Items.Add(d.Item(0))
        Next

        Me.usrCntlDatenarchiv_Load(sender, e)

    End Sub

    Private Sub cboPartnersystem_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPartnersystem.SelectedIndexChanged
        btnSuche_Click(sender, e)
    End Sub


    Public Sub loaddgv()

        VERAG_PROG_ALLGEMEIN.cAllgemein.TESTSYSTEM = cbxTestsystem.Checked

        Try
            DatenarchivBind = New cEasyBinding(BIND_DB)


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


            DatenarchivBind.initBinding("SELECT " & TopMax & " *  FROM " & table & " WHERE 1 = 1 " & getDatenarchivwhere(), table)

            DataGridView.DataSource = DatenarchivBind.bindingdataTable


            With DataGridView

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


                For Each cc As DataGridViewColumn In .Columns
                    If cc.Name.ToLower.Contains("param_") Then
                        cc.HeaderText = cc.Name.Replace("Param_", "")
                        If cc.Name.ToLower.Contains("value") Then
                            cc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                        Else
                            cc.Width = 200
                        End If
                    End If
                Next


                lblErgebnis.Text = "Ergebnisse: " & .RowCount


            End With

        Catch ex As Exception
            VERAG_PROG_ALLGEMEIN.cErrorHandler.ERR(ex.Message, ex.StackTrace, System.Reflection.MethodInfo.GetCurrentMethod.Name)
        End Try

    End Sub

    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnSave.Click

        If DataGridView.Columns.Count = 0 Or DataGridView.SelectedRows.Count = 0 Then 'alles gelöscht oder markierte Zeile gelöscht
            DatenarchivBind.updateBinding()
            Exit Sub
        End If

        If DataGridView.CurrentRow.Cells("param").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("param").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("param").ToString <> "" AndAlso EditRow >= 0 Then
            DatenarchivBind.updateBinding()
        End If

    End Sub


    Private Sub DataGridView_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView.EditingControlShowing
        EditRow = DataGridView.CurrentRow.Index
    End Sub

    Private Sub DataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView.SelectionChanged
        If EditRow >= 0 Then
            Dim new_row As Integer = EditRow
            EditRow = -1
            DataGridView.CurrentCell = DataGridView.Rows(new_row).Cells(
                DataGridView.CurrentCell.ColumnIndex)
        End If
    End Sub

End Class
