<?php
    use App\Support\Format;
    $record = $getRecord();
    $installments = $record->installments()->orderBy('installment_number')->get();
    $overdueCount = $installments->where('status', 'overdue')->count();
    $paidCount = $installments->where('status', 'paid')->count();
    $totalCount = $installments->count();
?>

<div>
    
    <div class="stats-grid stats-grid-4">
        <div class="stat-card stat-card-default">
            <div class="stat-value"><?php echo e($totalCount); ?></div>
            <div class="stat-label">Total</div>
        </div>
        <div class="stat-card stat-card-success">
            <div class="stat-value stat-value-success"><?php echo e($paidCount); ?></div>
            <div class="stat-label">Pagadas</div>
        </div>
        <div class="stat-card stat-card-default">
            <div class="stat-value"><?php echo e($totalCount - $paidCount - $overdueCount); ?></div>
            <div class="stat-label">Pendientes</div>
        </div>
        <div class="stat-card <?php echo e($overdueCount > 0 ? 'stat-card-danger' : 'stat-card-default'); ?>">
            <div class="stat-value <?php echo e($overdueCount > 0 ? 'stat-value-danger' : ''); ?>"><?php echo e($overdueCount); ?></div>
            <div class="stat-label">Vencidas</div>
        </div>
    </div>

    
    <div class="credit-table-container">
        <table class="credit-table">
            <thead>
                <tr>
                    <th>#</th>
                    <th>Vencimiento</th>
                    <th class="text-right">Total</th>
                    <th class="text-right">Pagado</th>
                    <th class="text-right">Pendiente</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
                        $isOverdue = $installment->status === 'overdue';
                        $isPaid = $installment->status === 'paid';
                        $remaining = $installment->total_amount - $installment->amount_paid;
                        $rowClass = $isOverdue ? 'row-danger' : ($isPaid ? 'row-success' : '');

                        $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 <?php echo e($installment->amount_paid > 0 ? 'money-success' : ''); ?>">
                            <?php echo e(Format::money($installment->amount_paid)); ?>

                        </td>
                        <td class="text-right money <?php echo e($remaining > 0 ? 'money-danger' : ''); ?>" style="<?php echo e($remaining > 0 ? 'font-weight: 500;' : ''); ?>">
                            <?php echo e($remaining > 0 ? Format::money($remaining) : '-'); ?>

                        </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 cuotas registradas
                        </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"><?php echo e(Format::money($installments->sum('amount_paid'))); ?></td>
                    <td class="text-right money money-danger"><?php echo e(Format::money($installments->sum('total_amount') - $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/installments-table.blade.php ENDPATH**/ ?>