<x-app-layout title="Team members">
    <div class="flex items-center justify-between">
        <h1 class="text-xl font-semibold text-slate-900">Team members</h1>
        <a href="{{ route('meet.members.create') }}" class="btn-primary">Add member</a>
    </div>

    <div class="mt-4 overflow-hidden rounded-2xl border border-slate-200 bg-white">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                <tr><th class="px-4 py-3">Member</th><th class="px-4 py-3">Role</th><th class="px-4 py-3">Branch</th><th class="px-4 py-3"></th></tr>
            </thead>
            <tbody class="divide-y divide-slate-50">
                @foreach ($members as $member)
                    @php
                        $display = str_contains($member->user_ref, '@')
                            ? $member->user_ref
                            : (\App\Models\User::where('public_id', $member->user_ref)->value('email') ?? $member->user_ref);
                    @endphp
                    <tr>
                        <td class="px-4 py-3 text-sm">{{ $display }}</td>
                        <td class="px-4 py-3">{{ $roles[$member->role] ?? $member->role }}</td>
                        <td class="px-4 py-3">{{ $member->branch?->name ?? 'All branches' }}</td>
                        <td class="px-4 py-3 text-right">
                            @if ($member->user_ref !== auth()->user()->public_id)
                                <form method="POST" action="{{ route('meet.members.destroy', $member) }}" class="inline" onsubmit="return confirm('Remove this member?')">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="text-red-600">Remove</button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</x-app-layout>
