Imports System.Drawing
Imports System.Windows.Forms
Imports FlexCell.Grid
Imports FlexCell
Imports UTS_Core.UTSModule.Production

Namespace UTSModule.Station
    Public Class RowNodeGrid
        ''' <summary>
        '''  行节点表，当前选择行
        ''' </summary>
        Private _lastSingleRowSelRow As Integer

        ''' <summary>
        '''  行节点表，下拉框选择行
        ''' </summary>
        Private _grdSingleRowDropRow As Integer

        ''' <summary>
        ''' 行节点表，下拉框所在列
        ''' </summary>
        Private _grdSingleRowDropCol As Integer

        Private _grd As Grid

        ''' <summary>
        ''' 详细信息表格内容变更类型
        ''' </summary>
        Private _cellChanged As GridCellChangedEnum = GridCellChangedEnum.None

        Public Property RtxColTip As RichTextBox

        Private ReadOnly _planColManager As PlanColManager '测试列提示管理器
        Private ReadOnly _testCmdManager As TestCmdManager '测试命令管理器
        Private ReadOnly _errCodeManager As ErrCodeManager '错误代码管理器

        Private Property ActiveRowNode() As RowNode

        Public Event RowNodeTextChanged(ByVal sender As Object, ByVal e As RowNodeChangedEventArgs)     '自定义事件


        Public Sub New()
            _errCodeManager = ErrCodeManager.CreateManager()
            _testCmdManager = TestCmdManager.CreateManager()
            _planColManager = PlanColManager.CreateManager()
        End Sub

