<UserControl
    x:Class="BDU.Views.SingleStepEdit"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:BDU.Views"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:converters="clr-namespace:BDU.Converters"
    d:DataContext="{d:DesignInstance Type=local:SingleStepEdit}"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <UserControl.Resources>
        <converters:FilteredParametersConverter x:Key="FilteredParametersConverter"/>
        <converters:HexToDecimalConverter x:Key="HexToDecimalConverter"/>
        <!-- 定义转换器资源 -->
    </UserControl.Resources>
    <Grid>
        <GroupBox
            Grid.Row="1"
            Grid.Column="4"
            Header="单步编辑">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>

                <!--  步骤名称  -->
                <StackPanel Grid.Row="0">
                    <StackPanel
                        Height="30"
                        Margin="7"
                        Orientation="Horizontal">
                        <Label
                            Width="60"
                            VerticalAlignment="Bottom"
                            Content="名称" />
                        <TextBox
                            MinWidth="120"
                            VerticalAlignment="Bottom"
                            Text="{Binding SelectedStep.Name}" />
                    </StackPanel>
                </StackPanel>

                <!--  合格条件  -->
                <StackPanel Grid.Row="1">
                    <StackPanel
                        Height="30"
                        Margin="7"
                        Orientation="Horizontal">
                        <Label
                            Width="60"
                            VerticalAlignment="Bottom"
                            Content="合格条件" />
                        <TextBox
                            MinWidth="120"
                            VerticalAlignment="Bottom"
                            Text="{Binding SelectedStep.OKExpression}" />
                    </StackPanel>
                </StackPanel>

                <!--  合格条件  -->
                <StackPanel Grid.Row="2">
                    <StackPanel
                        Height="30"
                        Margin="7"
                        Orientation="Horizontal">
                        <Label
                            Width="60"
                            VerticalAlignment="Bottom"
                            Content="跳转"
                            ToolTip="格式：OK跳转序号/NG跳转序号（默认为0/0）" />
                        <TextBox
                            MinWidth="120"
                            VerticalAlignment="Bottom"
                            Text="{Binding SelectedStep.ID, Converter={StaticResource ParameterToGotoSettingStringConverter}}" />
                    </StackPanel>
                </StackPanel>

                <!--  备注  -->
                <StackPanel Grid.Row="3">
                    <StackPanel
                        Height="30"
                        Margin="7"
                        Orientation="Horizontal">
                        <Label
                            Width="60"
                            VerticalAlignment="Bottom"
                            Content="备注" />
                        <TextBox
                            MinWidth="120"
                            VerticalAlignment="Bottom"
                            Text="{Binding SelectedStep.Description}" />
                    </StackPanel>
                </StackPanel>

                <!--  参数编辑区  -->
                <Grid Grid.Row="4">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <StackPanel Height="30" Orientation="Horizontal">
                        <Label
                            Margin="7,0"
                            VerticalAlignment="Bottom"
                            Content="参数" />
                    </StackPanel>
                    <ScrollViewer
                        Grid.Row="1"
                        Margin="40,0,0,0"
                        HorizontalScrollBarVisibility="Auto"
                        VerticalScrollBarVisibility="Auto">
                        <StackPanel>
                            <ItemsControl ItemsSource="{Binding SelectedStep.Method.Parameters}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid>
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="50" />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel
                                                Grid.Column="0"
                                                Margin="7,7,0,7"
                                                Orientation="Horizontal">
                                                <TextBlock VerticalAlignment="Bottom" Text="{Binding Name}" />
                                                <TextBlock VerticalAlignment="Bottom" Text="(" />
                                                <TextBlock VerticalAlignment="Bottom" Text="{Binding Type.Name}" />
                                                <TextBlock VerticalAlignment="Bottom" Text=")" />
                                            </StackPanel>
                                            <StackPanel
                                                Grid.Column="1"
                                                Margin="7"
                                                Orientation="Horizontal">
                                                <TextBlock
                                                    Margin="5,0"
                                                    VerticalAlignment="Bottom"
                                                    Text="{Binding Category, Converter={StaticResource ParameterCategoryToStringConverter}}" />
                                            </StackPanel>
                                            <StackPanel
                                                Grid.Column="2"
                                                Margin="7"
                                                Orientation="Horizontal">
                                                <!--  输入参数编辑  -->
                                                <StackPanel
                                                    Height="30"
                                                    IsEnabled="{Binding Type, Converter={StaticResource ParameterTypeToBoolConverter}}"
                                                    Orientation="Horizontal"
                                                    Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}}">
                                                    <CheckBox VerticalAlignment="Bottom" IsChecked="{Binding IsUseVar}" />
                                                    <Grid>
                                                        <!--  常量输入区域  -->
                                                        <Grid Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverse}">
                                                            <!--  非枚举类型  -->
                                                            <TextBox
                                                                Height="22"
                                                                MinWidth="120"
                                                                VerticalAlignment="Bottom"
                                                                Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
                                                                Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}, ConverterParameter=Collapse}" />
                                                            <!-- "{Binding Value, Converter={StaticResource HexToDecimalConverter}, UpdateSourceTrigger=PropertyChanged}" -->

                                                            <!--  枚举类型  -->
                                                            <ComboBox
                                                                Height="22"
                                                                MinWidth="120"
                                                                VerticalAlignment="Bottom"
                                                                ItemsSource="{Binding Type, Converter={StaticResource EnumValuesConverter}}"
                                                                Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}}">
                                                                <ComboBox.SelectedItem>
                                                                    <MultiBinding Converter="{StaticResource EnumValueConverter}">
                                                                        <Binding Path="Type" />
                                                                        <Binding Path="Value" />
                                                                    </MultiBinding>
                                                                </ComboBox.SelectedItem>
                                                            </ComboBox>
                                                        </Grid>

                                                        <!--  变量选择框  -->
                                                        <ComboBox
                                                            Height="22"
                                                            MinWidth="120"
                                                            VerticalAlignment="Bottom"
                                                            SelectedValue="{Binding VariableName}"
                                                            SelectedValuePath="Name"
                                                            Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                            <!-- 修改开始：使用 FilteredParametersConverter -->
                                                            <ComboBox.ItemsSource>
                                                                <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                    <Binding Path="Type" />
                                                                    <Binding Path="DataContext.Program.Parameters" RelativeSource="{RelativeSource AncestorType=Window}" />
                                                                </MultiBinding>
                                                            </ComboBox.ItemsSource>
                                                            <ComboBox.ItemTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <TextBlock Text="{Binding Name}" />
                                                                        <TextBlock Text="(" />
                                                                        <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                        <TextBlock Text=")" />
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </ComboBox.ItemTemplate>
                                                            <!-- 修改结束 -->
                                                        </ComboBox>

                                                    </Grid>
                                                </StackPanel>

                                                <!--  输出参数编辑  -->
                                                <StackPanel Height="30" Orientation="Horizontal">
                                                    <CheckBox VerticalAlignment="Bottom" 
                                                              IsChecked="{Binding IsOutputToReport}" 
                                                              Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}"
                                                              ToolTip="勾选后，该输出参数的结果会写入到报告中" />
                                                    <ComboBox
                                                        Width="120"
                                                        Margin="0,0,0,0"
                                                        VerticalAlignment="Bottom"
                                                        SelectedValue="{Binding VariableName}"
                                                        SelectedValuePath="Name"
                                                        Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}">
                                                        <!-- 修改开始：使用 FilteredParametersConverter -->
                                                        <ComboBox.ItemsSource>
                                                            <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                <Binding Path="Type" />
                                                                <Binding Path="DataContext.Program.Parameters" RelativeSource="{RelativeSource AncestorType=Window}" />
                                                            </MultiBinding>
                                                        </ComboBox.ItemsSource>
                                                        <ComboBox.ItemTemplate>
                                                            <DataTemplate>
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding Name}" />
                                                                    <TextBlock Text="(" />
                                                                    <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                    <TextBlock Text=")" />
                                                                </StackPanel>
                                                            </DataTemplate>
                                                        </ComboBox.ItemTemplate>
                                                        <!-- 修改结束 -->
                                                    </ComboBox>
                                                </StackPanel>

                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <ItemsControl ItemsSource="{Binding SelectedStep.SubProgram.Parameters}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Item}">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="100" />
                                                <ColumnDefinition Width="50" />
                                                <ColumnDefinition />
                                            </Grid.ColumnDefinitions>
                                            <StackPanel
                                                Grid.Column="0"
                                                Margin="7,7,0,7"
                                                Orientation="Horizontal">
                                                <TextBlock VerticalAlignment="Bottom" Text="{Binding Name}" />
                                                <TextBlock VerticalAlignment="Bottom" Text="(" />
                                                <TextBlock VerticalAlignment="Bottom" Text="{Binding Type.Name}" />
                                                <TextBlock VerticalAlignment="Bottom" Text=")" />
                                            </StackPanel>
                                            <StackPanel
                                                Grid.Column="1"
                                                Margin="7"
                                                Orientation="Horizontal">
                                                <TextBlock
                                                    Margin="5,0"
                                                    VerticalAlignment="Bottom"
                                                    Text="{Binding Category, Converter={StaticResource ParameterCategoryToStringConverter}}" />
                                            </StackPanel>
                                            <StackPanel
                                                Grid.Column="2"
                                                Margin="7"
                                                Orientation="Horizontal">
                                                <!--  输入参数编辑  -->
                                                <StackPanel
                                                    Height="30"
                                                    Orientation="Horizontal"
                                                    Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}}">
                                                    <CheckBox VerticalAlignment="Bottom" IsChecked="{Binding IsUseVar}" />
                                                    <Grid>
                                                        <!--  常量输入区域  -->
                                                        <Grid Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=Inverse}">
                                                            <!--  非枚举类型  -->
                                                            <TextBox
                                                                Height="22"
                                                                MinWidth="120"
                                                                VerticalAlignment="Bottom"
                                                                Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}"
                                                                Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}, ConverterParameter=Collapse}" />

                                                            <!--  枚举类型  -->
                                                            <ComboBox
                                                                Height="22"
                                                                MinWidth="120"
                                                                VerticalAlignment="Bottom"
                                                                ItemsSource="{Binding Type, Converter={StaticResource EnumValuesConverter}}"
                                                                SelectedItem="{Binding Value}"
                                                                Visibility="{Binding Type, Converter={StaticResource IsEnumTypeConverter}}" />
                                                        </Grid>

                                                        <!--  变量选择框  -->
                                                        <ComboBox
                                                            Height="22"
                                                            MinWidth="120"
                                                            VerticalAlignment="Bottom"
                                                            SelectedValue="{Binding VariableName}"
                                                            SelectedValuePath="Name"
                                                            Visibility="{Binding IsUseVar, Converter={StaticResource BooleanToVisibilityConverter}}">
                                                            <!-- 修改开始：使用 FilteredParametersConverter -->
                                                            <ComboBox.ItemsSource>
                                                                <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                    <Binding Path="Type" />
                                                                    <Binding Path="DataContext.Program.Parameters" RelativeSource="{RelativeSource AncestorType=Window}" />
                                                                </MultiBinding>
                                                            </ComboBox.ItemsSource>
                                                            <ComboBox.ItemTemplate>
                                                                <DataTemplate>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <TextBlock Text="{Binding Name}" />
                                                                        <TextBlock Text="(" />
                                                                        <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                        <TextBlock Text=")" />
                                                                    </StackPanel>
                                                                </DataTemplate>
                                                            </ComboBox.ItemTemplate>
                                                            <!-- 修改结束 -->
                                                        </ComboBox>
                                                    </Grid>
                                                </StackPanel>

                                                <!--  输出参数编辑  -->
                                                <StackPanel Height="30" Orientation="Horizontal">
                                                    <CheckBox VerticalAlignment="Bottom" IsChecked="{Binding IsUseVar}" Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}" />
                                                    <ComboBox
                                                        Width="120"
                                                        Margin="0,0,0,0"
                                                        VerticalAlignment="Bottom"
                                                        SelectedValue="{Binding VariableName}"
                                                        SelectedValuePath="Name"
                                                        Visibility="{Binding Category, Converter={StaticResource ParameterCategoryToVisibilityConverter}, ConverterParameter=Inverse}">
                                                        <!-- 修改开始：使用 FilteredParametersConverter -->
                                                        <ComboBox.ItemsSource>
                                                            <MultiBinding Converter="{StaticResource FilteredParametersConverter}">
                                                                <Binding Path="Type" />
                                                                <Binding Path="DataContext.Program.Parameters" RelativeSource="{RelativeSource AncestorType=Window}" />
                                                            </MultiBinding>
                                                        </ComboBox.ItemsSource>
                                                        <ComboBox.ItemTemplate>
                                                            <DataTemplate>
                                                                <StackPanel Orientation="Horizontal">
                                                                    <TextBlock Text="{Binding Name}" />
                                                                    <TextBlock Text="(" />
                                                                    <TextBlock Text="{Binding Type.Name, TargetNullValue=Unknown}" />
                                                                    <TextBlock Text=")" />
                                                                </StackPanel>
                                                            </DataTemplate>
                                                        </ComboBox.ItemTemplate>
                                                        <!-- 修改结束 -->
                                                    </ComboBox>
                                                </StackPanel>

                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ScrollViewer>
                </Grid>

                <!--  按钮  -->
                <StackPanel
                    Grid.Row="5"
                    Margin="5,0"
                    FlowDirection="RightToLeft"
                    Orientation="Horizontal">
                    <Button
                        Width="90"
                        Click="CancelStepEdit_Click"
                        Content="取消" />
                    <Button
                        Width="90"
                        Margin="20,0"
                        Click="SaveStep_Click"
                        Content="保存" />
                </StackPanel>
            </Grid>
        </GroupBox>
    </Grid>
</UserControl>