<mah:MetroWindow x:Class="BDU.Windows.DeviceManageWindow"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BDU.Windows"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:DeviceManageWindow}"
        WindowStyle="None"
        WindowStartupLocation="CenterScreen"
        ShowTitleBar="False"
        Title="DeviceManageWindow" Height="750" Width="900">
    <Grid>
        <GroupBox Header="设备管理界面"
                  Padding="0,10,0,0"
                  MouseLeftButtonDown="GroupBox_MouseLeftButtonDown">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="400"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <!-- 左侧：设备列表 -->
                <GroupBox Grid.Row="0" Grid.Column="0" Header="预配置设备列表" Margin="0,0,5,0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        
                        <!-- 设备列表 -->
                        <ListBox Grid.Row="0" 
                                 ItemsSource="{Binding PreDefineDevices}"
                                 SelectedItem="{Binding SelectedDevice}"
                                 SelectionChanged="DeviceList_SelectionChanged"
                                 Margin="5">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Vertical" Margin="5">
                                        <TextBlock Text="{Binding Name}" 
                                                   FontWeight="Bold" 
                                                   FontSize="14"/>
                                        <TextBlock Text="{Binding Type}" 
                                                   FontSize="12" 
                                                   Foreground="Gray"/>
                                        <TextBlock Text="{Binding Description}" 
                                                   FontSize="11" 
                                                   Foreground="DarkGray"
                                                   TextTrimming="CharacterEllipsis"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                        
                        <!-- 按钮区 -->
                        <StackPanel Grid.Row="1" 
                                    Orientation="Horizontal" 
                                    HorizontalAlignment="Center"
                                    Margin="5">
                            <Button Content="新增设备" 
                                    Width="100" 
                                    Margin="15,5,15,5"
                                    Click="AddDevice_Click"/>
                            <Button Content="删除设备" 
                                    Width="100" 
                                    Margin="15,5,15,5"
                                    Click="DeleteDevice_Click"
                                    Foreground="Red"/>
                        </StackPanel>
                    </Grid>
                </GroupBox>

                <!-- 右侧：设备编辑区 -->
                <GroupBox Grid.Row="0" Grid.Column="1" Header="设备配置" Margin="5,0,0,0">
                    <ScrollViewer VerticalScrollBarVisibility="Auto">
                        <StackPanel Margin="10" IsEnabled="{Binding SelectedDevice, Converter={StaticResource StringToVisibilityConverter}}">
                            <!-- 设备名称 -->
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <Label Content="设备名称" Width="100" VerticalAlignment="Center"/>
                                <TextBox Text="{Binding SelectedDevice.Name, UpdateSourceTrigger=PropertyChanged}" 
                                         Width="250" 
                                         VerticalAlignment="Center"/>
                            </StackPanel>

                            <!-- 通讯类型 -->
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <Label Content="通讯类型" Width="100" VerticalAlignment="Center"/>
                                <ComboBox ItemsSource="{Binding Types}"
                                          SelectedItem="{Binding SelectedDevice.Type}"
                                          Width="250"
                                          SelectionChanged="DeviceType_SelectionChanged"
                                          VerticalAlignment="Center"/>
                            </StackPanel>

                            <!-- 设备描述 -->
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <Label Content="设备描述" Width="100" VerticalAlignment="Center"/>
                                <TextBox Text="{Binding SelectedDevice.Description, UpdateSourceTrigger=PropertyChanged}" 
                                         Width="250" 
                                         TextWrapping="Wrap"
                                         AcceptsReturn="True"
                                         VerticalAlignment="Top"/>
                            </StackPanel>

                            <!-- 连接参数 -->
                            <Label Content="连接参数" 
                                   FontWeight="Bold" 
                                   FontSize="14"
                                   Margin="0,20,0,10"/>
                            
                            <ItemsControl ItemsSource="{Binding DeviceConnectSettings}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal" Margin="0,5">
                                            <Label Content="{Binding Name}" 
                                                   Width="100" 
                                                   VerticalAlignment="Center"/>
                                            
                                            <!-- 文本输入框（除COM口和奇偶校验外） -->
                                            <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" 
                                                     Width="250"
                                                     VerticalAlignment="Center"
                                                     LostFocus="ConnectSettingValue_LostFocus"
                                                     Visibility="{Binding Name, Converter={StaticResource DeviceNameConverter}}"/>
                                            
                                            <!-- 下拉框（COM口和奇偶校验） -->
                                            <ComboBox SelectedItem="{Binding Value}"
                                                      Width="250"
                                                      VerticalAlignment="Center"
                                                      ItemsSource="{Binding Name, Converter={StaticResource DeviceNameConverter}, ConverterParameter=Items}"
                                                      Visibility="{Binding Name, Converter={StaticResource DeviceNameConverter}, ConverterParameter=Inverse}"
                                                      SelectionChanged="ConnectSettingValue_LostFocus"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>

                            <!-- 提示信息 -->
                            <TextBlock Text="请选择左侧设备进行编辑"
                                       Foreground="Gray"
                                       FontStyle="Italic"
                                       Margin="0,20"
                                       HorizontalAlignment="Center"
                                       Visibility="{Binding SelectedDevice, Converter={StaticResource BoolInverseConverter}}"/>
                        </StackPanel>
                    </ScrollViewer>
                </GroupBox>

                <!-- 底部按钮区 -->
                <StackPanel Grid.Row="1" 
                            Grid.Column="0" 
                            Grid.ColumnSpan="2"
                            Orientation="Horizontal" 
                            HorizontalAlignment="Right"
                            Margin="10">
                    <Button Content="保存" 
                            Width="100" 
                            Margin="15,5,15,5"
                            Click="Save_Click"/>
                    <Button Content="关闭" 
                            Width="100" 
                            Margin="15,5,15,5"
                            Click="Close_Click"/>
                </StackPanel>
            </Grid>
        </GroupBox>
    </Grid>
</mah:MetroWindow>
