<UserControl x:Class="MainShell.Manual.View.LogisticsOperationView"
             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:custom="clr-namespace:MainShell.Resources.CustomControl"
             xmlns:mw="http://www.maxwell-gp.com/"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <Style x:Key="PanelBorderStyle" TargetType="Border">
            <Setter Property="Background" Value="#F8FAFD"/>
            <Setter Property="BorderBrush" Value="#D4DCE8"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="CornerRadius" Value="6"/>
            <Setter Property="Margin" Value="6"/>
            <Setter Property="Padding" Value="12"/>
        </Style>

        <Style x:Key="IndicatorDotStyle" TargetType="Ellipse">
            <Setter Property="Width" Value="11"/>
            <Setter Property="Height" Value="11"/>
            <Setter Property="Fill" Value="#CBD5E1"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOn}" Value="True">
                    <Setter Property="Fill" Value="#22C55E"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="PhaseCardBorderStyle" TargetType="Border">
            <Setter Property="BorderBrush" Value="#C9D6EA"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="CornerRadius" Value="3"/>
            <Setter Property="Padding" Value="8"/>
            <Setter Property="Margin" Value="0,0,0,6"/>
            <Setter Property="Background" Value="#F2F6FC"/>
        </Style>

        <Style x:Key="PhaseActionButtonStyle" TargetType="Button" BasedOn="{StaticResource LogisticsActionButtonStyle}">
            <Setter Property="Width" Value="190"/>
            <Setter Property="Height" Value="36"/>
            <Setter Property="Margin" Value="0,4,0,0"/>
        </Style>
    </UserControl.Resources>

    <Grid Background="#EDF1F7">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="290"/>
        </Grid.ColumnDefinitions>

        <Grid Grid.Column="0" Margin="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Border Style="{StaticResource PanelBorderStyle}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding State.SubstrateLoad.Title}" FontWeight="Bold" FontSize="18" Foreground="#0F3B7A"/>
                        <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
                            <Ellipse Width="12" Height="12" Fill="#22C55E" Margin="0,0,6,0"/>
                        </StackPanel>
                    </Grid>
                    <Separator Grid.Row="1" Margin="0,8,0,10"/>

                    <ItemsControl Grid.Row="2" ItemsSource="{Binding State.SubstrateLoad.Indicators}" VerticalAlignment="Top">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="2"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,4">
                                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Foreground="#20354C"/>
                                    <Ellipse Style="{StaticResource IndicatorDotStyle}" Margin="6,0,0,0"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <StackPanel Grid.Row="3" Margin="0,10,0,0">
                        <Button Content="{Binding State.SubstrateLoad.PrimaryActionText}" Style="{StaticResource LogisticsActionButtonStyle}"/>
                        <Button Content="{Binding State.SubstrateLoad.SecondaryActionText}" Style="{StaticResource LogisticsSecondaryButtonStyle}"/>
                    </StackPanel>
                </Grid>
            </Border>

            <Border Grid.Column="1" Style="{StaticResource PanelBorderStyle}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding State.ChipLoad.Title}" FontWeight="Bold" FontSize="18" Foreground="#0F3B7A"/>
                        <StackPanel Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Center">
                            <CheckBox Content="无EFEM调试" IsChecked="{Binding State.IsManualWithoutEfem}" Margin="0,0,10,0" Foreground="#6B7C95"/>
                        </StackPanel>
                    </Grid>
                    <Separator Grid.Row="1" Margin="0,8,0,10"/>

                    <StackPanel Grid.Row="2" VerticalAlignment="Top">
                        <Border Style="{StaticResource PhaseCardBorderStyle}" IsEnabled="{Binding State.IsManualWithoutEfem, Converter={StaticResource boolToInversionConverter}}">
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding State.ChipLoad.PhaseSteps[0].Title}" Foreground="#2455C7" FontWeight="Bold"/>
                                </Grid>
                                <Button Content="{Binding State.ChipLoad.PhaseSteps[0].ActionText}"
                                        Style="{StaticResource PhaseActionButtonStyle}"
                                        IsEnabled="{Binding State.ChipLoad.PhaseSteps[0].IsEnabled}"
                                        Click="{mw:Action ChipLoadPhase1Async}"/>
                                <StackPanel Orientation="Horizontal" Margin="0,4,0,0">
                                    <TextBlock Text="料盒层数" VerticalAlignment="Center" Foreground="#5B6B83" Margin="0,0,6,0"/>
                                    <mw:IntNumberBox Minimum="1" Maximum="25" Width="60"
                                                     Value="{Binding State.ChipLoad.PhaseSteps[0].TargetLayer}"
                                                     mw:NumericKeypadAttach.IsEnabled="True"/>
                                </StackPanel>
                            </StackPanel>
                        </Border>
                        <Border Style="{StaticResource PhaseCardBorderStyle}">
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding State.ChipLoad.PhaseSteps[1].Title}" Foreground="#96A1B3" FontWeight="Bold"/>
                                </Grid>
                                <Button Content="{Binding State.ChipLoad.PhaseSteps[1].ActionText}"
                                        Style="{StaticResource PhaseActionButtonStyle}"
                                        IsEnabled="{Binding State.ChipLoad.PhaseSteps[1].IsEnabled}"
                                        Click="{mw:Action ChipLoadPhase2Async}"/>
                            </StackPanel>
                        </Border>
                        <ItemsControl ItemsSource="{Binding State.ChipLoad.Indicators}" VerticalAlignment="Top">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Columns="2"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="0,4">
                                        <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Foreground="#20354C"/>
                                        <Ellipse Style="{StaticResource IndicatorDotStyle}" Margin="6,0,0,0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>

                    <StackPanel Grid.Row="3" Margin="0,8,0,0">
                        <Button Content="{Binding State.ChipLoad.SecondaryActionText}" Style="{StaticResource LogisticsSecondaryButtonStyle}"/>
                    </StackPanel>
                </Grid>
            </Border>

            <Border Grid.Row="1" Style="{StaticResource PanelBorderStyle}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding State.SubstrateOut.Title}" FontWeight="Bold" FontSize="18" Foreground="#0F3B7A"/>
                        <Ellipse Grid.Column="1" Width="12" Height="12" Fill="#CBD5E1" VerticalAlignment="Center"/>
                    </Grid>
                    <Separator Grid.Row="1" Margin="0,8,0,10"/>

                    <ItemsControl Grid.Row="2" ItemsSource="{Binding State.SubstrateOut.Indicators}" VerticalAlignment="Top">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="2"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Margin="0,4">
                                    <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Foreground="#20354C"/>
                                    <Ellipse Style="{StaticResource IndicatorDotStyle}" Margin="6,0,0,0"/>
                                </StackPanel>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>

                    <StackPanel Grid.Row="3" Margin="0,10,0,0">
                        <Button Content="{Binding State.SubstrateOut.PrimaryActionText}" Style="{StaticResource LogisticsOutlineActionButtonStyle}"/>
                        <Button Content="{Binding State.SubstrateOut.SecondaryActionText}" Style="{StaticResource LogisticsSecondaryButtonStyle}"/>
                    </StackPanel>
                </Grid>
            </Border>

            <Border Grid.Row="1" Grid.Column="1" Style="{StaticResource PanelBorderStyle}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding State.ChipUnload.Title}" FontWeight="Bold" FontSize="18" Foreground="#0F3B7A"/>
                    </Grid>
                    <Separator Grid.Row="1" Margin="0,8,0,10"/>

                    <StackPanel Grid.Row="2" VerticalAlignment="Top">
                        <Border Style="{StaticResource PhaseCardBorderStyle}">
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding State.ChipUnload.PhaseSteps[0].Title}" Foreground="#D17A00" FontWeight="Bold"/>
                                </Grid>
                                <Button Content="{Binding State.ChipUnload.PhaseSteps[0].ActionText}"
                                        Style="{StaticResource LogisticsWarningActionButtonStyle}"
                                        IsEnabled="{Binding State.ChipUnload.PhaseSteps[0].IsEnabled}"
                                        Click="{mw:Action ChipUnloadPhase1Async}"/>
                            </StackPanel>
                        </Border>
                        <Border Style="{StaticResource PhaseCardBorderStyle}" IsEnabled="{Binding State.IsManualWithoutEfem, Converter={StaticResource boolToInversionConverter}}">
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding State.ChipUnload.PhaseSteps[1].Title}" Foreground="#96A1B3" FontWeight="Bold"/>
                                </Grid>
                                <Button Content="{Binding State.ChipUnload.PhaseSteps[1].ActionText}"
                                        Style="{StaticResource PhaseActionButtonStyle}"
                                        IsEnabled="{Binding State.ChipUnload.PhaseSteps[1].IsEnabled}"
                                        Click="{mw:Action ChipUnloadPhase2Async}"/>
                                <StackPanel Orientation="Horizontal" Margin="0,4,0,0">
                                    <TextBlock Text="料盒层数" VerticalAlignment="Center" Foreground="#5B6B83" Margin="0,0,6,0"/>
                                    <mw:IntNumberBox Minimum="1" Maximum="25" Width="60"
                                                     Value="{Binding State.ChipUnload.PhaseSteps[1].TargetLayer}"
                                                     mw:NumericKeypadAttach.IsEnabled="True"/>
                                </StackPanel>
                            </StackPanel>
                        </Border>
                        <ItemsControl ItemsSource="{Binding State.ChipUnload.Indicators}" VerticalAlignment="Top">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <UniformGrid Columns="2"/>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal" Margin="0,4">
                                        <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Foreground="#20354C"/>
                                        <Ellipse Style="{StaticResource IndicatorDotStyle}" Margin="6,0,0,0"/>
                                    </StackPanel>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>

                    <StackPanel Grid.Row="3" Margin="0,8,0,0">
                        <Button Content="{Binding State.ChipUnload.SecondaryActionText}" Style="{StaticResource LogisticsSecondaryButtonStyle}"/>
                    </StackPanel>
                </Grid>
            </Border>
        </Grid>

        <Grid Grid.Column="1" Margin="6,8,8,8">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="120"/>
            </Grid.RowDefinitions>

            <custom:MagazineMapControl MapViewModel="{Binding State.MagazineMap}"/>

            <Button Grid.Row="1"
                    Content="自动扫码 SCAN"
                    Style="{StaticResource LogisticsScanButtonStyle}"
                    IsEnabled="{Binding State.IsScanning, Converter={StaticResource boolToInversionConverter}}"
                    Click="{mw:Action ScanAsync}"/>

            <Grid Grid.Row="2" Margin="0,0,0,8" Visibility="{Binding State.IsScanning, Converter={StaticResource BoolToVisibleConverter}}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Text="扫码中..." Foreground="#1A2A47" FontWeight="Bold" Margin="2,0,2,4"/>
                <ProgressBar Grid.Row="1" Height="10" Minimum="0" Maximum="100" Value="{Binding State.ScanProgress}"/>
            </Grid>

            <Border Grid.Row="3" Background="#07152F" BorderBrush="#2B3E63" BorderThickness="1" CornerRadius="4" Padding="10">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding State.FeedbackTitle}" Foreground="#42A5F5" FontWeight="Bold" FontStyle="Italic"/>
                        <TextBlock Grid.Column="1" Text="{Binding State.LastFeedbackTime}" Foreground="#58A6FF"/>
                    </Grid>
                    <ItemsControl Grid.Row="1" Margin="0,8,0,0" ItemsSource="{Binding State.FeedbackRecords}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Foreground="#D6E2F5" TextWrapping="Wrap" Margin="0,0,0,2">
                                    <Run Text="["/>
                                    <Run Text="{Binding Time, StringFormat={}{0:HH:mm:ss}}"/>
                                    <Run Text="] ["/>
                                    <Run Text="{Binding Level}"/>
                                    <Run Text="] "/>
                                    <Run Text="{Binding Message}"/>
                                </TextBlock>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>
            </Border>
        </Grid>
    </Grid>
</UserControl>
