<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="using:ZB.MOM.WW.OtOpcUa.Client.UI.ViewModels"
             xmlns:controls="using:ZB.MOM.WW.OtOpcUa.Client.UI.Controls"
             x:Class="ZB.MOM.WW.OtOpcUa.Client.UI.Views.HistoryView"
             x:DataType="vm:HistoryViewModel">
    <DockPanel Margin="8">
        <!-- Controls -->
        <StackPanel DockPanel.Dock="Top" Spacing="10">
            <TextBlock Text="History Read" FontWeight="Bold" />
            <TextBlock Text="{Binding SelectedNodeId, FallbackValue='(no node selected)'}"
                       Foreground="Gray" />

            <!-- Row 1: Time range -->
            <controls:DateTimeRangePicker StartDateTime="{Binding StartTime, Mode=TwoWay}"
                                          EndDateTime="{Binding EndTime, Mode=TwoWay}" />

            <!-- Row 2: Aggregate, Interval, Max Values, Read button -->
            <StackPanel Orientation="Horizontal" Spacing="12">
                <StackPanel Spacing="2">
                    <TextBlock Text="Aggregate" FontSize="11" Foreground="Gray" />
                    <ComboBox ItemsSource="{Binding AggregateTypes}"
                              SelectedItem="{Binding SelectedAggregateType}"
                              Width="150">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding, TargetNullValue='Raw'}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </StackPanel>
                <StackPanel Spacing="2" IsVisible="{Binding IsAggregateRead}">
                    <TextBlock Text="Interval (ms)" FontSize="11" Foreground="Gray" />
                    <NumericUpDown Value="{Binding IntervalMs}" Minimum="1000" Maximum="86400000" Width="150" />
                </StackPanel>
                <StackPanel Spacing="2">
                    <TextBlock Text="Max Values" FontSize="11" Foreground="Gray" />
                    <NumericUpDown Value="{Binding MaxValues}" Minimum="1" Maximum="100000" Width="120" />
                </StackPanel>
                <Button Content="Read History"
                        Command="{Binding ReadHistoryCommand}"
                        VerticalAlignment="Bottom"
                        Padding="16,6" />
            </StackPanel>

            <ProgressBar IsIndeterminate="True"
                         IsVisible="{Binding IsLoading}"
                         Height="4" />
        </StackPanel>

        <!-- Results -->
        <DataGrid ItemsSource="{Binding Results}"
                  AutoGenerateColumns="False"
                  IsReadOnly="True"
                  HorizontalScrollBarVisibility="Auto"
                  Margin="0,8,0,0">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="Auto" />
                <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="Auto" />
                <DataGridTextColumn Header="Source Timestamp" Binding="{Binding SourceTimestamp}" Width="Auto" />
                <DataGridTextColumn Header="Server Timestamp" Binding="{Binding ServerTimestamp}" Width="Auto" />
            </DataGrid.Columns>
        </DataGrid>
    </DockPanel>
</UserControl>
