<UserControl x:Class="Plugin.DeviceDebug.DeviceDebugTab"
             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:ui="clr-namespace:HMI.UI;assembly=HMI.UI"
             xmlns:pw="https://opensource.panuon.com/wpf"
             xmlns:pu="https://opensource.panuon.com/wpf-ui"
             xmlns:dp="clr-namespace:HMI.UI.DpUtil;assembly=HMI.UI"
             mc:Ignorable="d"
             d:DesignHeight="600" d:DesignWidth="1000">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/HMI.UI;component/Style/PanuonListView.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <BooleanToVisibilityConverter x:Key="BoolToVis"/>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid dp:GridHelper.Space="8">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- 设备信息栏 -->
        <Border Grid.Row="0" BorderBrush="#CCCCCC" BorderThickness="1" Padding="10,6" Background="#F9F9F9"
                Visibility="{Binding HasDevice, Converter={StaticResource BoolToVis}}">
            <WrapPanel dp:StackPanelHelper.ChildSpace="20">
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="设备ID:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding CurrentDeviceId}" Style="{StaticResource ListTableTextBlock}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="型号:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding CurrentDeviceKind}" Style="{StaticResource ListTableTextBlock}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="连接状态:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding ConnectionStateText}" Foreground="{Binding ConnectionBrush}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="版本:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding CurrentEffectiveVersion}" Style="{StaticResource ListTableTextBlock}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="预约状态:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding ReservationStatusText}" Style="{StaticResource ListTableTextBlock}"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="4">
                    <TextBlock Text="会话状态:" FontWeight="Bold" Foreground="#555"/>
                    <TextBlock Text="{Binding SessionStateText}" Foreground="{Binding SessionStateBrush}" FontWeight="Bold"/>
                </StackPanel>
            </WrapPanel>
        </Border>

        <!-- 操作按钮栏 -->
        <StackPanel Grid.Row="1" Orientation="Horizontal" dp:StackPanelHelper.ChildSpace="10"
                    Visibility="{Binding HasDevice, Converter={StaticResource BoolToVis}}">
            <Button x:Name="enterMaintenanceBtn" Content="进入维护控制" Padding="14,6"
                    IsEnabled="{Binding CanEnterMaintenance}"
                    Click="EnterMaintenanceBtn_Click"
                    Background="#2196F3" Foreground="White" BorderThickness="0" Cursor="Hand"/>
            <Button x:Name="exitMaintenanceBtn" Content="退出维护" Padding="14,6"
                    IsEnabled="{Binding CanExitMaintenance}"
                    Click="ExitMaintenanceBtn_Click"
                    Background="#FF5722" Foreground="White" BorderThickness="0" Cursor="Hand"/>
        </StackPanel>

        <!-- 主体区域：左面板 + 右日志 -->
        <Grid Grid.Row="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="350"/>
            </Grid.ColumnDefinitions>

            <Border Grid.Column="0" BorderBrush="#DDDDDD" BorderThickness="1" Margin="0,0,8,0">
                <ContentControl x:Name="panelContainer">
                    <TextBlock Text="正在初始化…" HorizontalAlignment="Center" VerticalAlignment="Center"
                               FontSize="16" Foreground="#999"/>
                </ContentControl>
            </Border>

            <Border Grid.Column="1" BorderBrush="#DDDDDD" BorderThickness="1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Border Grid.Row="0" Background="#F0F0F0" Padding="8,6">
                        <TextBlock Text="操作日志" FontWeight="Bold" FontSize="14"/>
                    </Border>
                    <ListView Grid.Row="1" x:Name="operationLogList"
                              ItemsSource="{Binding OperationLogs}"
                              Style="{StaticResource DefaultListTable}"
                              VirtualizingPanel.IsVirtualizing="True"
                              VirtualizingPanel.VirtualizationMode="Standard">
                        <ListView.View>
                            <GridView AllowsColumnReorder="False">
                                <GridViewColumn Width="60" Header="类型" DisplayMemberBinding="{Binding CommandType}"/>
                                <GridViewColumn Width="120" Header="参数" DisplayMemberBinding="{Binding Parameters}"/>
                                <GridViewColumn Width="60" Header="结果" DisplayMemberBinding="{Binding Outcome}"/>
                                <GridViewColumn Width="100" Header="时间" DisplayMemberBinding="{Binding Timestamp, StringFormat=HH:mm:ss}"/>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </Border>
        </Grid>
    </Grid>
</UserControl>
