{{-- ─── List View Partial ────────────────────────────────────────────── --}}
<div class="res-card">

    {{-- Toolbar --}}
    <form action="" method="GET" id="listFilterForm">
        <input type="hidden" name="view" value="list">
        @if(request()->has('period'))
            <input type="hidden" name="period" value="{{ request('period') }}">
        @endif
        @if(request()->has('year'))
            <input type="hidden" name="year" value="{{ request('year') }}">
        @endif
        @if(request()->has('month'))
            <input type="hidden" name="month" value="{{ request('month') }}">
        @endif
        @if(request()->has('target_type'))
            <input type="hidden" name="target_type" value="{{ request('target_type') }}">
        @endif

        <div class="tb-bar">
            {{-- Search --}}
            <div class="tb-search">
                <i class="fas fa-search"></i>
                <input type="text" name="search" value="{{ request('search') }}"
                    placeholder="Rezervasyon kodu veya müşteri adı ara..." onchange="this.form.submit()">
            </div>

            {{-- Ürün tipi filtresi --}}
            <select name="product_type_id" class="tb-filter-select" onchange="this.form.submit()" title="Ürün tipi">
                <option value="">Tüm Ürün Tipleri</option>
                @foreach($productTypes as $ptOpt)
                    <option value="{{ $ptOpt->id }}" {{ (string) request('product_type_id') === (string) $ptOpt->id ? 'selected' : '' }}>{{ $ptOpt->name }}</option>
                @endforeach
            </select>

            {{-- Ürün filtresi (satışlarda geçen ürünler) --}}
            @if(!empty($saleProducts))
                <select name="product_id" class="tb-filter-select" onchange="this.form.submit()" title="Ürün" style="max-width:220px">
                    <option value="">Tüm Ürünler</option>
                    @foreach($saleProducts as $spId => $spName)
                        <option value="{{ $spId }}" {{ (string) request('product_id') === (string) $spId ? 'selected' : '' }}>{{ $spName }}</option>
                    @endforeach
                </select>
            @endif

            {{-- Status filter --}}
            <select name="status" class="tb-filter-select" onchange="this.form.submit()">
                <option value="">Tüm Durumlar</option>
                <option value="potential" {{ request('status') === 'potential' ? 'selected' : '' }}>Potansiyel</option>
                <option value="offer_sent" {{ request('status') === 'offer_sent' ? 'selected' : '' }}>Teklif Verildi</option>
                <option value="revised" {{ request('status') === 'revised' ? 'selected' : '' }}>Revize Edildi</option>
                <option value="confirmed" {{ request('status') === 'confirmed' ? 'selected' : '' }}>Satış Tamamlandı</option>
                <option value="in_progress" {{ request('status') === 'in_progress' ? 'selected' : '' }}>Devam Ediyor</option>
                <option value="completed" {{ request('status') === 'completed' ? 'selected' : '' }}>Kapanan</option>
                <option value="cancelled" {{ request('status') === 'cancelled' ? 'selected' : '' }}>İptal Edildi</option>
                <option value="refunded" {{ request('status') === 'refunded' ? 'selected' : '' }}>İade Edildi</option>
            </select>

            {{-- Date range --}}
            <input type="date" name="date_from" value="{{ request('date_from') }}" class="tb-filter-select"
                style="width:140px;" onchange="this.form.submit()" title="Başlangıç tarihi">
            <input type="date" name="date_to" value="{{ request('date_to') }}" class="tb-filter-select"
                style="width:140px;" onchange="this.form.submit()" title="Bitiş tarihi">

            {{-- Clear filters --}}
            @if(request()->hasAny(['search', 'status', 'date_from', 'date_to', 'product_type_id', 'product_id']))
                <a href="{{ route('agency.sales.index', array_merge(request()->only(['view', 'period', 'year', 'month', 'target_type']))) }}"
                    class="tbl-action-btn" title="Filtreleri Temizle" style="color: #ef4444; border-color: #fecaca;">
                    <i class="fas fa-times"></i>
                </a>
            @endif

            {{-- Result count --}}
            @if($sales)
                <span
                    style="margin-left:auto; font-size:0.78rem; color:var(--text-muted); font-weight:600; white-space:nowrap;">
                    {{ $sales->total() }} sonuç
                </span>
            @endif
        </div>
    </form>

    {{-- Table --}}
    <div class="table-responsive">
        <table class="table res-table mb-0">
            <thead>
                <tr>
                    <th>KOD / TARİH</th>
                    <th>MÜŞTERİ</th>
                    <th>HİZMET</th>
                    <th>REZ. TARİHİ</th>
                    <th>KİŞİ</th>
                    <th>TUTAR</th>
                    <th>DURUM</th>
                </tr>
            </thead>
            <tbody>
                @forelse($sales as $r)
                    <tr class="sale-row" data-id="{{ $r->id }}">
                        {{-- Code + date --}}
                        <td>
                            <div style="font-weight:800; color:var(--cp); font-size:0.85rem; letter-spacing:0.3px;">
                                #{{ $r->sale_code }}
                            </div>
                            <div style="font-size:.72rem; color:var(--text-muted); margin-top:2px;">
                                {{ $r->created_at->format('d.m.Y H:i') }}
                            </div>
                        </td>

                        {{-- Customer --}}
                        <td>
                            <div class="d-flex align-items-center gap-2">
                                <div class="cust-avatar">
                                    {{ strtoupper(substr($r->customer?->first_name, 0, 1)) }}{{ strtoupper(substr($r->customer?->last_name, 0, 1)) }}
                                </div>
                                <div>
                                    <div style="font-weight:700; font-size:0.875rem; color:var(--text-main);">
                                        {{ $r->customer?->full_name ?? '—' }}
                                    </div>
                                    <div style="font-size:.72rem; color:var(--text-muted);">
                                        {{ $r->customer?->phone }}
                                    </div>
                                </div>
                            </div>
                        </td>

                        {{-- Service --}}
                        <td>
                            @php $svc = $r->service_summary; @endphp
                            <div class="d-flex flex-column" style="gap:3px;">
                                <span class="svc-badge" style="width:fit-content;">
                                    @if($r->productType?->icon)
                                        <i class="fas {{ $r->productType->icon }}" style="font-size:0.65rem;"></i>
                                    @endif
                                    {{ $r->productType?->name ?? '—' }}
                                </span>
                                @if($svc)
                                    {{-- Tura / kalkışa bağlı: tur adı + kalkış + kod --}}
                                    <div style="font-weight:700; font-size:0.83rem; color:var(--text-main);">
                                        <i class="fas fa-route" style="font-size:.62rem; color:#6366f1;"></i> {{ $svc['name'] ?: '—' }}
                                    </div>
                                    <div class="d-flex flex-wrap align-items-center" style="gap:3px 8px; font-size:.7rem; color:var(--text-muted);">
                                        @if($svc['has_departure'])
                                            <span style="display:inline-flex;align-items:center;gap:3px;font-weight:800;color:#047857;background:#d1fae5;border-radius:5px;padding:1px 6px;">
                                                <i class="fas fa-calendar-check" style="font-size:.56rem;"></i> Kayıtlı Kalkış
                                            </span>
                                        @else
                                            <span style="font-weight:800;color:#b45309;background:#fef3c7;border-radius:5px;padding:1px 6px;">Özel Tarih</span>
                                        @endif
                                        @if($svc['dep_code'])
                                            <span title="Kalkış kodu"><i class="fas fa-hashtag" style="font-size:.56rem;"></i> {{ $svc['dep_code'] }}</span>
                                        @endif
                                        @if(!is_null($svc['remaining']))
                                            <span title="Kalan kapasite"><i class="fas fa-chair" style="font-size:.56rem;"></i> {{ $svc['remaining'] }} boş</span>
                                        @endif
                                    </div>
                                @else
                                    {{-- Hiçbir hizmete bağlı değil: yalnızca satış başlığı --}}
                                    <div style="font-weight:700; font-size:0.83rem; color:var(--text-main);">
                                        <i class="fas fa-star" style="font-size:.62rem; color:#f59e0b;"></i> {{ $r->title ?: '—' }}
                                    </div>
                                @endif
                            </div>
                        </td>

                        {{-- Date --}}
                        <td>
                            <div style="font-weight:700; font-size:0.875rem;">
                                {{ $r->start_date?->format('d.m.Y') }}
                            </div>
                            @if($r->end_date && $r->end_date != $r->start_date)
                                <div style="font-size:.72rem; color:var(--text-muted);">
                                    → {{ $r->end_date->format('d.m.Y') }}
                                </div>
                            @endif
                        </td>

                        {{-- Pax --}}
                        <td>
                            <div style="font-weight:700; font-size:0.875rem;">{{ $r->total_pax }} Kişi</div>
                            <div style="font-size:.7rem; color:var(--text-muted);">
                                {{ $r->adult_count }}Y /
                                {{ $r->child_count }}Ç /
                                {{ $r->infant_count }}B
                            </div>
                        </td>

                        {{-- Amount --}}
                        <td>
                            <div style="font-weight:800; font-size:0.95rem; color:var(--text-main);">
                                {{ number_format($r->total_amount, 2, ',', '.') }} {{ $r->currency }}
                            </div>
                            @if($r->remaining_amount > 0)
                                <div style="font-size:.72rem; color:#ef4444; font-weight:600;">
                                    <i class="fas fa-clock" style="font-size:.6rem;"></i>
                                    Kalan: {{ number_format($r->remaining_amount, 2, ',', '.') }}
                                </div>
                            @else
                                <div style="font-size:.72rem; color:#10b981; font-weight:600;">
                                    <i class="fas fa-check" style="font-size:.6rem;"></i> Ödendi
                                </div>
                            @endif
                        </td>

                        {{-- Status --}}
                        <td>
                            <span class="sale-status-badge"
                                style="background: {{ $r->status_color }}18; color: {{ $r->status_color }}; border: 1px solid {{ $r->status_color }}30;">
                                {{ $r->status_label }}
                            </span>
                        </td>
                        {{-- Görüntüle/Düzenle butonları kaldırıldı: satıra tıklayınca quick-view açılır.
                             Ayrı show/edit sayfaları (route + blade) yedek olarak duruyor. --}}
                    </tr>
                @empty
                    <tr>
                        <td colspan="7">
                            <div class="empty-state">
                                <div class="empty-state-icon">
                                    <i class="fas fa-search"></i>
                                </div>
                                <h5>Kayıt Bulunamadı</h5>
                                <p>Arama kriterlerinize uygun satış kaydı yok.</p>
                            </div>
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    {{-- Pagination --}}
    @if($sales && $sales->hasPages())
        <div class="pagination-wrap">
            {{ $sales->links() }}
        </div>
    @endif
</div>