<UserControl
    x:Class="AppFramework.Views.OrganizationsView"
    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:i="http://schemas.microsoft.com/xaml/behaviors"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:models="clr-namespace:AppFramework.Common.Models;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" LastChildFill="False">
                <StackPanel VerticalAlignment="Center" Orientation="Horizontal">
                    <TextBlock
                        FontSize="18"
                        FontWeight="Bold"
                        Text="{extensions:Translate OrganizationUnits}" />
                    <TextBlock
                        Margin="10,0,0,0"
                        VerticalAlignment="Center"
                        Text="{extensions:Translate OrganizationUnitsHeaderInfo}" />
                </StackPanel>
            </DockPanel>

            <Grid Grid.Row="1" Margin="20">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="2*" />
                </Grid.ColumnDefinitions>

                <Border
                    CornerRadius="5"
                    Opacity="0.2"
                    Style="{StaticResource BorderStyle}" />

                <DockPanel Margin="5" LastChildFill="False">
                    <DockPanel DockPanel.Dock="Top" LastChildFill="False">
                        <TextBlock
                            VerticalAlignment="Center"
                            DockPanel.Dock="Left"
                            Text="{extensions:Translate OrganizationTree}" />
                        <Button
                            Command="{Binding ExecuteItemCommand}"
                            CommandParameter="AddOrganizationUnit"
                            Content="{extensions:Translate AddRootUnit}"
                            DockPanel.Dock="Right"
                            Style="{StaticResource SimpleButton}" />
                    </DockPanel>

                    <TreeView
                        x:Name="TreeViewOriganizations"
                        Margin="0,10,0,0"
                        BorderBrush="Transparent"
                        DockPanel.Dock="Top"
                        ItemsSource="{Binding dataPager.GridModelList}">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type models:OrganizationListModel}" ItemsSource="{Binding Items}">
                                <DockPanel Margin="0,5">
                                    <Image
                                        Width="20"
                                        Height="20"
                                        Margin="0,0,10,0"
                                        Source="/Assets/Images/OrganizationUnit.png" />
                                    <TextBlock
                                        Margin="5,0"
                                        VerticalAlignment="Center"
                                        Text="{Binding DisplayName}" />
                                    <TextBlock
                                        Margin="5,0"
                                        VerticalAlignment="Center"
                                        Text="{Binding MemberCount}" />
                                    <TextBlock VerticalAlignment="Center" Text="{extensions:Translate Members}" />
                                    <TextBlock
                                        Margin="5,0"
                                        VerticalAlignment="Center"
                                        Text="{Binding RoleCount}" />
                                    <TextBlock VerticalAlignment="Center" Text="{extensions:Translate Roles}" />

                                    <Button
                                        Margin="10,0,2,0"
                                        Command="{Binding DataContext.AddRootUnitCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TreeView}}"
                                        CommandParameter="{Binding}"
                                        Content="&#xe607;"
                                        Style="{StaticResource IconButtonStyle}"
                                        ToolTip="{extensions:Translate AddRootUnit}" />
                                    <Button
                                        Command="{Binding DataContext.ChangeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TreeView}}"
                                        CommandParameter="{Binding}"
                                        Content="&#xe651;"
                                        Style="{StaticResource IconButtonStyle}"
                                        ToolTip="{extensions:Translate Change}" />
                                    <Button
                                        Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TreeView}}"
                                        CommandParameter="{Binding}"
                                        Content="&#xe65c;"
                                        Style="{StaticResource IconButtonStyle}"
                                        ToolTip="{extensions:Translate Remove}" />
                                </DockPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="SelectedItemChanged">
                                <i:InvokeCommandAction Command="{Binding SelectedCommand}" CommandParameter="{Binding ElementName=TreeViewOriganizations, Path=SelectedItem}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </TreeView>
                </DockPanel>

                <Border
                    Grid.Column="1"
                    Margin="10,0,0,0"
                    CornerRadius="3"
                    Opacity="0.2"
                    Style="{StaticResource BorderStyle}" />

                <Grid Grid.Column="1" Margin="10,0,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <TextBlock
                        Margin="5"
                        FontSize="18"
                        FontWeight="Bold"
                        Text="{Binding ElementName=TreeViewOriganizations, Path=SelectedItem.DisplayName}" />

                    <TabControl Grid.Row="1" Margin="0,5,0,0">
                        <TabItem Background="Transparent" Header="{extensions:Translate Members}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto" />
                                    <RowDefinition />
                                    <RowDefinition Height="auto" />
                                </Grid.RowDefinitions>

                                <Button
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    Command="{Binding ExecuteItemCommand}"
                                    CommandParameter="AddMember"
                                    Content="{extensions:Translate AddMember}"
                                    Style="{StaticResource SimpleButton}" />

                                <sync:SfDataGrid
                                    Grid.Row="1"
                                    ItemsSource="{Binding memberdataPager.GridModelList}"
                                    Style="{StaticResource SimpleDataGridStyle}">
                                    <sync:SfDataGrid.Columns>
                                        <sync:GridTemplateColumn
                                            Width="60"
                                            AllowFiltering="False"
                                            AllowSorting="False">
                                            <sync:GridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button
                                                        Padding="8"
                                                        Command="{Binding DataContext.DeleteMemberCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=sync:SfDataGrid}}"
                                                        CommandParameter="{Binding}"
                                                        Content="&#xe65c;"
                                                        Style="{StaticResource IconButtonStyle}" />
                                                </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 HeaderText="{extensions:Translate EmailAddress}" MappingName="EmailAddress" />
                                        <sync:GridTextColumn
                                            DisplayBinding="{Binding Path=AddedTime, StringFormat='yyyy-MM-dd'}"
                                            HeaderText="{extensions:Translate AddedTime}"
                                            MappingName="AddedTime" />
                                    </sync:SfDataGrid.Columns>
                                </sync:SfDataGrid>

                                <sync:SfDataPager
                                    Grid.Row="2"
                                    HorizontalAlignment="Right"
                                    NumericButtonCount="{Binding memberdataPager.NumericButtonCount}"
                                    PageCount="{Binding memberdataPager.PageCount, Mode=TwoWay}"
                                    PageSize="{Binding memberdataPager.PageSize, Mode=TwoWay}"
                                    UseOnDemandPaging="True"
                                    PageIndex="{Binding memberdataPager.PageIndex, Mode=TwoWay}" />
                            </Grid>
                        </TabItem>

                        <TabItem Background="Transparent" Header="{extensions:Translate Role}">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto" />
                                    <RowDefinition />
                                    <RowDefinition Height="auto" />
                                </Grid.RowDefinitions>

                                <Button
                                    Margin="5"
                                    HorizontalAlignment="Right"
                                    Command="{Binding ExecuteItemCommand}"
                                    CommandParameter="AddRole"
                                    Content="{extensions:Translate CreateNewRole}"
                                    Style="{StaticResource SimpleButton}" />

                                <sync:SfDataGrid
                                    Grid.Row="1"
                                    ItemsSource="{Binding roledataPager.GridModelList}"
                                    Style="{StaticResource SimpleDataGridStyle}">
                                    <sync:SfDataGrid.Columns>
                                        <sync:GridTemplateColumn
                                            Width="60"
                                            AllowFiltering="False"
                                            AllowSorting="False">
                                            <sync:GridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <Button
                                                        Padding="8"
                                                        Command="{Binding DataContext.DeleteRoleCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=sync:SfDataGrid}}"
                                                        CommandParameter="{Binding}"
                                                        Content="&#xe65c;"
                                                        Style="{StaticResource IconButtonStyle}" />
                                                </DataTemplate>
                                            </sync:GridTemplateColumn.CellTemplate>
                                        </sync:GridTemplateColumn>
                                        <sync:GridTextColumn HeaderText="{extensions:Translate DisplayName}" MappingName="DisplayName" />
                                        <sync:GridTextColumn
                                            DisplayBinding="{Binding Path=AddedTime, StringFormat='yyyy-MM-dd'}"
                                            HeaderText="{extensions:Translate AddedTime}"
                                            MappingName="AddedTime" />
                                    </sync:SfDataGrid.Columns>
                                </sync:SfDataGrid>

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