<UserControl x:Class="MainShell.AlgorithmCalib.View.FusionMenuCalibView"
             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:MainShell.AlgorithmCalib.View"
             xmlns:customControl="clr-namespace:MainShell.Resources.CustomControl"
             mc:Ignorable="d" 
             xmlns:mw="http://www.maxwell-gp.com/"
             d:DesignHeight="800" d:DesignWidth="1200">
    <UserControl.Resources>
        <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/>

        <Style x:Key="ModuleCardStyle" TargetType="Border">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="#E0E4EA"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="CornerRadius" Value="6"/>
            <Setter Property="Margin" Value="0,0,0,12"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect Color="#000000" Opacity="0.06" BlurRadius="8" ShadowDepth="2" Direction="270"/>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="SectionHeaderTextStyle" TargetType="TextBlock">
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="BadgeStyle" TargetType="Border">
            <Setter Property="CornerRadius" Value="3"/>
            <Setter Property="Padding" Value="5,1"/>
            <Setter Property="Margin" Value="3,0"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="SmallActionButtonStyle" TargetType="Button">
            <Setter Property="Height" Value="28"/>
            <Setter Property="Padding" Value="10,0"/>
            <Setter Property="Margin" Value="4,2"/>
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="bd" Background="#EDF2F7" BorderBrush="#CBD5E0"
                                  BorderThickness="1" CornerRadius="4"
                                  Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#E2E8F0"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="bd" Property="Background" Value="#CBD5E0"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="bd" Property="Background" Value="#F7FAFC"/>
                                <Setter TargetName="bd" Property="BorderBrush" Value="#E2E8F0"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="StartCalibButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="120"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="0,0,10,0"/>
            <Setter Property="Background" Value="#1A202C"/>
            <Setter Property="BorderBrush" Value="#1A202C"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="#2D3748"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="#0F141B"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForeground}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ModuleWriteButtonStyle" TargetType="Button" BasedOn="{StaticResource BaseRoundedButtonStyle}">
            <Setter Property="Width" Value="110"/>
            <Setter Property="Height" Value="32"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="4,0"/>
            <Setter Property="Background" Value="{StaticResource TeachOrange}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TeachOrange}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource TeachOrangeHover}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource TeachOrangeHover}"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="{StaticResource TeachOrangePressed}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource TeachOrangePressed}"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource DisabledBackground}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForeground}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- 左侧：相机预览 -->
        <Border Grid.Column="0" Margin="0,0,4,0">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="32"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Border Grid.Row="0" Padding="10,0">
                    <Grid>
                        <TextBlock Text="相机预览" Foreground="#7A8FA6" FontSize="11"
                                     FontWeight="SemiBold" VerticalAlignment="Center"/>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                            <Ellipse Width="7" Height="7" Fill="#28A745" Margin="0,0,4,0"/>
                            <TextBlock Text="LIVE" Foreground="#28A745" FontSize="10" FontWeight="Bold"/>
                        </StackPanel>
                    </Grid>
                </Border>
                <ContentControl Grid.Row="1" Content="{Binding CameraAxisViewModelService}"/>
            </Grid>
        </Border>

        <!-- 右侧：工具栏 + 标定卡片 -->
        <Grid Grid.Column="1">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!-- 顶部工具栏 -->
            <Border Grid.Row="0" Background="White" BorderBrush="#E0E4EA"
                      BorderThickness="0,0,0,1" Padding="8,0">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
                    <Button Content="读取标定数据" Command="{mw:Action btnReadCalibData}"
                              Style="{StaticResource SmallActionButtonStyle}" Width="110" Height="34" Margin="5,0"/>
                    <Button Content="保存标定数据" Command="{mw:Action btnSaveCalibData}"
                              Style="{StaticResource SmallActionButtonStyle}" Width="110" Height="34" Margin="5,0"/>
                    <Button Content="应用标定数据" Command="{mw:Action btnApplyCalibData}"
                              Style="{StaticResource ModuleWriteButtonStyle}" Width="110" Height="34" Margin="5,0"/>
                </StackPanel>
            </Border>

            <!-- 标定卡片 -->
            <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto"
                            HorizontalScrollBarVisibility="Disabled" CanContentScroll="False"
                            PanningMode="VerticalOnly" Padding="8,8,8,0">
                <Border Style="{StaticResource ModuleCardStyle}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="36"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <!-- Header -->
                        <Border Grid.Row="0" Background="#F7F9FC" BorderBrush="#E0E4EA"
                                  BorderThickness="0,0,0,1" CornerRadius="6,6,0,0" Padding="10,0">
                            <Grid>
                                <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                    <TextBlock Text="[] " Foreground="#8892A0" FontSize="14"  VerticalAlignment="Center"/>
                                    <TextBlock FontWeight="SemiBold" FontSize="13" Foreground="#2D3748"  VerticalAlignment="Center">
                                          <Run Text="融合标定数据"/>
                                    </TextBlock>
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"  VerticalAlignment="Center">
                                    <Border Style="{StaticResource BadgeStyle}" Background="#EDF2F7">
                                        <TextBlock Text="4区域融合" FontSize="10" Foreground="#718096"/>
                                    </Border>
                                </StackPanel>
                            </Grid>
                        </Border>

                        <!-- Section 1: 标定参数表格 -->
                        <Border Grid.Row="1" BorderBrush="#E0E4EA"
                                  BorderThickness="0,0,0,1" Padding="10,8">
                            <StackPanel>
                                <Grid Margin="0,0,0,6">
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                        <Ellipse Width="8" Height="8" Fill="#2D3748" VerticalAlignment="Center"  Margin="0,0,5,0"/>
                                        <TextBlock Text="标定区域参数" Style="{StaticResource  SectionHeaderTextStyle}"/>
                                    </StackPanel>
                                </Grid>

                                <DataGrid ItemsSource="{Binding FusionCalibParItem}"
                                            SelectedItem="{Binding SelectedCalibPar}"
                                            Margin="0,4" Height="200"
                                            AutoGenerateColumns="false" SelectionMode="Single"
                                            CanUserSortColumns="False" HeadersVisibility="Column"
                                            GridLinesVisibility="Horizontal"
                                            HorizontalGridLinesBrush="#F0F4F8"
                                            BorderBrush="#E0E4EA" BorderThickness="1"
                                            RowBackground="White" AlternatingRowBackground="#FAFBFC"
                                            RowHeight="28">
                                    <DataGrid.ColumnHeaderStyle>
                                        <Style TargetType="DataGridColumnHeader">
                                            <Setter Property="Background" Value="#F7F9FC"/>
                                            <Setter Property="Foreground" Value="#8892A0"/>
                                            <Setter Property="FontWeight" Value="SemiBold"/>
                                            <Setter Property="FontSize" Value="11"/>
                                            <Setter Property="Padding" Value="8,4"/>
                                            <Setter Property="VerticalContentAlignment" Value="Center"/>
                                            <Setter Property="BorderBrush" Value="#E0E4EA"/>
                                            <Setter Property="BorderThickness" Value="0,0,0,1"/>
                                        </Style>
                                    </DataGrid.ColumnHeaderStyle>
                                    <DataGrid.CellStyle>
                                        <Style TargetType="DataGridCell">
                                            <Setter Property="Padding" Value="8,2"/>
                                            <Setter Property="BorderThickness" Value="0"/>
                                            <Style.Triggers>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter Property="Background" Value="#EBF5FF"/>
                                                    <Setter Property="Foreground" Value="#1568D5"/>
                                                </Trigger>
                                            </Style.Triggers>
                                        </Style>
                                    </DataGrid.CellStyle>
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="序号" IsReadOnly="True" Binding="{Binding Index}"  Width="50"/>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="Y1(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding MoveAxisPos, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="X1(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding ApproachXPos, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="Y2(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding ApproachYPos, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="StartX(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding StartX, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="StartY(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding StartY, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="步长(mm)"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding Step, StringFormat=F4}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                        <DataGridTemplateColumn Width="*">
                                            <DataGridTemplateColumn.Header>
                                                <TextBlock  Text="行列数"/>
                                            </DataGridTemplateColumn.Header>
                                            <DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <TextBlock VerticalAlignment="Center" Foreground="{StaticResource  PrimaryTextBrush}"
                                                                 Text="{Binding Count}"/>
                                                </DataTemplate>
                                            </DataGridTemplateColumn.CellTemplate>
                                        </DataGridTemplateColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </StackPanel>
                        </Border>

                        <!-- Section 2: 操作按钮 -->
                        <Border Grid.Row="2" BorderBrush="#E0E4EA"
                                  BorderThickness="0,0,0,1" Padding="10,8">
                            <StackPanel>
                                <Grid Margin="0,0,0,6">
                                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                                        <Ellipse Width="8" Height="8" Fill="#2B6CB0" VerticalAlignment="Center"  Margin="0,0,5,0"/>
                                        <TextBlock Text="标定操作" Foreground="#2B6CB0" Style="{StaticResource  SectionHeaderTextStyle}"/>
                                    </StackPanel>
                                </Grid>
                                <StackPanel Orientation="Horizontal" Margin="0,4">
                                    <Button Content="打开标定窗口" Command="{mw:Action btnOpenCalibWindow}"
                                              Style="{StaticResource StartCalibButtonStyle}" Width="120"/>
                                    <TextBlock Text="选择一行数据后打开对应标定窗口"
                                                 Foreground="#A0AEC0" FontSize="11" VerticalAlignment="Center"/>
                                </StackPanel>
                            </StackPanel>
                        </Border>

                        <!-- Footer -->
                        <Border Grid.Row="3" BorderBrush="#E0E4EA" BorderThickness="0,1,0,0"
                                  Background="#FAFBFC" CornerRadius="0,0,6,6" Padding="10,0">
                            <TextBlock Text="共 4 个标定区域" Foreground="#A0AEC0" FontSize="11"
  VerticalAlignment="Center"/>
                        </Border>
                    </Grid>
                </Border>
            </ScrollViewer>
        </Grid>

        <customControl:LoadingWaitView Grid.ColumnSpan="2" Panel.ZIndex="1000"/>
    </Grid>

</UserControl>
