<pu:WindowX x:Class="SWRIS.Pages.SettingDialog"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:local="clr-namespace:SWRIS.Pages" 
      mc:Ignorable="d" Foreground="#FFFFFF" 
      xmlns:pu="https://opensource.panuon.com/wpf-ui"
      xmlns:cvt="clr-namespace:SWRIS.Converters" AllowsTransparency="True"
      xmlns:vm="clr-namespace:SWRIS.Models.ViewModel"
      MinHeight="750"  Width="1100" WindowStyle="None" 
      ResizeMode="NoResize" FontSize="18" Background="#141332"
      FontFamily="{StaticResource PuHuiTiRegular}"
      Title="系统设置" pu:WindowXCaption.Height="0" 
      WindowStartupLocation="CenterScreen" IsEscEnabled="True" >
    <pu:WindowX.DataContext>
        <vm:SettingViewModel/>
    </pu:WindowX.DataContext>
    <pu:WindowX.Resources>
        <cvt:BooleanInverterConverter x:Key="InverterConverter"/>
        <cvt:PercentageConverter x:Key="PercentageConverter"/>
        <cvt:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
        <cvt:ModuleTypeToVisibilityConverter x:Key="ModuleTypeToVisibilityConverter"/>
    </pu:WindowX.Resources>
    <Border BorderThickness="2" BorderBrush="#3B3B7B" CornerRadius="10">
        <Grid Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="60"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="90"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="400"/>
                <ColumnDefinition Width="1.5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="系统设置" Grid.Row="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" FontSize="36" FontWeight="Bold"/>
            <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="0" Margin="35,0">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <Rectangle Height="2" Width="110" Stroke="#3B3B7B"/>
                    <TextBlock Text="基本参数" Foreground="#615CDD" FontSize="24" Margin="15" FontWeight="Regular"/>
                    <Rectangle Height="2" Width="110" Stroke="#3B3B7B"/>
                </StackPanel>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center">
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="设备名称" Foreground="#7886B2" FontWeight="Regular"/>
                            <TextBox Margin="0,10" Text="{Binding Crane}" Width="247"/>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Orientation="Vertical" Margin="0,0,27,0">
                            <TextBlock Text="服务地址" Foreground="#7886B2" FontWeight="Regular"/>
                            <TextBox Margin="0,10" Text="{Binding IpAddress}"/>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="端口号" Foreground="#7886B2" FontWeight="Regular"/>
                            <TextBox Margin="0,10" Text="{Binding Port}" Width="70"/>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Orientation="Vertical" Margin="0,0,27,0">
                            <TextBlock Text="数据清理时间" Foreground="#7886B2" FontWeight="Regular"/>
                            <TextBox Margin="0,10" Text="{Binding CleanScheduledTime}" Width="150"/>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="存储时长" Foreground="#7886B2" FontWeight="Regular"/>
                            <StackPanel Orientation="Horizontal" Margin="0,10" >
                                <TextBox Text="{Binding DataSaveDays}" Width="70"/>
                                <TextBlock  VerticalAlignment="Center" Margin="5" Text="天"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Orientation="Vertical" Margin="0,0,27,0">
                            <TextBlock Text="语音报警级别" Foreground="#7886B2" FontWeight="Regular"/>
                            <ComboBox Margin="0,10" Width="150" ItemsSource="{Binding RiskLevels}"
                                      SelectedValue="{Binding SoundRiskLevel}" 
                                      DisplayMemberPath="Value" SelectedValuePath="Key"/>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="速度波动阈值" Foreground="#7886B2" FontWeight="Regular"/>
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <TextBox Text="{Binding SpeedVariationThreshold,Converter={StaticResource PercentageConverter}}" Width="70"/>
                                <TextBlock Text="%" Margin="5,0" VerticalAlignment="Center"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel Orientation="Vertical" Margin="0,0,22,0">
                            <TextBlock Text="损伤容差范围" Foreground="#7886B2" FontWeight="Regular"/>
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <TextBox Text="{Binding DamageExtent}" Width="125"/>
                                <TextBlock Text="米" Margin="5,0" VerticalAlignment="Center"/>
                            </StackPanel>
                        </StackPanel>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="损伤确认频次" Foreground="#7886B2" FontWeight="Regular"/>
                            <StackPanel Orientation="Horizontal" Margin="0,10">
                                <TextBox Text="{Binding AlarmValidCount}" Width="70"/>
                                <TextBlock Text="次" Margin="5,0" VerticalAlignment="Center"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </StackPanel>
            </StackPanel>
            <Rectangle Grid.Row="1" Grid.Column="1">
                <Rectangle.Fill>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <GradientStop Offset="0" Color="Transparent"/>
                        <GradientStop Offset="0.5" Color="#3B3B7B"/>
                        <GradientStop Offset="1" Color="Transparent"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
            <StackPanel Orientation="Vertical" Grid.Row="1" Grid.Column="2">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <Rectangle Height="2" Width="250" Stroke="#3B3B7B"/>
                    <TextBlock Text="标定" Foreground="#615CDD" FontSize="24" Margin="15" FontWeight="Regular"/>
                    <Rectangle Height="2" Width="250" Stroke="#3B3B7B"/>
                </StackPanel>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Center" DataContext="{Binding Calibration}">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="类型" Foreground="#7886B2" FontWeight="Regular"/>
                        <ComboBox Margin="0,10" Width="320" ItemsSource="{Binding ModuleTypes}"
                                  SelectedValue="{Binding ModuleType}" 
                                  HorizontalAlignment="Left"
                                  DisplayMemberPath="Value" SelectedValuePath="Key"/>
                    </StackPanel>
                    <StackPanel Orientation="Vertical" 
                                Visibility="{Binding ModuleType,Converter={StaticResource ModuleTypeToVisibilityConverter},ConverterParameter=Tcp}">
                        <StackPanel Orientation="Horizontal" DataContext="{Binding Tcp}">
                            <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                <TextBlock Text="设备地址" Foreground="#7886B2" FontWeight="Regular"/>
                                <TextBox Margin="0,10" Text="{Binding IpAddress}" Width="150"/>
                            </StackPanel>
                            <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                <TextBlock Text="端口号" Foreground="#7886B2" FontWeight="Regular"/>
                                <TextBox Margin="0,10" Text="{Binding Port}" Width="75"/>
                            </StackPanel>
                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="站号" Foreground="#7886B2" FontWeight="Regular"/>
                                <TextBox Margin="0,10" Text="{Binding Station}" Width="55"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                    <StackPanel Orientation="Vertical" 
                                Visibility="{Binding ModuleType,Converter={StaticResource ModuleTypeToVisibilityConverter},ConverterParameter=Serial}">
                        <StackPanel Orientation="Vertical" DataContext="{Binding Serial}">
                            <StackPanel Orientation="Horizontal">
                                <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                    <TextBlock Text="串口名称" Foreground="#7886B2" FontWeight="Regular"/>
                                    <ComboBox ItemsSource="{Binding Path=SerialNames}" Margin="0,10" SelectedValue="{Binding PortName}" Width="150"/>
                                </StackPanel>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="波特率" Foreground="#7886B2" FontWeight="Regular"/>
                                    <ComboBox Margin="0,10" SelectedValue="{Binding BaudRate}"
                                          SelectedValuePath="Content" Width="150">
                                        <ComboBoxItem Content="1200"/>
                                        <ComboBoxItem Content="4800"/>
                                        <ComboBoxItem Content="9600"/>
                                        <ComboBoxItem Content="19200"/>
                                        <ComboBoxItem Content="38400"/>
                                        <ComboBoxItem Content="57600"/>
                                        <ComboBoxItem Content="115200"/>
                                    </ComboBox>
                                </StackPanel>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                    <TextBlock Text="数据位" Foreground="#7886B2" FontWeight="Regular"/>
                                    <ComboBox Margin="0,10" SelectedValue="{Binding DataBits}"
                                              SelectedValuePath="Content" Width="63">
                                        <ComboBoxItem Content="7"/>
                                        <ComboBoxItem Content="8"/>
                                    </ComboBox>
                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                    <TextBlock Text="校验位" Foreground="#7886B2" FontWeight="Regular"/>
                                    <ComboBox Margin="0,10" SelectedValue="{Binding Parity}" 
                                              SelectedValuePath="Content" Width="100">
                                        <ComboBoxItem Content="None"/>
                                        <ComboBoxItem Content="Odd"/>
                                        <ComboBoxItem Content="Even"/>
                                    </ComboBox>
                                </StackPanel>
                                <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                    <TextBlock Text="停止位" Foreground="#7886B2" FontWeight="Regular"/>
                                    <ComboBox Margin="0,10" SelectedValue="{Binding StopBits}" 
                                              SelectedValuePath="Content" Width="63">
                                        <ComboBoxItem Content="1"/>
                                        <ComboBoxItem Content="2"/>
                                    </ComboBox>
                                </StackPanel>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Text="站号" Foreground="#7886B2" FontWeight="Regular"/>
                                    <TextBox Margin="0,10" Text="{Binding Station}" Width="35"/>
                                </StackPanel>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                    <Rectangle Width="600" Height="1.5" Margin="-200,10,-200,5">
                        <Rectangle.Fill>
                            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                <GradientStop Offset="0" Color="Transparent"/>
                                <GradientStop Offset="0.5" Color="#3B3B7B"/>
                                <GradientStop Offset="1" Color="Transparent"/>
                            </LinearGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <ItemsControl ItemsSource="{Binding Limits}" Margin="0,10">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" >
                                    <CheckBox IsChecked="{Binding IsEnable}" ToolTip="是否启用" Margin="0,25,5,0" BorderThickness="1.6" pu:CheckBoxHelper.GlyphThickness="2"/>
                                    <StackPanel Orientation="Horizontal" IsEnabled="{Binding IsEnable}">
                                        <StackPanel Orientation="Vertical" Margin="0,0,20,0">
                                            <TextBlock Text="限位" Foreground="#7886B2" FontWeight="Regular"/>
                                            <TextBox Margin="0,10" Text="{Binding Name}" Width="120"/>
                                        </StackPanel>
                                        <StackPanel Orientation="Vertical" Margin="0,0,40,0">
                                            <TextBlock Text="地址" Foreground="#7886B2" FontWeight="Regular"/>
                                            <Grid Margin="0,10">
                                                <TextBox Text="{Binding Address}" Width="80" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                                <CheckBox Content="反" ToolTip="取反" IsChecked="{Binding IsReverse}" HorizontalAlignment="Right" VerticalAlignment="Top"
                                                      Foreground="LightGreen" BorderBrush="LightGreen"
                                                      Margin="0,0,-30,0" Width="35" Height="22" FontSize="15" Padding="1"
                                                      pu:CheckBoxHelper.CheckedGlyphBrush="OrangeRed"
                                                      pu:CheckBoxHelper.CheckedForeground="OrangeRed"
                                                      pu:CheckBoxHelper.CheckedBorderBrush="OrangeRed">
                                                    <CheckBox.LayoutTransform>
                                                        <ScaleTransform ScaleX="0.8" ScaleY="0.8"/>
                                                    </CheckBox.LayoutTransform>
                                                </CheckBox>
                                            </Grid>
                                        </StackPanel>
                                        <StackPanel Orientation="Vertical">
                                            <TextBlock Text="高度" Foreground="#7886B2" FontWeight="Regular"/>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBox Margin="0,10" Text="{Binding Position}" Width="65"/>
                                                <TextBlock Text="米" Margin="5,0" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </StackPanel>
                                        <ItemsControl ItemsSource="{Binding RelatedRopes}" Margin="10,0,0,0">
                                            <ItemsControl.ItemsPanel>
                                                <ItemsPanelTemplate>
                                                    <StackPanel Orientation="Vertical" VerticalAlignment="Bottom" Margin="0,5"/>
                                                </ItemsPanelTemplate>
                                            </ItemsControl.ItemsPanel>
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <CheckBox Content="{Binding RopeName}"
                                                          Tag="{Binding RopeNumber}" 
                                                          IsChecked="{Binding IsSelected}"/>
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </StackPanel>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </StackPanel>
            <Grid Grid.Row="4" Grid.ColumnSpan="3">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                    <Button Content="取消" Width="130" Height="60" FontWeight="Regular" pu:ButtonHelper.CornerRadius="15" IsCancel="True" Margin="30,0">
                        <Button.Background>
                            <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
                                <GradientStop Color="#615E7C" Offset="0.0"/>
                                <GradientStop Color="#807E9F" Offset="1.0"/>
                            </LinearGradientBrush>
                        </Button.Background>
                    </Button>
                    <Button Content="保存" Grid.Row="4" Width="250" Height="60" IsDefault="True" FontWeight="Bold"
                        FontFamily="{StaticResource PuHuiTiRegular}" Click="Save_Click"/>
                </StackPanel>
                <CheckBox Content="保存后重新启动程序" Foreground="Orange" BorderBrush="Orange"
                          pu:CheckBoxHelper.CheckedGlyphBrush="Orange" Margin="0,10,0,0" IsChecked="{Binding IsAutoStart}"
                          HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
            </Grid>
        </Grid>
    </Border>
</pu:WindowX>
