@extends('adminlte::page')

@section('title', config('app.name') . ' | ' . __('Account Management'))

@section('content_header')

    <h4>{{ __('Users / Accounts / :username / Manage', ['username' => $user->name]) }}</h4>

@stop

@section('js')
    <script src="/js/app.js"></script>
    <x-global-errors></x-global-errors>
@stop

@section('content')

    @if($user->has2FA())

        <x-modal id="resetTwoFactorModal" modal-label="resetTwoFactor" modal-title="{{ __('Verify your identity') }}" include-close-button="true">

            <p>{{ __('Resetting an account\'s two-factor authentication secret will automatically notify the account holder. Additionally, the user\'s password will also be forcefully reset during this process. Please confirm this action by verifying your identity below.') }}</p>

            <form id="resetAccountTwofaForm" method="POST" action="{{ route('reset-twofa', ['user' => $user]) }}">
                @csrf
                @method('PATCH')

                <x-confirm-password>
                    {{ __('Please re-enter your password.') }}
                </x-confirm-password>

                <x-confirm-second-factor>
                    {{ __('Please enter your two-factor authentication code.') }}
                </x-confirm-second-factor>

            </form>

            <x-slot:modalFooter>
                <button onclick="$('#resetAccountTwofaForm').submit()" type="button" class="btn btn-warning"><i class="fas fa-check"></i> {{ __('Re-authenticate and verify') }}</button>
            </x-slot:modalFooter>

        </x-modal>

    @endif

    @if($user->hasPassword())
        <x-modal id="resetAccountPasswordModal" modal-label="resetAccountPassword" modal-title="{{ __('Verify your identity') }}" include-close-button="true">

            <p>{{ __('Forcing a password reset will automatically notify the account holder and send them a password reset link. Please confirm this action by verifying your identity below.') }}</p>

            <form id="resetAccountPasswordForm" method="POST" action="{{ route('force-reset-user', ['user' => $user]) }}">
                @csrf
                @method('patch')

                <x-confirm-password>
                    {{ __('Re-entering your password is required to confirm sensitive administration actions.') }}
                </x-confirm-password>

                <x-confirm-second-factor>
                    {{ __('Two-factor authentication is required to confirm sensitive administration actions.') }}
                </x-confirm-second-factor>

            </form>

            <x-slot name="modalFooter">
                <button onclick="$('#resetAccountPasswordForm').submit()" type="button" class="btn btn-warning"><i class="fas fa-check"></i> {{ __('Re-authenticate and verify') }}</button>
            </x-slot>

        </x-modal>
    @endif

    <x-modal id="banAccountModal" modal-label="banAccount" modal-title="{{__('Please confirm')}}" include-close-button="true">

        <p>{{__("Please confirm that you want to suspend this account. You'll need to add a reason and expiration date to confirm this.")}}</p>

        <form id="banAccountForm" name="banAccount" method="POST" action="{{route('banUser', ['user' => $user->id])}}">
            @csrf

            @if($demoActive)
                <div class="alert alert-danger">
                    <p class="font-weight-bold"><i class="fas fa-exclamation-triangle"></i> {{ __('This feature is disabled') }}</p>
                </div>
            @endif

            <div class="row">

                <div class="col">
                    <label for="reason">{{__('Reason')}}</label>
                    <input type="text" name="reason" id="reason" class="form-control" placeholder="{{__('e.g. Spamming')}}">
                </div>

                <div class="col">
                    <label for="duration">{{ __('Duration') }}</label>
                    <input type="text" name="duration" id="duration" class="form-control" placeholder="{{ __('in days') }}">
                </div>
            </div>


            <div class="mt-2">
                <input type="hidden" name="suspensionType" value="off">

                <label for="suspensionType">{{ __('Suspension type') }}</label><br>
                <input type="checkbox" id="suspensionType" name="suspensionType" checked data-toggle="toggle" data-on="Temporary" data-off="Permanent" data-onstyle="success" data-offstyle="danger" data-width="130" data-height="40">
                <p class="text-muted text-sm"><i class="fas fa-info-circle"></i> {{ __('Temporary suspensions will be automatically lifted. The suspension note is visible to all users. Suspended users will not be able to login or register.') }}</p>
            </div>


        </form>

        <x-slot name="modalFooter">
            <button onclick="$('#banAccountForm').submit()" id="banAccountButton" type="button" class="btn btn-danger" {{ ($demoActive) ? 'disabled' : '' }} ><i class="fa fa-gavel"></i> {{__('Confirm')}}</button>
        </x-slot>

    </x-modal>

    @if (!Auth::user()->is($user) && $user->isStaffMember())
        <x-modal id="terminateUser" modal-label="terminateUser" modal-title="{{__('Please Confirm')}}" include-close-button="true">

            @if($demoActive)
                <div class="alert alert-danger">
                    <p class="font-weight-bold"><i class="fas fa-exclamation-triangle"></i> {{ __('This feature is disabled') }}</p>
                </div>
            @endif

            <p><i class="fa fa-exclamation-triangle"></i> <b>{{__('You are about to terminate a recruited staff member')}}</b></p>
            <p>
                {{__('Terminating a staff member will remove their privileges on the application management site and connected integrations configured for the vacancy they applied for.')}}
            </p>
            <p>
                <b>{{__('THIS PROCESS IS IRREVERSIBLE AND IMMEDIATE')}}</b>
            </p>


            <x-slot name="modalFooter">

                <form method="POST" action="{{route('terminateStaffMember', ['user' => $user->id])}}" id="terminateUserForm">
                    @csrf
                    @method('PATCH')
                    <button type="submit" class="btn btn-warning" {{ ($demoActive) ? 'disabled' : '' }}><i class="fas fa-exclamation-circle"></i> {{__('Confirm')}}</button>

                </form>

            </x-slot>

        </x-modal>
    @endif

    <x-modal id="deleteAccount" modal-label="deleteAccount" modal-title="{{__('Confirm')}}" include-close-button="true">

        @if($demoActive)
            <div class="alert alert-danger">
                <p class="font-weight-bold"><i class="fas fa-exclamation-triangle"></i> {{ __('This feature is disabled') }}</p>
            </div>
        @endif

        <p><i class="fa fa-exclamation-triangle"></i><b> {{__('WARNING: This is a potentially destructive action!')}}</b></p>

        <p>{{__("Deleting a user's account is an irreversible process. Historic and current applications, votes, and profile content, as well as any personally identifiable information will be immediately erased.")}}</p>

        <form id="deleteAccountForm" method="POST" action={{route('deleteUser', ['user' => $user->id])}}>

            @csrf
            @method('DELETE')

            <label for="promptConfirm">{{__('Type to confirm: ')}} "DELETE ACCOUNT"</label>
            <input type="text" name="confirmPrompt" class="form-control" placeholder="{{__('Please type the above text')}}">

        </form>

        <x-slot name="modalFooter">

            <button type="button" class="btn btn-danger" {{ ($demoActive) ? 'disabled' : '' }} onclick="document.getElementById('deleteAccountForm').submit()"><i class="fa fa-trash"></i> {{strtoupper(__('Confirm'))}}</button>

        </x-slot>
    </x-modal>


    <x-modal id="editUser" modal-label="editUser" modal-title="{{__('Edit account')}}" include-close-button="true">

        @if($demoActive)
            <div class="alert alert-danger">
                <p class="font-weight-bold"><i class="fas fa-exclamation-triangle"></i> {{ __('This feature is disabled') }}</p>
            </div>
        @endif

        <form id="updateUserForm" method="post" action="{{ route('updateUser', ['user' => $user->id]) }}">
            @csrf
            @method('PATCH')

            <label for="email">{{__('Email')}}</label>
            <input {{ ($demoActive) ? 'disabled' : '' }} id="email" type="text" name="email" class="form-control" required value="{{ $user->email }}" />

            <label for="name">{{__('Name')}}</label>
            <input {{ ($demoActive) ? 'disabled' : '' }} id="name" type="text" name="name" class="form-control" required value="{{ $user->name }}" />

            @if ($requireLicense)
                <label for="uuid">{{ __('Mojang UUID (deprecated)') }}</label>
                <input {{ ($demoActive) ? 'disabled' : '' }} id="uuid" type="text" name="uuid" class="form-control" required value="{{ $user->uuid ?? "disabled" }}" />
                <p class="text-muted text-sm">
                    <i class="fas fa-exclamation-triangle"></i> {{__('If the setting "Require Valid Game License" is activated, editing this field may have unintended consequences. Proceed with caution.')}}
                </p>
            @endif

            <div class="form-group mt-3">

                <label>{{__('Roles')}}</label>
                <table class="table table-borderless">
                    <tbody>

                    @foreach($roles as $roleName => $status)
                        <tr>
                            <th><input {{ ($demoActive) ? 'disabled' : '' }} type="checkbox" name="roles[]" value="{{ $roleName }}" {{ ($status) ? 'checked' : '' }}></th>
                            <td class="col-md-2">{{ ucfirst($roleName) }}</td>
                        </tr>

                    @endforeach

                    </tbody>


                </table>

            </div>

        </form>

        <x-slot name="modalFooter">

            <button type="button" {{ ($demoActive) ? 'disabled' : '' }} class="btn btn-warning" onclick="$('#updateUserForm').submit()"><i class="fa fa-exclamation-cicle"></i> {{__('Save changes')}}</button>

        </x-slot>

    </x-modal>


    <div class="row">

        <div class="col">

            <div class="card">

                <div class="card-header">
                    <h3><i class="fas fa-user-alt"></i>  {{ __('Personal details') }}</h3>
                </div>

                <div class="card-body">
                    <div class="row">

                        <div class="col">
                            <label for="uname">{{ __('Account name') }}</label>
                            <input id="uname" type="text" class="form-control" value="{{ $user->name }}" disabled>
                        </div>

                        <div class="col">
                            <label for="uemail">{{ __('Account email')  }}</label>
                            <input id="uemail" type="email" class="form-control" value="{{ $user->email }}" disabled>
                        </div>

                    </div>

                    <div class="row">
                        <div class="col">
                            <label for="uip">{{ __('Current IP') }}</label>
                            <input type="text" class="form-control" id="uip" value="{{ $user->currentIp }}" disabled>
                        </div>
                        <div class="col">
                            <label for="uip">{{ __('Registration IP') }}</label>
                            <input type="text" class="form-control" id="uip" value="{{ (empty($user->registrationIp)) ? __('Unavailable') : $user->registrationIp }}" disabled>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col">
                            <label for="uage">{{ __(' Age') }}</label>
                            <input id="uage" class="form-control" type="text" value="{{ (is_null($user->dob)) ? __('Age not provided yet') : __(':age years old', ['age' => \Carbon\Carbon::parse($user->dob)->age]) }}" disabled>
                        </div>

                        <div class="col">
                            <label for="uregdate">{{ __(' Registered at') }}</label>
                            <input id="uregdate" class="form-control" type="text" value="{{ $user->created_at }} ({{ $user->created_at->diffForHumans() }})" disabled>
                        </div>
                    </div>

                    <div class="mt-2 mb-2">
                        <label>{{ __('Account indicators') }}</label>
                        <x-account-status has-password="{{ $hasPassword }}" has-discord="{{ $hasDiscord }}" is-verified="{{ $isVerified }}" is-locked="{{ $isLocked }}" is-suspended="{{ $isSuspended }}" has2-f-a="{{ $has2FA }}" />
                    </div>

                    @if($user->hasDiscordConnection())
                        <div class="mt-2">
                            <h5><i class="fas fa-check-circle text-success"></i> {!! __('Connected to Discord as  <i style="color: #5865F2" class="fab fa-discord"></i> :tag', ['tag' => $user->username]) !!}</h5>
                            <p class="text-muted">{{ __('User ID: :discordUserID', ['discordUserID' => $user->discord_user_id]) }}</p>
                        </div>
                    @endif

                </div>

                <div class="card-footer">
                    <button onclick="$('#editUser').modal('show')" type="button" class="btn btn-warning"><i class="fas fa-pencil-alt"></i> {{ __('Edit account details') }}</button>
                </div>
            </div>

        </div>

    </div>

    <div class="row mt-5">
        <div class="col">

            <div class="card">
                <div class="card-header">
                    <h3><i class="fas fa-clipboard"></i> {{ __('Application history') }}</h3>
                </div>

                <div class="card-body">
                    @if (!$applications->isEmpty())

                        <table class="table table-borderless" style="whitespace: nowrap">

                            <thead>

                            <tr>
                                <th>#</th>
                                <th>{{__('Status')}}</th>
                                <th>{{ __('Vacancy') }}</th>
                                <th>{{__('Date')}}</th>
                                <th>{{__('Actions')}}</th>
                            </tr>

                            </thead>

                            <tbody>

                            @foreach($applications as $application)

                                <tr>
                                    <td>{{ $application->id }}</td>
                                    <td>
                                        @switch($application->applicationStatus)

                                            @case('STAGE_SUBMITTED')

                                                <span class="badge badge-primary"><i class="far fa-clock"></i> {{__('Outstanding (Submitted)')}}</span>
                                                @break

                                            @case('STAGE_PEERAPPROVAL')

                                                <span class="badge badge-warning"><i class="fas fa-vote-yea"></i> {{__('Peer Review')}}</span>
                                                @break

                                            @case('STAGE_INTERVIEW')

                                                <span class="badge badge-warning"><i class="fas fa-microphone-alt"></i> {{__('Interview')}}</span>

                                                @break

                                            @case('STAGE_INTERVIEW_SCHEDULED')

                                                <span class="badge badge-warning"><i class="far fa-clock"></i>{{__('Interview Scheduled')}}</span>

                                                @break

                                            @case('APPROVED')

                                                <span class="badge badge-success"><i class="fas fa-check"></i> {{__('Approved')}}</span>

                                                @break

                                            @case('DENIED')

                                                <span class="badge badge-danger"><i class="fas fa-times"></i> {{__('Denied')}}</span>

                                                @break;

                                            @default
                                                <span class="badge badge-secondary"><i class="fas fa-question-circle"></i> {{__('Denied')}}</span>

                                        @endswitch
                                    </td>
                                    <td>{{ $application->response->vacancy->vacancyName }}</td>
                                    <td>{{ $application->created_at }} ({{ $application->created_at->diffForHumans() }})</td>
                                    <td>
                                        <x-button id="view" link="{{ route('showUserApp', ['application' => $application->id]) }}" icon="fas fa-eye" target="_blank" color="success" size="sm">
                                            {{ __('View') }}
                                        </x-button>
                                    </td>
                                </tr>

                            @endforeach

                            </tbody>

                        </table>
                    @else
                        <x-alert alert-type="warning" icon="fas fa-exclamation-triangle" title="{{ __('No applications') }}">
                            {{ __('This user has not submitted any applications yet.') }}
                        </x-alert>
                    @endif
                </div>
            </div>
        </div>
    </div>


    <div class="row mt-5 mb-5">
        <div class="col">

            <div class="card">
                <div class="card-header">
                    <h3><i class="fas fa-shield-alt"></i> {{ __('Admin actions') }}</h3>
                </div>

                <div class="card-body">

                    @if ($isSuspended)
                        <x-alert extra-styling="mb-4" alert-type="danger" icon="fa fa-exclamation-triangle" title="{{ __('Account suspended') }}" >
                            <p><i class="fas fa-arrow-circle-right"></i>&nbsp;{{ __('This account has been :suspensionType suspended.', ['suspensionType' => (is_null($suspensionDuration)) ? __('permanently') : __('temporarily') ]) }}</p>
                            <p><i class="fas fa-arrow-circle-right"></i>&nbsp;{!! __('<b>Reason: </b>:reason', ['reason' => $suspensionReason]) !!}</p>
                            @if (!is_null($suspensionDuration))
                                <p><i class="fas fa-arrow-circle-right"></i>&nbsp;{!! __('<b>Suspension expires: </b>:duration', ['duration' => $suspensionDuration]) !!}</p>
                            @endif
                        </x-alert>
                    @endif

                        @if (!$isSuspended)
                            <button onclick="$('#banAccountModal').modal('show')" type="button" class="btn btn-warning mr-3"><i class="fas fa-user-slash"></i> {{ __('Suspend account') }}</button>
                        @else
                            <form class="d-inline" method="post" action="{{ route('unbanUser', ['user' => $user]) }}">
                                @csrf
                                @method('DELETE')

                                <button type="submit" class="btn btn-success mr-2"><i class="fas fa-user"></i> {{ __('Unsuspend account') }}</button>
                            </form>
                        @endif

                        @if($user->hasPassword())
                            <button onclick="$('#resetAccountPasswordModal').modal('show')" class="btn-danger btn mr-3" type="button"><i class="fas fa-key"></i> {{ __('Force password reset') }}</button>
                        @else
                            <button disabled class="btn-danger btn mr-3" type="button"><i class="fas fa-key"></i> {{ __('Force password reset') }}</button>
                        @endif

                        @if($user->has2FA())
                            <button onclick="$('#resetTwoFactorModal').modal('show')" class="btn-danger btn mr-3" type="button"><i class="fas fa-unlock"></i> {{ __('Reset MFA') }}</button>
                        @endif
                        <button onclick="$('#deleteAccount').modal('show')" type="button" class="btn btn-danger"><i class="fas fa-trash"></i> {{ __('Delete account') }}</button>
                </div>
            </div>
        </div>
    </div>

@stop


@section('footer')
    @include('breadcrumbs.dashboard.footer')
@stop
