Imports System.ComponentModel
Imports System.Data.SqlTypes
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()

        'cboPartnersystem.Items.Clear()
        Dim distinctDT As DataTable = DatenarchivBind.bindingdataTable.DefaultView.ToTable(True, "ms_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


    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 getParamwhere() As String

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

        If txtSuche.Text <> "" Then
            sqlstr &= " AND ([ms_subject] like '" & txtSuche.Text.Replace("*", "%") & "' Or [ms_an] like '" & txtSuche.Text.Replace("*", "%") & "' OR [ms_info] like '" & txtSuche.Text.Replace("*", "%") & "')  "
        End If


        sqlstr &= " and ms_aktiv = " & IIf(cbxaktiv.Checked, 1, 0)

        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 cbxaktiv.CheckedChanged

        'DatenarchivBind = New cEasyBinding(BIND_DB)

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


        DatenarchivBind.initBinding("SELECT " & TopMax & " [ms_id],[ms_param],[ms_kundenNr],[ms_an],[ms_cc],[ms_bcc],[ms_subject],[ms_mailtext],[ms_aktiv],[ms_info],[ms_firma]   FROM " & table & " WHERE 1 = 1 " & getParamwhere(), table)

        DataGridView.DataSource = DatenarchivBind.bindingdataTable

        Dim distinctDT As DataTable = DatenarchivBind.bindingdataTable.DefaultView.ToTable(True, "ms_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 = cbxaktiv.Checked

        Try
            DatenarchivBind = New cEasyBinding(BIND_DB)


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


            DatenarchivBind.initBinding("SELECT " & TopMax & " [ms_id],[ms_param],[ms_kundenNr],[ms_an],[ms_cc],[ms_bcc],[ms_subject],[ms_mailtext],[ms_aktiv],[ms_info],[ms_firma]  FROM " & table & " WHERE 1 = 1 " & getParamwhere(), 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("ms_") Then
                        cc.HeaderText = cc.Name.Replace("ms_", "")
                        If cc.Name.ToLower.Contains("mailtext") Then
                            cc.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                        ElseIf cc.Name.ToLower.Contains("aktiv") Then
                            cc.Width = 50

                        ElseIf cc.Name.ToLower.Contains("id") Then
                            cc.Visible = False

                        Else
                            cc.Width = 150
                        End If
                    End If
                Next


                lblErgebnis.Text = "Ergebnisse: " & .RowCount


                'If .Columns("open") Is Nothing Then

                '    Dim c As New DataGridViewImageColumn
                '    c.Name = "open" : c.HeaderText = "open"
                '    c.Image = New Bitmap(1, 1)
                '    c.Width = 40 : c.DefaultCellStyle.Padding = New Padding(2) : c.DefaultCellStyle.BackColor = Color.White
                '    c.DefaultCellStyle.Tag = "open"
                '    .Columns.Add(c)

                'End If

                For Each r As DataGridViewRow In .Rows

                    'If r.Cells("ms_param").Value IsNot DBNull.Value AndAlso r.Cells("ms_param").Value <> "" Then
                    '    Dim path As New System.IO.DirectoryInfo(r.Cells("ms_param").Value)

                    '    'If path.Exists Then
                    '    '    DirectCast(r.Cells("open"), DataGridViewImageCell).Value = My.Resources.search
                    '    'End If
                    'End If

                Next


            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("ms_param").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("ms_an").ToString <> "" AndAlso DataGridView.CurrentRow.Cells("ms_subject").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

    Private Sub txtSuche_KeyDown(sender As Object, e As KeyEventArgs) Handles txtSuche.KeyDown

        If e.KeyCode = Keys.Enter Then

            Me.btnSuche_Click(sender, e)
            e.Handled = True
        End If

    End Sub

    Private Sub DataGridView_DefaultValuesNeeded(sender As Object, e As DataGridViewRowEventArgs) Handles DataGridView.DefaultValuesNeeded
        e.Row.Cells("ms_aktiv").Value = False
    End Sub
End Class
