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

@section('content')

<div class="row g-3 mb-4">
    @foreach($plans as $plan)
    <div class="col-md-4">
        <div class="card h-100">
            <div class="card-header d-flex align-items-center justify-content-between">
                <span class="fw-bold">{{ $plan->name_ar }}</span>
                <div class="d-flex gap-1">
                    <button class="btn btn-sm btn-outline-primary"
                            onclick="openEdit({{ $plan->toJson() }})">
                        <i class="bi bi-pencil"></i>
                    </button>
                    <form method="POST" action="{{ route('admin.subscriptions.plans.destroy', $plan->id) }}">
                        @csrf @method('DELETE')
                        <button class="btn btn-sm btn-outline-danger"
                                onclick="return confirm('حذف الباقة؟')">
                            <i class="bi bi-trash"></i>
                        </button>
                    </form>
                </div>
            </div>
            <div class="card-body">
                <div class="d-flex gap-3 mb-3">
                    <div class="text-center">
                        <div class="fs-4 fw-bold text-primary">${{ $plan->price_monthly }}</div>
                        <small class="text-muted">شهرياً</small>
                    </div>
                    <div class="text-center">
                        <div class="fs-4 fw-bold text-success">${{ $plan->price_yearly }}</div>
                        <small class="text-muted">سنوياً</small>
                    </div>
                </div>
                <ul class="list-unstyled small">
                    <li class="mb-1">
                        <i class="bi bi-people-fill text-primary me-1"></i>
                        {{ $plan->max_daily_patients }} مريض/يوم
                    </li>
                    <li class="mb-1">
                        <i class="bi bi-{{ $plan->video_allowed ? 'check-circle-fill text-success' : 'x-circle-fill text-danger' }} me-1"></i>
                        {{ $plan->video_allowed ? 'رفع فيديوهات ('.$plan->max_videos.')' : 'بدون فيديوهات' }}
                    </li>
                    <li class="mb-1">
                        <i class="bi bi-{{ !$plan->show_ads ? 'check-circle-fill text-success' : 'x-circle-fill text-danger' }} me-1"></i>
                        {{ !$plan->show_ads ? 'بدون إعلانات' : 'تظهر إعلانات' }}
                    </li>
                    <li class="mb-1">
                        <i class="bi bi-{{ $plan->priority_listing ? 'check-circle-fill text-success' : 'x-circle-fill text-danger' }} me-1"></i>
                        {{ $plan->priority_listing ? 'أولوية في البحث' : 'ترتيب عادي' }}
                    </li>
                </ul>
                <div class="mt-2">
                    @if($plan->is_active)
                        <span class="badge badge-active">نشطة</span>
                    @else
                        <span class="badge badge-suspended">معطلة</span>
                    @endif
                    <span class="badge bg-light text-dark ms-1">
                        {{ $plan->subscriptions()->where('status','active')->count() }} مشترك
                    </span>
                </div>
            </div>
        </div>
    </div>
    @endforeach

    {{-- بطاقة إضافة باقة جديدة --}}
    <div class="col-md-4">
        <div class="card h-100 border-dashed d-flex align-items-center justify-content-center"
             style="border: 2px dashed #dee2e6; cursor:pointer; min-height:200px"
             onclick="openAdd()">
            <div class="text-center text-muted p-4">
                <i class="bi bi-plus-circle fs-1 d-block mb-2"></i>
                <span class="fw-semibold">إضافة باقة جديدة</span>
            </div>
        </div>
    </div>
</div>

{{-- Modal إضافة/تعديل --}}
<div class="modal fade" id="planModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle">إضافة باقة جديدة</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" id="planForm">
                @csrf
                <input type="hidden" name="_method" id="formMethod" value="POST">
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label">اسم الباقة (عربي)</label>
                            <input type="text" name="name_ar" id="f_name_ar" class="form-control" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">اسم الباقة (إنجليزي)</label>
                            <input type="text" name="name" id="f_name" class="form-control" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">السعر الشهري ($)</label>
                            <input type="number" name="price_monthly" id="f_price_monthly" class="form-control" step="0.01" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">السعر السنوي ($)</label>
                            <input type="number" name="price_yearly" id="f_price_yearly" class="form-control" step="0.01" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">الحد الأقصى للمرضى يومياً</label>
                            <input type="number" name="max_daily_patients" id="f_max_patients" class="form-control" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label">الترتيب</label>
                            <input type="number" name="sort_order" id="f_sort" class="form-control" value="0">
                        </div>
                        <div class="col-12">
                            <div class="row g-2">
                                <div class="col-auto">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" name="video_allowed"
                                               id="f_video" value="1" onchange="toggleVideos(this)">
                                        <label class="form-check-label" for="f_video">رفع فيديوهات</label>
                                    </div>
                                </div>
                                <div class="col-auto" id="videoCountWrap" style="display:none">
                                    <input type="number" name="max_videos" id="f_max_videos"
                                           class="form-control form-control-sm" placeholder="عدد الفيديوهات" style="width:140px">
                                </div>
                                <div class="col-auto">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" name="show_ads"
                                               id="f_ads" value="1">
                                        <label class="form-check-label" for="f_ads">تظهر إعلانات</label>
                                    </div>
                                </div>
                                <div class="col-auto">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" name="priority_listing"
                                               id="f_priority" value="1">
                                        <label class="form-check-label" for="f_priority">أولوية في البحث</label>
                                    </div>
                                </div>
                                <div class="col-auto">
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" name="is_active"
                                               id="f_active" value="1" checked>
                                        <label class="form-check-label" for="f_active">نشطة</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">إلغاء</button>
                    <button type="submit" class="btn btn-primary">حفظ</button>
                </div>
            </form>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
const modal = new bootstrap.Modal(document.getElementById('planModal'));

function openAdd() {
    document.getElementById('modalTitle').textContent = 'إضافة باقة جديدة';
    document.getElementById('planForm').action = '{{ route("admin.subscriptions.plans.store") }}';
    document.getElementById('formMethod').value = 'POST';
    document.getElementById('planForm').reset();
    modal.show();
}

function openEdit(plan) {
    document.getElementById('modalTitle').textContent = 'تعديل الباقة';
    document.getElementById('planForm').action = `/admin/subscriptions/plans/${plan.id}`;
    document.getElementById('formMethod').value = 'PUT';
    document.getElementById('f_name_ar').value     = plan.name_ar;
    document.getElementById('f_name').value         = plan.name;
    document.getElementById('f_price_monthly').value = plan.price_monthly;
    document.getElementById('f_price_yearly').value  = plan.price_yearly;
    document.getElementById('f_max_patients').value  = plan.max_daily_patients;
    document.getElementById('f_sort').value           = plan.sort_order;
    document.getElementById('f_video').checked         = !!plan.video_allowed;
    document.getElementById('f_max_videos').value      = plan.max_videos;
    document.getElementById('f_ads').checked            = !!plan.show_ads;
    document.getElementById('f_priority').checked       = !!plan.priority_listing;
    document.getElementById('f_active').checked          = !!plan.is_active;
    toggleVideos(document.getElementById('f_video'));
    modal.show();
}

function toggleVideos(el) {
    document.getElementById('videoCountWrap').style.display = el.checked ? 'block' : 'none';
}
</script>
@endpush
