<UserControl x:Class="MonitorModule.Views.RecordView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:b="clr-namespace:UIShare.Behaviors;assembly=UIShare"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d"
             d:DesignHeight="700"
             d:DesignWidth="1200">
    <!-- 外层 Border：1) 装 MouseDoubleClickBehavior 实现九宫格双击展开；
         2) 给整个记录界面一个浅色背景 -->
    <Border Background="#F5F7FA">
        <i:Interaction.Behaviors>
            <b:MouseDoubleClickBehavior
                Command="{Binding DataContext.RefreshCommand, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
        </i:Interaction.Behaviors>

        <Grid Margin="8">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <!-- 标题 -->
                <RowDefinition Height="Auto"/>
                <!-- 工具栏 -->
                <RowDefinition Height="*"/>
                <!-- 数据表 -->
                <RowDefinition Height="Auto"/>
                <!-- 分页 -->
                <RowDefinition Height="Auto"/>
                <!-- 状态栏 -->
            </Grid.RowDefinitions>

            <!-- 顶部：工位名称（区分九宫格） -->
            <TextBlock Grid.Row="0"
                       Text="{Binding TestStatus, StringFormat=记录界面 - {0}}"
                       FontSize="20" FontWeight="Bold"
                       Margin="4,0,0,8"/>

            <!-- 工具栏：监测项筛选 / 台架筛选 / 日期筛选 / 查询 / 清除 / 导出 -->
            <Border Grid.Row="1"
                    Background="White"
                    BorderBrush="#DDD" BorderThickness="1"
                    CornerRadius="4" Padding="8" Margin="0,0,0,6">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="150"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <!-- 监测项 -->
                    <TextBlock Grid.Column="0"
                               Text="监测项："
                               VerticalAlignment="Center"
                               Margin="0,0,4,0"/>
                    <ComboBox Grid.Column="1"
                              materialDesign:HintAssist.Hint="全部"
                              ItemsSource="{Binding MonitorNames}"
                              SelectedItem="{Binding SelectedMonitorName}"
                              IsEditable="True"
                              VerticalAlignment="Center"/>

                    <!-- 台架 -->
                    <TextBlock Grid.Column="2"
                               Text="台架："
                               VerticalAlignment="Center"
                               Margin="12,0,4,0"/>
                    <ComboBox Grid.Column="3"
                              materialDesign:HintAssist.Hint="全部"
                              ItemsSource="{Binding ScopeNames}"
                              SelectedItem="{Binding SelectedScope}"
                              IsEditable="True"
                              VerticalAlignment="Center"/>

                    <!-- 日期 -->
                    <TextBlock Grid.Column="4"
                               Text="日期："
                               VerticalAlignment="Center"
                               Margin="12,0,4,0"/>
                    <DatePicker Grid.Column="5"
                                SelectedDate="{Binding SelectedStartDate}"
                                materialDesign:HintAssist.Hint="开始时间"
                                VerticalAlignment="Center"/>  
                    <DatePicker Grid.Column="6"
                                Margin=" 5 0 0 0"
                                SelectedDate="{Binding SelectedEndDate}"
                                materialDesign:HintAssist.Hint="结束时间"
                                VerticalAlignment="Center"/>

                    <!-- 按钮 -->
                    <Button Grid.Column="7"
                            Content="查询"
                            Command="{Binding QueryCommand}"
                            Padding="12,4" Margin="12,0,0,0"/>
                    <Button Grid.Column="8"
                            Content="清除"
                            Command="{Binding ClearFilterCommand}"
                            Padding="12,4" Margin="6,0,0,0"/>
                    <Button Grid.Column="9"
                            Content="导出 CSV"
                            Command="{Binding ExportCsvCommand}"
                            Padding="12,4" Margin="6,0,0,0"/>
                </Grid>
            </Border>

            <!-- DataGrid：显式列定义，隐藏 Id / IsDel -->
            <DataGrid Grid.Row="2"
                      ItemsSource="{Binding ResultTable}"
                      AutoGenerateColumns="False"
                      IsReadOnly="True"
                      CanUserAddRows="False"
                      CanUserDeleteRows="False"
                      GridLinesVisibility="All"
                      HeadersVisibility="All"
                      AlternatingRowBackground="#F9FAFB"
                      RowBackground="White"
                      HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="监测项" Binding="{Binding MonitorName}" Width="*"/>
                    <DataGridTextColumn Header="监测值" Binding="{Binding MonitorValue}" Width="120"/>
                    <DataGridTextColumn Header="台架" Binding="{Binding Scope}" Width="120"/>
                    <DataGridTextColumn Header="记录时间" Binding="{Binding CreateTime, StringFormat=yyyy-MM-dd HH:mm:ss}" Width="180"/>
                </DataGrid.Columns>
            </DataGrid>

            <!-- 分页控件 -->
            <Border Grid.Row="3"
                    Background="White"
                    BorderBrush="#DDD" BorderThickness="1"
                    CornerRadius="4" Padding="8" Margin="0,6,0,0">
                <Grid>
                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="Left">
                        <TextBlock Text="每页：" VerticalAlignment="Center"/>
                        <ComboBox Text="{Binding PageSize, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
                                  IsEditable="True"
                                  materialDesign:HintAssist.Hint="页码"
                                  Width="80" VerticalAlignment="Center" Margin="4,0,0,0">
                            <ComboBoxItem Content="20"/>
                            <ComboBoxItem Content="50"/>
                            <ComboBoxItem Content="100"/>
                            <ComboBoxItem Content="200"/>
                            <ComboBoxItem Content="500"/>
                        </ComboBox>
                        <TextBlock Text="{Binding TotalCount, StringFormat=共 {0} 行}"
                                   VerticalAlignment="Center" Margin="16,0,0,0"/>
                    </StackPanel>

                    <StackPanel Orientation="Horizontal"
                                HorizontalAlignment="Right">
                        <Button Content="« 首页"
                                Command="{Binding FirstPageCommand}"
                                Padding="10,3"/>
                        <Button Content="‹ 上一页"
                                Command="{Binding PrevPageCommand}"
                                Padding="10,3" Margin="6,0,0,0"/>
                        <TextBlock VerticalAlignment="Center" Margin="12,0">
                            <Run Text="第 "/>
                            <Run Text="{Binding PageIndex}" FontWeight="Bold"/>
                            <Run Text=" / "/>
                            <Run Text="{Binding TotalPages, Mode=OneWay}" FontWeight="Bold"/>
                            <Run Text=" 页"/>
                        </TextBlock>
                        <Button Content="下一页 ›"
                                Command="{Binding NextPageCommand}"
                                Padding="10,3"/>
                        <Button Content="末页 »"
                                Command="{Binding LastPageCommand}"
                                Padding="10,3" Margin="6,0,0,0"/>
                    </StackPanel>
                </Grid>
            </Border>

            <!-- 底部状态栏 -->
            <Border Grid.Row="4"
                    Background="#ECEFF4"
                    Padding="8,4" Margin="0,6,0,0"
                    CornerRadius="2">
                <TextBlock Text="{Binding StatusMessage}"
                           Foreground="#444"
                           FontSize="12"/>
            </Border>
        </Grid>
    </Border>
</UserControl>
