@extends('layouts.app')

@section('content')
<div class="container-fluid">
    <h1 class="h3 mb-3">Facturas por familia</h1>

    <div class="card mb-3">
        <div class="card-body">
            <form action="{{ route('consultas.facturasFamilia.filtrar') }}" method="POST" class="row g-2 align-items-end">
                @csrf
                <div class="col-md-4">
                    <label class="form-label">Categoría / Familia</label>
                    <select name="categoria_id" class="form-select" required>
                        <option value="">-- Seleccione --</option>
                        @foreach ($categorias as $categoria)
                            <option value="{{ $categoria->id }}" @selected(($filtro['categoria_id'] ?? null) == $categoria->id)>{{ $categoria->nombre }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="form-label">Desde</label>
                    <input type="date" name="desde" class="form-control" value="{{ $filtro['desde'] ?? '' }}">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Hasta</label>
                    <input type="date" name="hasta" class="form-control" value="{{ $filtro['hasta'] ?? '' }}">
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">Buscar</button>
                </div>
            </form>
        </div>
    </div>

    @if (! empty($filtro['categoria_id']))
        <div class="card mb-3">
            <div class="table-responsive">
                <table class="table table-striped mb-0">
                    <thead>
                        <tr>
                            <th>Fecha</th>
                            <th>N° Factura</th>
                            <th>Cliente</th>
                            <th>Producto</th>
                            <th>Cantidad</th>
                            <th>Subtotal</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($detalles as $detalle)
                            <tr>
                                <td>{{ $detalle->factura->created_at->format('d/m/Y') }}</td>
                                <td><a href="{{ route('facturas.show', $detalle->factura) }}">{{ $detalle->factura->numeroFormateado() }}</a></td>
                                <td>{{ $detalle->factura->cliente->nombre }}</td>
                                <td>{{ $detalle->descripcion }}</td>
                                <td>{{ $detalle->cantidad }}</td>
                                <td>{{ number_format($detalle->subtotal, 2) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="text-center text-muted py-4">No hay ventas de esta familia en el rango indicado.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <div class="row">
            <div class="col-md-4 offset-md-8">
                <div class="card">
                    <div class="card-body d-flex justify-content-between">
                        <strong>Total vendido</strong>
                        <strong>{{ number_format($total, 2) }}</strong>
                    </div>
                </div>
            </div>
        </div>
    @endif
</div>
@endsection
