@extends('layouts.admin')

@section('title', 'Kelola Iklan')

@section('content')
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6">
    <p class="text-sm text-gray-500">Kelola iklan Adsterra atau native ads lainnya</p>
    <a href="{{ route('admin.ads.create') }}" class="inline-flex items-center justify-center bg-indigo-600 text-white px-5 py-2.5 rounded-lg text-sm font-medium hover:bg-indigo-700 whitespace-nowrap">
        + Tambah Iklan
    </a>
</div>

{{-- Stats --}}
<div class="grid grid-cols-2 md:grid-cols-4 gap-3 sm:gap-4 mb-6">
    <div class="bg-white rounded-xl border border-gray-100 p-3 sm:p-4 text-center">
        <div class="text-xl sm:text-2xl font-bold text-gray-800">{{ $ads->count() }}</div>
        <div class="text-[10px] sm:text-xs text-gray-400 mt-0.5">Total Iklan</div>
    </div>
    <div class="bg-white rounded-xl border border-gray-100 p-3 sm:p-4 text-center">
        <div class="text-xl sm:text-2xl font-bold text-green-600">{{ $ads->where('is_active', true)->count() }}</div>
        <div class="text-[10px] sm:text-xs text-gray-400 mt-0.5">Aktif</div>
    </div>
    <div class="bg-white rounded-xl border border-gray-100 p-3 sm:p-4 text-center">
        <div class="text-xl sm:text-2xl font-bold text-gray-400">{{ $ads->where('is_active', false)->count() }}</div>
        <div class="text-[10px] sm:text-xs text-gray-400 mt-0.5">Nonaktif</div>
    </div>
    <div class="bg-white rounded-xl border border-gray-100 p-3 sm:p-4 text-center">
        <div class="text-xl sm:text-2xl font-bold text-indigo-600">{{ $ads->pluck('position')->unique()->count() }}</div>
        <div class="text-[10px] sm:text-xs text-gray-400 mt-0.5">Posisi Terpakai</div>
    </div>
</div>

{{-- Group by Position --}}
@php
    $grouped = $ads->groupBy('position');
@endphp

@if($ads->count() > 0)
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
    @foreach($grouped as $position => $posAds)
        @foreach($posAds as $ad)
        <div class="bg-white rounded-xl border border-gray-100 p-4 sm:p-5 flex flex-col {{ !$ad->is_active ? 'opacity-60' : '' }}">
            {{-- Position Label --}}
            <div class="mb-2">
                <span class="inline-flex items-center text-[10px] sm:text-xs font-medium text-indigo-600 bg-indigo-50 px-2 py-0.5 rounded-full">
                    <span class="w-1.5 h-1.5 rounded-full bg-indigo-500 mr-1.5"></span>
                    {{ $positions[$position] ?? ucfirst(str_replace('_', ' ', $position)) }}
                </span>
            </div>

            {{-- Header: Name + Toggle --}}
            <div class="flex items-start justify-between gap-3 mb-2">
                <div class="flex-1 min-w-0">
                    <h4 class="font-semibold text-gray-800 text-sm truncate">{{ $ad->name }}</h4>
                    @if($ad->size)
                    <span class="inline-block px-2 py-0.5 text-xs font-mono bg-gray-100 text-gray-600 rounded mt-1">{{ $ad->size }}</span>
                    @endif
                </div>
                <form action="{{ route('admin.ads.toggle', $ad) }}" method="POST" class="flex-shrink-0">
                    @csrf @method('PATCH')
                    <button type="submit" title="{{ $ad->is_active ? 'Nonaktifkan' : 'Aktifkan' }}"
                        class="relative w-10 h-[22px] rounded-full {{ $ad->is_active ? 'bg-green-500' : 'bg-gray-300' }} focus:outline-none focus:ring-2 focus:ring-offset-1 focus:ring-indigo-400">
                        <span class="absolute top-[3px] {{ $ad->is_active ? 'right-[3px]' : 'left-[3px]' }} w-4 h-4 rounded-full bg-white shadow"></span>
                    </button>
                </form>
            </div>

            {{-- Description --}}
            @if($ad->description)
            <p class="text-xs text-gray-400 mb-2 line-clamp-2">{{ $ad->description }}</p>
            @endif

            {{-- Code Preview --}}
            <div class="bg-gray-50 rounded-lg p-2.5 mb-3 text-xs text-gray-500 max-h-16 overflow-hidden font-mono leading-relaxed break-all flex-grow">
                {{ Str::limit($ad->code, 100) }}
            </div>

            {{-- Actions --}}
            <div class="flex items-center justify-between mt-auto pt-2">
                <div class="flex items-center space-x-3">
                    <a href="{{ route('admin.ads.edit', $ad) }}" class="text-indigo-600 hover:text-indigo-800 text-sm font-medium">Edit</a>
                    <form action="{{ route('admin.ads.destroy', $ad) }}" method="POST" onsubmit="return confirm('Yakin hapus iklan ini?')">
                        @csrf @method('DELETE')
                        <button class="text-red-500 hover:text-red-700 text-sm font-medium">Hapus</button>
                    </form>
                </div>
                <span class="text-xs text-gray-400">Order: {{ $ad->sort_order }}</span>
            </div>
        </div>
        @endforeach
    @endforeach
</div>
@else
<div class="bg-white rounded-xl border border-gray-100 p-8 sm:p-12 text-center">
    <svg class="w-12 h-12 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M10.34 15.84c-.688-.06-1.386-.09-2.09-.09H7.5a4.5 4.5 0 110-9h.75c.704 0 1.402-.03 2.09-.09m0 9.18c.253.962.584 1.892.985 2.783.247.55.06 1.21-.463 1.511l-.657.38c-.551.318-1.26.117-1.527-.461a20.845 20.845 0 01-1.44-4.282m3.102.069a18.03 18.03 0 01-.59-4.59c0-1.586.205-3.124.59-4.59m0 9.18a23.848 23.848 0 018.835 2.535M10.34 6.66a23.847 23.847 0 008.835-2.535m0 0A23.74 23.74 0 0018.795 3m.38 1.125a23.91 23.91 0 011.014 5.395m-1.014 8.855c-.118.38-.245.754-.38 1.125m.38-1.125a23.91 23.91 0 001.014-5.395m0-3.46c.495.413.811 1.035.811 1.73 0 .695-.316 1.317-.811 1.73m0-3.46a24.347 24.347 0 010 3.46"/></svg>
    <p class="text-gray-500 mb-2">Belum ada iklan.</p>
    <p class="text-gray-400 text-sm">Tambahkan kode iklan dari Adsterra untuk mulai monetisasi.</p>
</div>
@endif
@endsection
