@extends('layouts.admin')

@section('title', 'Newsletter Subscribers')

@section('content')
<div class="max-w-[1200px] mx-auto">
    <div class="flex flex-col md:flex-row md:items-center justify-between gap-6 mb-10">
        <div>
            <p class="text-xs text-gray-500 mb-1">Marketing</p>
            <h1 class="text-2xl font-semibold text-gray-900 tracking-tight">Newsletter subscribers</h1>
            <p class="text-gray-500 text-sm mt-1">Monitor and manage email subscribers collected from your storefront.</p>
        </div>
        <div class="flex items-center gap-3">
            <button onclick="window.print()" class="px-4 py-2 bg-white border border-gray-200 rounded-lg text-sm text-gray-600 hover:text-gray-900 hover:bg-gray-50 transition flex items-center gap-2">
                <i class="fas fa-print text-xs"></i>
                Print
            </button>
        </div>
    </div>

    <div class="bg-white p-4 rounded-2xl border border-gray-100 shadow-sm mb-8">
        <form action="{{ route('admin.newsletter-subscriptions.index') }}" method="GET" class="flex flex-col md:flex-row gap-4">
            <div class="flex-1 relative">
                <i class="fas fa-search absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 text-xs"></i>
                <input type="text" name="search" value="{{ $search }}" 
                    class="w-full pl-10 pr-4 py-3 bg-white border border-gray-200 rounded-xl text-sm focus:border-brand focus:ring-2 focus:ring-brand/20 transition placeholder:text-gray-400"
                    placeholder="Search by email address...">
            </div>
            <div class="flex gap-2">
                <button type="submit" class="bg-brand text-white px-5 py-3 rounded-lg text-sm font-medium hover:bg-brand/90 transition">
                    Apply filter
                </button>
                @if($search)
                    <a href="{{ route('admin.newsletter-subscriptions.index') }}" class="bg-gray-100 text-gray-600 px-5 py-3 rounded-lg text-sm hover:bg-gray-200 transition flex items-center justify-center">
                        Reset
                    </a>
                @endif
            </div>
        </form>
    </div>

    <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left border-collapse">
                <thead>
                    <tr class="bg-gray-50 border-b border-gray-100">
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Email</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">IP address</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">User agent</th>
                        <th class="px-6 py-4 text-xs font-medium text-gray-500">Subscribed at</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @forelse($subscriptions as $subscription)
                    <tr class="hover:bg-gray-50 transition-colors group">
                        <td class="px-6 py-4">
                            <div class="flex items-center gap-3">
                                <div class="w-9 h-9 rounded-xl bg-gray-50 flex items-center justify-center text-gray-500 font-semibold text-xs group-hover:bg-brand/10 group-hover:text-brand transition">
                                    <i class="fas fa-envelope"></i>
                                </div>
                                <div>
                                    <p class="font-medium text-gray-900 group-hover:text-brand transition-colors">{{ $subscription->email }}</p>
                                </div>
                            </div>
                        </td>
                        <td class="px-6 py-4 text-sm text-gray-700">
                            {{ $subscription->ip_address ?: '—' }}
                        </td>
                        <td class="px-6 py-4 text-xs text-gray-500 max-w-xs">
                            <span class="line-clamp-2">{{ $subscription->user_agent ?: '—' }}</span>
                        </td>
                        <td class="px-6 py-4 text-sm text-gray-700">
                            <div class="flex flex-col">
                                <span class="text-xs font-medium text-gray-900">{{ $subscription->created_at->format('M d, Y H:i') }}</span>
                                <span class="text-xs text-gray-500 mt-1">{{ $subscription->created_at->diffForHumans() }}</span>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="4" class="px-6 py-16 text-center">
                            <div class="flex flex-col items-center justify-center">
                                <div class="w-20 h-20 bg-gray-50 rounded-2xl flex items-center justify-center mb-4">
                                    <i class="fas fa-user-slash text-gray-200 text-3xl"></i>
                                </div>
                                <h3 class="text-base font-semibold text-gray-900 mb-1">No subscribers yet</h3>
                                <p class="text-gray-500 text-sm max-w-xs mx-auto">Once customers join your newsletter, they will appear in this list.</p>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($subscriptions->hasPages())
        <div class="px-6 py-5 bg-gray-50 border-t border-gray-100">
            {{ $subscriptions->links() }}
        </div>
        @endif
    </div>
</div>
@endsection

