Imports System.Text
Imports System.IO

Public Class frmChooseParentRecord
    Private selected As Boolean = False

    Private Sub frmChooseParentRecord_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Save_GridLayout()
        If selected = False Then
            CURRENT_PARENT_RECORD_ID = 0
        End If
    End Sub
    Private Sub frmChooseParentRecord_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            If Not IsNothing(CURRENT_PARENT_DT) Then
                GridControl.DataSource = CURRENT_PARENT_DT
                HideColumns()
                Load_GridLayout()
            End If
        Catch ex As Exception
            MsgBox("Unexpected Error in Load Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Sub HideColumns()
        Try
            Dim SQL As String = "SELECT CONTROL_ID, CONTROL_COL_NAME, CONTROL_SHOW_COLUMN FROM VWPMO_CONTROL_SCREEN WHERE CONTROL_SHOW_COLUMN = 0 AND FORM_ID = " & CURRENT_PARENT_ENTITY_ID
            Dim DT As DataTable = MYDB_ECM.GetDatatable(sql)

            Dim cols As DevExpress.XtraGrid.Columns.GridColumnCollection = GridView.Columns

            If DT.Rows.Count <> 0 Then
                ' Alle Spalten, die ausgeblendet werden sollten durchgehen und Visible = False
                For Each row As DataRow In DT.Rows
                    Dim colname As String = row.Item("CONTROL_COL_NAME")
                    Dim col As DevExpress.XtraGrid.Columns.GridColumn = GridView.Columns.Item(colname)
                    If Not IsNothing(col) Then
                        col.Visible = False
                    End If
                Next
            End If

            ' FormID und RecordID immer ausblenden
            GridView.Columns.Item("Record-ID").Visible = False
            GridView.Columns.Item("Form-ID").Visible = False
            GridView.Columns.Item("ROW_COLOR").Visible = False

        Catch ex As Exception
            MsgBox("Error in HideColumns:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub GridView_RowStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView.RowStyle
        If e.RowHandle = DevExpress.XtraGrid.GridControl.AutoFilterRowHandle Then
            e.Appearance.BackColor = Color.Orange
        Else
            If e.RowHandle = -1 Then
                Exit Sub
            End If
            Try
                Dim rowCellValue = GridView.GetRowCellValue(e.RowHandle, "ROW_COLOR")
                If rowCellValue <> "" Then
                    Dim ColorRow As Color
                    Try
                        ColorRow = Color.FromName(rowCellValue)
                    Catch ex As Exception
                        LOGGER.Warn("RowBackColor '" & rowCellValue & "'could not be converted from name!")
                        Exit Sub
                    End Try
                    'Dim c As Color = DirectCast(rowCellValue, Color)
                    e.Appearance.BackColor = ColorRow
                    e.HighPriority = True
                End If
            Catch ex As Exception
                LOGGER.Warn("Unexpected Error in RowStyle: " & ex.Message)
            End Try
        End If

    End Sub

    Private Sub btnAddLink_Click(sender As Object, e As EventArgs) Handles btnAddLink.Click
        Try
            Dim RecID = GridView.GetFocusedRowCellValue(GridView.Columns("Record-ID"))
            If IsNothing(RecID) Then
                MsgBox("Could not get the Record-ID (Nothing)", MsgBoxStyle.Exclamation)
            Else
                If RecID = 0 Then
                    MsgBox("Could not get the Record-ID (0)", MsgBoxStyle.Exclamation)
                    Console.WriteLine("Grid_RecordID = 0")
                Else
                    CURRENT_PARENT_RECORD_ID = RecID
                    selected = True
                    Me.Close()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in btnAddLink:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btncancel_Click(sender As Object, e As EventArgs) Handles btncancel.Click
        selected = False
        Me.Close()
    End Sub
    Sub Save_GridLayout()
        Try
            Dim Filename As String = String.Format("PARENT_GRID_{0}-UserLayout.xml", CURRENT_PARENT_ENTITY_ID.ToString)
            Dim XMLPATH = System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
            GridView.SaveLayoutToXml(XMLPATH)
        Catch ex As Exception

        End Try

    End Sub
    Sub Load_GridLayout()
        Try
            Dim Filename As String = String.Format("PARENT_GRID_{0}-UserLayout.xml", CURRENT_PARENT_ENTITY_ID.ToString)
            Dim XMLPATH = System.IO.Path.Combine(Application.UserAppDataPath(), Filename)
            If File.Exists(XMLPATH) Then
                GridView.RestoreLayoutFromXml(XMLPATH)
                GridView.GuessAutoFilterRowValuesFromFilter()
            End If
        Catch ex As Exception

        End Try

    End Sub
End Class