@extends('admin.layouts.app')
@php
    use Illuminate\Support\Facades\Storage;
@endphp

@section('title', 'إدارة الإعلانات')

@section('content')
<div class="container-fluid px-0">
    <!-- Header -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center">
                <h1 class="h3 mb-0" style="color: var(--text-light);">
                    <i class="fas fa-bullhorn" style="color: var(--primary-yellow);"></i>
                    إدارة الإعلانات
                </h1>
                <div class="btn-group">
                    <a href="{{ route('admin.offers-ads.ads.create') }}" class="btn btn-primary">
                        <i class="fas fa-plus"></i> إضافة إعلان جديد
                    </a>
                    <a href="{{ route('admin.offers-ads.index', ['type' => 'offers']) }}" class="btn btn-outline-primary">
                        <i class="fas fa-tags"></i> العروض
                    </a>
                    <a href="{{ route('admin.dashboard') }}" class="btn btn-outline-primary">
                        <i class="fas fa-arrow-right"></i> العودة
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- علامات التبويب -->
    <div class="row mb-4">
        <div class="col-12">
            <ul class="nav nav-tabs">
                <li class="nav-item">
                    <a class="nav-link" href="{{ route('admin.offers-ads.index', ['type' => 'offers']) }}">
                        <i class="fas fa-tags"></i> العروض
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href="{{ route('admin.offers-ads.index', ['type' => 'ads']) }}">
                        <i class="fas fa-bullhorn"></i> الإعلانات
                    </a>
                </li>
            </ul>
        </div>
    </div>

    <!-- قائمة الإعلانات -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    @if($ads->count() > 0)
                        <div class="table-responsive">
                            <table class="table table-hover" style="background: var(--card-bg) !important; color: var(--text-light) !important;">
                                <thead>
                                    <tr style="background: transparent !important;">
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">#</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">الصورة</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">العنوان</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">نوع الإعلان</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">تاريخ البداية</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">تاريخ النهاية</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">المشاهدات</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">النقرات</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">الحالة</th>
                                        <th style="color: var(--primary-yellow) !important; font-weight: 600; background: transparent !important;">الإجراءات</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($ads as $ad)
                                        <tr style="background: var(--card-bg) !important;">
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">{{ $ad->id }}</td>
                                            <td style="background: var(--card-bg) !important;">
                                                @if($ad->image)
                                                    <img src="{{ Storage::url($ad->image) }}" alt="{{ $ad->title }}" style="width: 60px; height: 60px; object-fit: cover; border-radius: 8px;" onerror="this.onerror=null; this.parentElement.innerHTML='<div style=\'width: 60px; height: 60px; background: var(--primary-yellow); border-radius: 8px; display: flex; align-items: center; justify-content: center;\'><i class=\'fas fa-bullhorn\' style=\'color: var(--dark-bg);\'></i></div>';">
                                                @else
                                                    <div style="width: 60px; height: 60px; background: var(--primary-yellow); border-radius: 8px; display: flex; align-items: center; justify-content: center;">
                                                        <i class="fas fa-bullhorn" style="color: var(--dark-bg);"></i>
                                                    </div>
                                                @endif
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                <strong>{{ $ad->title_ar ?? $ad->title }}</strong>
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                @if($ad->ad_type == 'slider')
                                                    <span class="badge badge-info" style="background-color: var(--primary-yellow); color: var(--dark-bg);">سلايدر</span>
                                                @elseif($ad->ad_type == 'banner')
                                                    <span class="badge badge-info" style="background-color: var(--primary-yellow); color: var(--dark-bg);">بانر</span>
                                                @else
                                                    <span class="badge badge-info" style="background-color: var(--primary-yellow); color: var(--dark-bg);">منبثق</span>
                                                @endif
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                <small>{{ $ad->start_date->format('Y-m-d') }}</small>
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                <small>{{ $ad->end_date->format('Y-m-d') }}</small>
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                {{ number_format($ad->views_count) }}
                                            </td>
                                            <td style="color: var(--text-light) !important; background: var(--card-bg) !important;">
                                                {{ number_format($ad->clicks_count) }}
                                            </td>
                                            <td style="background: var(--card-bg) !important;">
                                                @php
                                                    $isActive = $ad->is_active;
                                                    $startDateOnly = $ad->start_date->format('Y-m-d');
                                                    $endDateOnly = $ad->end_date->format('Y-m-d');
                                                    $nowDateOnly = now()->format('Y-m-d');
                                                    $startDateOk = $startDateOnly <= $nowDateOnly;
                                                    $endDateOk = $endDateOnly >= $nowDateOnly;
                                                    $isCurrentlyActive = $isActive && $startDateOk && $endDateOk;
                                                @endphp
                                                @if($isCurrentlyActive)
                                                    <span class="badge badge-success" style="background-color: var(--success-green); color: white;">نشط</span>
                                                @elseif(!$isActive)
                                                    <span class="badge badge-warning" style="background-color: #FFC107; color: var(--dark-bg);">معطل</span>
                                                @elseif($startDateOnly > $nowDateOnly)
                                                    <span class="badge badge-info" style="background-color: rgba(33, 150, 243, 0.8); color: white;">قادم</span>
                                                @else
                                                    <span class="badge badge-secondary" style="background-color: rgba(245, 245, 245, 0.3); color: var(--text-light);">منتهي</span>
                                                @endif
                                            </td>
                                            <td style="background: var(--card-bg) !important;">
                                                <div class="btn-group" role="group">
                                                    <a href="{{ route('admin.offers-ads.ads.edit', $ad->id) }}" class="btn btn-sm btn-primary">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    <form action="{{ route('admin.offers-ads.ads.destroy', $ad->id) }}" method="POST" style="display: inline;" onsubmit="return confirm('هل أنت متأكد من حذف هذا الإعلان؟');">
                                                        @csrf
                                                        @method('DELETE')
                                                        <button type="submit" class="btn btn-sm btn-danger">
                                                            <i class="fas fa-trash"></i>
                                                        </button>
                                                    </form>
                                                </div>
                                            </td>
                                        </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        <div class="mt-3">
                            {{ $ads->links() }}
                        </div>
                    @else
                        <div class="text-center py-5" style="color: rgba(245, 245, 245, 0.7);">
                            <i class="fas fa-bullhorn fa-3x mb-3" style="color: var(--primary-yellow);"></i>
                            <p style="color: var(--text-light);">لا توجد إعلانات حالياً</p>
                            <a href="{{ route('admin.offers-ads.ads.create') }}" class="btn btn-primary mt-3">
                                <i class="fas fa-plus"></i> إضافة إعلان جديد
                            </a>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
