Imports IntraSell_DLL
Imports MySql.Data.MySqlClient

Public Class Tisch

    Private Sub Tisch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        NextName()
    End Sub

    Private Sub ButtonAbbrechen_Click(sender As Object, e As EventArgs) Handles ButtonAbbrechen.Click
        Close()
    End Sub

    Private Sub ButtonAnlegen_Click(sender As Object, e As EventArgs) Handles ButtonAnlegen.Click
        Try
            If TextBoxName.Text <> "" Then
                Dim cnt = 1
                Dim anzahlTische = CInt(TextBoxAnzahl.Text)

                While cnt <= anzahlTische
                    Dim name = TextBoxName.Text
                    If anzahlTische > 1 Then
                        name = TextBoxName.Text & cnt
                    End If
                    Dim cmdChk As New MySqlCommand("select idnr from ofAdressen where status=@Status and name=@Name")
                    cmdChk.Parameters.AddWithValue("Name", name)
                    cmdChk.Parameters.AddWithValue("Ort", TextBoxRaum.Text)
                    cmdChk.Parameters.AddWithValue("Status", "Tisch")

                    Dim exists = FirstRow(cmdChk)
                    If Not IsNumeric(exists) Then
                        Dim cmd As New MySqlCommand("Insert into ofAdressen (IDNR, Name, Ort, Status) Values (@IDNR, @Name, @Ort, @Status)")
                        cmd.Parameters.AddWithValue("IDNR", NextId("ofAdressen", "IDNR"))
                        cmd.Parameters.AddWithValue("Name", name)
                        cmd.Parameters.AddWithValue("Ort", TextBoxRaum.Text)
                        cmd.Parameters.AddWithValue("Status", "Tisch")
                        RunSQL(cmd)
                    Else

                    End If

                    'NextName()
                    cnt += 1
                End While

                Close()
            End If
        Catch ex As Exception
            'Ignore
        End Try
    End Sub

    Private Sub NextName()
        Try
            Dim cnt = "1"
            Try
                cnt = FirstRow("select 1 + count(Idnr) from ofAdressen where Status like 'Tisch%' or Name like 'Tisch%'")
            Catch ex As Exception
                'Ignore
            End Try
            TextBoxName.Text = "Tisch " & cnt
        Catch ex As Exception
            'Ignore
        End Try
    End Sub

End Class