@extends('layouts.app')

@section('title', 'Detalle de Cliente')

@section('header', 'Gestión de Clientes')

@section('content')
<div class="page-container">
    <div class="page-header">
        <h2 class="page-title">{{ $cliente->nombre }} {{ $cliente->apellido }}</h2>
        <div class="page-actions">
            <a href="{{ route('clientes.edit', $cliente) }}" class="btn btn-warning">
                <i class="fa-solid fa-pen-to-square"></i>
                Editar
            </a>
            <a href="{{ route('clientes.index') }}" class="btn btn-secondary">
                <i class="fa-solid fa-arrow-left"></i>
                Volver
            </a>
        </div>
    </div>

    <div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 1.5rem;">
        <!-- Información Personal -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa-solid fa-user"></i> Información Personal</h3>
            </div>
            <div class="card-body">
                <div class="info-row-2col">
                    <div class="info-item">
                        <span class="info-label">Nombre Completo</span>
                        <span class="info-value">{{ $cliente->nombre }} {{ $cliente->apellido }}</span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">Documento</span>
                        <span class="info-value fw-bold">{{ $cliente->documento }}</span>
                    </div>
                </div>

                <div class="info-row-2col">
                    <div class="info-item">
                        <span class="info-label">Teléfono</span>
                        <span class="info-value">{{ $cliente->telefono ?? '-' }}</span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">Email</span>
                        <span class="info-value">{{ $cliente->email ?? '-' }}</span>
                    </div>
                </div>

                @if($cliente->direccion)
                <div class="info-row">
                    <div class="info-item full">
                        <span class="info-label">Dirección</span>
                        <p class="info-text">{{ $cliente->direccion }}</p>
                    </div>
                </div>
                @endif

                <div class="info-row-2col">
                    <div class="info-item">
                        <span class="info-label">Estado</span>
                        <span class="badge {{ $cliente->estado ? 'badge-success' : 'badge-danger' }}">
                            {{ $cliente->estado ? 'Activo' : 'Inactivo' }}
                        </span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">Fecha de Registro</span>
                        <span class="info-value">{{ $cliente->created_at->format('d/m/Y H:i') }}</span>
                    </div>
                </div>
            </div>
        </div>

        <!-- Historial de Ventas -->
        <div class="card">
            <div class="card-header">
                <h3><i class="fa-solid fa-shopping-cart"></i> Historial de Ventas</h3>
            </div>
            <div class="card-body">
                @if($cliente->ventas && $cliente->ventas->count() > 0)
                    <div style="max-height: 300px; overflow-y: auto;">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>N° Venta</th>
                                    <th>Fecha</th>
                                    <th>Total</th>
                                    <th>Estado</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($cliente->ventas as $venta)
                                <tr>
                                    <td class="fw-bold">{{ $venta->numero_venta }}</td>
                                    <td>{{ $venta->fecha_venta->format('d/m/Y') }}</td>
                                    <td class="fw-bold">${{ number_format($venta->total, 0, ',', '.') }}</td>
                                    <td>
                                        @if($venta->estado === 'completada')
                                            <span class="badge badge-success">Completada</span>
                                        @elseif($venta->estado === 'pendiente')
                                            <span class="badge badge-warning">Pendiente</span>
                                        @else
                                            <span class="badge badge-danger">Cancelada</span>
                                        @endif
                                    </td>
                                    <td>
                                        <a href="{{ route('ventas.show', $venta) }}" class="btn-action btn-view" title="Ver">
                                            <i class="fa-solid fa-eye"></i>
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div style="margin-top: 1rem; padding-top: 1rem; border-top: 2px solid var(--border-color);">
                        <div class="info-row-2col">
                            <div class="info-item">
                                <span class="info-label">Total de Ventas</span>
                                <span class="info-value fw-bold">{{ $cliente->ventas->count() }}</span>
                            </div>
                            <div class="info-item">
                                <span class="info-label">Total Vendido</span>
                                <span class="info-value fw-bold" style="color: #10b981;">${{ number_format($cliente->ventas->sum('total'), 0, ',', '.') }}</span>
                            </div>
                        </div>
                    </div>
                @else
                    <div class="empty-state">
                        <p class="empty-message">Este cliente aún no tiene ventas registradas</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection
