<UserControl
    x:Class="AppFramework.Views.TenantView"
    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 TenantManagement}" />

                    <TextBlock
                        Margin="10,0,0,0"
                        VerticalAlignment="Center"
                        Text="{extensions:Translate TenantsHeaderInfo}" />
                </StackPanel>

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

            <Grid Grid.Row="1" Margin="20">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition />
                    <RowDefinition Height="auto" />
                </Grid.RowDefinitions>

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

                <Grid Margin="15">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

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

                    <TextBlock Margin="5" Text="{extensions:Translate TenantNameOrTenancyCode}" />

                    <DockPanel Grid.Row="1" Margin="5">
                        <Button
                            Padding="8"
                            Command="{Binding SearchCommand}"
                            Content="&#xe633;"
                            DockPanel.Dock="Right"
                            Style="{StaticResource IconButtonStyle}" />
                        <TextBox Text="{Binding Filter.Filter}" />
                    </DockPanel>

                    <TextBlock
                        Grid.Column="1"
                        Margin="5"
                        Text="{extensions:Translate Edition}" />
                    <ComboBox
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="5"
                        ItemsSource="{Binding Editions}"
                        SelectedItem="{Binding Edition}">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding DisplayName}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

                    <CheckBox
                        Grid.Row="2"
                        Margin="5"
                        Content="{extensions:Translate SubscriptionEndDate}"
                        IsChecked="{Binding IsSubscription, Mode=TwoWay}" />

                    <UniformGrid
                        Grid.Row="3"
                        Margin="0,5"
                        Columns="2">
                        <DatePicker
                            Margin="5"
                            IsEnabled="{Binding IsSubscription, Converter={StaticResource inverseBooleanConverter}}"
                            SelectedDate="{Binding Filter.SubscriptionEndDateStart}" />
                        <DatePicker
                            Margin="5"
                            IsEnabled="{Binding IsSubscription, Converter={StaticResource inverseBooleanConverter}}"
                            SelectedDate="{Binding Filter.SubscriptionEndDateEnd}" />
                    </UniformGrid>

                    <CheckBox
                        Grid.Row="2"
                        Grid.Column="1"
                        Margin="5"
                        Content="{extensions:Translate CreationTime}"
                        IsChecked="{Binding IsCreation, Mode=TwoWay}" />

                    <UniformGrid
                        Grid.Row="3"
                        Grid.Column="1"
                        Margin="0,5"
                        Columns="2">
                        <DatePicker
                            Margin="5"
                            IsEnabled="{Binding IsCreation, Converter={StaticResource inverseBooleanConverter}}"
                            SelectedDate="{Binding Filter.CreationDateStart}" />
                        <DatePicker
                            Margin="5"
                            IsEnabled="{Binding IsCreation, Converter={StaticResource inverseBooleanConverter}}"
                            SelectedDate="{Binding Filter.CreationDateEnd}" />
                    </UniformGrid>
                </Grid>

                <sync:SfDataGrid
                    x:Name="SfDataGrid"
                    Grid.Row="1"
                    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 TenancyName}" MappingName="TenancyName" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate Name}" MappingName="Name" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate DisplayName}" MappingName="EditionDisplayName" />
                        <sync:GridTextColumn HeaderText="{extensions:Translate IsActive}" MappingName="IsActive" />
                    </sync:SfDataGrid.Columns>
                </sync:SfDataGrid>

                <sync:SfDataPager
                    Grid.Row="2"
                    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>
