@extends('layouts.app')

@section('content')
<div class="container">
    <h1 class="h3 mb-3">Notas de crédito pendientes</h1>

    @if (session('status'))
        <div class="alert alert-success">{{ session('status') }}</div>
    @endif

    <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>Total</th>
                        <th>Monto a crédito</th>
                        <th class="text-end">Acciones</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($facturas as $factura)
                        <tr>
                            <td><a href="{{ route('facturas.show', $factura) }}">{{ $factura->numeroFormateado() }}</a></td>
                            <td>{{ $factura->created_at->format('d/m/Y H:i') }}</td>
                            <td>{{ $factura->cliente->nombre }}</td>
                            <td>{{ number_format($factura->total, 2) }}</td>
                            <td>{{ number_format($factura->credito, 2) }}</td>
                            <td class="text-end">
                                <form action="{{ route('notascredito.pagar', $factura) }}" method="POST" class="d-inline" onsubmit="return confirm('¿Confirmar el pago de esta nota de crédito?');">
                                    @csrf
                                    <button type="submit" class="btn btn-sm btn-success">Marcar como pagada</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="6" class="text-center text-muted py-4">No hay notas de crédito pendientes.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

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