<?php
    use App\Support\Format;
    use App\Filament\Resources\Credits\CreditResource;

    $record = $getRecord();
    $parent = $record->parent;
    $children = $record->children()->with('client')->get();

    // Build the ancestor chain (walk up)
    $ancestors = collect();
    $current = $parent;
    while ($current) {
        $ancestors->prepend($current);
        $current = $current->parent;
    }

    // Get operation type configuration
    $getOperationType = function($credit) {
        return match($credit->status) {
            'extended' => ['label' => 'Extendido', 'badgeClass' => 'badge-purple', 'iconClass' => 'lineage-icon-purple'],
            'refinanced' => ['label' => 'Refinanciado', 'badgeClass' => 'badge-warning', 'iconClass' => 'lineage-icon-warning'],
            'restructured' => ['label' => 'Reestructurado', 'badgeClass' => 'badge-gray', 'iconClass' => 'lineage-icon-gray'],
            'renewed' => ['label' => 'Renovado', 'badgeClass' => 'badge-success', 'iconClass' => 'lineage-icon-success'],
            'interest_capitalized' => ['label' => 'Prorrogado', 'badgeClass' => 'badge-info', 'iconClass' => 'lineage-icon-info'],
            default => ['label' => 'Derivado', 'badgeClass' => 'badge-gray', 'iconClass' => 'lineage-icon-gray'],
        };
    };

    // Get status badge class
    $getStatusBadge = function($status) {
        return match($status) {
            'active' => ['label' => 'Activo', 'class' => 'badge-success'],
            'paid' => ['label' => 'Pagado', 'class' => 'badge-info'],
            'overdue', 'delayed' => ['label' => 'En mora', 'class' => 'badge-danger'],
            'canceled' => ['label' => 'Cancelado', 'class' => 'badge-gray'],
            'extended' => ['label' => 'Extendido', 'class' => 'badge-purple'],
            'refinanced' => ['label' => 'Refinanciado', 'class' => 'badge-warning'],
            'restructured' => ['label' => 'Reestructurado', 'class' => 'badge-gray'],
            'renewed' => ['label' => 'Renovado', 'class' => 'badge-success'],
            default => ['label' => ucfirst($status), 'class' => 'badge-gray'],
        };
    };
?>

