@extends('layouts.app')
@section('title', 'Users')
@section('page_title', 'Users')
@section('breadcrumbs')
    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
    <li class="breadcrumb-item active">Users</li>
@endsection
@section('page_actions')
    @can('users.create')
        <a href="{{ route('users.create') }}" class="btn btn-primary"><i class="bi bi-plus-lg me-1"></i>Add User</a>
    @endcan
@endsection

@section('content')
    <div class="card">
        <div class="card-header"><i class="bi bi-person-badge me-1"></i>All Users ({{ $users->count() }})</div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover align-middle js-datatable">
                    <thead>
                        <tr>
                            <th>Name</th><th>Role</th><th>Branch</th><th>Employee #</th>
                            <th class="text-center">Status</th><th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($users as $u)
                            <tr>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <span class="sms-avatar sm">{{ $u->initials }}</span>
                                        <div>
                                            <div class="fw-semibold">{{ $u->name }}</div>
                                            <div class="small text-muted">{{ $u->email }}</div>
                                        </div>
                                    </div>
                                </td>
                                <td><span class="badge bg-soft-info">{{ $u->role_label }}</span></td>
                                <td>{{ $u->branch?->name ?? '—' }}</td>
                                <td>{{ $u->employee_code ?? '—' }}</td>
                                <td class="text-center">
                                    @if($u->is_active)<span class="badge bg-soft-success">Active</span>
                                    @else<span class="badge bg-soft-danger">Inactive</span>@endif
                                </td>
                                <td class="text-end">
                                    @can('users.update')
                                        <a href="{{ route('users.edit', $u) }}" class="btn btn-sm btn-light border"><i class="bi bi-pencil"></i></a>
                                    @endcan
                                    @can('users.delete')
                                        @if($u->id !== auth()->id() && !$u->isSuperAdmin())
                                            <form method="POST" action="{{ route('users.destroy', $u) }}" class="d-inline" data-confirm="Delete user “{{ $u->name }}”?">
                                                @csrf @method('DELETE')
                                                <button class="btn btn-sm btn-light border text-danger"><i class="bi bi-trash"></i></button>
                                            </form>
                                        @endif
                                    @endcan
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
