<?php
    use App\Support\Format;

    $record = $getRecord();
    $installments = $record->installments()
        ->orderBy('installment_number')
        ->get();

    $totalDistributed = $installments->sum('pivot.applied_amount');
    $affectedCount = $installments->count();
    $extraAmount = (float) $record->extra_amount;
?>

<div>
    
    <div class="stats-grid stats-grid-3">
        <div class="stat-card stat-card-success">
            <div class="stat-value stat-value-success"><?php echo e(Format::money($totalDistributed)); ?></div>
            <div class="stat-label">Total Distribuido</div>
        </div>
        <div class="stat-card stat-card-default">
            <div class="stat-value"><?php echo e($affectedCount); ?></div>
            <div class="stat-label">Cuotas Afectadas</div>
        </div>
        <div class="stat-card <?php echo e($extraAmount > 0 ? 'stat-card-warning' : 'stat-card-default'); ?>">
            <div class="stat-value <?php echo e($extraAmount > 0 ? 'stat-value-warning' : ''); ?>"><?php echo e(Format::money($extraAmount)); ?></div>
            <div class="stat-label">Excedente</div>
        </div>
    </div>

    
    <div class="credit-table-container">
        <table class="credit-table">
            <thead>
                <tr>
                    <th>Cuota #</th>
                    <th>Vencimiento</th>
                    <th class="text-right">Total Cuota</th>
                    <th class="text-right">Aplicado (este pago)</th>
                    <th class="text-right">Total Pagado</th>
                    <th class="text-center">Estado</th>
                </tr>
            </thead>
            <tbody>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__empty_1 = true; $__currentLoopData = $installments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $installment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                    <?php
                        $appliedThisPayment = (float) $installment->pivot->applied_amount;
                        $isPaid = $installment->status === 'paid';
                        $isOverdue = $installment->status === 'overdue';
                        $rowClass = $isPaid ? 'row-success' : ($isOverdue ? 'row-danger' : '');

                        $badgeClass = match($installment->status) {
                            'paid' => 'badge-success',
                            'partial_paid' => 'badge-info',
                            'overdue' => 'badge-danger',
                            default => 'badge-gray',
                        };
                    ?>
                    <tr class="<?php echo e($rowClass); ?>">
                        <td style="font-weight: 500;"><?php echo e($installment->installment_number); ?></td>
                        <td>
                            <?php echo e($installment->due_date->format('d/m/Y')); ?>

                            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($isOverdue): ?>
                                <span style="color: rgb(239 68 68); margin-left: 0.25rem;">!</span>
                            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
                        </td>
                        <td class="text-right money"><?php echo e(Format::money($installment->total_amount)); ?></td>
                        <td class="text-right money money-success" style="font-weight: 600;">
                            <?php echo e(Format::money($appliedThisPayment)); ?>

                        </td>
                        <td class="text-right money <?php echo e($installment->amount_paid > 0 ? 'money-success' : ''); ?>">
                            <?php echo e(Format::money($installment->amount_paid)); ?>

                        </td>
                        <td class="text-center">
                            <span class="credit-badge <?php echo e($badgeClass); ?>">
                                <?php echo e($installment->getStatusLabel()); ?>

                            </span>
                        </td>
                    </tr>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>
                    <tr>
                        <td colspan="6" class="empty-state">
                            No hay distribución registrada para este pago
                        </td>
                    </tr>
                <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
            </tbody>
            <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($installments->isNotEmpty()): ?>
            <tfoot>
                <tr>
                    <td colspan="2" class="text-right" style="font-size: 0.75rem; text-transform: uppercase;">Totales:</td>
                    <td class="text-right money"><?php echo e(Format::money($installments->sum('total_amount'))); ?></td>
                    <td class="text-right money money-success" style="font-weight: 600;"><?php echo e(Format::money($totalDistributed)); ?></td>
                    <td class="text-right money money-success"><?php echo e(Format::money($installments->sum('amount_paid'))); ?></td>
                    <td></td>
                </tr>
            </tfoot>
            <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
        </table>
    </div>
</div>
<?php /**PATH /var/www/html/credifygo/resources/views/filament/infolists/components/payment-distribution-table.blade.php ENDPATH**/ ?>