
<?php
    $sales = $customer->sales->sortByDesc(fn($s) => $s->start_date ?? $s->created_at);
    $totalAmt = (float) $sales->sum('total_amount');
    $paidAmt  = (float) $sales->sum('paid_amount');
    $dueAmt   = max(0, $totalAmt - $paidAmt);
    $openCnt  = $sales->whereNotIn('status', ['completed', 'cancelled', 'refunded'])->count();
?>

<div class="csx-wrap">

    
    <div class="csx-summary">
        <div class="csx-sum-item">
            <span class="csx-sum-l">Toplam Satış</span>
            <span class="csx-sum-v"><?php echo e(number_format($totalAmt, 0, ',', '.')); ?> ₺</span>
            <span class="csx-sum-s"><?php echo e($sales->count()); ?> kayıt</span>
        </div>
        <div class="csx-sum-item">
            <span class="csx-sum-l">Tahsil Edilen</span>
            <span class="csx-sum-v pos"><?php echo e(number_format($paidAmt, 0, ',', '.')); ?> ₺</span>
            <span class="csx-sum-s">%<?php echo e($totalAmt > 0 ? round($paidAmt / $totalAmt * 100) : 0); ?> tamamlandı</span>
        </div>
        <div class="csx-sum-item">
            <span class="csx-sum-l">Bakiye</span>
            <span class="csx-sum-v <?php echo e($dueAmt > 0 ? 'neg' : 'pos'); ?>"><?php echo e(number_format($dueAmt, 0, ',', '.')); ?> ₺</span>
            <span class="csx-sum-s"><?php echo e($openCnt); ?> açık satış</span>
        </div>
        <div class="csx-sum-bar">
            <div class="csx-sum-fill" style="width:<?php echo e($totalAmt > 0 ? min(100, round($paidAmt / $totalAmt * 100)) : 0); ?>%"></div>
        </div>
    </div>

    
    <?php if($sales->count() > 3): ?>
        <div class="csx-filters" id="csxFilters">
            <button type="button" class="csx-f active" data-f="all" onclick="csxFilter(this,'all')">Tümü <b><?php echo e($sales->count()); ?></b></button>
            <button type="button" class="csx-f" data-f="open" onclick="csxFilter(this,'open')">Açık <b><?php echo e($openCnt); ?></b></button>
            <button type="button" class="csx-f" data-f="due" onclick="csxFilter(this,'due')">Bakiyeli <b><?php echo e($sales->filter(fn($s) => $s->remaining_amount > 0)->count()); ?></b></button>
            <button type="button" class="csx-f" data-f="done" onclick="csxFilter(this,'done')">Kapanan <b><?php echo e($sales->where('status', 'completed')->count()); ?></b></button>
        </div>
    <?php endif; ?>

    
    <div class="csx-list">
        <?php $__empty_1 = true; $__currentLoopData = $sales; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $s): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
            <?php
                $pct = (float) $s->total_amount > 0 ? min(100, round((float) $s->paid_amount / (float) $s->total_amount * 100)) : 0;
                $due = (float) $s->remaining_amount;
                $isOpen = !in_array($s->status, ['completed', 'cancelled', 'refunded'], true);
                $tags = ['all'];
                if ($isOpen) $tags[] = 'open';
                if ($due > 0) $tags[] = 'due';
                if ($s->status === 'completed') $tags[] = 'done';
                $start = $s->start_date ? \Carbon\Carbon::parse($s->start_date) : null;
                $days = $start ? (int) \Carbon\Carbon::today()->diffInDays($start, false) : null;
            ?>

            <article class="csx-row" data-csx="<?php echo e(implode(' ', $tags)); ?>"
                     onclick="openSaleDrawer(<?php echo e($s->id); ?>)" title="Satış kartını aç">
                <span class="csx-rail" style="background:<?php echo e($s->status_color); ?>"></span>

                <div class="csx-date">
                    <?php if($start): ?>
                        <b><?php echo e($start->format('d')); ?></b>
                        <span><?php echo e($start->translatedFormat('M')); ?></span>
                        <small><?php echo e($start->format('Y')); ?></small>
                    <?php else: ?>
                        <b>—</b><span>tarih</span>
                    <?php endif; ?>
                </div>

                <div class="csx-main">
                    <div class="csx-title"><?php echo e($s->product_title ?: 'Satış'); ?></div>
                    <div class="csx-meta">
                        <span class="csx-code">#<?php echo e($s->sale_code ?: $s->id); ?></span>
                        <?php if($s->total_participants): ?><span><i class="fas fa-users"></i><?php echo e($s->total_participants); ?> kişi</span><?php endif; ?>
                        <?php if($days !== null && $isOpen): ?>
                            <?php if($days > 0): ?><span class="csx-soon"><i class="fas fa-hourglass-half"></i><?php echo e($days); ?> gün kaldı</span>
                            <?php elseif($days === 0): ?><span class="csx-soon now"><i class="fas fa-bolt"></i>Bugün</span><?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="csx-pay">
                    <div class="csx-pay-top">
                        <span class="csx-badge" style="background:<?php echo e($s->status_color); ?>1a;color:<?php echo e($s->status_color); ?>"><?php echo e($s->status_label); ?></span>
                        <?php if($due > 0): ?>
                            <span class="csx-due">Kalan <?php echo e(number_format($due, 0, ',', '.')); ?> ₺</span>
                        <?php else: ?>
                            <span class="csx-paid"><i class="fas fa-circle-check"></i> Tahsil edildi</span>
                        <?php endif; ?>
                    </div>
                    <div class="csx-prog"><div class="csx-prog-fill" style="width:<?php echo e($pct); ?>%"></div></div>
                </div>

                <div class="csx-amount">
                    <b><?php echo e(number_format((float) $s->total_amount, 2, ',', '.')); ?></b>
                    <span><?php echo e($s->currency); ?></span>
                </div>

                <i class="fas fa-chevron-right csx-go"></i>
            </article>
        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
            <div class="csx-empty">
                <i class="fas fa-cart-shopping"></i>
                <b>Bu müşterinin satışı yok</b>
                <span>Yeni bir satış oluşturduğunuzda burada listelenir.</span>
            </div>
        <?php endif; ?>
    </div>
</div>
<?php /**PATH C:\xampp\htdocs\acente\resources\views/agency/customers/partials/qv-sales.blade.php ENDPATH**/ ?>