@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Users</div>

                <div class="card-body">
                    <form action="/find" method="POST" role="search">
                        {{ csrf_field() }}
                        <div class="input-group">
                            <input type="text" class="form-control" name="q"                              
                                placeholder="Search users"><span class="input-group-btn">
                            </span>
                            <button type="submit" class="btn btn-default">
                                <span class="glyphicon glyphicon-search">Search</span>
                            </button>
                        </div>
                        <label class="checkbox-inline">
                            <input type="checkbox" name="roles[]" value="root"> Root 
                        </label>
                        <label class="checkbox-inline">
                            <input type="checkbox" name="roles[]" value="admin"> Admin 
                        </label>
                        <label class="checkbox-inline">
                            <input type="checkbox" name="roles[]" value="user"> User 
                        </label>
                    </form>
                    <table class="table">
                        <thead>
                          <tr>
                            <th scope="col">ID</th>
                            <th scope="col">Name</th>
                            <th scope="col">Surname</th>
                            <th scope="col">Email</th>
                            <th scope="col">Role</th>
                            <th scope="col">Action</th>
                          </tr>
                        </thead>
                        <tbody>
                        @forelse ($users as $user)
                            <tr>
                                <td>{{ $user->id }}</td>
                                <td>{{ $user->name }}</td>
                                <td>{{ $user->surname }}</td>                                 
                                <td>{{ $user->email }}</td>  
                                <td>{{ implode(',',$user->roles()->get()->pluck('name')->toArray()) }}</td>
                                <td>
                                @if (in_array('admin',$user->roles()->get()->pluck('name')->toArray()))
                                    @if (!in_array('root',$user->roles()->get()->pluck('name')->toArray()))
                                        <form action="/users/{{ $user->id }}/demote" method="GET">
                                            @csrf
                                            <button type="submit" class="btn btn-secondary float-left">Demote</button>
                                        </form>
                                    @endif
                                @else
                                    <form action="/users/{{ $user->id }}/promote" method="GET">
                                        @csrf
                                        <button type="submit" class="btn btn-primary float-left">Promote</button>
                                    </form>
                                @endif    
                            </td>                                                                         
                            </tr>
                        @empty
                            <tr>
                                <td>No Users available</td>                                
                            </tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
