@extends('admin.layouts.app')
@section('title', 'Ads Moderation')
@section('content')
<div class="page-header d-print-none">
    <div class="container-xl">
        <div class="row g-2 align-items-center">
            <div class="col"><h2 class="page-title">Ads Moderation</h2></div>
        </div>
    </div>
</div>
<div class="page-body">
    <div class="card mb-3">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    <select name="status" class="form-select">
                        <option value="">All Statuses</option>
                        @foreach(['pending','approved','rejected','expired'] as $s)
                            <option value="{{ $s }}" {{ request('status')==$s?'selected':'' }}>{{ ucfirst($s) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-3">
                    <select name="type" class="form-select">
                        <option value="">All Types</option>
                        <option value="banner" {{ request('type')=='banner'?'selected':'' }}>Banner</option>
                        <option value="video" {{ request('type')=='video'?'selected':'' }}>Video</option>
                    </select>
                </div>
                <div class="col-md-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="{{ route('admin.ads.index') }}" class="btn btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h3 class="card-title">All Ads ({{ $ads->total() }})</h3></div>
        <div class="table-responsive">
            <table class="table table-vcenter card-table">
                <thead>
                    <tr><th>Ad</th><th>Vendor</th><th>Type</th><th>Amount Paid</th><th>Status</th><th>Expires</th><th>Actions</th></tr>
                </thead>
                <tbody>
                    @forelse($ads as $ad)
                    <tr>
                        <td>
                            <div class="fw-medium">{{ $ad->title }}</div>
                            <div class="text-muted small">{{ Str::limit($ad->description, 50) }}</div>
                        </td>
                        <td>
                            <div>{{ $ad->vendor?->business_name }}</div>
                            <div class="text-muted small">{{ $ad->vendor?->user?->email }}</div>
                        </td>
                        <td><span class="badge bg-{{ $ad->type==='video'?'purple':'blue' }}-lt text-capitalize">{{ $ad->type }}</span></td>
                        <td>₹{{ number_format($ad->amount_paid, 2) }}</td>
                        <td>
                            @php $sc=['pending'=>'warning','approved'=>'success','rejected'=>'danger','expired'=>'secondary']; @endphp
                            <span class="badge bg-{{ $sc[$ad->status]??'secondary' }}-lt text-capitalize">{{ $ad->status }}</span>
                        </td>
                        <td>{{ $ad->expires_at?->format('d M Y') ?? '—' }}</td>
                        <td>
                            <div class="d-flex gap-1">
                                <a href="{{ route('admin.ads.show', $ad) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                @if($ad->status === 'pending')
                                <form action="{{ route('admin.ads.approve', $ad) }}" method="POST">@csrf<button class="btn btn-sm btn-success">Approve</button></form>
                                <button class="btn btn-sm btn-danger" data-bs-toggle="modal" data-bs-target="#rejectAd{{ $ad->id }}">Reject</button>
                                @endif
                                <form action="{{ route('admin.ads.destroy', $ad) }}" method="POST">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger" onclick="return confirm('Delete this ad?')">Delete</button></form>
                            </div>
                        </td>
                    </tr>
                    <div class="modal modal-blur fade" id="rejectAd{{ $ad->id }}" tabindex="-1">
                        <div class="modal-dialog modal-sm modal-dialog-centered"><div class="modal-content">
                            <form action="{{ route('admin.ads.reject', $ad) }}" method="POST">@csrf
                                <div class="modal-header"><h5 class="modal-title">Reject Ad</h5></div>
                                <div class="modal-body"><textarea name="reason" class="form-control" rows="3" placeholder="Reason for rejection..." required></textarea></div>
                                <div class="modal-footer">
                                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                                    <button type="submit" class="btn btn-danger btn-sm">Reject</button>
                                </div>
                            </form>
                        </div></div>
                    </div>
                    @empty
                    <tr><td colspan="7" class="text-center text-muted py-4">No ads found</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-footer d-flex align-items-center">{{ $ads->links() }}</div>
    </div>
</div>
@endsection
