<UserControl
    x:Class="SHH.CameraDashboard.WizardClientsControl"
    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"
    Width="850"
    Height="550"
    mc:Ignorable="d">

    <Border
        Background="{DynamicResource Brush.Bg.Window}"
        BorderBrush="{DynamicResource Brush.Border.Focus}"
        BorderThickness="1"
        CornerRadius="{DynamicResource Radius.Normal}">
        <Grid Margin="25">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Margin="0,0,0,20">
                <DockPanel LastChildFill="False">
                    <TextBlock
                        VerticalAlignment="Center"
                        FontSize="{DynamicResource Size.Font.Huge}"
                        FontWeight="Bold"
                        Foreground="{DynamicResource Brush.Text.Primary}"
                        Text="📡 服务节点配置" />
                    <Button
                        Width="30"
                        Height="30"
                        Command="{Binding CancelCommand}"
                        Content="✕"
                        DockPanel.Dock="Right"
                        Style="{DynamicResource Btn.Ghost}" />
                </DockPanel>
                <TextBlock
                    Margin="0,10,0,0"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="请添加或修改后端的 IP 地址与端口，配置将用于聚合监控数据。" />
            </StackPanel>

            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Border
                    Padding="10"
                    Background="{DynamicResource Brush.Bg.Panel}"
                    BorderBrush="{DynamicResource Brush.Border}"
                    BorderThickness="1,1,1,0">
                    <DockPanel LastChildFill="False">
                        <TextBlock
                            VerticalAlignment="Center"
                            FontWeight="Bold"
                            Text="节点清单" />
                        <Button
                            Width="90"
                            Height="26"
                            Padding="15,0"
                            Background="{DynamicResource Brush.Accent}"
                            BorderThickness="0"
                            Command="{Binding AddNodeCommand}"
                            Content="+ 新增一行"
                            DockPanel.Dock="Right"
                            Foreground="White" />
                    </DockPanel>
                </Border>

                <ListView
                    x:Name="NodeList"
                    Grid.Row="1"
                    Background="Transparent"
                    BorderBrush="{DynamicResource Brush.Border}"
                    BorderThickness="1"
                    ItemContainerStyle="{DynamicResource Style.ListViewItem.Table}"
                    ItemsSource="{Binding ServiceNodes}">
                    <ListView.View>
                        <GridView ColumnHeaderContainerStyle="{DynamicResource Style.GridViewHeader.Flat}">
                            <GridViewColumn Width="180" Header="节点名称">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBox Style="{DynamicResource Style.TextBox.InTable}" Text="{Binding ServiceNodeName, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>

                            <GridViewColumn Width="180" Header="IP 地址">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBox Style="{DynamicResource Style.TextBox.InTable}" Text="{Binding ServiceNodeIp, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>

                            <GridViewColumn Width="100" Header="端口">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBox Style="{DynamicResource Style.TextBox.InTable}" Text="{Binding ServiceNodePort, UpdateSourceTrigger=PropertyChanged}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="180" Header="连通性状态">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Border
                                            Padding="8,2"
                                            HorizontalAlignment="Left"
                                            Background="#11000000"
                                            CornerRadius="4">
                                            <TextBlock
                                                VerticalAlignment="Center"
                                                FontWeight="Bold"
                                                Text="{Binding Status}">
                                                <TextBlock.Style>
                                                    <Style TargetType="TextBlock">
                                                        <Setter Property="Foreground" Value="Gray" />

                                                        <Style.Triggers>
                                                            <DataTrigger Binding="{Binding Status}" Value="在线">
                                                                <Setter Property="Foreground" Value="{DynamicResource Brush.Status.Success}" />
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Status}" Value="离线">
                                                                <Setter Property="Foreground" Value="{DynamicResource Brush.Status.Danger}" />
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Status}" Value="正在检测...">
                                                                <Setter Property="Foreground" Value="{DynamicResource Brush.Accent}" />
                                                            </DataTrigger>
                                                            <DataTrigger Binding="{Binding Status}" Value="警告">
                                                                <Setter Property="Foreground" Value="{DynamicResource Brush.Status.Warning}" />
                                                            </DataTrigger>
                                                        </Style.Triggers>
                                                    </Style>
                                                </TextBlock.Style>
                                            </TextBlock>
                                        </Border>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>

                            <GridViewColumn Width="80" Header="操作">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button
                                            Width="45"
                                            Height="24"
                                            Padding="0"
                                            Command="{Binding DataContext.DeleteNodeCommand, RelativeSource={RelativeSource AncestorType=ListView}}"
                                            CommandParameter="{Binding}"
                                            Content="删除"
                                            FontSize="11"
                                            Style="{DynamicResource Btn.Danger}" />
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>

            <StackPanel
                Grid.Row="2"
                Margin="0,20,0,0"
                HorizontalAlignment="Right"
                Orientation="Horizontal">
                <TextBlock
                    Margin="0,0,15,0"
                    VerticalAlignment="Center"
                    FontSize="12"
                    Foreground="{DynamicResource Brush.Text.Secondary}"
                    Text="修改后请点击检测 →" />
                <Button
                    Width="110"
                    Height="32"
                    Margin="0,0,10,0"
                    Command="{Binding CheckCommand}"
                    Content="🔍 立即检测"
                    Style="{DynamicResource Btn.Ghost}" />
                <Button
                    Width="120"
                    Height="32"
                    Background="{DynamicResource Brush.Accent}"
                    BorderThickness="0"
                    Command="{Binding ConfirmCommand}"
                    Content="保存并应用"
                    Foreground="White" />
            </StackPanel>
        </Grid>
    </Border>
</UserControl>