<ui:ContentDialog
    x:Class="DMS.WPF.Views.Dialogs.DeviceDialog"
    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:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
    xmlns:ex="clr-namespace:DMS.Extensions"
    xmlns:hc="https://handyorg.github.io/handycontrol"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:vc="clr-namespace:DMS.WPF.ValueConverts"
    xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
    Title="{Binding Title}"
    d:DataContext="{d:DesignInstance vmd:DeviceDialogViewModel}"
    CloseButtonCommand="{Binding CancleButtonCommand}"
    CloseButtonText="取消"
    DefaultButton="Primary"
    PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
    PrimaryButtonText="{Binding PrimaryButText}"
    mc:Ignorable="d">

    <ui:ContentDialog.Resources>
        <ex:EnumBindingSource x:Key="DeviceType" EnumType="{x:Type enums:DeviceType}" />
        <ex:EnumBindingSource x:Key="ProtocolType" EnumType="{x:Type enums:ProtocolType}" />
        <ex:EnumBindingSource x:Key="CpuType" EnumType="{x:Type enums:CpuType}" />
        <vc:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
        <vc:EnumToStringConverter x:Key="EnumToStringConverter" />

        <Style x:Key="LabelStyle" TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Right" />
            <Setter Property="Margin" Value="0,0,10,0" />
        </Style>
    </ui:ContentDialog.Resources>

    <StackPanel Margin="16">
        <!--  主设置项  -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <!--  Row 0  -->
            <hc:TextBox
                Grid.Row="0"
                Grid.Column="0"
                Margin="0,15,0,0"
                hc:InfoElement.Title="设备名称:"
                Text="{Binding Device.Name, UpdateSourceTrigger=PropertyChanged}" />
            <hc:ComboBox
                x:Name="ProtocolComboBox"
                Grid.Row="0"
                Grid.Column="2"
                Margin="0,15,0,0"
                hc:InfoElement.Title="通讯协议:"
                IsEnabled="{Binding IsAddMode}"
                ItemsSource="{Binding Source={StaticResource ProtocolType}}"
                SelectedItem="{Binding Device.Protocol}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </hc:ComboBox>
          

            <!--  Row 1  -->
            <hc:TextBox
                Grid.Row="1"
                Grid.Column="0"
                Margin="0,15,0,0"
                hc:InfoElement.Title="IP 地址:"
                Text="{Binding Device.IpAddress, UpdateSourceTrigger=PropertyChanged}" />

            <hc:TextBox
                Grid.Row="1"
                Grid.Column="2"
                Margin="0,15,0,0"
                hc:InfoElement.Title="端口:"
                Text="{Binding Device.Port, UpdateSourceTrigger=PropertyChanged}" />

            <hc:ComboBox
                Grid.Row="2"
                Grid.Column="0"
                Margin="0,15,0,0"
                hc:InfoElement.Title="设备类型:"
                IsEnabled="{Binding IsAddMode}"
                ItemsSource="{Binding Source={StaticResource DeviceType}}"
                SelectedItem="{Binding Device.DeviceType}">
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </hc:ComboBox>

            <hc:TextBox
                Grid.Row="2"
                Grid.Column="2"
                Margin="0,15,0,0"
                hc:InfoElement.Title="设备描述:"
                Text="{Binding Device.Description, UpdateSourceTrigger=PropertyChanged}" />

            <!--  Row 3  -->
            <CheckBox
                Grid.Row="3"
                Grid.Column="0"
                Margin="0,20,0,0"
                Content="是否添加默认变量表"
                IsChecked="{Binding Device.IsAddDefVarTable}" />
            <CheckBox
                Grid.Row="3"
                Grid.Column="2"
                Margin="0,20,0,0"
                Content="是否启用"
                IsChecked="{Binding Device.IsActive}" />

        </Grid>

        <!--  OpcUa Specific Properties  -->
        <GroupBox Margin="0,15,0,0" Header="OpcUa 协议设置">
            <GroupBox.Style>
                <Style BasedOn="{StaticResource {x:Type GroupBox}}" TargetType="GroupBox">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=ProtocolComboBox, Path=SelectedItem, Converter={StaticResource EnumToStringConverter}}" Value="OpcUa">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </GroupBox.Style>
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <hc:TextBox
                    Grid.Column="1"
                    MinWidth="400"
                    hc:InfoElement.Title="OpcUa服务器地址:"
                    Text="{Binding Device.OpcUaServerUrl}" />
            </Grid>
        </GroupBox>

        <!--  S7 Specific Properties  -->
        <GroupBox Margin="0,10,0,0" Header="S7 协议设置">
            <GroupBox.Style>
                <Style BasedOn="{StaticResource {x:Type GroupBox}}" TargetType="GroupBox">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=ProtocolComboBox, Path=SelectedItem, Converter={StaticResource EnumToStringConverter}}" Value="S7">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </GroupBox.Style>
            <Grid Margin="5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="20" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock
                    Grid.Column="0"
                    Style="{StaticResource LabelStyle}"
                    Text="CPU 类型:" />
                <hc:ComboBox
                    Grid.Column="1"
                    ItemsSource="{Binding Source={StaticResource CpuType}}"
                    SelectedItem="{Binding Device.CpuType}" />

                <TextBlock
                    Grid.Column="3"
                    Style="{StaticResource LabelStyle}"
                    Text="机架号:" />
                <hc:TextBox Grid.Column="4" Text="{Binding Device.Rack, UpdateSourceTrigger=PropertyChanged}" />

                <TextBlock
                    Grid.Column="6"
                    Style="{StaticResource LabelStyle}"
                    Text="槽号:" />
                <hc:TextBox Grid.Column="7" Text="{Binding Device.Slot, UpdateSourceTrigger=PropertyChanged}" />
            </Grid>
        </GroupBox>

    </StackPanel>
</ui:ContentDialog>