@extends('admin.layouts.app')
@section('title', 'Abonelikler')
@section('page-title', 'Abonelikler')

@section('content')

<div class="page-header">
    <div>
        <h5 style="font-size:18px;font-weight:700;color:var(--text);margin:0">
            Abonelikler
            <span style="font-size:13px;font-weight:600;color:var(--text2);margin-left:6px">{{ number_format($subscriptions->total()) }} toplam</span>
        </h5>
    </div>
</div>

{{-- Filtreler --}}
<div class="filter-card mb-3">
    <form method="GET" class="d-flex flex-wrap gap-2 align-items-end">
        <div class="search-input-wrap flex-grow-1" style="min-width:200px;max-width:320px">
            <i class="bi bi-search"></i>
            <input type="text" name="search" class="form-control" placeholder="Kullanıcı adı veya e-posta..." value="{{ request('search') }}">
        </div>
        <select name="status" class="form-select" style="width:auto;min-width:150px">
            <option value="">Tüm Durumlar</option>
            <option value="active"    {{ request('status')=='active'    ?'selected':'' }}>Aktif</option>
            <option value="expired"   {{ request('status')=='expired'   ?'selected':'' }}>Süresi Dolmuş</option>
            <option value="cancelled" {{ request('status')=='cancelled' ?'selected':'' }}>İptal Edilmiş</option>
        </select>
        <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary btn-sm"><i class="bi bi-funnel"></i> Filtrele</button>
            <a href="{{ route('admin.subscriptions.index') }}" class="btn btn-secondary btn-sm"><i class="bi bi-x-lg"></i></a>
        </div>
    </form>
</div>

<div class="table-wrap">
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th style="padding-left:20px">Kullanıcı</th>
                    <th>Plan</th>
                    <th>Başlangıç</th>
                    <th>Bitiş</th>
                    <th style="text-align:center">Durum</th>
                    <th>Ödeme</th>
                    <th style="text-align:right;padding-right:20px">İşlem</th>
                </tr>
            </thead>
            <tbody>
                @forelse($subscriptions as $sub)
                <tr>
                    <td style="padding-left:20px">
                        <div class="d-flex align-items-center gap-2">
                            <div class="user-avatar sm">{{ strtoupper(substr($sub->user?->name ?? '?', 0, 1)) }}</div>
                            <div>
                                <div style="font-size:13px;font-weight:600;color:var(--text)">{{ $sub->user?->name ?? '—' }}</div>
                                <div style="font-size:11.5px;color:var(--text2)">{{ $sub->user?->email ?? '' }}</div>
                            </div>
                        </div>
                    </td>
                    <td style="font-size:13px;font-weight:600;color:var(--text)">{{ $sub->plan?->name ?? '—' }}</td>
                    <td style="font-size:13px;color:var(--text2)">{{ $sub->starts_at?->format('d.m.Y') ?? '—' }}</td>
                    <td style="font-size:13px;color:var(--text2)">{{ $sub->expires_at?->format('d.m.Y') ?? '—' }}</td>
                    <td style="text-align:center">
                        @if($sub->status === 'active')
                            <span class="badge bg-success">Aktif</span>
                        @elseif($sub->status === 'expired')
                            <span class="badge bg-secondary">Sona Erdi</span>
                        @else
                            <span class="badge bg-danger">İptal</span>
                        @endif
                    </td>
                    <td style="font-size:12.5px;color:var(--text2)">{{ $sub->payment_method ?? '—' }}</td>
                    <td style="text-align:right;padding-right:20px">
                        @if($sub->status === 'active')
                        <form method="POST" action="{{ route('admin.subscriptions.destroy', $sub) }}"
                              onsubmit="return confirm('Bu aboneliği iptal etmek istediğinize emin misiniz?')">
                            @csrf @method('DELETE')
                            <button type="submit" class="btn btn-sm btn-outline-danger">
                                <i class="bi bi-x-circle"></i> İptal Et
                            </button>
                        </form>
                        @else
                            <span style="font-size:12px;color:var(--text3)">—</span>
                        @endif
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7">
                        <div class="empty-state">
                            <i class="bi bi-credit-card"></i>
                            <h6>Abonelik bulunamadı</h6>
                            <p>Filtrelerinizi değiştirin.</p>
                        </div>
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="mt-3">
    {{ $subscriptions->links('admin.partials.pagination') }}
</div>

@endsection
