@extends('layouts.app')

@section('content')
<div class="container">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h1 class="mb-0">Usuarios</h1>
        <a href="{{ route('users.create') }}" class="btn btn-primary">
            Nuevo Usuario
        </a>
    </div>

    <div class="card">
        <div class="card-body p-0">

            <div class="table-responsive">
                <table class="table table-bordered table-hover mb-0">
                    <thead class="thead-light">
                        <tr>
                            <th>Nombre</th>
                            <th>Email</th>
                            <th class="d-none d-md-table-cell">Empresa</th>
                            <th class="d-none d-md-table-cell">Rol</th>
                            <th width="140">Acciones</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($users as $user)
                        <tr>
                            <td>{{ $user->name }}</td>
                            <td>{{ $user->email }}</td>

                            <td class="d-none d-md-table-cell">
                                {{ $user->company->nombre ?? 'Sin empresa' }}
                            </td>

                            <td class="d-none d-md-table-cell">
                                {{ $user->getRoleNames()->first() }}
                            </td>

                            <td>
                                <div class="d-flex flex-wrap gap-1">
                                    <a href="{{ route('users.edit', $user->id) }}" 
                                       class="btn btn-warning btn-sm">
                                        Editar
                                    </a>

                                    <form action="{{ route('users.destroy', $user->id) }}" 
                                          method="POST" 
                                          onsubmit="return confirm('¿Eliminar usuario?')">
                                        @csrf
                                        @method('DELETE')
                                        <button class="btn btn-danger btn-sm">
                                            Borrar
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="5" class="text-center text-muted p-3">
                                No hay usuarios registrados
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

        </div>
    </div>

</div>
@endsection
