<UserControl
    x:Class="AppFramework.Views.UserView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:extensions="clr-namespace:AppFramework.Extensions"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:perm="clr-namespace:AppFramework.Common;assembly=AppFramework.Common"
    xmlns:sync="http://schemas.syncfusion.com/wpf"
    d:DesignHeight="450"
    d:DesignWidth="800"
    mc:Ignorable="d">
    <sync:SfBusyIndicator AnimationType="DualRing" IsBusy="{Binding IsBusy}">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition />
            </Grid.RowDefinitions>

            <Border Style="{StaticResource HeaderBorderStyle}" />

            <DockPanel Margin="20,15" LastChildFill="False">
                <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                    <TextBlock
                        FontSize="18"
                        FontWeight="Bold"
                        Text="{extensions:Translate UserManagement}" />
                    <TextBlock
                        Margin="10,0,0,0"
                        VerticalAlignment="Center"
                        Text="{extensions:Translate UsersHeaderInfo}" />
                </StackPanel>

                <Button
                    VerticalAlignment="Center"
                    Command="{Binding AddCommand}"
                    Content="{extensions:Translate CreatingNewUser}"
                    DockPanel.Dock="Right"
                    Style="{StaticResource SimpleButton}"
                    Visibility="{extensions:HasPermission Text={x:Static perm:Permkeys.UserCreate}}" />
            </DockPanel>

            <Grid Grid.Row="1" Margin="20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <!--  高级筛选行  -->
                    <RowDefinition Height="auto" />
                    <!--  按钮行  -->
                    <RowDefinition Height="auto" />
                    <RowDefinition />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

                <Border Grid.RowSpan="5" Style="{StaticResource BorderStyle}" />

                <DockPanel Margin="5,15,5,0">
                    <Button
                        Padding="8"
                        Command="{Binding SearchCommand}"
                        Content="&#xe633;"
                        DockPanel.Dock="Right"
                        Style="{StaticResource IconButtonStyle}" />
                    <TextBox Margin="5,0" Text="{Binding FilterText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Delay=500}" />
                </DockPanel>

                <!--  高级筛选  -->
                <Grid
                    Grid.Row="1"
                    Margin="0,15,0,0"
                    Visibility="{Binding IsAdvancedFilter, Converter={StaticResource inverseBoolToVisibility}}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>

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

                    <Button
                        Margin="10,0"
                        Padding="5"
                        Command="{Binding SelectedCommand}"
                        Content="{Binding SelectPermissions}" />

                    <ComboBox
                        Grid.Column="1"
                        Margin="10,0"
                        ItemsSource="{Binding RoleList}"
                        SelectedItem="{Binding SelectedRole}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding DisplayName}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <CheckBox
                        Grid.Row="1"
                        Margin="10,10,0,0"
                        Content="{extensions:Translate OnlyLockedUsers}"
                        IsChecked="{Binding IsLockUser, Mode=TwoWay}" />

                    <Button
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="0,10,10,0"
                        Padding="5"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Center"
                        Command="{Binding ResetCommand}"
                        Content="{extensions:Translate Reset}" />
                </Grid>

                <Button
                    Grid.Row="2"
                    Margin="10"
                    Padding="5"
                    HorizontalAlignment="Left"
                    VerticalAlignment="Center"
                    Background="Transparent"
                    BorderThickness="0"
                    Command="{Binding AdvancedCommand}"
                    Content="{Binding FilerTitle, Mode=TwoWay}" />

                <sync:SfDataGrid
                    x:Name="SfDataGrid"
                    Grid.Row="3"
                    Margin="15,0"
                    ItemsSource="{Binding dataPager.GridModelList}"
                    SelectedItem="{Binding dataPager.SelectedItem}"
                    Style="{StaticResource SimpleDataGridStyle}">
                    <sync:SfDataGrid.Columns>
                        <sync:GridTemplateColumn
                            Width="120"
                            AllowFiltering="False"
                            AllowSorting="False"
                            HeaderText="{extensions:Translate Actions}">
                            <sync:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <sync:DropDownButtonAdv Label="{extensions:Translate Actions}" Style="{StaticResource OperationButtonStyle}">
                                        <ItemsControl ItemsSource="{Binding DataContext.proxyService.Permissions, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=sync:SfDataGrid}}">
                                            <ItemsControl.ItemTemplate>
                                                <DataTemplate>
                                                    <Button
                                                        Margin="5"
                                                        Padding="3"
                                                        Background="Transparent"
                                                        Command="{Binding DataContext.ExecuteCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=sync:SfDataGrid}}"
                                                        CommandParameter="{Binding Key}"
                                                        Content="{Binding Name}"
                                                        Foreground="White" />
                                                </DataTemplate>
                                            </ItemsControl.ItemTemplate>
                                        </ItemsControl>
                                    </sync:DropDownButtonAdv>
                                </DataTemplate>
                            </sync:GridTemplateColumn.CellTemplate>
                        </sync:GridTemplateColumn>
                        <sync:GridTextColumn HeaderText="{extensions:Translate Name}" MappingName="Name" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate Surname}" MappingName="Surname" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate UserName}" MappingName="UserName" />
                        <sync:GridTextColumn
                            Width="200"
                            HeaderText="{extensions:Translate EmailAddress}"
                            MappingName="EmailAddress" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate PhoneNumber}" MappingName="PhoneNumber" />
                        <sync:GridTemplateColumn
                            Width="60"
                            HeaderText="{extensions:Translate Active}"
                            MappingName="IsActive">
                            <sync:GridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Border
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Background="{Binding IsActive, Converter={StaticResource boolToBackgroundConverter}}"
                                        CornerRadius="5">
                                        <TextBlock
                                            Padding="5,3"
                                            Foreground="White"
                                            Text="{Binding IsActive, Converter={StaticResource boolToYesNoStrConverter}}" />
                                    </Border>
                                </DataTemplate>
                            </sync:GridTemplateColumn.CellTemplate>
                        </sync:GridTemplateColumn>
                        <sync:GridTextColumn
                            DisplayBinding="{Binding Path=CreationTime, StringFormat='yyyy-MM-dd hh:mm:ss'}"
                            HeaderText="{extensions:Translate CreationTime}"
                            MappingName="CreationTime" />
                    </sync:SfDataGrid.Columns>
                </sync:SfDataGrid>

                <sync:SfDataPager
                    Grid.Row="4"
                    HorizontalAlignment="Right"
                    NumericButtonCount="{Binding dataPager.NumericButtonCount}"
                    PageCount="{Binding dataPager.PageCount, Mode=TwoWay}"
                    PageSize="{Binding dataPager.PageSize, Mode=TwoWay}"
                    UseOnDemandPaging="True"
                    PageIndex="{Binding dataPager.PageIndex, Mode=TwoWay}" />
            </Grid>
        </Grid>
    </sync:SfBusyIndicator>
</UserControl>