@extends('admin.layouts.app')
@section('title', 'الاشتراكات')
@section('page-title', 'الاشتراكات والباقات')

@section('content')

{{-- إحصائيات --}}
<div class="row g-3 mb-4">
    <div class="col-md-3">
        <div class="stat-card bg-white">
            <div class="stat-icon" style="background:#e8f4fd;color:#0d6efd">
                <i class="bi bi-credit-card-fill"></i>
            </div>
            <div>
                <div class="stat-value">{{ $stats['total_active'] }}</div>
                <div class="stat-label">اشتراكات نشطة</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card bg-white">
            <div class="stat-icon" style="background:#e8faf0;color:#198754">
                <i class="bi bi-cash-stack"></i>
            </div>
            <div>
                <div class="stat-value">${{ number_format($stats['month_revenue'], 0) }}</div>
                <div class="stat-label">إيرادات هذا الشهر</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card bg-white">
            <div class="stat-icon" style="background:#e8faf0;color:#198754">
                <i class="bi bi-bank"></i>
            </div>
            <div>
                <div class="stat-value">${{ number_format($stats['total_revenue'], 0) }}</div>
                <div class="stat-label">إجمالي الإيرادات</div>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="stat-card bg-white">
            <div class="stat-icon" style="background:#fff8e1;color:#f59e0b">
                <i class="bi bi-exclamation-circle-fill"></i>
            </div>
            <div>
                <div class="stat-value">{{ $stats['expiring_soon'] }}</div>
                <div class="stat-label">تنتهي خلال 7 أيام</div>
            </div>
        </div>
    </div>
</div>

<div class="d-flex justify-content-between align-items-center mb-3">
    <h6 class="mb-0 fw-semibold">سجل الاشتراكات</h6>
    <a href="{{ route('admin.subscriptions.plans') }}" class="btn btn-sm btn-outline-primary">
        <i class="bi bi-layers me-1"></i> إدارة الباقات
    </a>
</div>

{{-- فلتر --}}
<div class="card mb-3">
    <div class="card-body py-2">
        <form method="GET" class="row g-2 align-items-center">
            <div class="col-md-3">
                <select name="status" class="form-select form-select-sm">
                    <option value="">كل الحالات</option>
                    <option value="active"    {{ request('status')=='active'    ? 'selected':'' }}>نشط</option>
                    <option value="expired"   {{ request('status')=='expired'   ? 'selected':'' }}>منتهي</option>
                    <option value="cancelled" {{ request('status')=='cancelled' ? 'selected':'' }}>ملغي</option>
                    <option value="trial"     {{ request('status')=='trial'     ? 'selected':'' }}>تجريبي</option>
                </select>
            </div>
            <div class="col-md-3">
                <select name="plan_id" class="form-select form-select-sm">
                    <option value="">كل الباقات</option>
                    @foreach($plans as $plan)
                        <option value="{{ $plan->id }}" {{ request('plan_id')==$plan->id ? 'selected':'' }}>
                            {{ $plan->name_ar }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-auto">
                <button class="btn btn-sm btn-primary"><i class="bi bi-funnel"></i> تصفية</button>
                <a href="{{ route('admin.subscriptions.index') }}" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-x-lg"></i>
                </a>
            </div>
        </form>
    </div>
</div>

<div class="card table-card">
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th>الطبيب</th>
                    <th>الباقة</th>
                    <th>دورة الدفع</th>
                    <th>المبلغ</th>
                    <th>تاريخ البدء</th>
                    <th>تاريخ الانتهاء</th>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                @forelse($subscriptions as $sub)
                <tr>
                    <td>
                        <div class="fw-semibold">{{ $sub->doctor->user->name }}</div>
                        <small class="text-muted">{{ $sub->doctor->specialty }}</small>
                    </td>
                    <td><span class="badge bg-primary">{{ $sub->plan->name_ar }}</span></td>
                    <td>{{ $sub->billing_cycle === 'monthly' ? 'شهري' : 'سنوي' }}</td>
                    <td class="fw-semibold">${{ number_format($sub->amount_paid, 2) }}</td>
                    <td><small>{{ $sub->starts_at->format('Y/m/d') }}</small></td>
                    <td>
                        <small class="{{ $sub->ends_at->isPast() ? 'text-danger' : ($sub->ends_at->diffInDays() <= 7 ? 'text-warning' : 'text-muted') }}">
                            {{ $sub->ends_at->format('Y/m/d') }}
                            @if(!$sub->ends_at->isPast())
                                <span class="ms-1">({{ $sub->ends_at->diffInDays() }} يوم)</span>
                            @endif
                        </small>
                    </td>
                    <td>
                        @php
                            $statusMap = [
                                'active'    => ['badge-active',   'نشط'],
                                'expired'   => ['badge-suspended','منتهي'],
                                'cancelled' => ['bg-secondary text-white','ملغي'],
                                'trial'     => ['badge-pending',  'تجريبي'],
                            ];
                            [$cls, $label] = $statusMap[$sub->status] ?? ['bg-secondary','—'];
                        @endphp
                        <span class="badge {{ $cls }}">{{ $label }}</span>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="text-center text-muted py-5">
                        <i class="bi bi-credit-card fs-2 d-block mb-2"></i>
                        لا توجد اشتراكات
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($subscriptions->hasPages())
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">
            عرض {{ $subscriptions->firstItem() }}–{{ $subscriptions->lastItem() }} من {{ $subscriptions->total() }}
        </small>
        {{ $subscriptions->withQueryString()->links('pagination::bootstrap-5') }}
    </div>
    @endif
</div>

@endsection
