Imports IntraSell_DLL
Imports IntraSell_Net_Base
Imports MySql.Data.MySqlClient

Public Class DataLoaderOrders

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="raumCode"></param>
    ''' <returns></returns>
    Shared Function SQL_OPEN_ORDERS_TISCHE(raumCode As String) As String
        Dim sql As String = <a>
        SELECT DISTINCT ofadressen.IDNR, ofadressen.Status, ofadressen.Name, ofadressen.LayoutX, ofadressen.LayoutY, v.Abgeschlossen, v.Bezahlt, v.Nummer, v.Typ, sum(v.SummeBrutto) as SummeBrutto, v.MitarbeiterNr, v.datum
        FROM            (SELECT  Nummer, Typ, KundNr, MitarbeiterNr, Datum, Notiz, NotizIntern, NotizExtern, Summe, Bezahlt, Ausgedruckt, Abgeschlossen, STATUS, SummeBrutto 
                                  FROM            buchvorgang
                                  WHERE        (Datum > date_add(CURRENT_TIMESTAMP, INTERVAL - 3  DAY )) AND (Typ = 'AU') AND (Bezahlt = 0) AND (Abgeschlossen = 0)) v RIGHT OUTER JOIN
                                 ofadressen ON v.KundNr = ofadressen.IDNR
        WHERE (ofadressen.Name Like 'Tisch%' or ofadressen.Status Like 'Tisch%')
          and (ofadressen.Ort Like '?Ort' or ofadressen.Ort is null or ofadressen.Ort = '')
        group BY ofadressen.IDNR
        ORDER BY v.Datum desc, ofadressen.IDNR, ofadressen.Name
        LIMIT 100
        </a>.Value
        Return sql.Replace("?Ort", raumCode)
    End Function

    Shared Sub DatenAktualisierenOrders(panel As Panel, cmdText As String)
        If CurrentDB.State <> ConnectionState.Open Then
            Return
        End If
        Dim TISCH_HEIGHT As Integer = VarValue_Default(Variablen.TISCH_HÖHE_PX, 80)
        Dim TISCH_WIDTH As Integer = VarValue_Default(Variablen.TISCH_BREITE_PX, 140)
        Dim FremdeTischeBedienen As Boolean = HasRecht(Rechte.Fremde_Tische_bedienen)
        Try
            Dim buttonList As New List(Of Button)
            Dim cmd As New MySqlCommand With {
                .CommandText = cmdText,
                .Connection = CurrentDB
            }
            Dim rs = OpenRecordsetInMemory(cmd)

            For Each rdr In rs.Rows

                Dim name As String = "N.A."
                If Not IsDBNull(rdr("Name")) Then
                    name = rdr("Name")
                End If

                Dim status As String = ""
                If Not IsDBNull(rdr("Status")) Then
                    status = rdr("Status")
                End If
                Dim btn = New Button()
                buttonList.Add(btn)
                btn.Name = rdr("IDNR")

                If status = "Tisch" Then
                    btn.BackgroundImageLayout = ImageLayout.None
                    btn.Image = My.Resources.table_32xGray
                    btn.ImageAlign = ContentAlignment.BottomLeft
                    btn.TextAlign = ContentAlignment.TopRight
                    btn.FlatAppearance.BorderSize = 1
                    btn.FlatAppearance.BorderColor = Color.LightGray
                    btn.FlatStyle = FlatStyle.Flat
                End If

                If IsNumeric(rdr("Nummer")) Then

                    btn.Tag = New OffeneBestellung(rdr("Nummer"), rdr("Typ"), rdr("IDNR"), name)
                    btn.Text = rdr("Name") & vbNewLine & rdr("SummeBrutto")
                    If rdr("Abgeschlossen") = 0 Then
                        btn.ForeColor = Color.Red
                        btn.BackColor = Color.GreenYellow
                    Else
                        btn.ForeColor = Color.Green
                    End If
                    'Kellner darf nur eigene Tische bearbeiten.
                    Try
                        btn.Enabled = (rdr("MitarbeiterNr") = MitarbeiterID) Or FremdeTischeBedienen

                        btn.FlatAppearance.BorderSize = 3
                        btn.FlatAppearance.BorderColor = MitarbeiterFarbe(rdr("MitarbeiterNr"))
                        btn.FlatStyle = FlatStyle.Flat
                    Catch ex As Exception
                        'Ignore
                    End Try
                Else
                    btn.Tag = New OffeneBestellung(Nothing, Nothing, rdr("IDNR"), name)
                    btn.Text = name
                    btn.ForeColor = Color.Gray
                End If


                btn.Height = TISCH_HEIGHT
                btn.Width = TISCH_WIDTH
                If Not IsDBNull(rdr("LayoutX")) Then
                    btn.Location = New Point(rdr("LayoutX"), rdr("LayoutY"))
                End If

            Next

            panel.SuspendLayout()
            AddOrUpdateButtonsToPanel(buttonList, panel)
            buttonList.Clear()

        Catch ex As Exception
            HandleAppError(ex)
        Finally
            panel.ResumeLayout()
        End Try
    End Sub

    Shared Sub AddOrUpdateButtonsToPanel(buttonList As List(Of Button), pnl As Panel)
        For Each btn In buttonList
            Dim oldB = pnl.Controls.Find(btn.Name, False)
            If oldB.Length >= 1 Then
                Dim oldButtton As Button = CType(oldB(0), Button)
                oldButtton.Text = btn.Text
                oldButtton.BackColor = btn.BackColor
                oldButtton.ForeColor = btn.ForeColor
                oldButtton.Enabled = btn.Enabled
                oldButtton.Tag = btn.Tag
                oldButtton.FlatAppearance.BorderColor = btn.FlatAppearance.BorderColor
                oldButtton.FlatAppearance.BorderSize = btn.FlatAppearance.BorderSize
                oldButtton.FlatStyle = btn.FlatStyle
            Else
                pnl.Controls.Add(btn)
            End If
        Next
    End Sub

End Class
