<controls:ContentDialog
    x:Class="DMS.WPF.Views.Dialogs.ImportOpcUaDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:helper="clr-namespace:DMS.WPF.Helper"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:DMS.WPF.ViewModels.Dialogs"
    Title="从OPC UA服务器导入变量"
    d:DataContext="{d:DesignInstance vm:ImportOpcUaDialogViewModel}"
    CloseButtonCommand="{Binding CloseButtonCommand}"
    CloseButtonText="取消"
    PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
    PrimaryButtonText="导入全部"
    SecondaryButtonCommand="{Binding SecondaryButtonCommand}"
    SecondaryButtonText="导入选择"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>

        <!--  连接区域  -->
        <StackPanel
            Grid.Row="0"
            Grid.Column="0"
            Grid.ColumnSpan="2"
            Margin="0,0,0,10"
            Orientation="Horizontal">
            <TextBox
                x:Name="EndpointUrlTextBox"
                Width="300"
                Margin="0,0,10,0"
                VerticalAlignment="Center"
                IsEnabled="False"
                Text="{Binding EndpointUrl, UpdateSourceTrigger=PropertyChanged}" />
            <Button
                x:Name="ConnectButton"
                Command="{Binding ConnectCommand}"
                Content="{Binding ConnectButtonText}"
                IsEnabled="{Binding IsConnectButtonEnabled}"
                Style="{StaticResource AccentButtonStyle}" />
            <Button
                x:Name="FindVariablesButton"
                Margin="10,0,0,0"
                Command="{Binding FindCurrentNodeVariablesCommand}"
                Content="查找当前节点下的所有变量">
                <Button.Style>
                    <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsConnected}" Value="True">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>

        <!--  节点树  -->
        <TreeView
            Name="treeView"
            Grid.Row="1"
            Grid.Column="0"
            Margin="0,0,10,0"
            ItemsSource="{Binding RootOpcUaNode.Children}"
            SelectedItemChanged="TreeView_SelectedItemChanged">
            <TreeView.Style>
                <Style BasedOn="{StaticResource {x:Type TreeView}}" TargetType="TreeView">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsConnected}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.Style>
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                    <TextBlock Text="{Binding DisplayName}" />
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
            <TreeView.ItemContainerStyle>
                <Style BasedOn="{StaticResource {x:Type TreeViewItem}}" TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Setter Property="FontWeight" Value="Normal" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>

        <!--  变量列表  -->
        <DataGrid
            Grid.Row="1"
            Grid.Column="1"
            AutoGenerateColumns="False"
            IsReadOnly="True"
            ItemsSource="{Binding OpcUaNodeVariables}">
            <i:Interaction.Behaviors>
                <helper:SelectedItemsBehavior SelectedItems="{Binding SelectedVariables}" />
            </i:Interaction.Behaviors>
            <DataGrid.Style>
                <Style BasedOn="{StaticResource {x:Type DataGrid}}" TargetType="DataGrid">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsConnected}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Style>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="名称" />
                <DataGridTextColumn Binding="{Binding CSharpDataType}" Header="数据类型" />
                <DataGridTextColumn Binding="{Binding OpcUaNodeId}" Header="节点ID" />

            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</controls:ContentDialog>