@extends('layouts.app')

@section('title', 'Detalle de Usuario')
@section('page-title', 'Detalle de Usuario')

@section('content')
<div class="row mb-4">
    <div class="col-12">
        <a href="{{ route('users.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-left me-2"></i>Volver a Usuarios
        </a>
    </div>
</div>

@if(session('success'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
    <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
@endif

<div class="row">
    <div class="col-lg-4">
        <div class="card">
            <div class="card-body text-center">
                <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center mx-auto mb-3" 
                     style="width: 100px; height: 100px; font-size: 48px;">
                    {{ substr($user->name, 0, 1) }}
                </div>
                
                <h4>{{ $user->name }}</h4>
                <p class="text-muted">{{ $user->email }}</p>
                
                <span class="badge {{ role_badge_class($user->role) }} mb-3">
                    {{ role_name($user->role) }}
                </span>
                
                @if($user->is_active)
                <span class="badge bg-success mb-3">Activo</span>
                @else
                <span class="badge bg-secondary mb-3">Inactivo</span>
                @endif
                
                <hr>
                
                <div class="text-start">
                    @if($user->phone)
                    <div class="mb-2">
                        <i class="bi bi-telephone me-2"></i>
                        <strong>Teléfono:</strong> {{ $user->phone }}
                    </div>
                    @endif
                    
                    @if($user->position)
                    <div class="mb-2">
                        <i class="bi bi-briefcase me-2"></i>
                        <strong>Cargo:</strong> {{ $user->position }}
                    </div>
                    @endif
                    
                    @if($user->company)
                    <div class="mb-2">
                        <i class="bi bi-building me-2"></i>
                        <strong>Empresa:</strong> {{ $user->company->name }}
                    </div>
                    @endif
                    
                    @if($user->project)
                    <div class="mb-2">
                        <i class="bi bi-folder me-2"></i>
                        <strong>Proyecto:</strong> {{ $user->project->name }}
                    </div>
                    @endif
                    
                    <div class="mb-2">
                        <i class="bi bi-calendar me-2"></i>
                        <strong>Miembro desde:</strong> {{ $user->created_at->format('d/m/Y') }}
                    </div>
                </div>
                
                <hr>
                
                <div class="d-grid gap-2">
                    @if(!$user->is_active && $user->activation_token)
                    <form action="{{ route('users.resend-activation', $user) }}" method="POST">
                        @csrf
                        <button type="submit" class="btn btn-warning w-100">
                            <i class="bi bi-envelope me-2"></i>Reenviar Activación
                        </button>
                    </form>
                    @endif
                    
                    <a href="{{ route('users.edit', $user) }}" class="btn btn-primary">
                        <i class="bi bi-pencil me-2"></i>Editar Usuario
                    </a>
                    
                    @if($user->id !== auth()->id())
                    <form action="{{ route('users.destroy', $user) }}" method="POST"
                          onsubmit="return confirm('¿Estás seguro de eliminar este usuario?')">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-outline-danger w-100">
                            <i class="bi bi-trash me-2"></i>Eliminar Usuario
                        </button>
                    </form>
                    @endif
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-lg-8">
        <!-- Estadísticas de Tickets -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h6 class="text-muted mb-2">Tickets Creados</h6>
                        <h2 class="mb-0">{{ $user->createdTickets->count() }}</h2>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card">
                    <div class="card-body">
                        <h6 class="text-muted mb-2">Tickets Asignados</h6>
                        <h2 class="mb-0">{{ $user->assignedTickets->count() }}</h2>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Tickets Creados -->
        @if($user->createdTickets->count() > 0)
        <div class="card mb-4">
            <div class="card-header bg-white">
                <h5 class="mb-0">Tickets Creados</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Número</th>
                                <th>Título</th>
                                <th>Estado</th>
                                <th>Prioridad</th>
                                <th>Fecha</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($user->createdTickets->take(5) as $ticket)
                            <tr onclick="window.location='{{ route('tickets.show', $ticket) }}'" style="cursor: pointer;">
                                <td><strong>{{ $ticket->ticket_number }}</strong></td>
                                <td>{{ Str::limit($ticket->title, 40) }}</td>
                                <td>
                                    <span class="badge badge-status-{{ $ticket->status }}">
                                        {{ ucfirst(str_replace('_', ' ', $ticket->status)) }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-priority-{{ $ticket->priority }}">
                                        {{ ucfirst($ticket->priority) }}
                                    </span>
                                </td>
                                <td>{{ $ticket->created_at->format('d/m/Y') }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @if($user->createdTickets->count() > 5)
                <div class="text-center mt-2">
                    <a href="{{ route('tickets.index') }}?requester={{ $user->id }}" class="btn btn-sm btn-outline-primary">
                        Ver todos los {{ $user->createdTickets->count() }} tickets
                    </a>
                </div>
                @endif
            </div>
        </div>
        @endif
        
        <!-- Tickets Asignados -->
        @if($user->assignedTickets->count() > 0)
        <div class="card">
            <div class="card-header bg-white">
                <h5 class="mb-0">Tickets Asignados</h5>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-sm">
                        <thead>
                            <tr>
                                <th>Número</th>
                                <th>Título</th>
                                <th>Estado</th>
                                <th>Prioridad</th>
                                <th>Fecha</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($user->assignedTickets->take(5) as $ticket)
                            <tr onclick="window.location='{{ route('tickets.show', $ticket) }}'" style="cursor: pointer;">
                                <td><strong>{{ $ticket->ticket_number }}</strong></td>
                                <td>{{ Str::limit($ticket->title, 40) }}</td>
                                <td>
                                    <span class="badge badge-status-{{ $ticket->status }}">
                                        {{ ucfirst(str_replace('_', ' ', $ticket->status)) }}
                                    </span>
                                </td>
                                <td>
                                    <span class="badge badge-priority-{{ $ticket->priority }}">
                                        {{ ucfirst($ticket->priority) }}
                                    </span>
                                </td>
                                <td>{{ $ticket->created_at->format('d/m/Y') }}</td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
                @if($user->assignedTickets->count() > 5)
                <div class="text-center mt-2">
                    <a href="{{ route('tickets.index') }}?assigned={{ $user->id }}" class="btn btn-sm btn-outline-primary">
                        Ver todos los {{ $user->assignedTickets->count() }} tickets
                    </a>
                </div>
                @endif
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