#Region "FlexCell"
        Public Property Grid() As Grid
            Get
                Return _grd
            End Get
            Set(value As Grid)
                _grd = value

                InitializeGrid()

                RemoveHandler _grd.CellChange, AddressOf Grid_CellChange
                RemoveHandler _grd.ComboClick, AddressOf Grid_ComboClick
                RemoveHandler _grd.ComboDropDown, AddressOf Grid_ComboDropDown
                RemoveHandler _grd.SelChange, AddressOf Grid_SelChange


                AddHandler _grd.CellChange, AddressOf Grid_CellChange
                AddHandler _grd.ComboClick, AddressOf Grid_ComboClick
                AddHandler _grd.ComboDropDown, AddressOf Grid_ComboDropDown
                AddHandler _grd.SelChange, AddressOf Grid_SelChange
            End Set
        End Property




        ''' <summary>
        ''' 初始化测试站表格
        ''' </summary>
        Public Sub InitializeGrid()
            With _grd
                .AutoRedraw = False

                .Rows = RowNames.Max '行数不确定
                .Cols = ColNames.Max

                .DisplayRowNumber = True    '首列显示数字
                .ExtendLastCol = True   '最后一列自动扩充
                .MultiSelect = False '是否允许选择多行

                .BorderStyle = BorderStyleEnum.None
                .DefaultFont = New Font("微软雅黑", 8)

                .Range(0, 0, 0, .Cols - 1).Font = New Font($"幼圆", 8) '首行样式


                For col As Integer = 0 To ColNames.Max - 1
                    .Cell(0, col).Text = [Enum].GetName(GetType(ColNames), col) '设置列名
                Next


                For i As Integer = 0 To RowNames.Max - 1
                    .Cell(i, ColNames.ColName).Text = [Enum].GetName(GetType(RowNames), i) '填充行内容
                Next

                .Column(ColNames.ColName).Locked = True


                .Cell(RowNames.RowType, ColNames.ColValue).CellType = CellTypeEnum.ComboBox
                .Cell(RowNames.ControlType, ColNames.ColValue).CellType = CellTypeEnum.ComboBox
                .Cell(RowNames.ErrorCode, ColNames.ColValue).CellType = CellTypeEnum.ComboBox
                .Cell(RowNames.Command, ColNames.ColValue).CellType = CellTypeEnum.ComboBox
                .Cell(RowNames.CommandType, ColNames.ColValue).CellType = CellTypeEnum.ComboBox
                .Cell(RowNames.ActionEn, ColNames.ColValue).CellType = CellTypeEnum.CheckBox
                .Cell(RowNames.Action, ColNames.ColValue).CellType = CellTypeEnum.CheckBox
                .Cell(RowNames.SaveToDb, ColNames.ColValue).CellType = CellTypeEnum.CheckBox

                .Cell(RowNames.ActionEn, ColNames.ColValue).Locked = True



                'CommandType 背景色
                .Range(RowNames.CommandType, ColNames.ColValue, RowNames.CommandType, ColNames.ColValue).BackColor = Color.LemonChiffon
                'Command 背景色
                .Range(RowNames.Command, ColNames.ColValue, RowNames.Command, ColNames.ColValue).BackColor = Color.Beige
                .Range(RowNames.Command, ColNames.ColValue, RowNames.Command, ColNames.ColValue).FontSize = 10
                .Range(RowNames.Command, ColNames.ColValue, RowNames.Command, ColNames.ColValue).FontBold = True

                'Description 背景色
                .Range(RowNames.Description, ColNames.ColValue, RowNames.Description, ColNames.ColValue).BackColor = Color.MistyRose
                .Range(RowNames.Description, ColNames.ColValue, RowNames.Description, ColNames.ColValue).FontSize = 10



                .ComboBox(0).Locked = True

                .AutoRedraw = True
                .Refresh()
            End With
        End Sub

#End Region


#Region "表格事件处理"
        Private _isUpload As Boolean
        Private _beforeNode As RowNode

        Private Sub Grid_CellChange(sender As Object, e As CellChangeEventArgs)
            If ActiveRowNode Is Nothing Then Return
            If _cellChanged = GridCellChangedEnum.None Then _cellChanged = GridCellChangedEnum.RowNodeInfo
            If _cellChanged <> GridCellChangedEnum.RowNodeInfo Then Return

            If Not _isUpload Then _beforeNode = ActiveRowNode.Clone
            _isUpload = True

            If e.Col = ColNames.ColValue Then
                Dim value As String = _grd.Cell(e.Row, e.Col).Text
                Dim changeType As RowNodeChangedEventArgs.RowNodeChangeType = RowNodeChangedEventArgs.RowNodeChangeType.None
                Select Case e.Row
                    Case RowNames.Action
                        ActiveRowNode.Action = _grd.Cell(e.Row, e.Col).BooleanValue
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Action
                    Case RowNames.Label
                        ActiveRowNode.Label = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Label
                    Case RowNames.CommandType
                        Dim cmdType As String = _grd.Cell(RowNames.CommandType, ColNames.ColValue).Text
                        If String.Compare(ActiveRowNode.CommandType, cmdType) <> 0 Then
                            ActiveRowNode.CommandType = cmdType
                            ActiveRowNode.Command = ""
                            ActiveRowNode.Parameters.Clear()
                            ActiveRowNode.Action = Not String.IsNullOrWhiteSpace(ActiveRowNode.Command)
                            CommandTypeChanged(ActiveRowNode)
                        End If
                        ActiveRowNode.CommandType = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.CommandType
                    Case RowNames.ControlType
                        ActiveRowNode.ControlType = _grd.Cell(RowNames.ControlType, ColNames.ColValue).Text
                    Case RowNames.Command
                        Dim cmdName As String = _grd.Cell(RowNames.Command, ColNames.ColValue).Text
                        If String.Compare(ActiveRowNode.Command, cmdName) <> 0 Then
                            ActiveRowNode.Command = cmdName
                            ActiveRowNode.Parameters.Clear()
                            ActiveRowNode.Action = Not String.IsNullOrWhiteSpace(ActiveRowNode.Command)

                            '拷贝所有参数到当前节点信息中
                            Dim planCommand As TestCmd = _testCmdManager.GetCommand(ActiveRowNode.CommandType, ActiveRowNode.Command)
                            If planCommand Is Nothing Then Return
                            For Each cmdParam As TestCmdParam In planCommand.Params
                                ActiveRowNode.Parameters.Add(cmdParam.Clone())
                            Next

                            '更新行节点信息表格内容
                            CommandChanged(ActiveRowNode)
                        End If

                        ActiveRowNode.Command = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Command
                    Case RowNames.ErrorCode
                        Dim codeMsg As String = _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).Text
                        Dim errCode As String = ErrCodeManager.CodeMsgToCode(codeMsg)
                        If String.Compare(ActiveRowNode.ErrorCode, errCode) <> 0 Then
                            ActiveRowNode.ErrorCode = errCode
                            ActiveRowNode.ErrorMessage = _errCodeManager(ActiveRowNode.ErrorCode).Msg
                            _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).Text = errCode
                            _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).BackColor = _errCodeManager(ActiveRowNode.ErrorCode).Color
                            _grd.Cell(RowNames.ErrorMessage, ColNames.ColValue).BackColor = _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).BackColor
                            ErrCodeChanged(ActiveRowNode)
                        End If
                        ActiveRowNode.ErrorCode = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.ErrorCode
                    Case RowNames.ErrorMessage
                        ActiveRowNode.ErrorMessage = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.ErrorMessage

                    Case RowNames.RowType
                        ActiveRowNode.RowType = CType([Enum].Parse(GetType(RowNode.RowTypeEnum), value), RowNode.RowTypeEnum)
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.RowType
                    Case RowNames.Description
                        ActiveRowNode.Description = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Description
                    Case RowNames.SaveToDb
                        ActiveRowNode.SaveToDb = _grd.Cell(e.Row, e.Col).BooleanValue
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.SaveToDb
                    Case RowNames.RecordName
                        ActiveRowNode.RecordName = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.RecordName
                    Case RowNames.Retry
                        ActiveRowNode.Retry = CInt(value)
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Retry
                    Case RowNames.RetryInterval
                        ActiveRowNode.RetryInterval = CInt(value)
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.RetryInterval

                    Case >= RowNames.Parameters
                        If e.Row - RowNames.Parameters > ActiveRowNode.Parameters.Count - 1 Then Return
                        ActiveRowNode.Parameters(e.Row - RowNames.Parameters).Value = value
                        changeType = RowNodeChangedEventArgs.RowNodeChangeType.Parameters
                End Select

                If changeType = RowNodeChangedEventArgs.RowNodeChangeType.None Then Return

                If _beforeNode IsNot Nothing Then
                    Dim args As New RowNodeChangedEventArgs With {
                        .Node = ActiveRowNode,
                        .BeforeNode = _beforeNode,
                        .ChangeType = changeType
                    }
                    RaiseEvent RowNodeTextChanged(sender, args)
                End If
            End If
            _isUpload = False
            _cellChanged = GridCellChangedEnum.None
        End Sub

        ''' <summary>
        ''' 单行信息表格命令修改后,同步刷新本身信息
        ''' </summary>
        Private Sub CommandChanged(node As RowNode)
            _grd.Rows = RowNames.Parameters + node.Parameters.Count
            For idx As Integer = 0 To node.Parameters.Count - 1
                Dim row As Integer = RowNames.Parameters + idx

                _grd.Cell(row, ColNames.ColName).Text = node.Parameters(idx).Desc
                _grd.Cell(row, ColNames.ColValue).Text = node.Parameters(idx).Value
            Next

            _grd.Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).BackColor = Color.LightGray
            _grd.Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).Alignment = AlignmentEnum.RightCenter
        End Sub


        Private Sub CommandTypeChanged(node As RowNode)
            _grd.Rows = RowNames.Parameters + 1

            _grd.Cell(RowNames.Command, ColNames.ColValue).Text = node.Command
            _grd.Cell(RowNames.Parameters, ColNames.ColName).Text = RowNames.Parameters.ToString()
            _grd.Cell(RowNames.Parameters, ColNames.ColValue).Text = ""

            _grd.Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).BackColor = Color.LightGray
            _grd.Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).Alignment = AlignmentEnum.RightCenter
        End Sub

        Private Sub ErrCodeChanged(node As RowNode)
            _grd.Cell(RowNames.ErrorMessage, ColNames.ColValue).Text = node.ErrorMessage
        End Sub

        ''' <summary>
        ''' 下拉框选择对象事件
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Grid_ComboClick(sender As Object, e As ComboClickEventArgs)
            'If ActiveRowNode Is Nothing Then Return
            'Dim changeType As RowNodeChangedEventArgs.RowNodeChangeType = RowNodeChangedEventArgs.RowNodeChangeType.None
            'Dim beforeNode As RowNode = ActiveRowNode.Clone
            'Select Case _grdSingleRowDropRow
            '    Case RowNames.ControlType
            '        ActiveRowNode.ControlType = _grd.Cell(RowNames.ControlType, ColNames.ColValue).Text
            '        changeType = RowNodeChangedEventArgs.RowNodeChangeType.ControlType
            '    Case RowNames.CommandType
            '        '如果命令类型变化则,清空命令
            '        Dim cmdType As String = _grd.Cell(RowNames.CommandType, ColNames.ColValue).Text
            '        If String.Compare(ActiveRowNode.CommandType, cmdType) <> 0 Then
            '            ActiveRowNode.CommandType = cmdType
            '            ActiveRowNode.Command = ""
            '            ActiveRowNode.Parameters.Clear()
            '            CommandTypeChanged(ActiveRowNode)
            '            changeType = RowNodeChangedEventArgs.RowNodeChangeType.CommandType
            '        End If
            '    Case RowNames.Command
            '        Dim cmdName As String = _grd.Cell(RowNames.Command, ColNames.ColValue).Text
            '        If String.Compare(ActiveRowNode.Command, cmdName) <> 0 Then
            '            ActiveRowNode.Command = cmdName
            '            ActiveRowNode.Parameters.Clear()

            '            '拷贝所有参数到当前节点信息中
            '            Dim planCommand As TestCmd = _testCmdManager.GetCommand(ActiveRowNode.CommandType, ActiveRowNode.Command)
            '            If planCommand Is Nothing Then Return
            '            For Each cmdParam As TestCmdParam In planCommand.Params
            '                ActiveRowNode.Parameters.Add(cmdParam.Clone())
            '            Next

            '            '更新行节点信息表格内容
            '            CommandChanged(ActiveRowNode)
            '            changeType = RowNodeChangedEventArgs.RowNodeChangeType.Command
            '        End If
            '    Case RowNames.ErrorCode
            '        Dim codeMsg As String = _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).Text
            '        Dim errCode As String = ErrCodeManager.CodeMsgToCode(codeMsg)
            '        If String.Compare(ActiveRowNode.ErrorCode, errCode) <> 0 Then
            '            ActiveRowNode.ErrorCode = errCode
            '            ActiveRowNode.ErrorMessage = _errCodeManager(ActiveRowNode.ErrorCode).Msg
            '            _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).Text = errCode
            '            _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).BackColor = _errCodeManager(ActiveRowNode.ErrorCode).Color
            '            _grd.Cell(RowNames.ErrorMessage, ColNames.ColValue).BackColor = _grd.Cell(RowNames.ErrorCode, ColNames.ColValue).BackColor
            '            ErrCodeChanged(ActiveRowNode)
            '            changeType = RowNodeChangedEventArgs.RowNodeChangeType.ErrorCode
            '        End If
            'End Select

            'If changeType = RowNodeChangedEventArgs.RowNodeChangeType.None Then Return

            'Dim args As New RowNodeChangedEventArgs With {
            '    .Node = ActiveRowNode,
            '    .BeforeNode = beforeNode,
            '    .ChangeType = changeType
            '}
            'RaiseEvent RowNodeTextChanged(sender, args)
        End Sub

        ''' <summary>
        ''' 表格下拉框出现时触发事件
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Grid_ComboDropDown(sender As Object, e As ComboDropDownEventArgs)
            _grdSingleRowDropRow = e.Row
            _grdSingleRowDropCol = e.Col

            Select Case e.Row
                Case RowNames.ControlType
                    _grd.ComboBox(0).Items.Clear()
                    If ActiveRowNode.ParentNode.ControlType = "If" Then
                        _grd.ComboBox(0).Items.Add("Then")
                        _grd.ComboBox(0).Items.Add("ElseIf")
                        _grd.ComboBox(0).Items.Add("Else")
                    Else
                        _grd.ComboBox(0).Items.Add("While")
                        _grd.ComboBox(0).Items.Add("If")
                    End If
                Case RowNames.CommandType
                    _grd.ComboBox(0).Items.Clear()
                    _grd.ComboBox(0).Items.AddRange(_testCmdManager.GetCommandTypes())
                Case RowNames.Command
                    _grd.ComboBox(0).Items.Clear()
                    _grd.ComboBox(0).Items.AddRange(_testCmdManager.GetCommandNames(ActiveRowNode.CommandType))
                Case RowNames.ErrorCode
                    _grd.ComboBox(0).Items.Clear()
                    _grd.ComboBox(0).Items.AddRange(_errCodeManager.GetAllCodeAndMsg())
                Case RowNames.RowType
                    _grd.ComboBox(0).Items.Clear()
                    _grd.ComboBox(0).Items.Add(RowNode.RowTypeEnum.Control)
                    _grd.ComboBox(0).Items.Add(RowNode.RowTypeEnum.Flow)
                    _grd.ComboBox(0).Items.Add(RowNode.RowTypeEnum.Module)
                Case RowNames.Parameters '特殊处理Call命令
                    _grd.ComboBox(0).Items.Clear()
                    _grd.ComboBox(0).Items.AddRange(UTS_Core.UTSModule.Test.UtsTester.CreateTester.GetCustomModuleName())
            End Select
        End Sub

        ''' <summary>
        ''' 更新流程列名提示
        ''' </summary>
        ''' <param name="colName"></param>
        Private Sub UpdatePlanColNameTip(colName As String)
            If _planColManager.IndexOf(colName) > 0 Then
                Dim planCol As PlanCol = _planColManager(colName)

                RtxColTip.SuspendLayout()
                RtxColTip.Clear()

                RtxColTip.AppendText($"{planCol.ColName}{vbCrLf}")
                RtxColTip.AppendText($"类型：{planCol.ColType}{vbCrLf}")
                RtxColTip.AppendText($"描述：{planCol.ColDesc}{vbCrLf}")
                RtxColTip.ResumeLayout(False)
            End If
        End Sub


        ''' <summary>
        ''' 更新流程命令提示
        ''' </summary>
        ''' <param name="colName"></param>
        ''' <param name="commandType"></param>
        ''' <param name="commandName"></param>
        ''' <param name="paramIndex"></param>
        Private Sub UpdateCommandTip(colName As String, commandType As String, commandName As String, paramIndex As Integer)

            Dim planCommand As TestCmd = _testCmdManager.GetCommand(commandType, commandName)
            If planCommand IsNot Nothing Then
                RtxColTip.SuspendLayout()
                RtxColTip.Clear()

                RtxColTip.AppendText($"{colName}{vbCrLf}")
                RtxColTip.AppendText($"类型：{planCommand.Params(paramIndex).Type}{vbCrLf}")
                RtxColTip.AppendText($"上限：{planCommand.Params(paramIndex).UpperLimit}{vbCrLf}")
                RtxColTip.AppendText($"下限：{planCommand.Params(paramIndex).LowerLimit}{vbCrLf}")
                RtxColTip.AppendText($"描述：{planCommand.Params(paramIndex).Desc}{vbCrLf}")
                RtxColTip.ResumeLayout(False)
            Else
                RtxColTip.AppendText($"{colName}{vbCrLf}")
            End If
        End Sub


        ''' <summary>
        ''' 单行信息表格更新当前行
        ''' </summary>
        ''' <param name="row"></param>
        Private Sub UpdateSingleRowTip(row As Integer)
            Dim colName As String = _grd.Cell(row, ColNames.ColName).Text

            If row >= RowNames.Parameters Then
                Dim commandType As String = _grd.Cell(RowNames.CommandType, ColNames.ColValue).Text
                Dim command As String = _grd.Cell(RowNames.Command, ColNames.ColValue).Text

                If String.IsNullOrEmpty(command) Then
                    UpdatePlanColNameTip(colName)
                Else
                    UpdateCommandTip(colName, commandType, command, row - RowNames.Parameters)
                End If
            Else
                UpdatePlanColNameTip(colName)
            End If

        End Sub


        Private Sub SingleRowSelChange(row As Integer)
            If _lastSingleRowSelRow = row Then Return
            If _lastSingleRowSelRow >= _grd.Rows Then Return
            '新增命令切换处理

            _grd.Cell(_lastSingleRowSelRow, ColNames.ColName).FontBold = False

            _grd.Cell(row, ColNames.ColName).FontBold = True

            UpdateSingleRowTip(row) '更新列提示

            _lastSingleRowSelRow = row
        End Sub

        Private Sub Grid_SelChange(sender As Object, e As SelChangeEventArgs)
            SingleRowSelChange(e.FirstRow)
        End Sub
#End Region


#Region "外部事件处理"
        ''' <summary>
        ''' 主表格选中单元格时触发事件
        ''' </summary>
        ''' <param name="node"></param>
        ''' <param name="LineNumber"></param>
        ''' <param name="LineActionEn"></param>
        Public Sub AfterSelectUpdateGrid(node As RowNode, LineNumber As Integer, LineActionEn As Boolean)
            Dim col As Integer = ColNames.ColValue
            Dim textColor As Color
            Dim RecordNameEn As Boolean

            _grd.AutoRedraw = False

            With _grd
                .SuspendLayout()

                .Column(col).Locked = Not node.CanChangeContent '是否允许编辑

                If node.Parameters.Count > 0 Then
                    .Rows = RowNames.Parameters + node.Parameters.Count

                    'todo:特殊处理Call命令
                    If node.Command = "Call" Then
                        .Cell(RowNames.Parameters, col).CellType = CellTypeEnum.ComboBox
                    Else
                        .Cell(RowNames.Parameters, col).CellType = CellTypeEnum.TextBox
                    End If

                    For i As Integer = 0 To node.Parameters.Count - 1
                        .Cell(RowNames.Parameters + i, ColNames.ColName).Text = node.Parameters(i).Desc
                        .Cell(RowNames.Parameters + i, col).Text = node.Parameters(i).Value
                    Next

                Else
                    .Rows = RowNames.Parameters + 1

                    .Cell(RowNames.Parameters, ColNames.ColName).Text = RowNames.Parameters.ToString()
                    .Cell(RowNames.Parameters, col).CellType = CellTypeEnum.TextBox
                    .Cell(RowNames.Parameters, col).Text = ""
                End If

                .Cell(RowNames.LineNumber, col).Text = LineNumber.ToString
                .Cell(RowNames.RowType, col).Text = node.RowType.ToString()
                .Cell(RowNames.ActionEn, col).Text = LineActionEn.ToString()
                .Cell(RowNames.Action, col).Text = IIf(node.Action, "1", "0").ToString()
                .Cell(RowNames.ErrorCode, col).Text = node.ErrorCode.ToString()
                .Cell(RowNames.ErrorMessage, col).Text = node.ErrorMessage.ToString()

                .Cell(RowNames.Command, col).Text = node.Command.ToString()
                .Cell(RowNames.CommandType, col).Text = node.CommandType.ToString()
                .Cell(RowNames.[Label], col).Text = node.[Label].ToString()
                .Cell(RowNames.Description, col).Text = node.Description.ToString()

                .Cell(RowNames.SaveToDb, col).Text = node.SaveToDb.ToString()
                .Cell(RowNames.RecordName, col).Text = node.RecordName.ToString()

                .Cell(RowNames.Retry, col).Text = node.Retry.ToString()
                .Cell(RowNames.RetryInterval, col).Text = node.RetryInterval.ToString()

                textColor = setRowTextForeColor(.Cell(RowNames.CommandType, col).Text, LineActionEn, node.RowType)

                .Cell(RowNames.CommandType, col).ForeColor = textColor
                .Cell(RowNames.RecordName, col).ForeColor = textColor

                Select Case node.RowType
                    Case RowNode.RowTypeEnum.FixedModule
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).BackColor = Color.PeachPuff
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).ForeColor = Color.Blue
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).FontBold = True
                    Case Else
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).BackColor = Color.White
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).ForeColor = textColor
                        .Range(RowNames.RowType, col, RowNames.RowType, ColNames.Max - 1).FontBold = False
                End Select


                If String.IsNullOrWhiteSpace(node.ErrorCode) = False Then
                    .Cell(RowNames.ErrorCode, col).BackColor = _errCodeManager(node.ErrorCode).Color
                    .Cell(RowNames.ErrorMessage, col).BackColor = .Cell(RowNames.ErrorCode, col).BackColor
                Else
                    .Cell(RowNames.ErrorCode, col).BackColor = Color.White
                    .Cell(RowNames.ErrorMessage, col).BackColor = Color.White
                End If

                ' .Range(RowNames.Label, ColNames.ColValue, _grd.Rows - 1, ColNames.ColValue).BackColor = Color.White
                .Range(RowNames.Label, ColNames.ColValue, _grd.Rows - 1, ColNames.ColValue).ForeColor = textColor
                ' .Range(RowNames.Label, ColNames.ColValue, _grd.Rows - 1, ColNames.ColValue).FontBold = False


                RecordNameEn = LineActionEn And .Cell(RowNames.SaveToDb, col).BooleanValue
                textColor = setRowTextForeColor(.Cell(RowNames.CommandType, col).Text, RecordNameEn, node.RowType)
                .Cell(RowNames.RecordName, col).ForeColor = textColor

                .Cell(RowNames.Command, ColNames.ColValue).ForeColor = Color.Blue

                .Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).BackColor = Color.LightGray
                .Range(RowNames.LineNumber, ColNames.ColName, _grd.Rows - 1, ColNames.ColName).Alignment = AlignmentEnum.RightCenter

                .ResumeLayout(False)
                .PerformLayout()
            End With


            _grd.AutoRedraw = True
            _grd.Refresh()
        End Sub
        ''' <summary>
        ''' 根据CmdType 和 isAction 返回该行字体颜色
        ''' </summary>
        ''' <returns></returns>
        Public Function setRowTextForeColor(rowCmdType As String, isAction As Boolean, Optional rowNodeType As Integer = 3) As Color
            If isAction = True Then
                If rowNodeType = RowNode.RowTypeEnum.Flow Then
                    Select Case rowCmdType
                        Case "System"
                            Return Color.DarkSlateGray
                        Case "ComPort"
                            Return Color.DarkCyan
                        Case "UtsComPort"
                            Return Color.SeaGreen
                        Case "Converter"
                            Return Color.DarkOrange
                        Case "Process"
                            Return Color.DarkBlue
                        Case Else
                            Return Color.Black
                    End Select
                ElseIf rowNodeType = RowNode.RowTypeEnum.Module Then
                    Return Color.DeepPink
                ElseIf rowNodeType = RowNode.RowTypeEnum.FixedModule Then
                    Return Color.Blue
                End If
            Else
                Return Color.LightGray
            End If
        End Function

        Public Sub Grid_PlanNodeSelectChanged(sender As Object, ByVal e As PlanNodeSelectChangedEventArgs)
            If e.Node Is Nothing Then Return

            If _cellChanged = GridCellChangedEnum.None Then _cellChanged = GridCellChangedEnum.SelectChange

            If _cellChanged = GridCellChangedEnum.SelectChange Then
                ActiveRowNode = e.Node

                AfterSelectUpdateGrid(ActiveRowNode, e.LineNumber, e.LineActionEn)

                _cellChanged = GridCellChangedEnum.None
            End If
        End Sub

        Public Sub RowNodeTextChangedUpdateGrid(e As RowNodeChangedEventArgs)
            Dim col As Integer = ColNames.ColValue
            Dim node As RowNode = e.Node

            With _grd
                Select Case e.ChangeType
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Action
                        .Cell(RowNames.Action, col).Text = IIf(node.Action, "1", "0").ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.SaveToDb
                        .Cell(RowNames.SaveToDb, col).Text = IIf(node.SaveToDb, "1", "0").ToString
                    Case RowNodeChangedEventArgs.RowNodeChangeType.RowType
                        .Cell(RowNames.RowType, col).Text = node.RowType.ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Label
                        .Cell(RowNames.[Label], col).Text = node.[Label].ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Description
                        .Cell(RowNames.Description, col).Text = node.Description.ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.CommandType
                        .Cell(RowNames.CommandType, col).Text = node.CommandType.ToString()
                        CommandTypeChanged(node)
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Command
                        .Cell(RowNames.Command, col).Text = node.Command.ToString()
                        CommandChanged(node)
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Parameters
                        If node.Parameters.Count > 0 Then
                            .Rows = RowNames.Parameters + node.Parameters.Count

                            For i As Integer = 0 To node.Parameters.Count - 1
                                .Cell(RowNames.Parameters + i, ColNames.ColName).Text = node.Parameters(i).Desc
                                .Cell(RowNames.Parameters + i, col).Text = node.Parameters(i).Value
                            Next
                        Else
                            .Rows = RowNames.Parameters + 1

                            .Cell(RowNames.Parameters, ColNames.ColName).Text = RowNames.Parameters.ToString()
                            .Cell(RowNames.Parameters, col).Text = ""
                        End If
                    Case RowNodeChangedEventArgs.RowNodeChangeType.RecordName
                        .Cell(RowNames.RecordName, col).Text = node.RecordName.ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.Retry
                        .Cell(RowNames.Retry, col).Text = node.Retry.ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.RetryInterval
                        .Cell(RowNames.RetryInterval, col).Text = node.RetryInterval.ToString()
                    Case RowNodeChangedEventArgs.RowNodeChangeType.ErrorCode
                        .Cell(RowNames.ErrorCode, col).Text = node.ErrorCode
                        If String.IsNullOrWhiteSpace(node.ErrorCode) = False Then
                            .Cell(RowNames.ErrorCode, col).BackColor = _errCodeManager(node.ErrorCode).Color
                            .Cell(RowNames.ErrorMessage, col).BackColor = .Cell(RowNames.ErrorCode, col).BackColor
                        Else
                            .Cell(RowNames.ErrorCode, col).BackColor = Color.White
                            .Cell(RowNames.ErrorMessage, col).BackColor = Color.White
                        End If
                    Case RowNodeChangedEventArgs.RowNodeChangeType.ErrorMessage
                        .Cell(RowNames.ErrorMessage, col).Text = node.ErrorMessage.ToString()
                End Select
            End With

        End Sub

        Public Sub Grid_RowNodeTextChanged(sender As Object, e As RowNodeChangedEventArgs)
            If _cellChanged = GridCellChangedEnum.None Then _cellChanged = GridCellChangedEnum.TextChange
            If _cellChanged <> GridCellChangedEnum.TextChange Then Return

            RowNodeTextChangedUpdateGrid(e)

            _cellChanged = GridCellChangedEnum.None
        End Sub
#End Region


        Public Enum ColNames
            Sn
            ColName
            ColValue
            Max
        End Enum

        Public Enum RowNames
            ColName

            LineNumber
            RowType

            [Label]
            ControlType
            Description

            ActionEn
            Action
            SaveToDb
            RecordName

            Retry
            RetryInterval

            ErrorCode
            ErrorMessage

            CommandType
            Command
            Parameters

            Max
        End Enum

        ''' <summary>
        ''' 详细信息表格内容变更类型枚举值
        ''' </summary>
        Enum GridCellChangedEnum
            ''' <summary>
            ''' 未变更
            ''' </summary>
            None
            ''' <summary>
            ''' 加载流程时时引起的变更
            ''' </summary>
            TestPlan
            ''' <summary>
            ''' 行节点信息修改引起的变更
            ''' </summary>
            RowNodeInfo
            ''' <summary>
            ''' 切换选择节点引起的变更
            ''' </summary>
            SelectChange
            ''' <summary>
            ''' 关联节点内容变更
            ''' </summary>
            TextChange
        End Enum

    End Class
End Namespace