<?xml version="1.0" encoding="utf-8"?>
<Page
    x:Class="Sonex.Client.Pages.Administrator.Accounts.Accounts"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Layouts="using:Sonex.Client.Layouts"
    xmlns:controls="using:Sonex.Client.Controls"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    mc:Ignorable="d">

    <Layouts:ItemsAndContentLayout
        x:Name="_Layout"
        Loaded="Layout_Loaded"
        Background="{ThemeResource SolidBackgroundFillColorSenaryBrush}"
        ItemsSource="{x:Bind AccountItems, Mode=OneWay}"
        SelectedItemChanged="Layout_SelectedItemChanged"
        AddClicked="Layout_AddClicked"
        DeleteClicked="Layout_DeleteClicked"
        SearchPlaceholder="Search..."
        ShowPicture="True"
        PictureSize="48"
        ShowSubtitle="True"
        ShowDescription="True"
        ShowDeleteButton="True"
        ShowAddButton="True"
        AddToolTip="Create New Account"
        DeleteToolTip="Delete Account"
        SortToolTip="Sort"
        SortBy="Title"
        SortByTitleMenuText="Full name"
        SortBySubtitleMenuText="Department"
        SortByDescriptionMenuText="User name"
        SortByIsEnabledMenuText="Is enabled"
        >
        <Layouts:ItemsAndContentLayout.DetailContent>
            <ScrollViewer VerticalScrollBarVisibility="Auto" Padding="8 18 8 8" Visibility="{x:Bind isSelectedAccount}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>

                    <Grid Grid.Row="0" Margin="0,0,0,0" MaxWidth="1000">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <PersonPicture Grid.Column="0"
                                       DisplayName="{x:Bind SelectedAccount.FullName}"
                                       VerticalAlignment="Center"
                                       Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
                                       Margin="0,18,18,18" />

                        <StackPanel Grid.Column="1" Spacing="2" VerticalAlignment="Center" Padding="8">
                            <TextBlock VerticalAlignment="Center"
                                       Text="{x:Bind SelectedAccount.FullName}"
                                       Style="{ThemeResource BodyLargeTextBlockStyle}"
                                       Foreground="{ThemeResource TextFillColorPrimaryBrush}" />

                            <TextBlock VerticalAlignment="Center"
                                       Style="{ThemeResource BodyTextBlockStyle}"
                                       Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                       TextWrapping="Wrap">
                                <Run Text="Account: " />
                                <Run Text="{x:Bind SelectedAccount.AccountName}" />
                            </TextBlock>

                            <TextBlock VerticalAlignment="Center"
                                       Style="{ThemeResource BodyTextBlockStyle}"
                                       Foreground="{ThemeResource TextFillColorSecondaryBrush}"
                                       TextWrapping="Wrap">
                                <Run Text="Department: " />
                                <Run Text="{x:Bind SelectedAccount.Department}" />
                            </TextBlock>
                        </StackPanel>
                    </Grid>

                    <controls:PropertyBox Grid.Row="1"
                                          Title="Account password"
                                          Description="Change the password for this user. Existing sessions won’t be affected. The new password will apply to the next session.">
                        <controls:PropertyBox.Icon>
                            <FontIcon Glyph="&#xE8D7;" />
                        </controls:PropertyBox.Icon>
                        <Button Style="{ThemeResource AccentButtonStyle}" Click="ChangePassword_Click">Change password</Button>
                    </controls:PropertyBox>

                    <controls:PropertyBox Grid.Row="2"
                                          Title="Login access"
                                          Description="Controls whether this account can log in and start new sessions. Existing sessions are not affected.">
                        <controls:PropertyBox.Icon>
                            <FontIcon Glyph="&#xE8F8;" />
                        </controls:PropertyBox.Icon>
                        <ToggleSwitch IsOn="{x:Bind SelectedAccount.IsEnabled, Mode=TwoWay}"
                                      HorizontalAlignment="Right"
                                      MinWidth="0"
                                      MinHeight="0"
                                      OnContent="{x:Null}"
                                      OffContent="{x:Null}"
                                      Toggled="LoginAccess_Toggled" />
                    </controls:PropertyBox>

                    <controls:PropertyBoxItemsExpander Grid.Row="3"
                                                       Title="Personal Information"
                                                       Description="Edit personal details associated with this account.">
                        <controls:PropertyBoxItemsExpander.Icon>
                            <FontIcon Glyph="&#xE77B;" />
                        </controls:PropertyBoxItemsExpander.Icon>
                        <controls:PropertyBoxItemsExpander.Footer>
                            <Button HorizontalAlignment="Right"
                                    Style="{ThemeResource AccentButtonStyle}"
                                    Click="SavePersonalInformation_Click">Save</Button>
                        </controls:PropertyBoxItemsExpander.Footer>

                        <TextBox MinWidth="300" MaxWidth="400"
                                 HorizontalAlignment="Stretch"
                                 Header="First name"
                                 PlaceholderText="First name"
                                 Text="{x:Bind SelectedAccount.FirstName, Mode=TwoWay}" />

                        <TextBox MinWidth="300" MaxWidth="400"
                                 HorizontalAlignment="Stretch"
                                 Header="Last name"
                                 PlaceholderText="Last name"
                                 Text="{x:Bind SelectedAccount.LastName, Mode=TwoWay}" />

                        <AutoSuggestBox MinWidth="300" MaxWidth="400"
                                        HorizontalAlignment="Stretch"
                                        Header="Department"
                                        PlaceholderText="Department"
                                        Text="{x:Bind SelectedAccount.Department, Mode=TwoWay}" />
                    </controls:PropertyBoxItemsExpander>

                    <controls:PropertyBoxItemsExpander Grid.Row="4"
                                                       Title="Linked Accounts"
                                                       Description="Edit account links between this user and other systems.">
                        <controls:PropertyBoxItemsExpander.Icon>
                            <FontIcon Glyph="&#xE71B;" />
                        </controls:PropertyBoxItemsExpander.Icon>
                        <controls:PropertyBoxItemsExpander.Footer>
                            <Button HorizontalAlignment="Right"
                                    Style="{ThemeResource AccentButtonStyle}"
                                    Click="SaveLinkedAccounts_Click">Save</Button>
                        </controls:PropertyBoxItemsExpander.Footer>

                        <TextBox MinWidth="300" MaxWidth="400"
                                 HorizontalAlignment="Stretch"
                                 Header="Locus CLI Client"
                                 PlaceholderText="Locus CLI Client"
                                 Text="{x:Bind SelectedAccount.AccountLocusWms, Mode=TwoWay}" />

                        <TextBox MinWidth="300" MaxWidth="400"
                                 HorizontalAlignment="Stretch"
                                 Header="Locus Terminal Client"
                                 PlaceholderText="Locus Terminal Client"
                                 Text="{x:Bind SelectedAccount.AccountLocusTerminal, Mode=TwoWay}" />
                    </controls:PropertyBoxItemsExpander>

                    <controls:PropertyBoxContentExpander Grid.Row="5"
                                                         Title="Permission Groups"
                                                         Description="Manage the permission groups assigned to this account.">
                        <controls:PropertyBoxContentExpander.Icon>
                            <FontIcon Glyph="&#xEA18;" />
                        </controls:PropertyBoxContentExpander.Icon>
                        <controls:PropertyBoxContentExpander.Body>
                            <controls:CheckBoxListControl x:Name="PermissionsControl"
                                                          Height="160"
                                                          ItemChecked="PermissionsControl_ItemChecked"
                                                          ItemUnchecked="PermissionsControl_ItemUnchecked" />
                        </controls:PropertyBoxContentExpander.Body>
                    </controls:PropertyBoxContentExpander>

                    <controls:PropertyBox Grid.Row="6"
                      Title="Delete account"
                      Description="Permanently deletes this account. The user will lose access to the application. Existing sessions may remain active until they end. This action cannot be undone.">
                        <controls:PropertyBox.Icon>
                            <FontIcon Glyph="&#xE74D;" />
                        </controls:PropertyBox.Icon>
                        <Button Style="{ThemeResource AccentButtonStyle}" Click="Layout_DeleteClicked">Delete account</Button>
                    </controls:PropertyBox>
                </Grid>
            </ScrollViewer>
        </Layouts:ItemsAndContentLayout.DetailContent>
    </Layouts:ItemsAndContentLayout>
</Page>
