@php
    /** @var LaraBB\User\Models\User $user */
    /** @var LaraBB\Group\Models\Group $group */
@endphp
@extends('admin::Layouts.master')
@section('pagecss')
<link rel="stylesheet" type="text/css" href="{{ asset('css/bootstrap-select.min.css') }}">
@stop
@section('content')
<div class="container-fluid pt-4">
    @include('partials.messages')
    <ul class="nav nav-tabs mb-3">
        <li class="nav-item">
            <a class="nav-link{{ $subactive == 'user' ? ' active' : '' }}"
               href="{{ route('admin.users.edit', [$user->uuid ?? '#']) }}">
                <span class="fas fa-fw fa-sm fa-user"></span> Benutzer
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link{{ $subactive == 'profile' ? ' active' : '' }}{{ !isset($user) ? ' disabled' : '' }}"
               href="{{ route('admin.users.profile', [$user->uuid ?? '#']) }}">
                <span class="fas fa-fw fa-sm fa-address-card"></span> Profil
            </a>
        </li>
        <li class="nav-item">
            <a class="nav-link{{ $subactive == 'invites' ? ' active' : '' }}{{ !isset($user) ? ' disabled' : '' }}"
               href="#">
                <span class="fas fa-fw fa-sm fa-ticket-alt"></span> Einladungen
            </a>
        </li>
    </ul>
    <form action="{{ route('admin.users.' . (isset($user) ? 'update' : 'store')) }}" method="post">
        @csrf
        <input type="hidden" name="uuid" value="{{ $user->uuid ?? '' }}">
        <div class="row align-items-center mb-3">
            <div class="col-12 col-sm-4">
                <label for="username" class="required">Benutzername:</label>
                <input class="form-control @error('username')is-invalid @enderror" name="username" id="username" value="{{ old('username', $user->username ?? '') }}">
            </div>
            <div class="col-12 col-sm-4">
                <label for="email" class="required">E-Mail Adresse:</label>
                <input class="form-control @error('email')is-invalid @enderror" name="email" id="email" value="{{ old('email', $user->email_encrypted ?? '') }}">
            </div>
            <div class="col-12 col-sm-4">
                <label for="groups" class="required">Benutzergruppen:</label>
                <select id="groups" name="groups[]" class="selectpicker form-control @error('groups')is-invalid @enderror" data-live-search="true" title="Auswählen..." multiple>
                @foreach($groups as $group)
                    <option data-tokens="{{ Str::slug($group->group) }}"
                            value="{{ $group->uuid }}"
                            {{ isset($user) && $user->groups->contains('uuid', $group->uuid) ? ' selected' : (in_array($group->uuid, old('groups', [])) ? ' selected' : '') }}>
                        {{ $group->group }}
                    </option>
                @endforeach
                </select>
            </div>
        </div>
        <div class="row align-items-center mb-3">
            <div class="col-12 col-sm-6">
                <label for="password"{!! !isset($user) ? ' class="required"' : '' !!}>{{ isset($user) ? 'Neues ' : '' }}Passwort:</label>
                <div class="input-group">
                    <input type="password" class="form-control @error('password')is-invalid @enderror" name="password" id="password">
                    <button type="button" class="btn btn-outline-secondary js-show-password"><span class="far fa-fw fa-eye"></span></button>
                    <button type="button" class="btn btn-outline-secondary js-generate-password"><span class="fas fa-fw fa-sync"></span></button>
                </div>
            </div>
            <div class="col-12 col-sm-6">
                <label for="password_confirmation"{!! !isset($user) ? ' class="required"' : '' !!}>{{ isset($user) ? 'Neues ' : '' }}Passwort bestätigen:</label>
                <input type="password" class="form-control @error('password')is-invalid @enderror" name="password_confirmation" id="password_confirmation">
            </div>
        </div>
        <div class="row align-items-end mb-3">
            <div class="col-12 col-sm-6">
                @if(!isset($user))
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="send-credentials" value="1" id="send-credentials">
                    <label class="form-check-label" for="send-credentials">Zugangsdaten nach Erstellen versenden?</label>
                </div>
                @endif
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="force-password-change" value="1" id="force-password-change"{{ isset($user) && $user->passwordchange_required ? ' checked' : '' }}>
                    <label class="form-check-label" for="force-password-change">Passwortänderung bei Anmeldung erforderlich?</label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="account-disabled" value="1" id="account-disabled"{{ isset($user) && $user->deactivated_at ? ' checked' : '' }}>
                    <label class="form-check-label" for="account-disabled">Zugang deaktiviert?</label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-12 col-md-2 col-xl-1">
                @include('partials.button', ['context' => 'primary', 'type' => 'submit', 'icon' => 'save', 'label' => 'Speichern'])
            </div>
        </div>
    </form>
</div>
@stop
@section('pagejs')
<script src="{{ asset('js/bootstrap-select.min.js') }}"></script>
<script>
    $('.js-show-password').on('click', function() {
        $(this).parents('.row').find('input.form-control').each(function($index, $element) {
            $($element).attr('type', function($index, $attribute) {
                return $attribute === 'text' ? 'password' : 'text';
            });
        });

        $(this).find('.far').toggleClass('fa-eye fa-eye-slash');
    });

    $('.js-generate-password').on('click', function() {
        let $this = $(this);
        $.get('{{ route('random-string') }}', function($result) {
            if($result.length > 0) {
                $this.parents('.row').find('input.form-control').each(function($index, $element) {
                    $($element).val($result.string);
                });
            }
        });
    });
</script>
@stop
