@extends('layouts.app')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">Roles</h4>
        <a href="{{ route('users.index') }}" class="btn btn-outline-secondary btn-sm">Users</a>
    </div>
    <div class="row g-4">
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">Add role</div>
                <div class="card-body">
                    <form method="POST" action="{{ route('roles.store') }}" class="row g-2">
                        @csrf
                        <div class="col-12">
                            <label class="form-label">Name <span class="text-danger">*</span></label>
                            <input name="name" class="form-control" required maxlength="100" value="{{ old('name') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Slug</label>
                            <input name="slug" class="form-control" maxlength="80" placeholder="auto from name if empty" value="{{ old('slug') }}">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Description</label>
                            <input name="description" class="form-control" maxlength="255" value="{{ old('description') }}">
                        </div>
                        <div class="col-12 form-check">
                            <input type="hidden" name="is_platform_admin" value="0">
                            <input class="form-check-input" type="checkbox" name="is_platform_admin" value="1" id="ipa" {{ old('is_platform_admin') ? 'checked' : '' }}>
                            <label class="form-check-label" for="ipa">Platform administrator (full access)</label>
                        </div>
                        <div class="col-12 form-check">
                            <input type="hidden" name="is_active" value="0">
                            <input class="form-check-input" type="checkbox" name="is_active" value="1" id="act" {{ old('is_active', true) ? 'checked' : '' }}>
                            <label class="form-check-label" for="act">Active</label>
                        </div>
                        <div class="col-12 d-grid">
                            <button class="btn btn-success">Save role</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <div class="col-lg-8">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">All roles</div>
                <div class="card-body p-0 table-responsive">
                    <table class="table table-sm mb-0">
                        <thead>
                        <tr>
                            <th>Name</th>
                            <th>Slug</th>
                            <th>Admin</th>
                            <th>Users</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($roles as $r)
                            <tr>
                                <td>{{ $r->name }}</td>
                                <td><code>{{ $r->slug }}</code></td>
                                <td>{{ $r->is_platform_admin ? 'Yes' : 'No' }}</td>
                                <td>{{ $r->users_count }}</td>
                                <td class="text-end">
                                    <a href="{{ route('roles.edit', $r) }}" class="btn btn-sm btn-outline-secondary">Edit</a>
                                    @if(!in_array($r->slug, ['admin', 'user'], true))
                                        <form method="POST" action="{{ route('roles.destroy', $r) }}" class="d-inline" onsubmit="return confirm('Delete this role?');">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="5" class="text-center text-muted py-3">No roles.</td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
