<Window x:Class="PetroleumViscosityTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="石油产品运动粘度测试系统 (GB/T 265-1988)"
        Width="1024" Height="768"
        WindowStartupLocation="CenterScreen"
        FontSize="13">

    <Window.Resources>
        <!-- 计时按钮样式 -->
        <Style x:Key="TimerButtonStyle" TargetType="Button">
            <Setter Property="Width" Value="80"/>
            <Setter Property="Height" Value="45"/>
            <Setter Property="FontSize" Value="13"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="border" 
                            CornerRadius="5" 
                            Background="{TemplateBinding Background}"
                            BorderThickness="0">
                            <ContentPresenter HorizontalAlignment="Center" 
                                          VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="#FF1E3C72"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="border" Property="Background" Value="#FF0F1F3A"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <!-- 默认背景（渐变） -->
            <Setter Property="Background">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                        <GradientStop Color="#FF4A90E2" Offset="0.0"/>
                        <GradientStop Color="#FF357ABD" Offset="1.0"/>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- ========== 顶部：试样信息、粘度计常数、恒温条件 ========= -->
        <Border Grid.Row="0" BorderBrush="DarkGray" BorderThickness="1" Padding="5" CornerRadius="3" Margin="0,0,0,5">
            <StackPanel>
                <!-- 第一行：试样信息 -->
                <WrapPanel Margin="0,0,0,3">
                    <TextBlock Text="试样编号：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtSampleID" Width="120" Margin="5,0,10,0"/>
                    <TextBlock Text="试样名称：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtSampleName" Width="150" Margin="5,0,10,0"/>
                    <TextBlock Text="试验温度(℃)：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtTestTemp" Width="60" Margin="5,0,10,0" TextChanged="TxtTestTemp_TextChanged"/>
                    <TextBlock Text="恒温时间(min)：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtThermostatTime" Width="50" IsReadOnly="True" Background="LightGray" Margin="5,0,0,0"/>
                </WrapPanel>

                <!-- 第二行：粘度计常数、密度、操作者 -->
                <WrapPanel Margin="0,3,0,3">
                    <TextBlock Text="粘度计常数 C (mm²/s²)：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtConstant" Width="100" Margin="5,0,10,0"/>
                    <TextBlock Text="密度 ρ (g/cm³)：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtDensity" Width="80" Margin="5,0,10,0"/>
                    <TextBlock Text="操作者：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtOperator" Width="100" Margin="5,0,10,0"/>
                    <TextBlock Text="试验日期：" VerticalAlignment="Center"/>
                    <DatePicker x:Name="dpTestDate" Width="120" Margin="5,0,0,0"/>
                </WrapPanel>

                <!-- 第三行：备注、控制按钮 -->
                <WrapPanel Margin="0,3,0,0">
                    <TextBlock Text="备注：" VerticalAlignment="Center"/>
                    <TextBox x:Name="txtRemark" Width="300" Margin="5,0,10,0"/>
                    <Button x:Name="btnClearAll" Content="清空所有"   Style="{StaticResource TimerButtonStyle}"  Margin="5,0" Click="BtnClearAll_Click"/>
                    <Button x:Name="btnLoadExample" Content="加载示例"   Style="{StaticResource TimerButtonStyle}"  Margin="5,0" Click="BtnLoadExample_Click"/>
                </WrapPanel>
            </StackPanel>
        </Border>

        <!-- ========== 中间：流动时间测量及计算 ========= -->
        <TabControl Grid.Row="1" Margin="0,5,0,5">
            <!-- 流动时间录入 -->
            <TabItem Header="流动时间测量">
                <ScrollViewer VerticalScrollBarVisibility="Auto">
                    <StackPanel Margin="5">
                        <GroupBox Header="流动时间记录 (秒)" Padding="5">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <!-- 第一行：4个时间输入框 -->
                                <!-- 第1次 -->
                                <StackPanel Grid.Row="0" Grid.Column="0" Margin="5">
                                    <TextBlock Text="第1次" FontWeight="Bold"/>
                                    <TextBox x:Name="txtTime1" Width="80" Margin="0,2"/>
                                    <WrapPanel HorizontalAlignment="Center" Orientation="Vertical">
                                        <Button x:Name="btnStart1" Content="开始" Style="{StaticResource TimerButtonStyle}" Click="BtnStart1_Click" Margin="0,4"/>
                                        <Button x:Name="btnStop1"  Content="结束" Style="{StaticResource TimerButtonStyle}" Click="BtnStop1_Click"/>
                                    </WrapPanel>
                                </StackPanel>

                                <!-- 第2次 -->
                                <StackPanel Grid.Row="0" Grid.Column="1" Margin="5">
                                    <TextBlock Text="第2次" FontWeight="Bold"/>
                                    <TextBox x:Name="txtTime2" Width="80" Margin="0,2"/>
                                    <WrapPanel HorizontalAlignment="Center" Orientation="Vertical">
                                        <Button x:Name="btnStart2" Content="开始"  Style="{StaticResource TimerButtonStyle}" Click="BtnStart2_Click"/>
                                        <Button x:Name="btnStop2"  Content="结束"  Style="{StaticResource TimerButtonStyle}"  Click="BtnStop2_Click" Margin="0,4"/>
                                    </WrapPanel>
                                </StackPanel>

                                <!-- 第3次 -->
                                <StackPanel Grid.Row="0" Grid.Column="2" Margin="5">
                                    <TextBlock Text="第3次" FontWeight="Bold"/>
                                    <TextBox x:Name="txtTime3" Width="80" Margin="0,2"/>
                                    <WrapPanel HorizontalAlignment="Center" Orientation="Vertical">
                                        <Button x:Name="btnStart3" Content="开始"   Style="{StaticResource TimerButtonStyle}"   FontSize="11" Click="BtnStart3_Click"/>
                                        <Button x:Name="btnStop3"  Content="结束"  Style="{StaticResource TimerButtonStyle}"  Click="BtnStop3_Click" Margin="0,4"/>
                                    </WrapPanel>
                                </StackPanel>

                                <!-- 第4次 -->
                                <StackPanel Grid.Row="0" Grid.Column="3" Margin="5">
                                    <TextBlock Text="第4次" FontWeight="Bold"/>
                                    <TextBox x:Name="txtTime4" Width="80" Margin="0,2"/>
                                    <WrapPanel HorizontalAlignment="Center" Orientation="Vertical">
                                        <Button x:Name="btnStart4" Content="开始"   Style="{StaticResource TimerButtonStyle}"  Click="BtnStart4_Click"/>
                                        <Button x:Name="btnStop4"  Content="结束"   Style="{StaticResource TimerButtonStyle}"  Click="BtnStop4_Click" Margin="0,4"/>
                                    </WrapPanel>
                                </StackPanel>

                                <!-- 第二行：更多时间输入（支持最多6次） -->
                                <StackPanel Grid.Row="1" Grid.Column="0" Margin="5">
                                    <TextBlock Text="第5次 (可选)"/>
                                    <TextBox x:Name="txtTime5" Width="80" Margin="0,2"/>
                                </StackPanel>
                                <StackPanel Grid.Row="1" Grid.Column="1" Margin="5">
                                    <TextBlock Text="第6次 (可选)"/>
                                    <TextBox x:Name="txtTime6" Width="80" Margin="0,2"/>
                                </StackPanel>
                            </Grid>
                        </GroupBox>

                        <WrapPanel Margin="0,10,0,10" HorizontalAlignment="Center">
                            <Button x:Name="btnCalcViscosity" Content="计算运动粘度"   Style="{StaticResource TimerButtonStyle}"  Width="150" Height="30" Margin="5" Click="BtnCalcViscosity_Click"/>
                            <Button x:Name="btnAddMeasurement" Content="增加一次测量"   Style="{StaticResource TimerButtonStyle}" Width="120" Margin="5" Click="BtnAddMeasurement_Click"/>
                            <Button x:Name="btnClearTimes"  Style="{StaticResource TimerButtonStyle}"  Content="清空时间" Width="100" Margin="5" Click="BtnClearTimes_Click"/>
                        </WrapPanel>

                        <!-- 计算结果展示 -->
                        <GroupBox Header="计算结果" Padding="5">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>

                                <TextBlock Grid.Row="0" Grid.Column="0" Text="平均流动时间 τ (s)：" Margin="5"/>
                                <TextBox x:Name="txtAvgTime" Grid.Row="0" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                                <TextBlock Grid.Row="1" Grid.Column="0" Text="运动粘度 ν (mm²/s)：" Margin="5"/>
                                <TextBox x:Name="txtViscosity" Grid.Row="1" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5" FontWeight="Bold"/>

                                <TextBlock Grid.Row="2" Grid.Column="0" Text="动力粘度 η (mPa·s)：" Margin="5"/>
                                <TextBox x:Name="txtDynamicViscosity" Grid.Row="2" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                                <TextBlock Grid.Row="3" Grid.Column="0" Text="有效测量次数：" Margin="5"/>
                                <TextBox x:Name="txtValidCount" Grid.Row="3" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                                <TextBlock Grid.Row="4" Grid.Column="0" Text="精密度判定：" Margin="5"/>
                                <TextBox x:Name="txtPrecisionJudge" Grid.Row="4" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5" Foreground="Red"/>
                            </Grid>
                        </GroupBox>

                        <!-- 流动时间原始数据显示 -->
                        <GroupBox Header="流动时间原始记录" Margin="0,10,0,0">
                            <ListBox x:Name="lstRawTimes" Height="80" DisplayMemberPath="Display"/>
                        </GroupBox>
                    </StackPanel>
                </ScrollViewer>
            </TabItem>

            <!-- 精密度及报告 -->
            <TabItem Header="精密度与报告">
                <StackPanel Margin="5">
                    <GroupBox Header="精密度要求 (GB/T 265-1988)" Padding="5">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <TextBlock Grid.Row="0" Grid.Column="0" Text="重复性 (同一操作者)：" Margin="5"/>
                            <TextBox x:Name="txtRepeatability" Grid.Row="0" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                            <TextBlock Grid.Row="1" Grid.Column="0" Text="再现性 (不同实验室)：" Margin="5"/>
                            <TextBox x:Name="txtReproducibility" Grid.Row="1" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                            <TextBlock Grid.Row="2" Grid.Column="0" Text="本次测量重复性 (%)：" Margin="5"/>
                            <TextBox x:Name="txtMeasuredRepeatability" Grid.Row="2" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5"/>

                            <TextBlock Grid.Row="3" Grid.Column="0" Text="重复性判定：" Margin="5"/>
                            <TextBox x:Name="txtRepeatJudge" Grid.Row="3" Grid.Column="1" IsReadOnly="True" Background="LightGray" Margin="5" Foreground="Red"/>
                        </Grid>
                    </GroupBox>

                    <WrapPanel Margin="0,10" HorizontalAlignment="Center">
                        <Button x:Name="btnGenRecord"  Style="{StaticResource TimerButtonStyle}"  Content="生成原始记录 (附录样式)" Width="180" Margin="5" Padding="5" Click="BtnGenRecord_Click"/>
                        <Button x:Name="btnGenReport"  Style="{StaticResource TimerButtonStyle}"  Content="生成测试报告" Width="150" Margin="5" Padding="5" Click="BtnGenReport_Click"/>
                        <Button x:Name="btnPrintPreview"  Style="{StaticResource TimerButtonStyle}"  Content="打印预览" Width="100" Margin="5" Padding="5" Click="BtnPrintPreview_Click"/>
                    </WrapPanel>

                    <Border BorderBrush="LightBlue" BorderThickness="1" Height="220" CornerRadius="2" Background="WhiteSmoke">
                        <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
                            <ContentControl x:Name="ReportContent" Margin="5"/>
                        </ScrollViewer>
                    </Border>
                </StackPanel>
            </TabItem>
        </TabControl>

        <!-- ========== 底部：状态栏 ========= -->
        <StatusBar Grid.Row="2" Background="LightGray">
            <StatusBarItem>
                <TextBlock x:Name="txtStatus" Text="就绪"/>
            </StatusBarItem>
        </StatusBar>
    </Grid>
</Window>