@extends('admin.layouts.app')
@section('title', 'الإعلانات')
@section('page-title', 'إدارة الإعلانات')

@section('content')

<div class="d-flex justify-content-end mb-3">
    <a href="{{ route('admin.ads.create') }}" class="btn btn-primary">
        <i class="bi bi-plus-lg me-1"></i> إضافة إعلان
    </a>
</div>

<div class="row g-3">
    @forelse($ads as $ad)
    <div class="col-md-4">
        <div class="card h-100">
            @if($ad->isVideo())
                <div class="card-img-top bg-dark d-flex align-items-center justify-content-center" style="height:160px">
                    <video src="{{ asset('storage/'.$ad->video_path) }}" muted style="max-height:160px;width:100%;object-fit:cover"></video>
                </div>
            @elseif($ad->image)
                <img src="{{ asset('storage/'.$ad->image) }}" class="card-img-top"
                     style="height:160px;object-fit:cover" alt="">
            @else
                <div class="bg-light d-flex align-items-center justify-content-center"
                     style="height:160px">
                    <i class="bi bi-image text-muted fs-1"></i>
                </div>
            @endif
            <div class="card-body">
                <div class="d-flex align-items-start justify-content-between mb-2">
                    <h6 class="mb-0">{{ $ad->title_ar ?? $ad->title }}</h6>
                    @if($ad->is_active)
                        <span class="badge badge-active">نشط</span>
                    @else
                        <span class="badge badge-suspended">موقوف</span>
                    @endif
                </div>
                <div class="small text-muted mb-2">
                    @php
                        $positions = [
                            'home_top'       => 'أعلى الرئيسية',
                            'home_middle'    => 'وسط الرئيسية',
                            'search_results' => 'نتائج البحث',
                            'doctor_profile' => 'صفحة الطبيب',
                        ];
                        $modes = ['slider' => 'سلايدر', 'splash' => 'شاشة كاملة'];
                        $mtypes = ['image' => 'صورة', 'gif' => 'GIF', 'video' => 'فيديو'];
                    @endphp
                    <span class="badge bg-secondary me-1">{{ $modes[$ad->display_mode] ?? $ad->display_mode }}</span>
                    <span class="badge bg-light text-dark border me-1">{{ $mtypes[$ad->media_type] ?? $ad->media_type }}</span>
                    <br class="d-md-none">
                    <i class="bi bi-geo-alt me-1"></i>{{ $positions[$ad->position] ?? $ad->position }}
                </div>
                <div class="small text-muted">
                    <i class="bi bi-eye me-1"></i>{{ number_format($ad->impressions) }} مشاهدة
                    &nbsp;·&nbsp;
                    <i class="bi bi-hand-index me-1"></i>{{ number_format($ad->clicks) }} نقرة
                    &nbsp;·&nbsp;
                    CTR: {{ $ad->ctr }}%
                </div>
                @if($ad->ends_at)
                    <div class="small mt-1 {{ $ad->ends_at->isPast() ? 'text-danger' : 'text-muted' }}">
                        <i class="bi bi-calendar me-1"></i>
                        ينتهي: {{ $ad->ends_at->format('Y/m/d') }}
                    </div>
                @endif
            </div>
            <div class="card-footer bg-transparent d-flex gap-2">
                <a href="{{ route('admin.ads.edit', $ad->id) }}"
                   class="btn btn-sm btn-outline-primary flex-fill">
                    <i class="bi bi-pencil me-1"></i> تعديل
                </a>
                <form method="POST" action="{{ route('admin.ads.toggle', $ad->id) }}">
                    @csrf @method('PATCH')
                    <button class="btn btn-sm btn-outline-{{ $ad->is_active ? 'warning' : 'success' }}">
                        <i class="bi bi-{{ $ad->is_active ? 'pause' : 'play' }}-fill"></i>
                    </button>
                </form>
                <form method="POST" action="{{ route('admin.ads.destroy', $ad->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>
    @empty
    <div class="col-12">
        <div class="card text-center py-5 text-muted">
            <i class="bi bi-megaphone fs-1 d-block mb-2"></i>
            <p>لا توجد إعلانات بعد</p>
            <div>
                <a href="{{ route('admin.ads.create') }}" class="btn btn-primary">
                    <i class="bi bi-plus-lg me-1"></i> أضف أول إعلان
                </a>
            </div>
        </div>
    </div>
    @endforelse
</div>

@if($ads->hasPages())
<div class="mt-3 d-flex justify-content-center">
    {{ $ads->links('pagination::bootstrap-5') }}
</div>
@endif

@endsection
