@extends('layouts.app')

@section('title', __('messages.roles'))

@section('content')
<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="fw-bold mb-0">Role & Permission Management</h5>
        <span class="text-muted fs-xs">Define Custom Roles (e.g. Junior Cashier, Auditor) with Granular Module-Level Controls</span>
    </div>
    <div>
        <a href="{{ route('roles.create') }}" class="btn btn-sm btn-primary">
            <i class="bi bi-shield-plus me-1"></i> Create Custom Role
        </a>
    </div>
</div>

<div class="card-erp">
    <div class="table-responsive">
        <table class="table table-dense table-hover mb-0">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Role Name</th>
                    <th>Slug</th>
                    <th>Description</th>
                    <th>Assigned Users</th>
                    <th>Type</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                @foreach($roles as $role)
                    <tr>
                        <td>{{ $role->id }}</td>
                        <td class="fw-bold text-dark">{{ $role->name }}</td>
                        <td><code class="text-primary">{{ $role->slug }}</code></td>
                        <td>{{ $role->description ?? 'Custom ERP Role' }}</td>
                        <td><span class="badge bg-secondary">{{ $role->users_count }} Users</span></td>
                        <td>
                            @if($role->is_system)
                                <span class="badge bg-dark">System Default</span>
                            @else
                                <span class="badge bg-info text-dark">Custom Role</span>
                            @endif
                        </td>
                        <td class="text-end">
                            <a href="{{ route('roles.edit', $role->id) }}" class="btn btn-dense btn-outline-primary me-1">
                                <i class="bi bi-shield-check"></i> Edit Permissions
                            </a>
                            @if(!$role->is_system)
                                <form action="{{ route('roles.destroy', $role->id) }}" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this role?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-dense btn-outline-danger">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>
@endsection
