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

    {{-- Toolbar --}}
    <form action="" method="GET" id="listFilterForm">
        <input type="hidden" name="view" value="list">
        @if(request()->has('product_type_id'))
            <input type="hidden" name="product_type_id" value="{{ request('product_type_id') }}">
        @endif
        @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>

            {{-- 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']))
                <a href="{{ route('agency.sales.index', array_merge(request()->only(['view', 'product_type_id', '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>
                    <th style="text-align:right">İŞLEM</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>
                            <span class="svc-badge">
                                @if($r->productType?->icon)
                                    <i class="fas {{ $r->productType->icon }}" style="font-size:0.65rem;"></i>
                                @endif
                                {{ $r->productType?->name ?? '—' }}
                            </span>
                        </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>

                        {{-- Actions --}}
                        <td>
                            <div class="d-flex gap-1 justify-content-end">
                                <a href="{{ route('agency.sales.show', $r) }}" class="tbl-action-btn eye" title="Görüntüle"
                                    onclick="event.stopPropagation()">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="{{ route('agency.sales.edit', $r) }}" class="tbl-action-btn edit" title="Düzenle"
                                    onclick="event.stopPropagation()">
                                    <i class="fas fa-pen"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="8">
                            <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>