<div>
    <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php endif; ?><?php if($ancestors->isEmpty() && $children->isEmpty()): ?>
        <div class="empty-state">
            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" style="width: 3rem; height: 3rem; margin: 0 auto; color: rgb(156 163 175);">
                <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" />
            </svg>
            <p style="margin-top: 0.75rem;">Este crédito no tiene historial de linaje</p>
            <p style="font-size: 0.75rem; color: rgb(156 163 175); margin-top: 0.25rem;">Es un crédito original sin padres ni hijos derivados</p>
        </div>
    <?php else: ?>
        
        <div class="lineage-legend">
            <div class="lineage-legend-item">
                <span class="lineage-legend-dot lineage-legend-dot-purple"></span>
                <span>Crédito Padre</span>
            </div>
            <div class="lineage-legend-item">
                <span class="lineage-legend-dot lineage-legend-dot-blue"></span>
                <span>Crédito Actual</span>
            </div>
            <div class="lineage-legend-item">
                <span class="lineage-legend-dot lineage-legend-dot-gray"></span>
                <span>Créditos Derivados</span>
            </div>
        </div>

        
        <div class="lineage-container">
            <div class="lineage-line"></div>
            <div class="lineage-items">
                
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $ancestors; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ancestor): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                    <?php
                        $opType = $getOperationType($ancestor);
                        $statusBadge = $getStatusBadge($ancestor->status);
                    ?>
                    <div class="lineage-item">
                        <div class="lineage-icon <?php echo e($opType['iconClass']); ?>">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" />
                            </svg>
                        </div>

                        <a href="<?php echo e(CreditResource::getUrl('view', ['record' => $ancestor])); ?>" class="lineage-content lineage-content-ancestor">
                            <div class="lineage-header">
                                <div>
                                    <div class="lineage-title">Crédito #<?php echo e($ancestor->id); ?></div>
                                    <div class="lineage-subtitle"><?php echo e($ancestor->client->name ?? 'Sin cliente'); ?></div>
                                </div>
                                <div class="lineage-right">
                                    <span class="credit-badge <?php echo e($statusBadge['class']); ?>"><?php echo e($statusBadge['label']); ?></span>
                                </div>
                            </div>
                            <div class="lineage-meta">
                                <div class="lineage-meta-item">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                        <path d="M10.464 8.746c.227-.18.497-.311.786-.394v2.795a2.252 2.252 0 01-.786-.393c-.394-.313-.546-.681-.546-1.004 0-.323.152-.691.546-1.004zM12.75 15.662v-2.824c.347.085.664.228.921.421.427.32.579.686.579.991 0 .305-.152.671-.579.991a2.534 2.534 0 01-.921.42z" />
                                        <path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zM12.75 6a.75.75 0 00-1.5 0v.816a3.836 3.836 0 00-1.72.756c-.712.566-1.112 1.35-1.112 2.178 0 .829.4 1.612 1.113 2.178.502.4 1.102.647 1.719.756v2.978a2.536 2.536 0 01-.921-.421l-.879-.66a.75.75 0 00-.9 1.2l.879.66c.533.4 1.169.645 1.821.75V18a.75.75 0 001.5 0v-.81a4.124 4.124 0 001.821-.749c.745-.559 1.179-1.344 1.179-2.191 0-.847-.434-1.632-1.179-2.191a4.122 4.122 0 00-1.821-.75V8.354c.29.082.559.213.786.393l.415.33a.75.75 0 00.933-1.175l-.415-.33a3.836 3.836 0 00-1.719-.755V6z" clip-rule="evenodd" />
                                    </svg>
                                    <span class="money"><?php echo e(Format::money($ancestor->amount)); ?></span>
                                </div>
                                <div class="lineage-meta-item">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                        <path fill-rule="evenodd" d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z" clip-rule="evenodd" />
                                    </svg>
                                    <span><?php echo e($ancestor->created_at->format('d/m/Y')); ?></span>
                                </div>
                                <span class="credit-badge <?php echo e($opType['badgeClass']); ?>"><?php echo e($opType['label']); ?></span>
                            </div>
                        </a>
                    </div>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>

                
                <div class="lineage-item">
                    <div class="lineage-icon lineage-icon-current">
                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                            <path fill-rule="evenodd" d="M5.625 1.5c-1.036 0-1.875.84-1.875 1.875v17.25c0 1.035.84 1.875 1.875 1.875h12.75c1.035 0 1.875-.84 1.875-1.875V12.75A3.75 3.75 0 0016.5 9h-1.875a1.875 1.875 0 01-1.875-1.875V5.25A3.75 3.75 0 009 1.5H5.625zM7.5 15a.75.75 0 01.75-.75h7.5a.75.75 0 010 1.5h-7.5A.75.75 0 017.5 15zm.75 2.25a.75.75 0 000 1.5H12a.75.75 0 000-1.5H8.25z" clip-rule="evenodd" />
                            <path d="M12.971 1.816A5.23 5.23 0 0114.25 5.25v1.875c0 .207.168.375.375.375H16.5a5.23 5.23 0 013.434 1.279 9.768 9.768 0 00-6.963-6.963z" />
                        </svg>
                    </div>

                    <div class="lineage-content lineage-content-current">
                        <div class="lineage-header">
                            <div>
                                <div class="lineage-title">
                                    Crédito #<?php echo e($record->id); ?>

                                    <span class="credit-badge badge-info" style="margin-left: 0.5rem;">ACTUAL</span>
                                </div>
                                <div class="lineage-subtitle"><?php echo e($record->client->name ?? 'Sin cliente'); ?></div>
                            </div>
                            <div class="lineage-right">
                                <?php $currentStatus = $getStatusBadge($record->status); ?>
                                <span class="credit-badge <?php echo e($currentStatus['class']); ?>"><?php echo e($currentStatus['label']); ?></span>
                            </div>
                        </div>
                        <div class="lineage-meta">
                            <div class="lineage-meta-item">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                    <path d="M10.464 8.746c.227-.18.497-.311.786-.394v2.795a2.252 2.252 0 01-.786-.393c-.394-.313-.546-.681-.546-1.004 0-.323.152-.691.546-1.004zM12.75 15.662v-2.824c.347.085.664.228.921.421.427.32.579.686.579.991 0 .305-.152.671-.579.991a2.534 2.534 0 01-.921.42z" />
                                    <path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zM12.75 6a.75.75 0 00-1.5 0v.816a3.836 3.836 0 00-1.72.756c-.712.566-1.112 1.35-1.112 2.178 0 .829.4 1.612 1.113 2.178.502.4 1.102.647 1.719.756v2.978a2.536 2.536 0 01-.921-.421l-.879-.66a.75.75 0 00-.9 1.2l.879.66c.533.4 1.169.645 1.821.75V18a.75.75 0 001.5 0v-.81a4.124 4.124 0 001.821-.749c.745-.559 1.179-1.344 1.179-2.191 0-.847-.434-1.632-1.179-2.191a4.122 4.122 0 00-1.821-.75V8.354c.29.082.559.213.786.393l.415.33a.75.75 0 00.933-1.175l-.415-.33a3.836 3.836 0 00-1.719-.755V6z" clip-rule="evenodd" />
                                </svg>
                                <span class="money"><?php echo e(Format::money($record->amount)); ?></span>
                            </div>
                            <div class="lineage-meta-item">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                    <path fill-rule="evenodd" d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z" clip-rule="evenodd" />
                                </svg>
                                <span><?php echo e($record->created_at->format('d/m/Y')); ?></span>
                            </div>
                            <div class="lineage-meta-item">
                                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                    <path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 119 0 4.5 4.5 0 01-9 0zM3.751 20.105a8.25 8.25 0 0116.498 0 .75.75 0 01-.437.695A18.683 18.683 0 0112 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 01-.437-.695z" clip-rule="evenodd" />
                                </svg>
                                <span><?php echo e($record->client->name ?? 'Sin cliente'); ?></span>
                            </div>
                        </div>
                    </div>
                </div>

                
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if BLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::openLoop(); ?><?php endif; ?><?php $__currentLoopData = $children; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $child): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::startLoopIteration(); ?><?php endif; ?>
                    <?php
                        $opType = $getOperationType($child);
                        $statusBadge = $getStatusBadge($child->status);
                    ?>
                    <div class="lineage-item lineage-item-child">
                        <div class="lineage-icon <?php echo e($opType['iconClass']); ?>">
                            <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 14.25v-2.625a3.375 3.375 0 00-3.375-3.375h-1.5A1.125 1.125 0 0113.5 7.125v-1.5a3.375 3.375 0 00-3.375-3.375H8.25m0 12.75h7.5m-7.5 3H12M10.5 2.25H5.625c-.621 0-1.125.504-1.125 1.125v17.25c0 .621.504 1.125 1.125 1.125h12.75c.621 0 1.125-.504 1.125-1.125V11.25a9 9 0 00-9-9z" />
                            </svg>
                        </div>

                        <a href="<?php echo e(CreditResource::getUrl('view', ['record' => $child])); ?>" class="lineage-content lineage-content-child">
                            <div class="lineage-header">
                                <div>
                                    <div class="lineage-title">Crédito #<?php echo e($child->id); ?></div>
                                    <div class="lineage-subtitle"><?php echo e($child->client->name ?? 'Sin cliente'); ?></div>
                                </div>
                                <div class="lineage-right">
                                    <span class="credit-badge <?php echo e($statusBadge['class']); ?>"><?php echo e($statusBadge['label']); ?></span>
                                </div>
                            </div>
                            <div class="lineage-meta">
                                <div class="lineage-meta-item">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                        <path d="M10.464 8.746c.227-.18.497-.311.786-.394v2.795a2.252 2.252 0 01-.786-.393c-.394-.313-.546-.681-.546-1.004 0-.323.152-.691.546-1.004zM12.75 15.662v-2.824c.347.085.664.228.921.421.427.32.579.686.579.991 0 .305-.152.671-.579.991a2.534 2.534 0 01-.921.42z" />
                                        <path fill-rule="evenodd" d="M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25zM12.75 6a.75.75 0 00-1.5 0v.816a3.836 3.836 0 00-1.72.756c-.712.566-1.112 1.35-1.112 2.178 0 .829.4 1.612 1.113 2.178.502.4 1.102.647 1.719.756v2.978a2.536 2.536 0 01-.921-.421l-.879-.66a.75.75 0 00-.9 1.2l.879.66c.533.4 1.169.645 1.821.75V18a.75.75 0 001.5 0v-.81a4.124 4.124 0 001.821-.749c.745-.559 1.179-1.344 1.179-2.191 0-.847-.434-1.632-1.179-2.191a4.122 4.122 0 00-1.821-.75V8.354c.29.082.559.213.786.393l.415.33a.75.75 0 00.933-1.175l-.415-.33a3.836 3.836 0 00-1.719-.755V6z" clip-rule="evenodd" />
                                    </svg>
                                    <span class="money"><?php echo e(Format::money($child->amount)); ?></span>
                                </div>
                                <div class="lineage-meta-item">
                                    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
                                        <path fill-rule="evenodd" d="M6.75 2.25A.75.75 0 017.5 3v1.5h9V3A.75.75 0 0118 3v1.5h.75a3 3 0 013 3v11.25a3 3 0 01-3 3H5.25a3 3 0 01-3-3V7.5a3 3 0 013-3H6V3a.75.75 0 01.75-.75zm13.5 9a1.5 1.5 0 00-1.5-1.5H5.25a1.5 1.5 0 00-1.5 1.5v7.5a1.5 1.5 0 001.5 1.5h13.5a1.5 1.5 0 001.5-1.5v-7.5z" clip-rule="evenodd" />
                                    </svg>
                                    <span><?php echo e($child->created_at->format('d/m/Y')); ?></span>
                                </div>
                                <span class="credit-badge <?php echo e($opType['badgeClass']); ?>"><?php echo e($opType['label']); ?></span>
                            </div>
                        </a>
                    </div>
                <?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::endLoop(); ?><?php endif; ?><?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php \Livewire\Features\SupportCompiledWireKeys\SupportCompiledWireKeys::closeLoop(); ?><?php endif; ?>
            </div>
        </div>

        
        <div class="lineage-summary">
            <h4 class="lineage-summary-title">Resumen del Linaje</h4>
            <div class="stats-grid stats-grid-3">
                <div class="stat-card stat-card-default">
                    <div class="stat-value">
                        #<?php echo e($ancestors->first()?->id ?? $record->id); ?>

                    </div>
                    <div class="stat-label">Crédito Original</div>
                </div>
                <div class="stat-card stat-card-default">
                    <div class="stat-value">
                        <?php echo e($ancestors->count() + 1); ?>ª
                    </div>
                    <div class="stat-label">Generación</div>
                </div>
                <div class="stat-card stat-card-default">
                    <div class="stat-value">
                        <?php echo e($children->count()); ?>

                    </div>
                    <div class="stat-label">Créditos Derivados</div>
                </div>
            </div>
        </div>
    <?php endif; ?><?php if(\Livewire\Mechanisms\ExtendBlade\ExtendBlade::isRenderingLivewireComponent()): ?><!--[if ENDBLOCK]><![endif]--><?php endif; ?>
</div>
<?php /**PATH /var/www/html/credifygo/resources/views/filament/infolists/components/credit-lineage.blade.php ENDPATH**/ ?>