@extends('layouts.admin')

@section('title', 'Pengurusan Pengguna')
@section('header', 'Pengurusan Pengguna')

@section('breadcrumb')
<li class="breadcrumb-item active">Pengguna</li>
@endsection

@section('header-actions')
<a href="{{ route('admin.users.create') }}" class="btn btn-sm btn-primary">
    <i class="bi bi-person-plus me-1"></i> Tambah Pengguna
</a>
@endsection

@section('content')

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-hover align-middle mb-0">
            <thead class="table-light">
                <tr>
                    <th>Nama</th>
                    <th>Emel</th>
                    <th>Peranan</th>
                    <th class="text-center">Program</th>
                    <th>Tarikh Daftar</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            @forelse($users as $user)
                <tr>
                    <td>
                        <div class="fw-medium">{{ $user->name }}</div>
                        @if($user->id === auth()->id())
                        <span class="badge bg-light text-muted border" style="font-size:.65rem;">Anda</span>
                        @endif
                    </td>
                    <td class="small text-muted">{{ $user->email }}</td>
                    <td>
                        @if($user->role === 'super_admin')
                            <span class="badge bg-danger">Super Admin</span>
                        @else
                            <span class="badge bg-primary">Admin Program</span>
                        @endif
                    </td>
                    <td class="text-center">
                        <span class="badge bg-secondary">{{ $user->programs_count }}</span>
                    </td>
                    <td class="small text-muted">{{ $user->created_at->format('d/m/Y') }}</td>
                    <td class="text-end">
                        <a href="{{ route('admin.users.edit', $user) }}"
                           class="btn btn-sm btn-outline-secondary">
                            <i class="bi bi-pencil"></i>
                        </a>
                        @if($user->id !== auth()->id())
                        <form method="POST" action="{{ route('admin.users.destroy', $user) }}"
                              class="d-inline"
                              onsubmit="return confirm('Padam pengguna {{ addslashes($user->name) }}? Program mereka tidak akan terjejas.')">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-outline-danger">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" class="text-center py-4 text-muted">Tiada pengguna dijumpai.</td>
                </tr>
            @endforelse
            </tbody>
        </table>
    </div>

    @if($users->hasPages())
    <div class="card-footer bg-white">{{ $users->links() }}</div>
    @endif
</div>

@endsection
