<table>
    <thead>
        <tr>
            <th>Référence</th>
            <th>Client</th>
            <th>Produit</th>
            <th>Magasin</th>
            <th>Montant total</th>
            <th>Montant payé</th>
            <th>Montant restant</th>
            <th>Date</th>
        </tr>
    </thead>
    <tbody>
        @foreach($creances as $c)
            @php
                $vente = $c->vente;
                $client = $c->client;
                $produit = $vente?->produit;
                $magasin = $vente?->magasin;

                $montant = (float) ($vente?->montant ?? 0);
                $reste = (float) ($c->montant_restant ?? 0);
                $paye = max(0, $montant - $reste);
            @endphp
            <tr>
                <td>{{ $vente?->reference ?? ('CR-' . $c->id) }}</td>
                <td>{{ $client?->nom ?? '—' }}</td>
                <td>{{ $produit?->designation ?? '—' }}</td>
                <td>{{ $magasin?->nom ?? '—' }}</td>
                <td>{{ $montant }}</td>
                <td>{{ $paye }}</td>
                <td>{{ $reste }}</td>
                <td>{{ optional($vente?->date_vente)->format('Y-m-d') }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

