{{-- Вьюха: Список пользователей и их ролей --}}
@extends('admin.layout')

@section('title', 'Пользователи')

@section('content_header')
    <div class="d-flex justify-content-between align-items-center">
        <h1 class="m-0">Пользователи</h1>
        @can('users.manage')
            <a href="{{ route('admin.users.create') }}" class="btn btn-success btn-sm">
                <i class="fas fa-user-plus"></i> Создать пользователя
            </a>
        @endcan
    </div>
@stop

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Главная</a></li>
    <li class="breadcrumb-item active">Пользователи</li>
@stop

@section('content')
    @if(session('success'))
        <div class="alert alert-success alert-dismissible">
            <button type="button" class="close" data-dismiss="alert">&times;</button>
            {{ session('success') }}
        </div>
    @endif

    <div class="card card-primary card-outline">
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Имя</th>
                        <th>Email</th>
                        <th>Роль</th>
                        <th>Инд. права</th>
                        <th class="text-right">Действия</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($users as $user)
                        @php $badge = $user->roleBadge(); @endphp
                        <tr>
                            <td class="text-muted">{{ $user->id }}</td>
                            <td>
                                <strong>{{ $user->name }}</strong>
                                @if($user->id === auth()->id())
                                    <span class="badge badge-light ml-1">вы</span>
                                @endif
                            </td>
                            <td>{{ $user->email }}</td>
                            <td>
                                @if($user->role)
                                    <span class="badge badge-{{ $badge['color'] }}">{{ $badge['label'] }}</span>
                                @else
                                    <span class="text-muted">—</span>
                                @endif
                            </td>
                            <td>
                                @if($user->userPermissions->count())
                                    <span class="badge badge-info">{{ $user->userPermissions->count() }} overrides</span>
                                @else
                                    <span class="text-muted">нет</span>
                                @endif
                            </td>
                            <td class="text-right">
                                @can('users.manage')
                                    @if(auth()->user()->canManage($user))
                                        <a href="{{ route('admin.users.edit', $user) }}" class="btn btn-sm btn-primary">
                                            <i class="fas fa-user-cog"></i> Права
                                        </a>
                                        @if($user->id !== auth()->id())
                                        <form action="{{ route('admin.users.destroy', $user) }}" method="POST" class="d-inline"
                                              onsubmit="return confirm('Удалить пользователя «{{ $user->name }}»?')">
                                            @csrf @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-danger"><i class="fas fa-trash"></i></button>
                                        </form>
                                        @endif
                                    @endif
                                @endcan
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@stop
