@extends('layouts.admin')

@section('title', 'Admin Users')

@section('content')
    <section class="dashboard-head">
        <div>
            <span class="eyebrow">Admin</span>
            <h1>Admin Users</h1>
        </div>
        <a class="button primary" href="{{ route('admin.admin-users.create') }}">Add admin user</a>
    </section>

    @if (session('status'))
        <p class="form-status">{{ session('status') }}</p>
    @endif

    <section class="panel">
        <div class="table-wrap">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Roles</th>
                        <th>Direct Permissions</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($adminUsers as $adminUser)
                        <tr>
                            <td><strong>{{ $adminUser->name }}</strong></td>
                            <td>{{ $adminUser->email }}</td>
                            <td>{{ $adminUser->roles->pluck('name')->implode(', ') ?: 'No role' }}</td>
                            <td>{{ $adminUser->permissions->pluck('name')->implode(', ') ?: 'No direct permission' }}</td>
                            <td><span class="status {{ $adminUser->is_active ? 'paid' : 'refunded' }}">{{ $adminUser->is_active ? 'Active' : 'Inactive' }}</span></td>
                            <td>
                                <div class="table-actions">
                                    <a class="text-button" href="{{ route('admin.admin-users.edit', $adminUser) }}">Edit</a>
                                    <form method="POST" action="{{ route('admin.admin-users.toggle', $adminUser) }}">
                                        @csrf
                                        @method('PATCH')
                                        <button class="text-button" type="submit">{{ $adminUser->is_active ? 'Disable' : 'Enable' }}</button>
                                    </form>
                                    <form method="POST" action="{{ route('admin.admin-users.destroy', $adminUser) }}" onsubmit="return confirm('Delete this admin user?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="text-button danger" type="submit">Delete</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6">No admin users available.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $adminUsers->links() }}
    </section>
@endsection
