@extends('admin.layout')

@section('title', 'Modifica Ruolo')
@section('page_title', 'Modifica Ruolo: ' . $ruolo->name)

@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="/admin/ruoli">Admin</a></li>
    <li class="breadcrumb-item"><a href="/admin/ruoli">Ruoli</a></li>
    <li class="breadcrumb-item active">{{ $ruolo->name }}</li>
@endsection

@section('content')
<div class="row">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Modifica Ruolo: {{ $ruolo->name }}</h3>
                @if($usersCount > 0)
                    <span class="badge badge-info ml-2">{{ $usersCount }} utenti</span>
                @endif
            </div>
            <div class="card-body">
                @if(session('success'))
                    <div class="alert alert-success">{{ session('success') }}</div>
                @endif

                <form method="POST" action="{{ url('/admin/ruoli/' . $ruolo->id) }}">
                    @csrf @method('PUT')
                    <div class="form-group">
                        <label>Nome</label>
                        <input type="text" class="form-control" value="{{ $ruolo->name }}" disabled>
                        <small class="text-muted">Il nome del ruolo non può essere modificato</small>
                    </div>
                    <div class="form-group">
                        <label>Descrizione</label>
                        <input type="text" name="description" class="form-control" value="{{ old('description', $ruolo->description) }}">
                    </div>

                    <h5 class="mt-4">Permessi</h5>
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Modulo</th>
                                <th>Nessuno</th>
                                <th>Lettura</th>
                                <th>Completo</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach(['individui', 'gruppi', 'eventi', 'documenti', 'mailing', 'viste'] as $module)
                            <tr>
                                <td><strong>{{ ucfirst($module) }}</strong></td>
                                <td><input type="radio" name="permissions[{{ $module }}]" value="0" {{ ($ruolo->permissions[$module] ?? 0) == 0 ? 'checked' : '' }}></td>
                                <td><input type="radio" name="permissions[{{ $module }}]" value="1" {{ ($ruolo->permissions[$module] ?? 0) == 1 ? 'checked' : '' }}></td>
                                <td><input type="radio" name="permissions[{{ $module }}]" value="2" {{ ($ruolo->permissions[$module] ?? 0) == 2 ? 'checked' : '' }}></td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>

                    @if($usersCount > 0)
                    <div class="form-group mt-3">
                        <div class="custom-control custom-checkbox">
                            <input type="checkbox" class="custom-control-input" id="updateUsers" name="update_users" value="1">
                            <label class="custom-control-label" for="updateUsers">
                                Applica queste modifiche a tutti i {{ $usersCount }} utenti con questo ruolo
                            </label>
                        </div>
                    </div>
                    @endif

                    <div class="mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> Salva
                        </button>
                        <a href="/admin/ruoli" class="btn btn-secondary">Annulla</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection