@extends('layouts.admin')

@section('title', 'Plans')

@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-4 md:flex-row md:items-center md:justify-between">
            <div>
                <h1 class="text-2xl font-bold text-slate-800 md:text-3xl">Subscription Plans</h1>
                <p class="mt-1 text-sm text-slate-500">
                    আপনার SaaS এর সকল প্ল্যান এখানে ম্যানেজ করুন।
                </p>
            </div>

            <a href="{{ route('plans.create') }}"
               class="inline-flex items-center justify-center rounded-2xl bg-blue-600 px-5 py-3 text-sm font-semibold text-white shadow-sm transition hover:bg-blue-700 hover:shadow-lg">
                <span class="mr-2 text-base">+</span>
                Add Plan
            </a>
        </div>

        {{-- Flash Message --}}
        @if(session('success'))
            <div class="mb-5 rounded-2xl border border-green-200 bg-green-50 px-4 py-3 text-sm text-green-700">
                {{ session('success') }}
            </div>
        @endif

        {{-- Filters --}}
        <div class="mb-6 rounded-2xl border border-slate-200 bg-white p-4 shadow-sm">
            <form method="GET" action="{{ route('plans.index') }}" class="grid grid-cols-1 gap-4 md:grid-cols-4">
                <div>
                    <label class="mb-1 block text-sm font-medium text-slate-700">Search</label>
                    <input type="text"
                           name="search"
                           value="{{ request('search') }}"
                           placeholder="Plan name..."
                           class="w-full rounded-xl border border-slate-300 px-4 py-2.5 text-sm outline-none ring-0 transition focus:border-blue-500">
                </div>

                <div>
                    <label class="mb-1 block text-sm font-medium text-slate-700">Status</label>
                    <select name="status"
                            class="w-full rounded-xl border border-slate-300 px-4 py-2.5 text-sm outline-none focus:border-blue-500">
                        <option value="">All</option>
                        <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>Active</option>
                        <option value="inactive" {{ request('status') === 'inactive' ? 'selected' : '' }}>Inactive</option>
                    </select>
                </div>

                <div>
                    <label class="mb-1 block text-sm font-medium text-slate-700">Trial</label>
                    <select name="trial"
                            class="w-full rounded-xl border border-slate-300 px-4 py-2.5 text-sm outline-none focus:border-blue-500">
                        <option value="">All</option>
                        <option value="yes" {{ request('trial') === 'yes' ? 'selected' : '' }}>Trial</option>
                        <option value="no" {{ request('trial') === 'no' ? 'selected' : '' }}>Paid</option>
                    </select>
                </div>

                <div class="flex items-end gap-2">
                    <button type="submit"
                            class="inline-flex w-full items-center justify-center rounded-xl bg-slate-800 px-4 py-2.5 text-sm font-semibold text-white transition hover:bg-slate-900">
                        Filter
                    </button>

                    <a href="{{ route('plans.index') }}"
                       class="inline-flex w-full items-center justify-center rounded-xl border border-slate-300 bg-white px-4 py-2.5 text-sm font-semibold text-slate-700 transition hover:bg-slate-50">
                        Reset
                    </a>
                </div>
            </form>
        </div>

        {{-- Table --}}
        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm">
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-slate-200">
                    <thead class="bg-slate-100">
                        <tr>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">#</th>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">Plan Name</th>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">Price</th>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">Duration</th>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">Type</th>
                            <th class="px-6 py-4 text-left text-xs font-bold uppercase tracking-wider text-slate-600">Status</th>
                            <th class="px-6 py-4 text-right text-xs font-bold uppercase tracking-wider text-slate-600">Actions</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y divide-slate-100 bg-white">
                        @forelse($plans as $key => $plan)
                            <tr class="hover:bg-slate-50">
                                <td class="px-6 py-4 text-sm text-slate-600">
                                    {{ $plans->firstItem() + $key }}
                                </td>

                                <td class="px-6 py-4">
                                    <div class="font-semibold text-slate-800">{{ $plan->name }}</div>
                                    <div class="text-xs text-slate-500">Created: {{ $plan->created_at?->format('d M Y') }}</div>
                                </td>

                                <td class="px-6 py-4 text-sm font-semibold text-emerald-600">
                                    ৳ {{ number_format($plan->price, 2) }}
                                </td>

                                <td class="px-6 py-4 text-sm text-slate-700">
                                    {{ $plan->duration_days }} Days
                                </td>

                                <td class="px-6 py-4">
                                    @if($plan->is_trial)
                                        <span class="inline-flex rounded-full bg-indigo-100 px-3 py-1 text-xs font-semibold text-indigo-700">
                                            Trial
                                        </span>
                                    @else
                                        <span class="inline-flex rounded-full bg-sky-100 px-3 py-1 text-xs font-semibold text-sky-700">
                                            Paid
                                        </span>
                                    @endif
                                </td>

                                <td class="px-6 py-4">
                                    @if($plan->status)
                                        <span class="inline-flex rounded-full bg-green-100 px-3 py-1 text-xs font-semibold text-green-700">
                                            Active
                                        </span>
                                    @else
                                        <span class="inline-flex rounded-full bg-rose-100 px-3 py-1 text-xs font-semibold text-rose-700">
                                            Inactive
                                        </span>
                                    @endif
                                </td>

                                <td class="px-6 py-4">
                                    <div class="flex justify-end gap-2">
                                        <a href="{{ route('plans.edit', $plan->id) }}"
                                           class="inline-flex items-center rounded-xl bg-amber-500 px-3 py-2 text-xs font-semibold text-white transition hover:bg-amber-600">
                                            Edit
                                        </a>

                                        <form action="{{ route('plans.toggle-status', $plan->id) }}" method="POST">
                                            @csrf
                                            @method('PATCH')
                                            <button type="submit"
                                                    class="inline-flex items-center rounded-xl px-3 py-2 text-xs font-semibold text-white transition {{ $plan->status ? 'bg-slate-700 hover:bg-slate-800' : 'bg-green-600 hover:bg-green-700' }}">
                                                {{ $plan->status ? 'Deactivate' : 'Activate' }}
                                            </button>
                                        </form>

                                        <form action="{{ route('plans.destroy', $plan->id) }}" method="POST"
                                              onsubmit="return confirm('Are you sure you want to delete this plan?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit"
                                                    class="inline-flex items-center rounded-xl bg-rose-600 px-3 py-2 text-xs font-semibold text-white transition hover:bg-rose-700">
                                                Delete
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" class="px-6 py-10 text-center text-sm text-slate-500">
                                    কোনো প্ল্যান পাওয়া যায়নি।
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

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