@extends('layouts.app')

@section('content')
<div class="container mx-auto max-w-5xl px-4">
    @include('admin.panel._nav')

    <div class="mb-4 flex items-center justify-between">
        <h1 class="text-xl font-semibold text-gray-900">Role</h1>
        <a href="{{ route('admin.roles.create') }}" class="rounded bg-gray-900 px-3 py-2 text-sm text-white hover:bg-gray-700">
            Tambah Role
        </a>
    </div>

    <div class="overflow-x-auto rounded border border-gray-200">
        <table class="min-w-full text-sm">
            <thead class="bg-gray-50 text-left text-gray-600">
                <tr>
                    <th class="px-4 py-2">Nama</th>
                    <th class="px-4 py-2">Jumlah Permission</th>
                    <th class="px-4 py-2">Jumlah Pengguna</th>
                    <th class="px-4 py-2">Aksi</th>
                </tr>
            </thead>
            <tbody>
                @forelse ($roles as $role)
                    <tr class="border-t border-gray-100">
                        <td class="px-4 py-2">{{ $role->name }}</td>
                        <td class="px-4 py-2">{{ $role->permissions_count }}</td>
                        <td class="px-4 py-2">{{ $role->users_count }}</td>
                        <td class="px-4 py-2">
                            <div class="flex flex-wrap gap-2">
                                <a href="{{ route('admin.roles.edit', $role) }}" class="text-blue-600 hover:underline">Edit</a>
                                @unless (in_array($role->name, ['superadmin', 'admin'], true))
                                    <form method="POST" action="{{ route('admin.roles.destroy', $role) }}"
                                          onsubmit="return confirm('Hapus role ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="text-red-600 hover:underline">Hapus</button>
                                    </form>
                                @endunless
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="4" class="px-4 py-6 text-center text-gray-500">Belum ada role.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="mt-4">
        {{ $roles->links() }}
    </div>
</div>
@endsection
