<UserControl x:Class="ASTM_D7896_Tester.Views.D7896View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:oxy="http://oxyplot.org/wpf"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="768" d:DesignWidth="1024" Loaded="UserControl_Loaded">

    <UserControl.Resources>
        <!-- 全局字体 -->
        <Style TargetType="TextBlock">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="13"/>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="BorderBrush" Value="#CCCCCC"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Padding" Value="4,2"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#F0F0F0" Offset="0"/>
                        <GradientStop Color="#E0E0E0" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" Value="#ADADAD"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Padding" Value="12,6"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#E6F2FF" Offset="0"/>
                                <GradientStop Color="#D0E4F7" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderBrush" Value="#7CA4C7"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#C0D9F0" Offset="0"/>
                                <GradientStop Color="#A8C4E0" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

        <!-- 卡片阴影效果 -->
        <DropShadowEffect x:Key="CardShadow" BlurRadius="8" ShadowDepth="2" Opacity="0.15" Color="Gray"/>

        <!-- 主按钮绿色渐变（修复：将资源定义在正确的位置） -->
        <LinearGradientBrush x:Key="PrimaryButtonBrush" EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#4CAF50" Offset="0"/>
            <GradientStop Color="#388E3C" Offset="1"/>
        </LinearGradientBrush>

        <!-- 数据表格样式 -->
        <Style TargetType="DataGrid">
            <Setter Property="FontFamily" Value="Segoe UI"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="RowHeight" Value="28"/>
            <Setter Property="HeadersVisibility" Value="Column"/>
            <Setter Property="GridLinesVisibility" Value="Horizontal"/>
            <Setter Property="BorderBrush" Value="#E0E0E0"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="AlternatingRowBackground" Value="#F9F9F9"/>
        </Style>
        <Style TargetType="DataGridColumnHeader">
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                        <GradientStop Color="#F5F5F5" Offset="0"/>
                        <GradientStop Color="#E8E8E8" Offset="1"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
            <Setter Property="BorderBrush" Value="#D0D0D0"/>
            <Setter Property="BorderThickness" Value="0,0,1,1"/>
            <Setter Property="Foreground" Value="#333333"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>

        <!-- 逆布尔转换器（如果还没在App.xaml中定义，可以在这里定义） -->
        <!-- 注意：如果已经在App.xaml中定义了InverseBooleanConverter，请注释掉下面这一行 -->
        <!-- <converters:InverseBooleanConverter x:Key="InverseBooleanConverter"/> -->
    </UserControl.Resources>


    <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" Background="#F2F2F2">
        <Grid Margin="10">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!-- 1. 样品信息 + 复选框（合并的4项确认） -->
            <Border Grid.Row="0" Background="White" CornerRadius="6" Padding="10" Margin="0,0,0,8" Effect="{StaticResource CardShadow}">
                <StackPanel>
                    <WrapPanel Margin="0,0,0,8">
                        <TextBlock Text="样品ID:" VerticalAlignment="Center" FontWeight="SemiBold" Margin="0,0,8,0"/>
                        <TextBox Text="{Binding SampleId}" Width="120" Margin="0,0,20,0"/>
                        <TextBlock Text="测试温度(℃):" VerticalAlignment="Center" FontWeight="SemiBold" Margin="0,0,8,0"/>
                        <TextBox Text="{Binding TestTemperature}" Width="70" IsReadOnly="True" Background="#F0F8FF" Margin="0,0,20,0"/>
                        <TextBlock Text="日期:" VerticalAlignment="Center" FontWeight="SemiBold" Margin="0,0,8,0"/>
                        <TextBox Text="{Binding TestDateTime}" Width="140" IsReadOnly="True" Background="#F0F8FF"/>
                    </WrapPanel>
                    <WrapPanel>
                        <!-- 四个确认复选框，默认勾选 -->
                        <CheckBox IsChecked="{Binding IsCleanConfirmed}" Content="采样池清洁 (7.1)" Margin="0,0,15,0"/>
                        <CheckBox IsChecked="{Binding BubbleRemoved}" Content="气泡清除 (7.6)" Margin="0,0,15,0"/>
                        <CheckBox IsChecked="{Binding PlatinumCompatible}" Content="铂兼容性 (1.4)" Margin="0,0,15,0"/>
                        <CheckBox IsChecked="{Binding AmbientCalibrated}" Content="环境温度校准 (8.1)" Margin="0,0,15,0"/>
                        <TextBlock Text="状态:" VerticalAlignment="Center" Margin="20,0,5,0"/>
                        <TextBox Text="{Binding StatusMessage}" Width="200" IsReadOnly="True" Background="#FFF7E6"/>
                    </WrapPanel>
                </StackPanel>
            </Border>

            <!-- 2. 核心参数仪表盘（温度、电压、电阻、压力） -->
            <Border Grid.Row="1" Background="White" CornerRadius="6" Padding="10" Margin="0,5" Effect="{StaticResource CardShadow}">
                <WrapPanel>




                    <!-- 核心参数仪表盘 - 更新绑定属性 -->
                    <Border Background="#E8F0FE" Padding="6" CornerRadius="4" Margin="0,0,15,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="🌡️ 样品温度:" FontWeight="SemiBold" Margin="0,0,5,0"/>
                            <TextBox Text="{Binding CurrentTestTemperature, StringFormat=F2}" Width="60" IsReadOnly="True"/>
                            <TextBlock Text="℃" Margin="3,0,0,0"/>
                        </StackPanel>
                    </Border>
                    <Border Background="#E8F0FE" Padding="6" CornerRadius="4" Margin="0,0,15,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="⏲️ 压力:" FontWeight="SemiBold" Margin="0,0,5,0"/>
                            <TextBox Text="{Binding ChamberPressure, StringFormat=F2}" Width="60" IsReadOnly="True"/>
                            <TextBlock Text="kPa" Margin="3,0,0,0"/>
                        </StackPanel>
                    </Border>
                    <Border Background="#E8F0FE" Padding="6" CornerRadius="4" Margin="0,0,15,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="⚡ 电阻电压:" FontWeight="SemiBold" Margin="0,0,5,0"/>
                            <TextBox Text="{Binding StandardResistorVoltage, StringFormat=F10}" Width="120" IsReadOnly="True"/>
                            <TextBlock Text="V" Margin="3,0,0,0"/>
                        </StackPanel>
                    </Border>
                    <Border Background="#E8F0FE" Padding="6" CornerRadius="4" Margin="0,0,15,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="⚡ 铂丝电压:" FontWeight="SemiBold" Margin="0,0,5,0"/>
                            <TextBox Text="{Binding PlatinumVoltage, StringFormat=F4}" Width="70" IsReadOnly="True"/>
                            <TextBlock Text="V" Margin="3,0,0,0"/>
                        </StackPanel>
                    </Border>
                    <Border Background="#E8F0FE" Padding="6" CornerRadius="4" Margin="0,0,15,0">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="🔌 铂丝电阻:" FontWeight="SemiBold" Margin="0,0,5,0"/>
                            <TextBox Text="{Binding PlatinumResistance, StringFormat=F4}" Width="70" IsReadOnly="True"/>
                            <TextBlock Text="Ω" Margin="3,0,0,0"/>
                        </StackPanel>
                    </Border>
                </WrapPanel>
            </Border>

            <!-- 3. 样品体积 + 密度 + 加压设置 -->
            <Border Grid.Row="2" Background="White" CornerRadius="6" Padding="10" Margin="0,5" Effect="{StaticResource CardShadow}">
                <WrapPanel>
                    <TextBlock Text="样品体积 (mL):" VerticalAlignment="Center" Width="100"/>
                    <TextBox Text="{Binding SampleVolume}" Width="60" Margin="0,0,20,0"/>

                    <!-- 新增密度输入框 -->
                    <TextBlock Text="密度 (kg/m³):" VerticalAlignment="Center" Width="100"/>
                    <TextBox Text="{Binding SampleDensity}" Width="80" Margin="0,0,20,0" ToolTip="输入样品密度，用于计算比热容"/>

                    <CheckBox IsChecked="{Binding UsePressure}" Content="加压测试" VerticalAlignment="Center" Margin="0,0,15,0"/>
                    <TextBlock Text="压力 (kPa):" VerticalAlignment="Center" Margin="0,0,5,0"/>
                    <TextBox Text="{Binding PressureValue}" Width="60" IsEnabled="{Binding UsePressure}" Margin="0,0,20,0"/>
                    <TextBlock Text="（蒸气压>33.8kPa时需加压）" Foreground="Gray" VerticalAlignment="Center"/>
                </WrapPanel>
            </Border>

            <!-- 4. 温升曲线图（核心新增） -->
            <Border Grid.Row="3" Background="White" CornerRadius="6" Padding="8" Margin="0,5" Effect="{StaticResource CardShadow}">
                <StackPanel>
                    <TextBlock Text="{Binding CurveTitle}" FontWeight="Bold" FontSize="13" Margin="0,0,0,5" Foreground="#1E4F7A"/>
                    <oxy:PlotView Model="{Binding TemperatureCurveModel}" Height="220"/>
                </StackPanel>
            </Border>

            <!-- 5. 测量数据表格（占用剩余高度） -->
            <Border Grid.Row="4" Background="White" CornerRadius="6" Padding="8" Margin="0,5" Effect="{StaticResource CardShadow}">
                <DataGrid ItemsSource="{Binding Measurements}" AutoGenerateColumns="False"  HorizontalAlignment="Center" VerticalAlignment="Center"
                          CanUserAddRows="False" IsReadOnly="True" 
                          HeadersVisibility="Column" GridLinesVisibility="Horizontal"
                          RowHeight="28" MinHeight="150">
                    
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="序号" Binding="{Binding Index}" Width="60">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn Header="热导率 (W/m·K)" Binding="{Binding ThermalConductivity, StringFormat=F5}" Width="*">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn Header="热扩散率 (m²/s)" Binding="{Binding ThermalDiffusivity, StringFormat=F10}" Width="*">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                        </DataGridTextColumn>

                        <DataGridTextColumn Header="体积热容 (kJ/m³·K)" Binding="{Binding VolumetricHeatCapacity, StringFormat=F2}" Width="*">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Border>

            <!-- 6. 平均值 + 操作按钮 -->
            <Border Grid.Row="5" Background="#E8F0FE" CornerRadius="6" Padding="10" Margin="0,5" Effect="{StaticResource CardShadow}">



                <StackPanel>
                    <WrapPanel Margin="0,0,0,8">
                        <TextBlock Text="平均热导率:" Width="100" FontWeight="SemiBold"/>
                        <TextBox Text="{Binding AverageThermalConductivity, StringFormat=F5}" Width="100" IsReadOnly="True"/>
                        <TextBlock Text="W/m·K" Margin="6,0,30,0"/>
                        <TextBlock Text="平均热扩散率:" Width="120" FontWeight="SemiBold"/>
                        <TextBox Text="{Binding AverageThermalDiffusivity, StringFormat=F5}" Width="100" IsReadOnly="True"/>
                        <TextBlock Text="×10⁻⁶ m²/s" Margin="6,0,30,0"/>
                        <TextBlock Text="平均体积热容:" Width="100" FontWeight="SemiBold"/>
                        <TextBox Text="{Binding AverageVolumetricHeatCapacity, StringFormat=F2}" Width="100" IsReadOnly="True"/>
                        <TextBlock Text="kJ/m³·K" Margin="6,0,0,0"/>
                    </WrapPanel>
                    <!-- 新增：压力校准、电阻归零、进排气按钮 -->
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,8">
                        <Button Content="压力校准" Command="{Binding PressureCalibrationCommand}" Width="90" Margin="5"/>
                        <Button Content="电阻归零" Command="{Binding ResistanceZeroCommand}" Width="90" Margin="5"/>
                        <Button Content="进气阀" Command="{Binding InletValveControlCommand}" Width="80" Margin="5"/>
                        <Button Content="排气阀" Command="{Binding OutletValveControlCommand}" Width="80" Margin="5"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">

                        <Button Content="▶ 开始测试" Command="{Binding StartTestCommand}" Width="130" Height="36" Margin="8" 
                                IsEnabled="{Binding IsTesting, Converter={StaticResource InverseBooleanConverter}}"
                                Background="{StaticResource PrimaryButtonBrush}"/>

                        <Button Content="■ 停止测试" Command="{Binding StopTestCommand}" Width="130" Height="36" Margin="8" Background="Orange"/>


                        <Button Content="⟳ 重置" Command="{Binding ResetCommand}" Width="100" Height="36" Margin="8"/>
                        <Button Content="📄 生成报告" Command="{Binding GenerateReportCommand}" Width="110" Height="36" Margin="8"/>
                    </StackPanel>
                </StackPanel>
            </Border>

            <!-- 7. 系统校准（可选，可收起到折叠区域，为节省空间放在底部） -->
            <Border Grid.Row="6" Background="White" CornerRadius="6" Padding="8" Margin="0,5" Effect="{StaticResource CardShadow}">
                <StackPanel>
                    <TextBlock Text="系统校准 (附录A3)" FontWeight="Bold" Margin="0,0,0,5"/>
                    <WrapPanel>
                        <TextBlock Text="参考液:" VerticalAlignment="Center"/>
                        <ComboBox ItemsSource="{Binding ReferenceLiquids}" SelectedItem="{Binding SelectedReferenceLiquid}" Width="100" Margin="5,0"/>
                        <TextBlock Text="参考热导率 (W/m·K):" VerticalAlignment="Center" Margin="10,0,5,0"/>
                        <TextBox Text="{Binding ReferenceConductivity}" Width="70" Margin="0,0,10,0"/>
                        <!--<Button Content="开始校准" Command="{Binding PerformSystemCalibrationCommand}" Width="100"/>-->
                    </WrapPanel>
                    <TextBlock Text="{Binding CalibrationStatus}" FontSize="11" Foreground="Blue" Margin="0,5,0,0"/>
                </StackPanel>
            </Border>
        </Grid>
    </ScrollViewer>
</UserControl>