<UserControl x:Class="MainShell.Resources.CustomControl.PaginationControl"
             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.Resources.CustomControl"
             xmlns:mw="http://www.maxwell-gp.com/"
             mc:Ignorable="d" 
            >
    <UserControl.Resources>
        <SolidColorBrush x:Key="PaginationPanelBackgroundBrush" Color="#F7F8FB"/>
        <SolidColorBrush x:Key="PaginationPanelBorderBrush" Color="#D7DEE8"/>
        <SolidColorBrush x:Key="PaginationTextBrush" Color="#2F3A4C"/>
        <SolidColorBrush x:Key="PaginationMutedTextBrush" Color="#90A0B5"/>
        <SolidColorBrush x:Key="PaginationAccentBrush" Color="#3D7DFF"/>
        <LinearGradientBrush x:Key="PaginationPrimaryButtonBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#4B668D" Offset="0"/>
            <GradientStop Color="#2E4566" Offset="1"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="PaginationPrimaryButtonHoverBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#57749C" Offset="0"/>
            <GradientStop Color="#344D71" Offset="1"/>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="PaginationPrimaryButtonPressedBrush" StartPoint="0,0" EndPoint="0,1">
            <GradientStop Color="#2C4261" Offset="0"/>
            <GradientStop Color="#21344F" Offset="1"/>
        </LinearGradientBrush>
        <SolidColorBrush x:Key="PaginationButtonBackgroundBrush" Color="#F8FAFD"/>
        <SolidColorBrush x:Key="PaginationButtonHoverBrush" Color="#EEF3FA"/>
        <SolidColorBrush x:Key="PaginationComboBoxHoverBrush" Color="#F3F7FC"/>
        <SolidColorBrush x:Key="PaginationComboBoxDropDownBrush" Color="#7C8DA5"/>
        <SolidColorBrush x:Key="PaginationComboBoxPopupBorderBrush" Color="#CDD7E3"/>

        <Style x:Key="PaginationTextStyle" TargetType="TextBlock">
            <Setter Property="Foreground" Value="{StaticResource PaginationTextBrush}"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
        </Style>

        <Style x:Key="PaginationButtonStyle" TargetType="Button">
            <Setter Property="Width" Value="32"/>
            <Setter Property="Height" Value="24"/>
            <Setter Property="Margin" Value="1,0"/>
            <Setter Property="Padding" Value="8,0"/>
            <Setter Property="Foreground" Value="{StaticResource PaginationTextBrush}"/>
            <Setter Property="Background" Value="{StaticResource PaginationButtonBackgroundBrush}"/>
            <Setter Property="BorderBrush" Value="{StaticResource PaginationPanelBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="ButtonBorder"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="3">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="ButtonBorder" Property="Background" Value="{StaticResource PaginationButtonHoverBrush}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="ButtonBorder" Property="Background" Value="{StaticResource PaginationPanelBorderBrush}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="ButtonBorder" Property="Opacity" Value="0.5"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="PaginationPrimaryButtonStyle" TargetType="Button" BasedOn="{StaticResource PaginationButtonStyle}">
            <Setter Property="Width" Value="36"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="Background" Value="{StaticResource PaginationPrimaryButtonBrush}"/>
            <Setter Property="BorderBrush" Value="{StaticResource PaginationPrimaryButtonBrush}"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource PaginationPrimaryButtonHoverBrush}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource PaginationPrimaryButtonHoverBrush}"/>
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Background" Value="{StaticResource PaginationPrimaryButtonPressedBrush}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource PaginationPrimaryButtonPressedBrush}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="PaginationComboBoxToggleButtonStyle" TargetType="ToggleButton">
            <Setter Property="Focusable" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border x:Name="ToggleBorder"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="3">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="22"/>
                                </Grid.ColumnDefinitions>
                                <ContentPresenter Grid.Column="0"
                                                  Margin="8,0,0,0"
                                                  VerticalAlignment="Center"
                                                  HorizontalAlignment="Left"/>
                                <Border Grid.Column="1"
                                        BorderBrush="{StaticResource PaginationPanelBorderBrush}"
                                        BorderThickness="1,0,0,0"
                                        Background="Transparent">
                                    <Path x:Name="Arrow"
                                          Width="8"
                                          Height="5"
                                          HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Fill="{StaticResource PaginationComboBoxDropDownBrush}"
                                          Data="M 0 0 L 4 4 L 8 0 Z"/>
                                </Border>
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="ToggleBorder" Property="Background" Value="{StaticResource PaginationComboBoxHoverBrush}"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="ToggleBorder" Property="Background" Value="{StaticResource PaginationComboBoxHoverBrush}"/>
                                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource PaginationAccentBrush}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="ToggleBorder" Property="Opacity" Value="0.6"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="PaginationComboBoxItemStyle" TargetType="ComboBoxItem">
            <Setter Property="Foreground" Value="{StaticResource PaginationTextBrush}"/>
            <Setter Property="Padding" Value="8,4"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBoxItem">
                        <Border x:Name="ItemBorder"
                                Background="Transparent"
                                CornerRadius="2">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="Center"
                                              Margin="2,0"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsHighlighted" Value="True">
                                <Setter TargetName="ItemBorder" Property="Background" Value="{StaticResource PaginationButtonHoverBrush}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="ItemBorder" Property="Background" Value="{StaticResource PaginationComboBoxHoverBrush}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="PaginationComboBoxStyle" TargetType="ComboBox">
            <Setter Property="Height" Value="24"/>
            <Setter Property="Width" Value="64"/>
            <Setter Property="Margin" Value="4,0,0,0"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Foreground" Value="{StaticResource PaginationTextBrush}"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="{StaticResource PaginationPanelBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="ItemContainerStyle" Value="{StaticResource PaginationComboBoxItemStyle}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton x:Name="ToggleButton"
                                          Background="{TemplateBinding Background}"
                                          BorderBrush="{TemplateBinding BorderBrush}"
                                          BorderThickness="{TemplateBinding BorderThickness}"
                                          Focusable="False"
                                          IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                          ClickMode="Press"
                                          Style="{StaticResource PaginationComboBoxToggleButtonStyle}">
                                <TextBlock Text="{Binding SelectionBoxItem, RelativeSource={RelativeSource TemplatedParent}}"
                                           Foreground="{TemplateBinding Foreground}"
                                           FontSize="12"
                                           VerticalAlignment="Center"/>
                            </ToggleButton>
                            <Popup x:Name="Popup"
                                   AllowsTransparency="True"
                                   Focusable="False"
                                   IsOpen="{TemplateBinding IsDropDownOpen}"
                                   Placement="Bottom"
                                   PopupAnimation="Slide">
                                <Border Margin="0,2,0,0"
                                        MinWidth="{Binding ActualWidth, RelativeSource={RelativeSource TemplatedParent}}"
                                        Background="White"
                                        BorderBrush="{StaticResource PaginationComboBoxPopupBorderBrush}"
                                        BorderThickness="1"
                                        CornerRadius="3">
                                    <ScrollViewer CanContentScroll="True"
                                                  MaxHeight="180"
                                                  Padding="2">
                                        <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained"/>
                                    </ScrollViewer>
                                </Border>
                            </Popup>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="PaginationJumpBoxStyle" TargetType="mw:IntNumberBox">
            <Setter Property="Width" Value="42"/>
            <Setter Property="Height" Value="24"/>
            <Setter Property="Margin" Value="4,0,0,0"/>
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderBrush" Value="{StaticResource PaginationPanelBorderBrush}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{StaticResource PaginationTextBrush}"/>
            <Setter Property="VerticalContentAlignment" Value="Center"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
        </Style>
    </UserControl.Resources>

    <ScrollViewer HorizontalScrollBarVisibility="Auto"
                  VerticalScrollBarVisibility="Disabled"
                  Style="{StaticResource ProcessScrollViewerStyle}"
                  Padding="0"
                  Background="Transparent">
        <Border Background="{StaticResource PaginationPanelBackgroundBrush}"
                BorderBrush="{StaticResource PaginationPanelBorderBrush}"
                BorderThickness="1"
                CornerRadius="2"
                Padding="6,3"
                MinWidth="470">
            <Grid VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="12"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="14"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="20"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
                    <TextBlock Text="每页:" Style="{StaticResource PaginationTextStyle}" FontSize="12"/>
                    <ComboBox Style="{StaticResource PaginationComboBoxStyle}"
                              ItemsSource="{Binding PageSizeOptions}"
                              SelectedItem="{Binding PageSize}"/>
                </StackPanel>

                <StackPanel Grid.Column="2" Orientation="Horizontal" VerticalAlignment="Center">
                    <Button Content="首页" Command="{Binding MoveFirstCommand}" Width="36" Style="{StaticResource PaginationButtonStyle}"/>
                    <Button Content="&lt;" Command="{Binding MoveBackCommand}" Style="{StaticResource PaginationButtonStyle}"/>

                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="10,0">
                        <TextBlock Text="{Binding CurrentPage}"
                                   Foreground="{StaticResource PaginationAccentBrush}"
                                   FontSize="17"
                                   FontWeight="Normal"
                                   VerticalAlignment="Center"/>
                        <TextBlock Text=" / "
                                   Style="{StaticResource PaginationTextStyle}"
                                   FontSize="15"
                                   Margin="3,0"/>
                        <TextBlock Text="{Binding TotalPages}"
                                   Style="{StaticResource PaginationTextStyle}"
                                   FontSize="15"
                                   FontWeight="Normal"/>
                        <TextBlock Text="{Binding TotalItems, StringFormat='（共{0}项）'}"
                                   Foreground="{StaticResource PaginationMutedTextBrush}"
                                   FontSize="10"
                                   Margin="4,4,0,0"
                                   VerticalAlignment="Center"/>
                    </StackPanel>

                    <Button Content=">" Command="{Binding MoveNextCommand}" Style="{StaticResource PaginationButtonStyle}"/>
                    <Button Content="尾页" Command="{Binding MoveLastCommand}" Width="36" Style="{StaticResource PaginationButtonStyle}"/>
                </StackPanel>

                <Border Grid.Column="4"
                        Width="1"
                        Height="16"
                        Background="{StaticResource PaginationPanelBorderBrush}"
                        VerticalAlignment="Center"/>

                <StackPanel Grid.Column="6" Orientation="Horizontal" VerticalAlignment="Center">
                    <TextBlock Text="跳转:" Style="{StaticResource PaginationTextStyle}" FontSize="12"/>
                    <mw:IntNumberBox Style="{StaticResource PaginationJumpBoxStyle}"
                                     mw:NumericKeypadAttach.IsEnabled="True"
                                     Value="{Binding JumpPage, UpdateSourceTrigger=PropertyChanged}"/>
                    <Button Content="Go" Command="{Binding JumpToPageCommand}" Margin="6,0,0,0" Style="{StaticResource PaginationPrimaryButtonStyle}"/>
                </StackPanel>
            </Grid>
        </Border>
    </ScrollViewer>
</UserControl>
