Public Class usrcntlVerteiler

    Dim NeuerEintrag As Boolean = False
    Dim Reci As New cEmpfaenger

    Private Sub usrcntlVerteiler_Load(sender As Object, e As EventArgs) Handles Me.Load
        LoadDGV()
    End Sub

    Function LoadDGV()

        Dim ds As New DataSet
        cSQL.SQL2DS("SELECT EmpfName, EmpfEMail, EmpfID FROM GVerteilerListe WHERE EmpfName LIKE '%" & txtSuche.Text & "%' OR EmpfEMail LIKE '%" & txtSuche.Text & "%' ORDER BY EmpfName", ds)

        With dgv
            .MultiSelect = False
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect

            .DataSource = ds.Tables(0)
            .Columns("EmpfEMail").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            .Columns("EmpfID").Visible = False
        End With
    End Function

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        LoadReci()
        If NeuerEintrag = True And SQLTools.CheckIfExist("SELECT * FROM GVerteilerListe WHERE EmpfEMail LIKE '%" & txtEMail.Text & "%' ") Then
            MsgBox("Diese EMail Adresse ist bereits registriert!") : Exit Sub
        ElseIf NeuerEintrag = True And SQLTools.CheckIfExist("SELECT * FROM GVerteilerListe WHERE EmpfEMail LIKE '%" & txtEMail.Text & "%' ") = False Then
            'Reci.EmpfID = Now
            Reci.saveAsNew()
            NeuerEintrag = False
            txtEMail.BackColor = Color.White : txtName.BackColor = Color.White
            LoadDGV()
        Else
            Reci.update()
            LoadDGV()
        End If
    End Sub

    Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
        txtName.Enabled = True : txtEMail.Enabled = True
        NeuerEintrag = True
        Reci = New cEmpfaenger
        txtName.Text = "" : txtEMail.Text = ""
        txtName.BackColor = Color.Beige : txtEMail.BackColor = Color.Beige
    End Sub

    Function LoadReci()
        Reci.EmpfName = txtName.Text
        Reci.EmpfEMail = txtEMail.Text
        If NeuerEintrag = True Then Reci.EmpfID = Now
    End Function

    Private Sub txtSuche_TextChanged(sender As Object, e As EventArgs) Handles txtSuche.TextChanged
        LoadDGV()
    End Sub

    Private Sub btnChange_Click(sender As Object, e As EventArgs) Handles btnChange.Click, dgv.CellDoubleClick
        txtName.Enabled = True : txtEMail.Enabled = True
        Reci = New cEmpfaenger
        Reci.loadME(dgv.CurrentRow.Cells("EmpfID").Value)
        txtEMail.Text = Reci.EmpfEMail
        txtName.Text = Reci.EmpfName
    End Sub

    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
        If Reci.Delete() Then
            LoadDGV()
            txtName.Text = "" : txtEMail.Text = ""
        End If

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim lines = IO.File.ReadAllLines("C:\Gastro\reci.csv")
        Dim colCount = lines.First.Split(";"c).Length

        For Each line In lines
            Dim objFields = From field In line.Split(";"c)
            Dim verteiler As New cEmpfaenger
            verteiler.EmpfName = SQLTools.auf49kuerzen(objFields(0).ToString)
            verteiler.EmpfEMail = objFields(1)
            verteiler.EmpfID = SQLTools.auf49kuerzen(Now & objFields(1).ToString)
            'verteiler.saveAsNew()
        Next
    End Sub
End Class
