@extends('layouts.admin')

@section('title', 'Active Subscriptions')

@section('content')
<div class="min-h-screen bg-slate-50">
    <div class="mx-auto max-w-7xl px-4 py-6 sm:px-6 lg:px-8">

        {{-- Header --}}
        <div class="mb-6 flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
            <div>
                <h1 class="text-2xl font-bold text-slate-800 md:text-3xl">Active Subscriptions</h1>
                <p class="mt-1 text-sm text-slate-500">
                    Currently active subscriptions with quick force-expire action.
                </p>
            </div>

            <div class="rounded-2xl border border-emerald-100 bg-emerald-50 px-4 py-3 text-sm text-emerald-700 shadow-sm">
                Total Active:
                <span class="font-bold">{{ $subscriptions->total() }}</span>
            </div>
        </div>

        {{-- Session Messages --}}
        @if(session('success'))
            <div class="mb-6 rounded-2xl border border-emerald-200 bg-emerald-50 px-4 py-3 text-sm text-emerald-700 shadow-sm">
                {{ session('success') }}
            </div>
        @endif

        @if(session('error'))
            <div class="mb-6 rounded-2xl border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-700 shadow-sm">
                {{ session('error') }}
            </div>
        @endif

        {{-- Cards --}}
        <div class="grid grid-cols-1 gap-6 md:grid-cols-2 xl:grid-cols-3">
            @forelse($subscriptions as $item)
                <div class="group rounded-3xl border border-slate-200 bg-white p-6 shadow-sm transition duration-300 hover:-translate-y-1 hover:shadow-lg">
                    
                    <div class="flex items-start justify-between gap-4">
                        <div>
                            <h3 class="text-lg font-bold text-slate-800">{{ $item->user->name ?? 'N/A' }}</h3>
                            <p class="text-sm text-slate-500">{{ $item->user->email ?? 'N/A' }}</p>
                        </div>

                        <span class="inline-flex rounded-full bg-emerald-100 px-3 py-1 text-xs font-bold uppercase tracking-wide text-emerald-700">
                            Active
                        </span>
                    </div>

                    <div class="mt-5 space-y-3">
                        <div class="rounded-2xl border border-slate-100 bg-slate-50 p-4">
                            <div class="text-xs text-slate-500">Plan</div>
                            <div class="mt-1 font-semibold text-slate-800">
                                {{ $item->plan->name ?? 'N/A' }}
                            </div>
                        </div>

                        <div class="rounded-2xl border border-slate-100 bg-slate-50 p-4">
                            <div class="text-xs text-slate-500">Validity</div>
                            <div class="mt-1 text-sm text-slate-700">
                                {{ $item->start_date ? $item->start_date->format('d M, Y') : 'N/A' }}
                                -
                                {{ $item->end_date ? $item->end_date->format('d M, Y') : 'N/A' }}
                            </div>
                        </div>

                        <div class="rounded-2xl border border-slate-100 bg-slate-50 p-4">
                            <div class="text-xs text-slate-500">Remaining Days</div>
                            <div class="mt-1 font-semibold text-emerald-700">
                                {{ $item->end_date ? max(0, now()->diffInDays($item->end_date, false)) : 0 }} days
                            </div>
                        </div>

                        <div class="rounded-2xl border border-slate-100 bg-slate-50 p-4">
                            <div class="text-xs text-slate-500">Type</div>
                            <div class="mt-1">
                                @if($item->is_trial)
                                    <span class="inline-flex rounded-full bg-blue-100 px-3 py-1 text-xs font-bold text-blue-700">
                                        Trial
                                    </span>
                                @else
                                    <span class="inline-flex rounded-full bg-violet-100 px-3 py-1 text-xs font-bold text-violet-700">
                                        Paid
                                    </span>
                                @endif
                            </div>
                        </div>
                    </div>

                    <div class="mt-6 border-t border-slate-100 pt-4">
                        <form action="{{ route('admin.subscriptions.force_expire', $item->id) }}"
                              method="POST"
                              class="force-expire-form">
                            @csrf
                            @method('PATCH')

                            <button type="submit"
                                    class="inline-flex w-full items-center justify-center rounded-2xl bg-rose-600 px-4 py-3 text-sm font-semibold text-white shadow-sm transition hover:bg-rose-700">
                                Force Expire
                            </button>
                        </form>
                    </div>
                </div>
            @empty
                <div class="col-span-full rounded-3xl border border-slate-200 bg-white p-12 text-center shadow-sm">
                    <div class="mx-auto flex h-16 w-16 items-center justify-center rounded-full bg-slate-100 text-2xl">
                        📭
                    </div>
                    <h3 class="mt-4 text-lg font-semibold text-slate-700">No active subscriptions found</h3>
                    <p class="mt-2 text-sm text-slate-500">
                        Active subscription records will appear here when available.
                    </p>
                </div>
            @endforelse
        </div>

        {{-- Pagination --}}
        @if($subscriptions->hasPages())
            <div class="mt-6">
                {{ $subscriptions->links() }}
            </div>
        @endif
    </div>
</div>
@endsection

@section('scripts')
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
document.addEventListener('DOMContentLoaded', function () {
    document.querySelectorAll('.force-expire-form').forEach(function (form) {
        form.addEventListener('submit', function (e) {
            e.preventDefault();

            Swal.fire({
                title: 'Force expire subscription?',
                text: 'This will immediately mark the subscription as expired.',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#dc2626',
                cancelButtonColor: '#64748b',
                confirmButtonText: 'Yes, expire it',
                cancelButtonText: 'No, keep it'
            }).then((result) => {
                if (result.isConfirmed) {
                    form.submit();
                }
            });
        });
    });

    @if(session('success'))
        Swal.fire({
            icon: 'success',
            title: 'Success',
            text: @json(session('success')),
            confirmButtonColor: '#2563eb'
        });
    @endif

    @if(session('error'))
        Swal.fire({
            icon: 'error',
            title: 'Error',
            text: @json(session('error')),
            confirmButtonColor: '#2563eb'
        });
    @endif
});
</script>
@endsection