<UserControl
    x:Class="DMS.WPF.Views.TriggerDetailView"
    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:helper="clr-namespace:DMS.WPF.Helper"
    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
    xmlns:converters="clr-namespace:DMS.WPF.Converters"
    xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
    xmlns:vm="clr-namespace:DMS.WPF.ViewModels"
    d:DataContext="{d:DesignInstance vm:TriggerDetailViewModel}"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <UserControl.Resources>
        <valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>
        <converters:BoolToStringConverter x:Key="BoolToStringConverter" />
        <converters:BoolToColorConverter x:Key="BoolToColorConverter"/>
        <converters:NullableTimeSpanToSecondsConverter x:Key="NullableTimeSpanToSecondsConverter"/>
    </UserControl.Resources>

    <DockPanel>
        <!--  顶部导航栏  -->
        <Border
            Margin="20,10"
            Padding="15"
            DockPanel.Dock="Top">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <!--  返回触发器列表按钮  -->
                <Button
                    Grid.Column="0"
                    Margin="0,0,10,0"
                    Command="{Binding NavigateToTriggersCommand}"
                    Style="{StaticResource ButtonDefault}"
                    ToolTip="返回触发器列表">
                    <Button.Content>
                        <StackPanel Orientation="Horizontal">
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.ArrowLeft8}" />
                        </StackPanel>
                    </Button.Content>
                </Button>

                <!--  操作菜单栏  -->
                <ui:CommandBar
                    Grid.Column="1"
                    HorizontalAlignment="Right"
                    DefaultLabelPosition="Right"
                    IsOpen="False">

                    <ui:AppBarButton
                        x:Name="EditButton"
                        Command="{Binding EditTriggerCommand}"
                        Label="编辑">
                        <ui:AppBarButton.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Edit}" />
                        </ui:AppBarButton.Icon>
                    </ui:AppBarButton>

                    <ui:AppBarButton x:Name="ShareButton" Label="分享">
                        <ui:AppBarButton.Icon>
                            <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Share}" />
                        </ui:AppBarButton.Icon>
                    </ui:AppBarButton>

                    <ui:CommandBar.SecondaryCommands>
                        <ui:AppBarButton
                            x:Name="SettingsButton"
                            Icon="Setting"
                            Label="设置" />
                    </ui:CommandBar.SecondaryCommands>
                </ui:CommandBar>
            </Grid>
        </Border>

        <!--  主内容区域  -->
        <ScrollViewer DockPanel.Dock="Top" VerticalScrollBarVisibility="Auto">
            <StackPanel Margin="20">
                <!--  Trigger Details  -->
                <Border
                    Margin="0,0,0,10"
                    Padding="15"
                    Background="White"
                    BorderBrush="#E0E0E0"
                    BorderThickness="1"
                    CornerRadius="8">
                    <Border.Effect>
                        <DropShadowEffect
                            BlurRadius="5"
                            Opacity="0.1"
                            ShadowDepth="2"
                            Color="#888888" />
                    </Border.Effect>

                    <StackPanel>
                        <Grid Margin="0,0,0,10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="0" Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,10,0"
                                    VerticalAlignment="Center"
                                    FontFamily="{StaticResource MdFontIcons}"
                                    FontSize="24"
                                    Foreground="#2196F3"
                                    Glyph="&#xE7F3;" />
                                <TextBlock
                                    VerticalAlignment="Center"
                                    Style="{StaticResource SubtitleTextBlockStyle}"
                                    Text="触发器详情" />
                            </StackPanel>
                        </Grid>

                        <Grid Margin="0,8,0,8">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <StackPanel
                                Grid.Row="0"
                                Grid.Column="0"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.Tag}" />
                                <TextBlock VerticalAlignment="Center" Text="名称:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="0"
                                Grid.Column="1"
                                Margin="0,0,10,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.Name}" />

                            <StackPanel
                                Grid.Row="0"
                                Grid.Column="2"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.Tag}" />
                                <TextBlock VerticalAlignment="Center" Text="描述:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="0"
                                Grid.Column="3"
                                Margin="0,0,0,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.Description}" />

                            <StackPanel
                                Grid.Row="1"
                                Grid.Column="0"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.StatusCircleCheckmark}" />
                                <TextBlock VerticalAlignment="Center" Text="状态:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="1"
                                Grid.Column="1"
                                Margin="0,0,10,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="{Binding CurrentTrigger.IsActive, 
                                            Converter={StaticResource BoolToColorConverter}, 
                                            ConverterParameter='Green;Red'}"
                                Text="{Binding CurrentTrigger.IsActive, 
                                       Converter={StaticResource BoolToStringConverter}, 
                                       ConverterParameter='已激活;未激活'}" />

                            <StackPanel
                                Grid.Row="1"
                                Grid.Column="2"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.Mail}" />
                                <TextBlock VerticalAlignment="Center" Text="动作类型:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="1"
                                Grid.Column="3"
                                Margin="0,0,0,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.Action}" />

                            <StackPanel
                                Grid.Row="2"
                                Grid.Column="0"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontFamily="{StaticResource MdFontIcons}"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Glyph="&#xF0A86;" />
                                <TextBlock VerticalAlignment="Center" Text="抑制持续时间:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="2"
                                Grid.Column="1"
                                Margin="0,0,10,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.SuppressionDuration, 
                                       Converter={StaticResource NullableTimeSpanToSecondsConverter}}" />

                            <StackPanel
                                Grid.Row="2"
                                Grid.Column="2"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.Calendar}" />
                                <TextBlock VerticalAlignment="Center" Text="创建时间:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="2"
                                Grid.Column="3"
                                Margin="0,0,0,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.CreatedAt, StringFormat='yyyy-MM-dd HH:mm:ss'}" />

                            <StackPanel
                                Grid.Row="3"
                                Grid.Column="0"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.Calendar}" />
                                <TextBlock VerticalAlignment="Center" Text="最后更新:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="3"
                                Grid.Column="1"
                                Margin="0,0,10,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.UpdatedAt, StringFormat='yyyy-MM-dd HH:mm:ss'}" />

                            <StackPanel
                                Grid.Row="3"
                                Grid.Column="2"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Icon="{x:Static ui:SegoeFluentIcons.DateTime}" />
                                <TextBlock VerticalAlignment="Center" Text="最后触发:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="3"
                                Grid.Column="3"
                                Margin="0,0,0,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.LastTriggeredAt, StringFormat='yyyy-MM-dd HH:mm:ss'}" />

                            <StackPanel
                                Grid.Row="4"
                                Grid.Column="0"
                                Margin="0,0,5,12"
                                Orientation="Horizontal">
                                <ui:FontIcon
                                    Margin="0,0,5,0"
                                    VerticalAlignment="Center"
                                    FontFamily="{StaticResource MdFontIcons}"
                                    FontSize="16"
                                    Foreground="#666666"
                                    Glyph="&#xF06A7;" />
                                <TextBlock VerticalAlignment="Center" Text="关联变量数:" />
                            </StackPanel>
                            <TextBlock
                                Grid.Row="4"
                                Grid.Column="1"
                                Margin="0,0,10,12"
                                VerticalAlignment="Center"
                                FontWeight="SemiBold"
                                Foreground="#333333"
                                Text="{Binding CurrentTrigger.VariableIds.Count}" />
                        </Grid>
                    </StackPanel>
                </Border>

                <!--  Associated Variables  -->
                <Border
                    Margin="0,0,0,10"
                    Padding="15"
                    Background="White"
                    BorderBrush="#E0E0E0"
                    BorderThickness="1"
                    CornerRadius="8">
                    <Border.Effect>
                        <DropShadowEffect
                            BlurRadius="5"
                            Opacity="0.1"
                            ShadowDepth="2"
                            Color="#888888" />
                    </Border.Effect>

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <TextBlock
                            Grid.Row="0"
                            Margin="0,0,0,10"
                            Style="{StaticResource SubtitleTextBlockStyle}"
                            Text="关联变量" />
                        <DataGrid
                            x:Name="AssociatedVariablesDataGrid"
                            Grid.Row="1"
                            AutoGenerateColumns="False"
                            CanUserAddRows="False"
                            CanUserDeleteRows="False"
                            IsReadOnly="True"
                            ItemsSource="{Binding CurrentTrigger.Variables}"
                            SelectionMode="Extended">

                            <i:Interaction.Behaviors>
                                <helper:SelectedItemsBehavior SelectedItems="{Binding SelectedVariables}" />
                            </i:Interaction.Behaviors>

                            <DataGrid.Columns>
                                <DataGridTextColumn Binding="{Binding Name}" Header="变量名称" />
                                <DataGridTextColumn Binding="{Binding Description}" Header="变量描述" />
                                <DataGridTextColumn Binding="{Binding DeviceName}" Header="设备名称" />
                                <DataGridTextColumn Binding="{Binding VariableTableName}" Header="变量表名称" />
                                <DataGridTextColumn Binding="{Binding S7Address}" Header="变量地址" />
                                <DataGridTextColumn Binding="{Binding DataType}" Header="数据类型" />
                                <DataGridTextColumn Binding="{Binding DisplayValue}" Header="显示值" />
                                <DataGridTextColumn Binding="{Binding LastModified, StringFormat='yyyy-MM-dd HH:mm:ss'}" Header="最后更新" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </Grid>
                </Border>
            </StackPanel>
        </ScrollViewer>
    </DockPanel>
</UserControl>