<UserControl x:Class="Plugin.DeviceDebug.Panels.PlcDebugPanel"
             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:dp="clr-namespace:HMI.UI.DpUtil;assembly=HMI.UI"
             mc:Ignorable="d" 
             d:DesignHeight="500" d:DesignWidth="600">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/HMI.UI;component/Style/PanuonListView.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

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

        <!-- 标题 -->
        <Border Grid.Row="0" Background="#9C27B0" CornerRadius="4" Padding="12,8">
            <TextBlock Text="PLC 调试面板" FontSize="16" FontWeight="Bold" Foreground="White"/>
        </Border>

        <!-- IO 控制区 -->
        <Border Grid.Row="1" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4" Padding="12">
            <Grid dp:GridHelper.Space="10">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- 地址 -->
                <TextBlock Grid.Row="0" Grid.Column="0" Text="地址:" VerticalAlignment="Center" FontWeight="Bold"/>
                <TextBox Grid.Row="0" Grid.Column="1" x:Name="addressInput"
                         IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                         VerticalContentAlignment="Center" Padding="6,4" Text="M0.0"/>

                <!-- 值 -->
                <TextBlock Grid.Row="1" Grid.Column="0" Text="值:" VerticalAlignment="Center" FontWeight="Bold"/>
                <ComboBox Grid.Row="1" Grid.Column="1" x:Name="valueSelect"
                          IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                          VerticalContentAlignment="Center" Padding="6,4">
                    <ComboBoxItem Content="True" IsSelected="True"/>
                    <ComboBoxItem Content="False"/>
                </ComboBox>

                <!-- 写入按钮 -->
                <Button Grid.Row="2" Grid.ColumnSpan="2" x:Name="writeBtn" Content="写入" Padding="20,6"
                        IsEnabled="{Binding IsMaintenanceMode, RelativeSource={RelativeSource AncestorType=UserControl}}"
                        Click="WriteBtn_Click" HorizontalAlignment="Left"
                        Background="#9C27B0" Foreground="White" BorderThickness="0" Cursor="Hand"/>
            </Grid>
        </Border>

        <!-- IO 状态显示区 -->
        <Border Grid.Row="2" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" Background="#F5F5F5" Padding="8,6">
                    <TextBlock Text="IO 点位状态" FontWeight="Bold" FontSize="14"/>
                </Border>

                <ListView Grid.Row="1" x:Name="ioStatusList" Height="180"
                          Style="{StaticResource DefaultListTable}"
                          VirtualizingPanel.IsVirtualizing="True"
                          VirtualizingPanel.VirtualizationMode="Standard">
                    <ListView.View>
                        <GridView AllowsColumnReorder="False">
                            <GridViewColumn Width="120" Header="地址">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding Address}" FontFamily="Consolas"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="80" Header="值">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Text="{Binding Value}">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock" BasedOn="{StaticResource ListTableTextBlock}">
                                                        <Style.Triggers>
                                                            <DataTrigger Binding="{Binding Value}" Value="True">
                                                                <Setter Property="Foreground" Value="#4CAF50"/>
                                                                <Setter Property="FontWeight" Value="Bold"/>
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Value}" Value="False">
                                                                <Setter Property="Foreground" Value="#999"/>
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="150" Header="更新时间">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border Style="{StaticResource CellBorder}">
                                            <TextBlock Style="{StaticResource ListTableTextBlock}" 
                                                       Text="{Binding LastUpdate, StringFormat=HH:mm:ss}"/>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </Border>

        <!-- 互锁状态区 -->
        <Border Grid.Row="3" BorderBrush="#DDD" BorderThickness="1" CornerRadius="4">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" Background="#FFF3E0" Padding="8,6">
                    <TextBlock Text="互锁状态" FontWeight="Bold" FontSize="14" Foreground="#E65100"/>
                </Border>

                <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
                    <ItemsControl x:Name="interlockList" Margin="8">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="#EEE" BorderThickness="0,0,0,1" Padding="4,6">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="{Binding Name}" FontWeight="Bold" Width="150"/>
                                        <TextBlock Text="{Binding Status}" Margin="10,0,0,0">
                                            <TextBlock.Style>
                                                <Style TargetType="TextBlock">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Status}" Value="满足">
                                                            <Setter Property="Foreground" Value="#4CAF50"/>
                                                        </DataTrigger>
                                                        <DataTrigger Binding="{Binding Status}" Value="不满足">
                                                            <Setter Property="Foreground" Value="#F44336"/>
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </TextBlock.Style>
                                        </TextBlock>
                                    </StackPanel>
                                </Border>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </ScrollViewer>
            </Grid>
        </Border>
    </Grid>
</UserControl>
