<UserControl
    x:Class="HBLConsole.View.VariableConfig"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:convert="clr-namespace:HBLConsole.Converter"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:HBLConsole.View"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:pry="clr-namespace:HBLConsole.PryUserControl;assembly=HBLConsole.PryUserControl"
    xmlns:vm="clr-namespace:HBLConsole.ViewModel"
    d:DesignHeight="450"
    d:DesignWidth="1000"
    mc:Ignorable="d">

    <UserControl.DataContext>
        <vm:VariableConfig />
    </UserControl.DataContext>

    <UserControl.Resources>
        <ResourceDictionary Source="../Resources/ResourceDictionarys/BasicStyle.xaml">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <convert:TextDisplayConvert x:Key="textDisplayConvert" />
                    <convert:IsEnableConvert x:Key="isEnableConvert" />
                    <convert:AnalogAlarmConvert x:Key="analogAlarmConvert" />
                    <convert:DiscreteAlarmConvert x:Key="discreteAlarmConvert" />
                    <convert:AlarmTypeTextConvert x:Key="alarmTypeTextConvert" />
                    <SolidColorBrush x:Key="BorderSolid" Color="#5523CACA" />
                    <SolidColorBrush x:Key="FontColor" Color="#FF2AB2E7" />
                    <SolidColorBrush x:Key="TitleFontColor" Color="#ddd" />
                    <SolidColorBrush x:Key="CursorColor" Color="Aqua" />
                    <SolidColorBrush x:Key="TitleBorderColor" Color="#FF2AB2E7" />

                    <Style x:Key="TextBlockStyle" TargetType="TextBlock">
                        <Setter Property="FontFamily" Value="楷体" />
                        <Setter Property="FontSize" Value="18" />
                        <Setter Property="Foreground" Value="{StaticResource TextBlockForeground}" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>

                    <Style x:Key="TextBoxStyle" TargetType="TextBox">
                        <Setter Property="FontFamily" Value="楷体" />
                        <Setter Property="FontSize" Value="22" />
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="Foreground" Value="{StaticResource TextBlockForeground}" />
                        <Setter Property="BorderBrush" Value="#FF23CACA" />
                        <Setter Property="CaretBrush" Value="Aqua" />
                        <Setter Property="VerticalAlignment" Value="Center" />
                    </Style>

                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal">
            <!--<Button
                Margin="20,0,20,0"
                Command="{Binding GenerateData}"
                Content="添加数据" />
            <Button
                Margin="20,0,20,0"
                Command="{Binding SaveData}"
                Content="保存数据" />-->
        </StackPanel>

        <!--#region 表格标题栏设置-->
        <Grid Grid.Row="1" Background="#dd2AB2E7">
            <Grid.ColumnDefinitions>
                <!--<ColumnDefinition Width="40" />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition Width="100" />
                <ColumnDefinition Width="0" />
                <ColumnDefinition Width="120" />
                <ColumnDefinition Width="100" />-->
                <ColumnDefinition Width="0.3*" />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition Width="0.7*" />
                <ColumnDefinition Width="0" />
                <ColumnDefinition Width="0.7*" />
                <ColumnDefinition Width="0.5*" />
            </Grid.ColumnDefinitions>
            <TextBlock
                Grid.Column="0"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="16"
                Foreground="{StaticResource TitleFontColor}"
                Text="ID" />

            <Grid Grid.Column="1">
                <TextBlock
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="16"
                    Foreground="{StaticResource TitleFontColor}"
                    Text="名称" />
                <Border
                    BorderBrush="{StaticResource TitleBorderColor}"
                    BorderThickness="1,0,1,0"
                    Cursor="SizeWE" />
            </Grid>

            <TextBlock
                Grid.Column="2"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="16"
                Foreground="{StaticResource TitleFontColor}"
                Text="地址" />

            <Grid Grid.Column="3">
                <TextBlock
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="16"
                    Foreground="{StaticResource TitleFontColor}"
                    Text="数据类型" />
                <Border
                    BorderBrush="{StaticResource TitleBorderColor}"
                    BorderThickness="1,0,1,0"
                    Cursor="SizeWE" />
            </Grid>

            <TextBlock
                Grid.Column="4"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="16"
                Foreground="{StaticResource TitleFontColor}"
                Text="报警类型" />

            <Grid Grid.Column="5">
                <TextBlock
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    FontSize="16"
                    Foreground="{StaticResource TitleFontColor}"
                    Text="是否启用报警" />
                <Border
                    BorderBrush="{StaticResource TitleBorderColor}"
                    BorderThickness="0,0,1,0"
                    Cursor="SizeWE" />
            </Grid>

            <TextBlock
                Grid.Column="6"
                HorizontalAlignment="Center"
                VerticalAlignment="Center"
                FontSize="16"
                Foreground="{StaticResource TitleFontColor}"
                Text="报警配置" />

        </Grid>
        <!--#endregion-->

        <!--#region 表格数据显示-->
        <ScrollViewer
            Grid.Row="2"
            HorizontalScrollBarVisibility="Hidden"
            VerticalScrollBarVisibility="Hidden">
            <ItemsControl ItemsSource="{Binding varialeInfos}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="gr">
                            <Grid.ColumnDefinitions>
                                <!--<ColumnDefinition Width="40" />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition Width="100" />
                                <ColumnDefinition Width="0" />
                                <ColumnDefinition Width="120" />
                                <ColumnDefinition Width="100" />-->
                                <ColumnDefinition Width="0.3*" />
                                <ColumnDefinition />
                                <ColumnDefinition />
                                <ColumnDefinition Width="0.7*" />
                                <ColumnDefinition Width="0" />
                                <ColumnDefinition Width="0.7*" />
                                <ColumnDefinition Width="0.5*" />
                            </Grid.ColumnDefinitions>

                            <TextBlock
                                Grid.Column="0"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Background="Transparent"
                                FontSize="14"
                                Foreground="{StaticResource FontColor}"
                                Text="{Binding ID}" />

                            <Grid Grid.Column="1" KeyDown="Grid_KeyDown">
                                <TextBox
                                    Background="Transparent"
                                    CaretBrush="{StaticResource CursorColor}"
                                    Foreground="{StaticResource FontColor}"
                                    Style="{StaticResource InputTextboxStyle}"
                                    TabIndex="{Binding ID}"
                                    Text="{Binding VarName}" />
                                <Border BorderBrush="{StaticResource BorderSolid}" BorderThickness="1,0,1,0" />
                            </Grid>


                            <TextBox
                                Grid.Column="2"
                                Background="Transparent"
                                CaretBrush="{StaticResource CursorColor}"
                                Foreground="{StaticResource FontColor}"
                                KeyDown="Grid_KeyDown"
                                Style="{StaticResource InputTextboxStyle}"
                                TabIndex="{Binding ID}"
                                Text="{Binding Address}" />

                            <Grid Grid.Column="3">
                                <ComboBox
                                    VerticalAlignment="Center"
                                    BorderBrush="Transparent"
                                    BorderThickness="1"
                                    FontFamily="楷体"
                                    FontSize="20"
                                    Foreground="{StaticResource FontColor}"
                                    IsEditable="False"
                                    ItemsSource="{Binding dataType}"
                                    SelectedValue="{Binding DataType}"
                                    Style="{StaticResource ComboBoxStyle}"
                                    Text="{Binding DataType}" />
                                <Border BorderBrush="{StaticResource BorderSolid}" BorderThickness="1,0,1,0" />
                            </Grid>


                            <Grid Grid.Column="5">
                                <CheckBox
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Background="#ff23CACA"
                                    Content="{Binding IsEnableAlarm, Converter={StaticResource textDisplayConvert}}"
                                    FontSize="16"
                                    Foreground="{StaticResource FontColor}"
                                    IsChecked="{Binding IsEnableAlarm}"
                                    Template="{StaticResource CbTemplate}" />
                                <Border BorderBrush="{StaticResource BorderSolid}" BorderThickness="0,0,1,0" />
                            </Grid>

                            <Grid Grid.Column="6">
                                <Grid>
                                    <ToggleButton
                                        Name="tb"
                                        Width="{Binding RelativeSource={RelativeSource AncestorType=Grid, AncestorLevel=1}, Path=ActualWidth}"
                                        Height="{Binding RelativeSource={RelativeSource AncestorType=Grid, AncestorLevel=1}, Path=ActualHeight}"
                                        Background="Transparent"
                                        BorderThickness="0"
                                        Content="编辑"
                                        FontSize="14"
                                        IsChecked="{Binding IsOpen}"
                                        IsEnabled="{Binding IsEnableAlarm}"
                                        Style="{StaticResource EditToggleButtonStyle}" />
                                </Grid>

                                <!--  当 StaysOpen 设置为 True 时 popup失去焦点的时候不会自动关闭  -->
                                <Popup
                                    AllowsTransparency="True"
                                    Focusable="False"
                                    IsOpen="{Binding IsOpen}"
                                    Placement="Left"
                                    PlacementTarget="{Binding ElementName=tb}"
                                    StaysOpen="False">
                                    <Border Background="#081424" ClipToBounds="True">
                                        <Grid Margin="0">
                                            <Grid.Background>
                                                <ImageBrush ImageSource="../Resources/Images/边框1.png" />
                                            </Grid.Background>
                                            <Grid.RowDefinitions>
                                                <RowDefinition />
                                                <RowDefinition />
                                                <RowDefinition />
                                            </Grid.RowDefinitions>

                                            <!--#region 离散量报警设置-->
                                            <StackPanel
                                                Grid.Row="0"
                                                Margin="10"
                                                Orientation="Vertical"
                                                Visibility="{Binding DataType, Converter={StaticResource discreteAlarmConvert}}">

                                                <Grid Margin="5">
                                                    <pry:TitleTextBlock />
                                                    <TextBlock
                                                        Margin="10,5,30,5"
                                                        Background="Transparent"
                                                        FontSize="16"
                                                        Foreground="#dd01FFFF"
                                                        Text="离散量报警设置" />
                                                </Grid>

                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition />
                                                        <ColumnDefinition />
                                                    </Grid.ColumnDefinitions>

                                                    <TextBlock
                                                        Margin="5"
                                                        Style="{StaticResource TextBlockStyle}"
                                                        Text="选择触发条件：" />

                                                    <ComboBox
                                                        Grid.Column="1"
                                                        Width="180"
                                                        Margin="5"
                                                        VerticalAlignment="Center"
                                                        BorderBrush="#FF23CACA"
                                                        BorderThickness="1"
                                                        FontFamily="楷体"
                                                        FontSize="20"
                                                        Foreground="#ff23caca"
                                                        IsEditable="False"
                                                        ItemsSource="{Binding PopupDiscreteAlarmType}"
                                                        SelectedIndex="1"
                                                        Style="{StaticResource ComboBoxStyle}"
                                                        Text="{Binding AlarmSetProp.DiscreteAlarmInfoSet.TrigAlarm}" />
                                                </Grid>

                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition />
                                                        <ColumnDefinition />
                                                        <ColumnDefinition />
                                                    </Grid.ColumnDefinitions>
                                                    <TextBlock
                                                        Grid.Column="0"
                                                        Margin="5"
                                                        Style="{StaticResource TextBlockStyle}"
                                                        Text="输入报警信息：" />
                                                    <TextBox
                                                        Grid.Column="1"
                                                        Grid.ColumnSpan="2"
                                                        Margin="5"
                                                        Style="{StaticResource TextBoxStyle}"
                                                        Text="{Binding AlarmSetProp.DiscreteAlarmInfoSet.AlarmInfo}" />
                                                </Grid>

                                            </StackPanel>
                                            <!--#endregion-->

                                            <!--#region 模拟量报警设置-->
                                            <StackPanel
                                                Grid.Row="1"
                                                Margin="10"
                                                Visibility="{Binding DataType, Converter={StaticResource analogAlarmConvert}}">

                                                <Grid Margin="5">
                                                    <pry:TitleTextBlock />
                                                    <TextBlock
                                                        Margin="10,5,30,5"
                                                        Background="Transparent"
                                                        FontSize="16"
                                                        Foreground="#dd01FFFF"
                                                        Text="模拟量报警设置" />
                                                </Grid>

                                                <ItemsControl ItemsSource="{Binding AlarmSetProp.AnalogAlarmModels}">
                                                    <ItemsControl.ItemTemplate>
                                                        <DataTemplate>
                                                            <Grid>
                                                                <Grid.ColumnDefinitions>
                                                                    <ColumnDefinition />
                                                                    <ColumnDefinition />
                                                                    <ColumnDefinition />
                                                                </Grid.ColumnDefinitions>

                                                                <Grid.RowDefinitions>
                                                                    <RowDefinition />
                                                                    <RowDefinition />
                                                                </Grid.RowDefinitions>

                                                                <CheckBox
                                                                    Margin="5,5,0,0"
                                                                    HorizontalAlignment="Left"
                                                                    VerticalAlignment="Center"
                                                                    Background="#ff23CACA"
                                                                    Content="{Binding AlarmTag}"
                                                                    FontSize="14"
                                                                    Foreground="#ff23CACA"
                                                                    IsChecked="{Binding IsEnable}"
                                                                    Template="{StaticResource CbTemplate}" />

                                                                <TextBlock
                                                                    Grid.Column="1"
                                                                    Margin="0,5,5,0"
                                                                    Style="{StaticResource TextBlockStyle}"
                                                                    Text="请输入报警值：" />
                                                                <TextBox
                                                                    Grid.Column="2"
                                                                    Width="100"
                                                                    Margin="0,5,5,0"
                                                                    IsEnabled="{Binding IsEnable}"
                                                                    Style="{StaticResource TextBoxStyle}"
                                                                    Text="{Binding AlarmValue}" />

                                                                <TextBlock
                                                                    Grid.Row="1"
                                                                    Grid.Column="0"
                                                                    Margin="5"
                                                                    Style="{StaticResource TextBlockStyle}"
                                                                    Text="输入报警信息：" />
                                                                <TextBox
                                                                    Grid.Row="1"
                                                                    Grid.Column="1"
                                                                    Grid.ColumnSpan="2"
                                                                    Margin="5"
                                                                    IsEnabled="{Binding IsEnable}"
                                                                    Style="{StaticResource TextBoxStyle}"
                                                                    Text="{Binding AlarmInfo}" />

                                                            </Grid>
                                                        </DataTemplate>
                                                    </ItemsControl.ItemTemplate>
                                                </ItemsControl>

                                            </StackPanel>
                                            <!--#endregion-->

                                            <!--#region 按钮控制-->
                                            <Grid Grid.Row="2" Margin="10">
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition />
                                                    <ColumnDefinition />
                                                </Grid.ColumnDefinitions>

                                                <Button
                                                    Margin="10,5,10,10"
                                                    Command="{Binding CancelCommand}"
                                                    Content="取消"
                                                    FontFamily="楷体"
                                                    FontSize="20"
                                                    Foreground="#aa01FFFF"
                                                    Template="{StaticResource CancelButtonTemp}" />

                                                <Button
                                                    Grid.Column="1"
                                                    Margin="10,5,10,10"
                                                    Command="{Binding ConfirmCommand}"
                                                    Content="确认"
                                                    FontFamily="楷体"
                                                    FontSize="20"
                                                    Foreground="#aa01FFFF"
                                                    Template="{StaticResource ConfirmButtonTemp}" />
                                            </Grid>
                                            <!--#endregion-->

                                        </Grid>
                                    </Border>
                                </Popup>
                            </Grid>

                            <Border
                                Grid.ColumnSpan="8"
                                BorderBrush="{StaticResource BorderSolid}"
                                BorderThickness="1" />

                        </Grid>

                        <DataTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter TargetName="gr" Property="Background" Value="#112AB2E7" />
                            </Trigger>
                        </DataTemplate.Triggers>

                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>
        <!--#endregion-->


    </Grid>
</UserControl>
