Imports DevExpress.XtraEditors.Controls
Imports DevExpress.XtraEditors.Repository
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraGrid.Views.Grid

Public Class ClassPosGrid

    Private Shared Sub FormatDateColumn(grid As GridView, column As GridColumn, dateEdit As RepositoryItemDateEdit)
        column.ColumnEdit = dateEdit
    End Sub

    Private Shared Sub FormatBooleanColumn(grid As GridView, column As GridColumn, checkboxEdit As RepositoryItemCheckEdit)
        column.ColumnEdit = checkboxEdit
    End Sub

    Private Shared Sub FormatComboColumn(grid As GridView, column As GridColumn, comboEdit As RepositoryItemComboBox, sqlCommand As String, staticList As String)
        Dim comboItems As ComboBoxItemCollection = comboEdit.Items

        If staticList <> "" Then
            Dim staticListItems() As String = staticList.Split(";")

            comboItems.BeginUpdate()
            Try
                For Each item In staticListItems
                    comboItems.Add(item)
                Next
            Finally
                comboItems.EndUpdate()
            End Try
        ElseIf sqlCommand <> "" Then
            Dim DT As DataTable

            Try
                DT = MYDB_ECM.GetDatatable(sqlCommand)
            Catch ex As Exception
                LOGGER.Warn("Error in FormatComboColumn: " & ex.Message)
                Exit Sub
            End Try

            comboItems.BeginUpdate()
            Try
                For Each row As DataRow In DT.Rows
                    Dim value As String = row.Item(0)
                    comboItems.Add(value)
                Next
            Finally
                comboItems.EndUpdate()
            End Try
        End If

        column.ColumnEdit = comboEdit
    End Sub

    Private Shared Sub FormatTextColumn(grid As GridView, column As GridColumn, controlId As Integer)
        Dim format As String = MYDB_ECM.GetScalarValue("SELECT FORMAT_TYPE FROM TBPMO_CONTROL WHERE GUID = " & controlId)

        If IsNothing(format) Then
            Exit Sub
        End If

        If (format = "Currency") Then
            column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom
            column.DisplayFormat.FormatString = "c"
        ElseIf (format = "Decimal") Then
            column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        End If
    End Sub

    Shared Sub FormatColumns(grid As GridView, formId As Integer)
        ' Dim SQL As String = String.Format("SELECT CONTROL_ID, CONTROL_COL_NAME, CTRLTYPE_ID, CONTROL_SQLCOMMAND_1 AS CONTROL_SQL, CONTROL_STATIC_LIST, CONTROL_SHOW_COLUMN FROM VWPMO_CONTROL_SCREEN WHERE CTRLTYPE_ID IN (3,4,10,11) AND FORM_ID = {0}", formId)
        Dim Expression As String = String.Format("CTRLTYPE_ID IN (3,4,10,11) AND FORM_ID = {0}", formId)
        Dim DT As DataTable = ClassHelper.FILTER_DATATABLE(DT_VWPMO_CONTROL_SCREEN, Expression, "")

        Dim dateEdit As New RepositoryItemDateEdit()
        Dim comboEdit As New RepositoryItemComboBox()
        Dim checkboxEdit = New RepositoryItemCheckEdit()

        ' Spalteneditoren dem Grid hinzufügen
        grid.GridControl.RepositoryItems.AddRange({dateEdit, comboEdit, checkboxEdit})

        For Each row As DataRow In DT.Rows
            Dim typeId = row.Item("CTRLTYPE_ID")
            Dim controlId = row.Item("CONTROL_ID")
            Dim columnName = row.Item("CONTROL_COL_NAME")
            Dim sqlCommand = row.Item("CONTROL_SQL")
            Dim staticList = row.Item("CONTROL_STATIC_LIST")
            Dim showColumn = row.Item("CONTROL_SHOW_COLUMN")

            Dim column As GridColumn = grid.Columns.Item(columnName)

            ' Überprüfen, ob der Spaltenname exisitiert
            If IsNothing(column) Then
                Resume Next
            End If

            ' Spalten ausblenden, die nicht angezeigt werden sollen
            If (showColumn = False) Then
                column.Visible = False
                Resume Next
            End If

            ' Editoren zuweisen
            Select Case typeId
                Case 2 'Textbox
                    FormatTextColumn(grid, column, controlId)
                Case 3 'Combobox
                    FormatComboColumn(grid, column, comboEdit, sqlCommand, staticList)
                Case 4  'Datum
                    FormatDateColumn(grid, column, dateEdit)
                Case 10, 11 'Checkbox & Radiobutton
                    FormatBooleanColumn(grid, column, checkboxEdit)
            End Select
        Next

        ' Spalten Statisch Verstecken
        HideColumnIfExists(grid.Columns.Item("Record-ID"))
        HideColumnIfExists(grid.Columns.Item("Form-ID"))
        HideColumnIfExists(grid.Columns.Item("ROW_COLOR"))
        HideColumnIfExists(grid.Columns.Item("files?"))
        HideColumnIfExists(grid.Columns.Item("file count"))

        ' Spalten Statisch ReadOnly Machen
        MakeReadOnlyIfExists(grid.Columns.Item("AddedWho"))
        MakeReadOnlyIfExists(grid.Columns.Item("AddedWhen"))
        MakeReadOnlyIfExists(grid.Columns.Item("ChangedWho"))
        MakeReadOnlyIfExists(grid.Columns.Item("ChangedWhen"))
    End Sub

    Private Shared Sub HideColumnIfExists(column As GridColumn)
        If Not IsNothing(column) Then
            column.Visible = False
        End If
    End Sub

    Shared Sub MakeReadOnlyIfExists(column As GridColumn)
        If Not IsNothing(column) Then
            column.OptionsColumn.AllowEdit = False
        End If
    End Sub
End Class
