{{--
    Müşteri kartı → Faturalar sekmesi.
    Kişiye/şirkete kesilmiş tüm faturalar klasik tablo düzeninde; satırlar
    tıklanınca fatura kartı açılır. Hücrelerin sağındaki simgeyle kopyalanır.

    Beklenen: $customer, $invoices
--}}
@include('agency.partials.qv-table')

@php
    $totalAmount = $invoices->whereNotIn('status', ['cancelled'])->sum('amount');
    $totalPaid = $invoices->whereNotIn('status', ['cancelled'])->sum('paid_amount');
    $officialPending = $invoices->where('official_status', 'pending')->where('status', '!=', 'cancelled')->count();
@endphp

<div class="cix-wrap">

    <div class="cix-summary">
        <div class="cix-sum">
            <span class="cix-sum-lbl">Fatura</span>
            <span class="cix-sum-val">{{ $invoices->count() }}</span>
        </div>
        <div class="cix-sum">
            <span class="cix-sum-lbl">Toplam Tutar</span>
            <span class="cix-sum-val">{{ number_format((float) $totalAmount, 2, ',', '.') }}₺</span>
        </div>
        <div class="cix-sum ok">
            <span class="cix-sum-lbl">Tahsil Edilen</span>
            <span class="cix-sum-val">{{ number_format((float) $totalPaid, 2, ',', '.') }}₺</span>
        </div>
        <div class="cix-sum {{ $officialPending ? 'warn' : '' }}">
            <span class="cix-sum-lbl">Resmi Fatura Bekleyen</span>
            <span class="cix-sum-val">{{ $officialPending }}</span>
        </div>
    </div>

    <div class="qvt-bar">
        <span class="qvt-bar-info"><i class="fas fa-file-invoice-dollar"></i> Bu müşteriye kesilen faturalar</span>
        <span class="qvt-bar-r">
            <button type="button" class="cix-add" onclick="openQuickView('{{ route('agency.invoices.quick-create') }}')">
                <i class="fas fa-plus"></i> Fatura Kes
            </button>
        </span>
    </div>

    <div class="qvt-box">
        <div class="qvt-scroll">
            <table class="qvt">
                <thead>
                    <tr>
                        <th>Fatura No</th>
                        <th>Tarih</th>
                        <th>Muhatap</th>
                        <th>Durum</th>
                        <th>Resmi</th>
                        <th>Satış</th>
                        <th class="ta-r">Tutar</th>
                        <th class="ta-r">Kalan</th>
                        <th class="qvt-w1"></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($invoices as $inv)
                        @php
                            $remaining = (float) $inv->remaining_amount;
                            $copyAll = implode("\n", array_filter([
                                'Fatura No: ' . $inv->invoice_number,
                                'Tarih: ' . $inv->issue_date?->format('d.m.Y'),
                                'Muhatap: ' . trim($inv->billing_type_label . ' · ' . $inv->billing_name, ' ·'),
                                'Durum: ' . $inv->status_label,
                                'Tutar: ' . number_format((float) $inv->amount, 2, ',', '.') . ' ' . $inv->currency,
                                $remaining > 0 ? 'Kalan: ' . number_format($remaining, 2, ',', '.') . ' ' . $inv->currency : null,
                            ]));
                        @endphp
                        <tr class="qvt-click {{ $inv->status === 'cancelled' ? 'is-muted' : '' }}"
                            data-quickview-url="{{ route('agency.invoices.quick-view', $inv) }}"
                            title="Fatura kartını aç">
                            <td class="qvt-nowrap">
                                <span class="qvt-c qvt-mono qvt-strong" data-copy="{{ $inv->invoice_number }}">
                                    @if($inv->invoice_type === 'refund')<i class="fas fa-rotate-left qvt-dim" title="İade faturası"></i>@endif
                                    {{ $inv->invoice_number }}
                                </span>
                            </td>
                            <td class="qvt-mono qvt-nowrap">{{ $inv->issue_date?->format('d.m.Y') ?: '—' }}</td>
                            <td class="qvt-trunc" title="{{ $inv->billing_type_label }} · {{ $inv->billing_name }}">
                                <span class="qvt-c" data-copy="{{ $inv->billing_name }}">{{ $inv->billing_name ?: '—' }}</span>
                                <span class="qvt-dim"> · {{ $inv->billing_type_label }}</span>
                            </td>
                            <td><span class="qvt-badge" style="--sc:{{ $inv->status_color }}">{{ $inv->status_label }}</span></td>
                            <td>
                                @if($inv->official_status === 'pending' && $inv->status !== 'cancelled')
                                    <span class="qvt-chip warn"><i class="fas fa-hourglass-half"></i> bekliyor</span>
                                @elseif($inv->official_status === 'issued')
                                    <span class="qvt-chip ok"><i class="fas fa-stamp"></i> kesildi</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td>
                                @if($inv->sale)
                                    <span class="qvt-link" title="Satış kartını aç"
                                          onclick="event.stopPropagation(); openSaleDrawer({{ $inv->sale_id }})">#{{ $inv->sale->sale_code }}</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td class="ta-r"><span class="qvt-num">{{ number_format((float) $inv->amount, 2, ',', '.') }} <small>{{ $inv->currency }}</small></span></td>
                            <td class="ta-r">
                                @if($remaining > 0)
                                    <span class="qvt-num neg">{{ number_format($remaining, 2, ',', '.') }}</span>
                                @elseif((float) $inv->amount > 0)
                                    <span class="qvt-chip ok"><i class="fas fa-circle-check"></i> tahsil edildi</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td class="qvt-w1">
                                <button type="button" class="qvt-copyall" data-copy="{{ $copyAll }}" title="Fatura bilgilerini kopyala">
                                    <i class="fas fa-clone"></i>
                                </button>
                            </td>
                        </tr>
                    @empty
                        <tr class="qvt-empty">
                            <td colspan="9">
                                <i class="fas fa-file-invoice"></i>
                                <b>Fatura yok</b>
                                <span>Bu müşteriye henüz fatura kesilmemiş.</span>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@once
<style>
    .cix-wrap { font-family: 'Manrope', system-ui, sans-serif; }

    .cix-summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 9px; margin-bottom: .9rem; }
    .cix-sum { padding: .65rem .85rem; border-radius: 13px; background: #fff; border: 1px solid #e8edf4; border-left: 3px solid #cbd5e1; }
    .cix-sum.ok { border-left-color: #10b981; }
    .cix-sum.warn { border-left-color: #f59e0b; }
    .cix-sum-lbl { display: block; font-size: .64rem; font-weight: 800; text-transform: uppercase; letter-spacing: .06em; color: #94a3b8; }
    .cix-sum-val { display: block; margin-top: 2px; font-family: 'JetBrains Mono', monospace; font-size: 1.02rem; font-weight: 700; color: #0f172a; }
    .cix-sum.ok .cix-sum-val { color: #047857; }
    .cix-sum.warn .cix-sum-val { color: #b45309; }

    .cix-add {
        display: inline-flex; align-items: center; gap: 6px; border: none; cursor: pointer;
        background: linear-gradient(135deg, #059669, #047857); color: #fff;
        font-family: inherit; font-size: .76rem; font-weight: 800; padding: .45rem .95rem; border-radius: 10px;
        box-shadow: 0 6px 16px -8px rgba(5, 150, 105, .8); transition: all .16s ease;
    }
    .cix-add:hover { transform: translateY(-1px); filter: brightness(1.07); }
</style>
@endonce
