@extends('layouts.app')

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

    <div class="card mb-3">
        <div class="card-body">
            <form action="{{ route('facturas.filtrar') }}" method="POST" class="row g-2 align-items-end">
                @csrf
                <div class="col-md-3">
                    <label class="form-label">Cliente (nombre o cédula)</label>
                    <input type="text" name="cliente" class="form-control" value="{{ $filtro['cliente'] ?? '' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">N° de factura</label>
                    <input type="number" name="numero" class="form-control" value="{{ $filtro['numero'] ?? '' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Desde</label>
                    <input type="date" name="desde" class="form-control" value="{{ $filtro['desde'] ?? '' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Hasta</label>
                    <input type="date" name="hasta" class="form-control" value="{{ $filtro['hasta'] ?? '' }}">
                </div>
                <div class="col-md-2">
                    <label class="form-label">Estado</label>
                    <select name="estado" class="form-select">
                        <option value="">-- Todos --</option>
                        <option value="pagada" @selected(($filtro['estado'] ?? '') === 'pagada')>Pagada</option>
                        <option value="nota_credito" @selected(($filtro['estado'] ?? '') === 'nota_credito')>Nota de crédito</option>
                    </select>
                </div>
                <div class="col-md-1 d-flex gap-1">
                    <button type="submit" class="btn btn-primary w-100">Buscar</button>
                </div>
            </form>
            @if (! empty($filtro))
                <form action="{{ route('facturas.filtrar') }}" method="POST" class="mt-2">
                    @csrf
                    <input type="hidden" name="limpiar" value="1">
                    <button type="submit" class="btn btn-sm btn-outline-secondary">Limpiar filtros</button>
                </form>
            @endif
        </div>
    </div>

    <div class="card">
        <div class="table-responsive">
            <table class="table table-striped mb-0">
                <thead>
                    <tr>
                        <th>N° Factura</th>
                        <th>Fecha</th>
                        <th>Cliente</th>
                        <th>Cédula/RIF</th>
                        <th>Total</th>
                        <th>Estado</th>
                        <th class="text-end">Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($facturas as $factura)
                        <tr>
                            <td>{{ $factura->numeroFormateado() }}</td>
                            <td>{{ $factura->created_at->format('d/m/Y H:i') }}</td>
                            <td>{{ $factura->cliente->nombre }}</td>
                            <td>{{ $factura->cliente->cedula }}</td>
                            <td>{{ number_format($factura->total, 2) }}</td>
                            <td>
                                @if ($factura->esNotaCredito())
                                    <span class="badge bg-warning text-dark">Nota de crédito</span>
                                @else
                                    <span class="badge bg-success">Pagada</span>
                                @endif
                            </td>
                            <td class="text-end">
                                <a href="{{ route('facturas.show', $factura) }}" class="btn btn-sm btn-outline-secondary">Ver</a>
                                <a href="{{ route('facturas.pdf', $factura) }}?formato=a4" target="_blank" class="btn btn-sm btn-outline-primary">PDF A4</a>
                                <a href="{{ route('facturas.pdf', $factura) }}?formato=ticket" target="_blank" class="btn btn-sm btn-outline-primary">PDF Ticket</a>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center text-muted py-4">No se encontraron facturas con esos filtros.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-3">
        {{ $facturas->links() }}
    </div>
</div>
@endsection
