<UserControl x:Class="PerformanceMonitorDashboard.ServerTab"
             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:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:converters="clr-namespace:PerformanceMonitorDashboard.Converters"
             xmlns:controls="clr-namespace:PerformanceMonitorDashboard.Controls"
             xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF">
        <UserControl.Resources>
        <ResourceDictionary>
            <converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
            <converters:StatusToBrushConverter x:Key="StatusToBrushConverter"/>
            <converters:LatencyToBrushConverter x:Key="LatencyToBrushConverter"/>
            <converters:CpuToBrushConverter x:Key="CpuToBrushConverter"/>
            <converters:BlockingDurationToBrushConverter x:Key="BlockingDurationToBrushConverter"/>
            <converters:QueryTextCleanupConverter x:Key="QueryTextCleanupConverter"/>

        <!-- Context Menu for DataGrid Copy/Export -->
        <ContextMenu x:Key="DataGridContextMenu">
            <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                <MenuItem.Icon><TextBlock Text="📋"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy Row" Click="CopyRow_Click">
                <MenuItem.Icon><TextBlock Text="📄"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                <MenuItem.Icon><TextBlock Text="📑"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                <MenuItem.Icon><TextBlock Text="📊"/></MenuItem.Icon>
            </MenuItem>
        </ContextMenu>

        <!-- Context Menu for Blocked Process Report rows (copy/export + View Plan per side) -->
        <ContextMenu x:Key="BlockingEventsContextMenu">
            <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                <MenuItem.Icon><TextBlock Text="📋"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy Row" Click="CopyRow_Click">
                <MenuItem.Icon><TextBlock Text="📄"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                <MenuItem.Icon><TextBlock Text="📑"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                <MenuItem.Icon><TextBlock Text="📊"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="View Block Chain" Click="ViewBlockChain_Click"/>
            <MenuItem Header="View Blocked Plan" Click="ViewBlockedSidePlan_Click"/>
            <MenuItem Header="View Blocking Plan" Click="ViewBlockingSidePlan_Click"/>
        </ContextMenu>

        <!-- Context Menu for Deadlock rows (copy/export + View Plan) -->
        <ContextMenu x:Key="DeadlocksContextMenu">
            <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                <MenuItem.Icon><TextBlock Text="📋"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy Row" Click="CopyRow_Click">
                <MenuItem.Icon><TextBlock Text="📄"/></MenuItem.Icon>
            </MenuItem>
            <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                <MenuItem.Icon><TextBlock Text="📑"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                <MenuItem.Icon><TextBlock Text="📊"/></MenuItem.Icon>
            </MenuItem>
            <Separator/>
            <MenuItem Header="View Deadlock Graph" Click="ViewDeadlockGraph_Click"/>
            <MenuItem Header="View Plan" Click="ViewDeadlockProcessPlan_Click"/>
        </ContextMenu>

        <!-- Row Styles for Visual Indicators -->
        <Style x:Key="HealthRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding HealthStatus}" Value="ERROR">
                    <Setter Property="Background" Value="#5C2626"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding HealthStatus}" Value="FAILING">
                    <Setter Property="Background" Value="#5C2626"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding HealthStatus}" Value="STALE">
                    <Setter Property="Background" Value="#4A4A26"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="LatencyRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
            <Setter Property="Background" Value="{Binding AvgReadLatencyMs, Converter={StaticResource LatencyToBrushConverter}}"/>
        </Style>

        <Style x:Key="BlockingRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
            <!-- Visual highlighting based on blocking duration: -->
            <!-- Yellow: 1-10 seconds, Orange: 10-60 seconds, Red: 60+ seconds -->
            <Setter Property="Background" Value="{Binding BlockingDurationSeconds, Converter={StaticResource BlockingDurationToBrushConverter}}"/>
        </Style>

        <Style x:Key="DefaultRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
        </Style>

        <Style x:Key="BlockingEventsPlanRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource BlockingEventsContextMenu}"/>
        </Style>

        <Style x:Key="DeadlocksPlanRowStyle" TargetType="DataGridRow">
            <Setter Property="ContextMenu" Value="{StaticResource DeadlocksContextMenu}"/>
        </Style>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <!-- Global Time Range Control Bar -->
        <Border Grid.Row="0" Background="{DynamicResource BackgroundLightBrush}" Padding="8" Margin="10,10,10,5">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- Row 1: Time Range Buttons -->
                <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Horizontal">
                    <TextBlock Text="Time Range:" VerticalAlignment="Center" Margin="0,0,10,0" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button x:Name="GlobalLast1HourButton" Content="1 Hour" Click="GlobalTimeRange_Click" Tag="1" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast4HoursButton" Content="4 Hours" Click="GlobalTimeRange_Click" Tag="4" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast8HoursButton" Content="8 Hours" Click="GlobalTimeRange_Click" Tag="8" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast12HoursButton" Content="12 Hours" Click="GlobalTimeRange_Click" Tag="12" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast24HoursButton" Content="24 Hours" Click="GlobalTimeRange_Click" Tag="24" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast7DaysButton" Content="7 Days" Click="GlobalTimeRange_Click" Tag="168" Style="{DynamicResource TimeRangeButton}"/>
                    <Button x:Name="GlobalLast30DaysButton" Content="30 Days" Click="GlobalTimeRange_Click" Tag="720" Style="{DynamicResource TimeRangeButton}"/>
                </StackPanel>

                <!-- Row 1: Action Buttons and Status -->
                <StackPanel Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Vertical" Margin="15,0,0,0">
                    <StackPanel Orientation="Horizontal">
                        <Button x:Name="ApplyToAllButton" Content="Apply to All Tabs" Click="ApplyToAllTabs_Click" Margin="2,0" Padding="12,5" Style="{DynamicResource AccentButton}"/>
                        <Button x:Name="RefreshButton" Content="Refresh Tab" Click="RefreshButton_Click" Margin="2,0" Padding="12,5" Style="{DynamicResource SuccessButton}" ToolTip="Refresh the current tab. Ctrl+Click to refresh all tabs."/>
                        <ToggleButton x:Name="AutoRefreshToggle" Content="Auto-Refresh: Off" Click="AutoRefreshToggle_Click" Margin="8,0,2,0" Padding="12,5" MinWidth="130" ToolTip="Toggle auto-refresh on/off. Configure interval in Settings."/>
                        <Button Content="Edit Schedules" Click="EditSchedules_Click" Margin="8,0,2,0" Padding="12,5" Style="{DynamicResource AccentButton}" ToolTip="Edit collector schedules (frequency, retention, enabled/disabled)"/>
                        <TextBlock Text="|" VerticalAlignment="Center" Margin="8,0,4,0" Foreground="{DynamicResource ForegroundBrush}"/>
                        <TextBlock Text="Compare:" VerticalAlignment="Center" Margin="0,0,4,0" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ComboBox x:Name="CompareToCombo" SelectedIndex="0" Width="130" Margin="0,0,8,0"
                                  SelectionChanged="CompareToCombo_SelectionChanged"
                                  ToolTip="Compare current period against a baseline">
                            <ComboBoxItem Content="None"/>
                            <ComboBoxItem Content="Yesterday"/>
                            <ComboBoxItem Content="Last week"/>
                            <ComboBoxItem Content="Same day last week"/>
                        </ComboBox>
                        <TextBlock Text="|" VerticalAlignment="Center" Margin="0,0,4,0" Foreground="{DynamicResource ForegroundBrush}"/>
                        <TextBlock Text="Times:" VerticalAlignment="Center" Margin="0,0,4,0" Foreground="{DynamicResource ForegroundBrush}"/>
                        <ComboBox x:Name="TimeDisplayModeBox" Width="120" SelectionChanged="TimeDisplayMode_SelectionChanged" ToolTip="How timestamps are displayed across all tabs">
                            <ComboBoxItem Content="Server Time" Tag="ServerTime"/>
                            <ComboBoxItem Content="Local Time" Tag="LocalTime"/>
                            <ComboBoxItem Content="UTC" Tag="UTC"/>
                        </ComboBox>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="2,4,0,0">
                        <TextBlock x:Name="GlobalDateRangeIndicator"
                                   Text="Showing: Last 24 Hours"
                                   FontStyle="Italic"
                                   Foreground="{DynamicResource AccentBrush}"
                                   FontWeight="SemiBold"/>
                        <TextBlock x:Name="OriginalRangeIndicator"
                                   Text=""
                                   FontStyle="Italic"
                                   Foreground="{DynamicResource AccentBrush}"
                                   FontWeight="SemiBold"
                                   Margin="12,0,0,0"
                                   Visibility="Collapsed"/>
                        <TextBlock x:Name="RevertHintText"
                                   Text="(double-click chart to revert)"
                                   FontStyle="Italic"
                                   Foreground="{DynamicResource AccentBrush}"
                                   FontWeight="SemiBold"
                                   Margin="8,0,0,0"
                                   Visibility="Collapsed"/>
                    </StackPanel>
                </StackPanel>

                <!-- Row 2: Custom Date/Time Range Controls -->
                <StackPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Orientation="Horizontal" Margin="0,8,0,0">
                    <TextBlock Text="Custom Range:" VerticalAlignment="Center" Margin="0,0,10,0" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}"/>
                    <DatePicker x:Name="GlobalFromDate" Width="120" Margin="2,0" SelectedDateChanged="GlobalCustomDateTime_Changed" CalendarOpened="DatePicker_CalendarOpened"/>
                    <ComboBox x:Name="GlobalFromHour" Width="70" Margin="2,0" SelectionChanged="GlobalTimeCombo_Changed" ToolTip="Hour"/>
                    <ComboBox x:Name="GlobalFromMinute" Width="52" Margin="2,0" SelectionChanged="GlobalTimeCombo_Changed" ToolTip="Minute"/>
                    <TextBlock Text="to" VerticalAlignment="Center" Margin="8,0" Foreground="{DynamicResource ForegroundBrush}"/>
                    <DatePicker x:Name="GlobalToDate" Width="120" Margin="2,0" SelectedDateChanged="GlobalCustomDateTime_Changed" CalendarOpened="DatePicker_CalendarOpened"/>
                    <ComboBox x:Name="GlobalToHour" Width="70" Margin="2,0" SelectionChanged="GlobalTimeCombo_Changed" ToolTip="Hour"/>
                    <ComboBox x:Name="GlobalToMinute" Width="52" Margin="2,0" SelectionChanged="GlobalTimeCombo_Changed" ToolTip="Minute"/>
                </StackPanel>
            </Grid>
        </Border>

        <!-- Tab Control -->
        <TabControl Grid.Row="1" x:Name="DataTabControl" Margin="10,0,10,0" SelectionChanged="DataTabControl_SelectionChanged">
            <!-- Overview Tab - At-a-glance health and status -->
            <TabItem Header="Overview">
                <TabControl ItemContainerStyle="{DynamicResource SubTabItemStyle}">
                    <!-- Resource Overview Sub-Tab - Consolidated view of CPU, Memory, I/O, Waits -->
                    <TabItem Header="Resource Overview">
                        <!-- 2x2 Chart Grid -->
                        <Grid Margin="5">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <!-- CPU Chart (Top Left) -->
                            <Border Grid.Row="0" Grid.Column="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="CPU Utilization" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="ResourceOverviewCpuChart" Margin="5"/>
                                </Grid>
                            </Border>

                            <!-- Memory Chart (Top Right) -->
                            <Border Grid.Row="0" Grid.Column="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="Memory Utilization" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="ResourceOverviewMemoryChart" Margin="5"/>
                                </Grid>
                            </Border>

                            <!-- I/O Chart (Bottom Left) -->
                            <Border Grid.Row="1" Grid.Column="0" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="File I/O Latency" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="ResourceOverviewIoChart" Margin="5"/>
                                </Grid>
                            </Border>

                            <!-- Wait Stats Chart (Bottom Right) -->
                            <Border Grid.Row="1" Grid.Column="1" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Margin="5">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="Wait Statistics" FontWeight="Bold" FontSize="14" Margin="10,5" Foreground="{DynamicResource ForegroundBrush}"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="ResourceOverviewWaitChart" Margin="5"/>
                                </Grid>
                            </Border>
                        </Grid>
                    </TabItem>


                    <!-- Daily Summary Sub-Tab -->
                    <TabItem Header="Daily Summary">
                        <controls:DailySummaryContent x:Name="DailySummaryTab" />
                    </TabItem>

                    <!-- Recommendations Sub-Tab -->
                    <TabItem Header="Recommendations">
                        <controls:RecommendationsContent x:Name="RecommendationsTab" />
                    </TabItem>

                    <!-- Default Trace Sub-Tab -->
                    <TabItem Header="Default Trace">
                        <controls:DefaultTraceContent x:Name="DefaultTraceTab" />
                    </TabItem>

                    <!-- Current Configuration Sub-Tab -->
                    <TabItem Header="Current Configuration">
                        <controls:CurrentConfigContent x:Name="CurrentConfigTab" />
                    </TabItem>

                    <!-- Configuration Changes Sub-Tab -->
                    <TabItem Header="Configuration Changes">
                        <controls:ConfigChangesContent x:Name="ConfigChangesTab" />
                    </TabItem>

                    <!-- Collection Health Sub-Tab -->
                    <TabItem Header="Collection Health">
                      <DockPanel>
                        <!-- Banner shown when collection has stopped (jobs disabled / Agent down / collectors failing).
                             Populated by the app's own check, so it surfaces here even when collection is off. -->
                        <Border x:Name="CollectionStoppedBanner" DockPanel.Dock="Top" Visibility="Collapsed"
                                Background="#5C2626" BorderBrush="#8B3A3A" BorderThickness="1" CornerRadius="3"
                                Padding="10,6" Margin="5,5,5,0">
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="&#x26A0;" FontSize="14" VerticalAlignment="Center" Margin="0,0,8,0" Foreground="White"/>
                                <TextBlock x:Name="CollectionStoppedBannerText" VerticalAlignment="Center" Foreground="White"
                                           TextWrapping="Wrap" FontWeight="SemiBold"/>
                            </StackPanel>
                        </Border>
                        <TabControl ItemContainerStyle="{DynamicResource SubTabItemStyle}">
                            <TabItem Header="Health Summary">
                                <Grid>
                                    <DataGrid x:Name="HealthDataGrid"
                                              RowStyle="{StaticResource HealthRowStyle}"
                                              AutoGenerateColumns="False"
                                              IsReadOnly="True"
                                              CanUserSortColumns="True"
                                              GridLinesVisibility="All" CanUserResizeColumns="True"
                                              MouseDoubleClick="HealthDataGrid_MouseDoubleClick">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn Binding="{Binding CollectorName}" Width="250">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectorName" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Collector" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding HealthStatus}" Width="100">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HealthStatus" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding HoursSinceSuccess}" ElementStyle="{StaticResource NumericCell}" Width="120">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HoursSinceSuccess" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Hours Since Success" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding LastSuccessTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastSuccessTime" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Last Success" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding FailureRatePercent, StringFormat='{}{0:F2}%'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FailureRatePercent" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Failure %" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding TotalRuns7d, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalRuns7d" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Runs (7d)" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding FailedRuns7d, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FailedRuns7d" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Failed (7d)" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgDurationMs" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Avg Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                            <DataGridTextColumn Binding="{Binding TotalRowsCollected7d, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="150">
                                                <DataGridTextColumn.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalRowsCollected7d" Click="CollectionHealthFilter_Click" Margin="0,0,4,0"/>
                                                        <TextBlock Text="Rows Collected (7d)" FontWeight="Bold" VerticalAlignment="Center"/>
                                                    </StackPanel>
                                                </DataGridTextColumn.Header>
                                            </DataGridTextColumn>
                                        </DataGrid.Columns>
                                    </DataGrid>
                                    <TextBlock x:Name="HealthNoDataMessage" Style="{StaticResource NoDataMessage}"/>
                                </Grid>
                            </TabItem>
                            <TabItem Header="Duration Trends">
                                <ScottPlot:WpfPlot x:Name="CollectorDurationChart" Margin="4"/>
                            </TabItem>
                        </TabControl>
                      </DockPanel>
                    </TabItem>

                    <!-- Running Jobs Sub-Tab -->
                    <TabItem Header="Running Jobs">
                        <Grid>
                            <DataGrid x:Name="RunningJobsDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                                      RowHeight="28" GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                                <DataGrid.Resources>
                                    <Style TargetType="DataGridRow">
                                        <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsRunningLong}" Value="True">
                                                <Setter Property="Background" Value="#5C2626"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGrid.Resources>
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding JobName}" Width="250">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="Job Name" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding StartTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="Start Time" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding CurrentDurationFormatted}" Width="120" SortMemberPath="CurrentDurationSeconds">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="Current Duration" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding AvgDurationFormatted}" Width="120" SortMemberPath="AvgDurationSeconds">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="Avg Duration" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding P95DurationSeconds, StringFormat='{}{0:N0}s'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="P95 Duration" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding PercentOfAverage, StringFormat='{}{0:N0}%'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="% of Average" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridCheckBoxColumn Binding="{Binding IsRunningLong, Mode=OneWay}" Width="90">
                                        <DataGridCheckBoxColumn.Header>
                                            <TextBlock Text="Running Long" FontWeight="Bold"/>
                                        </DataGridCheckBoxColumn.Header>
                                    </DataGridCheckBoxColumn>
                                    <DataGridTextColumn Binding="{Binding SuccessfulRunCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <TextBlock Text="History Runs" FontWeight="Bold"/>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridCheckBoxColumn Binding="{Binding JobEnabled, Mode=OneWay}" Width="70">
                                        <DataGridCheckBoxColumn.Header>
                                            <TextBlock Text="Enabled" FontWeight="Bold"/>
                                        </DataGridCheckBoxColumn.Header>
                                    </DataGridCheckBoxColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="RunningJobsNoDataMessage" Style="{StaticResource NoDataMessage}"/>
                        </Grid>
                    </TabItem>
                </TabControl>
            </TabItem>

            <!-- Queries Tab -->
            <TabItem Header="Queries" x:Name="QueriesTabItem">
                <controls:QueryPerformanceContent x:Name="PerformanceTab"/>
            </TabItem>

            <!-- Plan Viewer Tab -->
            <TabItem Header="Plan Viewer" x:Name="PlanViewerTabItem">
                <Grid Background="Transparent">
                    <Grid x:Name="PlanEmptyState">
                        <!-- Dashed drop-zone border fills the whole tab -->
                        <Rectangle Margin="24"
                                   Stroke="{DynamicResource ForegroundMutedBrush}"
                                   StrokeThickness="1.5"
                                   StrokeDashArray="6,4"
                                   RadiusX="10" RadiusY="10"
                                   Opacity="0.25"/>
                        <!-- Centered content -->
                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                            <TextBlock Text="&#xE896;" FontFamily="Segoe MDL2 Assets" FontSize="52"
                                       HorizontalAlignment="Center"
                                       Foreground="{DynamicResource ForegroundMutedBrush}"
                                       Margin="0,0,0,12"/>
                            <TextBlock Text="No Plan Loaded" FontSize="20" FontWeight="Light"
                                       Foreground="{DynamicResource ForegroundMutedBrush}" HorizontalAlignment="Center"/>
                            <TextBlock Text="Right-click a query row and choose 'View Plan' or 'Get Actual Plan'"
                                       FontSize="13" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,8,0,0"/>
                        </StackPanel>
                    </Grid>
                    <StackPanel x:Name="PlanLoadingState" HorizontalAlignment="Center"
                                VerticalAlignment="Center" Visibility="Collapsed">
                        <ProgressBar IsIndeterminate="True" Width="200" Height="4" Margin="0,0,0,12"/>
                        <TextBlock x:Name="PlanLoadingLabel" Text="Executing query for actual plan..."
                                   FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                                   HorizontalAlignment="Center"/>
                        <Button x:Name="CancelPlanButton" Content="Cancel" Click="CancelPlanButton_Click"
                                HorizontalAlignment="Center" Margin="0,12,0,0" Padding="16,4"/>
                    </StackPanel>
                    <TabControl x:Name="PlanTabControl" Visibility="Collapsed"
                                Background="Transparent"/>
                </Grid>
            </TabItem>

            <!-- Resource Metrics Tab -->
            <TabItem x:Name="ResourceMetricsTabItem">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Resource Metrics" VerticalAlignment="Center"/>
                        <Border x:Name="ResourceMetricsBadge" Style="{StaticResource AlertBadge}" Visibility="Collapsed">
                            <TextBlock Text="!" FontWeight="Bold" Foreground="White"/>
                        </Border>
                    </StackPanel>
                </TabItem.Header>
                <controls:ResourceMetricsContent x:Name="ResourceMetricsContent" />
            </TabItem>

            <!-- Memory Tab -->
            <TabItem x:Name="MemoryTabItem">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Memory" VerticalAlignment="Center"/>
                        <Border x:Name="MemoryBadge" Style="{StaticResource AlertBadge}" Visibility="Collapsed">
                            <TextBlock Text="!" FontWeight="Bold" Foreground="White"/>
                        </Border>
                    </StackPanel>
                </TabItem.Header>
                <controls:MemoryContent x:Name="MemoryTab"/>
            </TabItem>

            <!-- Locking Tab -->
            <TabItem x:Name="LockingTabItem">
                <TabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Locking" VerticalAlignment="Center"/>
                        <Border x:Name="LockingBadge" Style="{StaticResource AlertBadgeCritical}" Visibility="Collapsed">
                            <TextBlock Text="!" FontWeight="Bold" Foreground="White"/>
                        </Border>
                    </StackPanel>
                </TabItem.Header>
                <TabControl x:Name="LockingSubTabControl" ItemContainerStyle="{DynamicResource SubTabItemStyle}">
                    <!-- Blocking/Deadlock Stats Sub-Tab (default tab) -->
                    <TabItem Header="Blocking/Deadlock Trends">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>

                            <!-- Lock Wait Stats Chart - Full Width -->
                            <Border Grid.Row="0" BorderBrush="LightGray" BorderThickness="1" Margin="5,5,5,2">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0" Text="Lock Wait Stats (LCK%)" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="LockWaitStatsChart"/>
                                </Grid>
                            </Border>

                            <!-- Trend Charts - 2x2 Grid -->
                            <Grid Grid.Row="1" Margin="5,2,5,5">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>

                                <Border Grid.Row="0" Grid.Column="0" BorderBrush="LightGray" BorderThickness="1" Margin="2">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Text="Blocking Events" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="BlockingStatsBlockingEventsChart"/>
                                    </Grid>
                                </Border>

                                <Border Grid.Row="0" Grid.Column="1" BorderBrush="LightGray" BorderThickness="1" Margin="2">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Text="Blocking Duration (ms)" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="BlockingStatsDurationChart"/>
                                    </Grid>
                                </Border>

                                <Border Grid.Row="1" Grid.Column="0" BorderBrush="LightGray" BorderThickness="1" Margin="2">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Text="Deadlock Count" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="BlockingStatsDeadlocksChart"/>
                                    </Grid>
                                </Border>

                                <Border Grid.Row="1" Grid.Column="1" BorderBrush="LightGray" BorderThickness="1" Margin="2">
                                    <Grid>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="*"/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Row="0" Text="Deadlock Wait Time (ms)" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                        <ScottPlot:WpfPlot Grid.Row="1" x:Name="BlockingStatsDeadlockWaitTimeChart"/>
                                    </Grid>
                                </Border>
                            </Grid>
                        </Grid>
                    </TabItem>

                    <!-- Current Waits Sub-Tab -->
                    <TabItem Header="Current Waits">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>

                            <Border Grid.Row="0" BorderBrush="LightGray" BorderThickness="1" Margin="5,5,5,2">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0" Text="Wait Duration by Wait Type" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="CurrentWaitsDurationChart"/>
                                </Grid>
                            </Border>

                            <Border Grid.Row="1" BorderBrush="LightGray" BorderThickness="1" Margin="5,2,5,5">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0" Text="Blocked Sessions by Database" FontWeight="Bold" HorizontalAlignment="Center" Margin="0,5"/>
                                    <ScottPlot:WpfPlot Grid.Row="1" x:Name="CurrentWaitsBlockedChart"/>
                                </Grid>
                            </Border>
                        </Grid>
                    </TabItem>

                    <!-- Blocking Sub-Tab -->
                    <TabItem Header="Blocking">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <controls:TimeRangeSlicerControl x:Name="BlockingSlicer" Grid.Row="0"/>
                        <DataGrid x:Name="BlockingEventsDataGrid" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True"
                                      MouseDoubleClick="BlockingEventsDataGrid_MouseDoubleClick"
                                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                                      RowStyle="{StaticResource BlockingEventsPlanRowStyle}"
                                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding EventTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="EventTime" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Event Time" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Source}" Width="130">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Source" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Source" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Activity}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Activity" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Activity" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Spid, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="50">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Spid" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding BlockingTree}" Width="200">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockingTree" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Blocking Tree" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding ContentiousObject}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ContentiousObject" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Contentious Object" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaitTimeMs, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitTimeMs" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Wait (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaitResource}" Width="180">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitResource" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Wait Resource" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LockMode}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LockMode" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Lock Mode" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding IsolationLevel}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IsolationLevel" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Isolation" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Status}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Status" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LoginName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LoginName" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Login" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding HostName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostName" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Host" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding ClientApp}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ClientApp" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Application" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTemplateColumn Width="300">
                                        <DataGridTemplateColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTemplateColumn.Header>
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                    <DataGridTextColumn Binding="{Binding TransactionName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TransactionName" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Tran Name" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TransactionCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TransactionCount" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Tran Count" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Priority, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="55">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Priority" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Priority" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LogUsed, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LogUsed" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Log Used (bytes)" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="BlockingEventsFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Collection Time" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTemplateColumn Header="XML" Width="90">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <Button Content="Download"
                                                        Click="DownloadBlockingXml_Click"
                                                        IsEnabled="{Binding HasBlockedProcessReport}"
                                                        Width="75" Height="22" FontSize="11" Foreground="{DynamicResource ForegroundBrush}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        <TextBlock x:Name="BlockingEventsNoDataMessage" Grid.Row="1" Style="{StaticResource NoDataMessage}"/>
                        </Grid>
                    </TabItem>

                    <!-- Deadlocks Sub-Tab -->
                    <TabItem Header="Deadlocks">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <controls:TimeRangeSlicerControl x:Name="DeadlockSlicer" Grid.Row="0"/>
                        <DataGrid x:Name="DeadlocksDataGrid" Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True"
                                      RowHeight="28" GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                                      RowStyle="{StaticResource DeadlocksPlanRowStyle}"
                                      MouseDoubleClick="DeadlocksDataGrid_MouseDoubleClick"
                                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding EventDate, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="EventDate" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Event Date" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding DeadlockType}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DeadlockType" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Type" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding DeadlockGroup}" Width="120">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DeadlockGroup" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Group" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Spid, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="50">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Spid" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaitTime, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitTime" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Wait (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaitResource}" Width="180">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitResource" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Wait Resource" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LockMode}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LockMode" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Lock Mode" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding OwnerMode}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OwnerMode" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Owner Mode" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaiterMode}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaiterMode" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Waiter Mode" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding IsolationLevel}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IsolationLevel" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Isolation" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Status}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Status" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LoginName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LoginName" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Login" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding HostName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostName" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Host" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding ClientApp}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ClientApp" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Application" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Query}" Width="300">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Query" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Query" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TransactionName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TransactionName" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Tran Name" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TransactionCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="70">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TransactionCount" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Tran Count" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Priority, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="55">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Priority" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Priority" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LogUsed, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="80">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LogUsed" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Log Used (bytes)" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding ServerName}" Width="100">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ServerName" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Server" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="DeadlocksFilter_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Collection Time" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTemplateColumn Header="Graph" Width="90">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <!-- Always enabled: the graph is fetched on demand (deferred out of the grid query); a deadlock always has one. -->
                                                <Button Content="Download"
                                                        Click="DownloadDeadlockGraph_Click"
                                                        IsEnabled="True"
                                                        Width="75" Height="22" FontSize="11" Foreground="{DynamicResource ForegroundBrush}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                        <TextBlock x:Name="DeadlocksNoDataMessage" Grid.Row="1" Style="{StaticResource NoDataMessage}"/>
                        </Grid>
                    </TabItem>

                </TabControl>
            </TabItem>

            <!-- System Events Tab -->
            <TabItem Header="System Events">
                <controls:SystemEventsContent x:Name="SystemEventsContent" />
            </TabItem>
        </TabControl>

        <!-- Footer -->
        <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,5,10,5">
            <TextBlock x:Name="FooterText"
                       Text="Initializing..."
                       FontSize="12"
                       Foreground="{DynamicResource ForegroundBrush}"/>
            <TextBlock Text=" | " FontSize="12" Foreground="{DynamicResource ForegroundBrush}"/>
            <TextBlock x:Name="StatusText"
                       Text="Ready"
                       FontSize="12"
                       Foreground="{DynamicResource ForegroundBrush}"/>
        </StackPanel>
    </Grid>
</UserControl>
