{{--
    Gömülü ödeme listesi — satış, müşteri, tedarikçi ve fatura kartlarında
    aynı bileşen kullanılır. Klasik tablo düzeni; satır tıklanınca ödemenin
    quick-view kartı açılır, böylece kartlar arasında ileri-geri gezilebilir.

    Beklenen:
      $payments   : Payment koleksiyonu
      $createUrl  : "Ödeme Ekle" bağlantısı (null ise buton çıkmaz)
      $summary    : ['label' => ..., 'value' => ..., 'tone' => 'ok|warn|neutral'] listesi (opsiyonel)
      $emptyText  : boş durum metni (opsiyonel)
--}}
@include('agency.partials.qv-table')

@php
    $summary = $summary ?? [];
    $emptyText = $emptyText ?? 'Bu kayda ait ödeme hareketi yok.';
    $createUrl = $createUrl ?? null;
@endphp

<div class="pmt-wrap">

    @if(count($summary))
        <div class="pmt-summary">
            @foreach($summary as $s)
                <div class="pmt-sum {{ $s['tone'] ?? 'neutral' }}">
                    <span class="pmt-sum-lbl">{{ $s['label'] }}</span>
                    <span class="pmt-sum-val">{{ $s['value'] }}</span>
                </div>
            @endforeach
        </div>
    @endif

    <div class="qvt-bar">
        <span class="qvt-bar-info"><i class="fas fa-money-bill-transfer"></i> {{ $payments->count() }} hareket</span>
        @if($createUrl)
            <span class="qvt-bar-r">
                <button type="button" class="pmt-add" onclick="openQuickView('{{ $createUrl }}')">
                    <i class="fas fa-plus"></i> Ödeme Ekle
                </button>
            </span>
        @endif
    </div>

    <div class="qvt-box">
        <div class="qvt-scroll">
            <table class="qvt">
                <thead>
                    <tr>
                        <th>Tarih</th>
                        <th>Yöntem</th>
                        <th>Muhatap</th>
                        <th>Referans</th>
                        <th>Açıklama</th>
                        <th>Fatura</th>
                        <th>Durum</th>
                        <th class="ta-r">Tutar</th>
                        <th class="qvt-w1"></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($payments as $p)
                        @php
                            $docCount = $p->documents_count ?? $p->documents()->count();
                            $copyAll = implode("\n", array_filter([
                                'Tarih: ' . $p->paid_at?->format('d.m.Y H:i'),
                                'Yöntem: ' . $p->method_label,
                                'Muhatap: ' . $p->counterparty_label,
                                $p->reference_no ? 'Referans: ' . $p->reference_no : null,
                                'Tutar: ' . ($p->direction === 'in' ? '+' : '−') . number_format((float) $p->amount, 2, ',', '.') . ' ' . $p->currency,
                                $p->invoice ? 'Fatura: ' . $p->invoice->invoice_number : null,
                                $p->description ? 'Açıklama: ' . $p->description : null,
                            ]));
                        @endphp
                        <tr class="qvt-click pmt-tr dir-{{ $p->direction }}"
                            onclick="openQuickView('{{ route('agency.payments.quick-view', $p) }}')"
                            title="Ödeme kartını aç">
                            <td class="qvt-mono qvt-nowrap">{{ $p->paid_at?->format('d.m.Y H:i') ?: '—' }}</td>
                            <td class="qvt-nowrap">
                                <i class="fas {{ $p->method_icon }} qvt-dim"></i>
                                <span class="qvt-strong">{{ $p->method_label }}</span>
                                @if($docCount)
                                    <span class="qvt-chip" title="{{ $docCount }} belge"><i class="fas fa-paperclip"></i>{{ $docCount }}</span>
                                @endif
                            </td>
                            <td class="qvt-trunc" title="{{ $p->counterparty_label }}">
                                @if($p->counterparty_quick_view_url)
                                    <span class="qvt-link" title="Kartını aç"
                                          onclick="event.stopPropagation(); openQuickView('{{ $p->counterparty_quick_view_url }}')">{{ $p->counterparty_label }}</span>
                                @else
                                    {{ $p->counterparty_label ?: '—' }}
                                @endif
                            </td>
                            <td class="qvt-nowrap">
                                @if($p->reference_no)
                                    <span class="qvt-c qvt-mono" data-copy="{{ $p->reference_no }}">{{ $p->reference_no }}</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td class="qvt-trunc qvt-dim" title="{{ $p->description }}">{{ $p->description ?: '—' }}</td>
                            <td class="qvt-nowrap">
                                @if($p->invoice)
                                    <span class="qvt-link qvt-mono" title="Fatura kartını aç"
                                          onclick="event.stopPropagation(); openQuickView('{{ route('agency.invoices.quick-view', $p->invoice) }}')">{{ $p->invoice->invoice_number }}</span>
                                @elseif($p->sale_id && $p->direction === 'in')
                                    <span class="qvt-chip warn" title="Bu tahsilat bir faturaya bağlanmadı">faturasız</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td>
                                @if($p->status !== 'completed')
                                    <span class="qvt-badge" style="--sc:{{ $p->status_color }}">{{ $p->status_label }}</span>
                                @else
                                    <span class="qvt-none">—</span>
                                @endif
                            </td>
                            <td class="ta-r">
                                <span class="qvt-num {{ $p->direction === 'in' ? 'pos' : 'neg' }}">
                                    {{ $p->direction === 'in' ? '+' : '−' }}{{ number_format((float) $p->amount, 2, ',', '.') }}
                                    <small>{{ $p->currency }}</small>
                                </span>
                            </td>
                            <td class="qvt-w1">
                                <button type="button" class="qvt-copyall" data-copy="{{ $copyAll }}" title="Ödeme bilgilerini kopyala">
                                    <i class="fas fa-clone"></i>
                                </button>
                            </td>
                        </tr>
                    @empty
                        <tr class="qvt-empty">
                            <td colspan="9">
                                <i class="fas fa-money-bill-wave"></i>
                                <b>Ödeme kaydı yok</b>
                                <span>{{ $emptyText }}</span>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    @if($createUrl && $payments->isEmpty())
        <div class="pmt-empty-cta">
            <button type="button" class="pmt-add" onclick="openQuickView('{{ $createUrl }}')">
                <i class="fas fa-plus"></i> İlk Ödemeyi Ekle
            </button>
        </div>
    @endif
</div>

{{-- Quick-view parçaları layout'suz döndüğü için stil satır içi verilir --}}
@once
<style>
    .pmt-wrap { font-family: 'Manrope', system-ui, sans-serif; }

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

    .pmt-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;
    }
    .pmt-add:hover { transform: translateY(-1px); filter: brightness(1.07); }

    /* Yön rayı: tahsilat yeşil, ödeme kırmızı — tablo satırının sol kenarında */
    .qvt tbody tr.pmt-tr > td:first-child { box-shadow: inset 3px 0 0 var(--pmt-rail, transparent); }
    .qvt tbody tr.pmt-tr.dir-in { --pmt-rail: #10b981; }
    .qvt tbody tr.pmt-tr.dir-out { --pmt-rail: #f43f5e; }

    .pmt-empty-cta { display: flex; justify-content: center; margin-top: .8rem; }
</style>
@endonce
