@extends('layouts.admin')

@section('title', 'Flash Sale')

@section('content')
    <div class="flex flex-col md:flex-row md:items-end justify-between gap-6 mb-8 border-b border-gray-100 pb-6">
        <div class="space-y-1">
            <h1 class="text-3xl font-bold text-gray-900 tracking-tight">Flash Sale</h1>
            <p class="text-gray-500 text-sm">Control your flash sale window and featured products.</p>
        </div>
        <div class="flex items-center gap-3">
            <a href="{{ route('admin.products.flash_sale.select') }}" class="inline-flex items-center px-5 py-3 bg-white border border-gray-200 hover:bg-gray-50 text-gray-700 text-xs font-bold rounded-xl shadow-sm transition-all">
                <i class="fas fa-plus mr-2 text-[10px] text-brand"></i>
                Add From Products
            </a>
        </div>
    </div>

    @if(session('success'))
        <div x-data="{ show: true }" x-show="show" x-transition:leave="transition ease-in duration-300" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95" class="bg-emerald-50 border border-emerald-100 text-emerald-700 p-5 mb-8 rounded-2xl flex items-center justify-between shadow-sm" role="alert">
            <div class="flex items-center gap-3">
                <div class="h-8 w-8 rounded-lg bg-white flex items-center justify-center shadow-sm">
                    <i class="fas fa-check text-emerald-500 text-xs"></i>
                </div>
                <p class="text-sm font-bold">{{ session('success') }}</p>
            </div>
            <button @click="show = false" class="text-emerald-400 hover:text-emerald-600 transition-colors">
                <i class="fas fa-times text-xs"></i>
            </button>
        </div>
    @endif

    <div class="grid grid-cols-1 lg:grid-cols-[minmax(0,1.6fr)_minmax(0,2.4fr)] gap-6 mb-8">
        <div class="bg-zinc-900 rounded-3xl p-6 md:p-7 text-white relative overflow-hidden">
            <div class="absolute -right-16 -top-16 w-40 h-40 bg-emerald-500/10 rounded-full blur-3xl"></div>
            <div class="flex items-center gap-3 mb-4 relative z-10">
                <div class="w-10 h-10 rounded-2xl bg-emerald-500/20 flex items-center justify-center text-emerald-300">
                    <i class="fas fa-bolt"></i>
                </div>
                <div>
                    <p class="text-[10px] font-black uppercase tracking-[0.3em] text-emerald-300/80">Flash Sale Control</p>
                    <p class="text-sm text-white/50 font-medium">Fine-tune when your deals go live.</p>
                </div>
            </div>

            <div class="mt-4 space-y-4 relative z-10">
                <div class="inline-flex items-center gap-2 px-3 py-1.5 rounded-full text-[10px] font-bold uppercase tracking-[0.25em]
                    {{ $flashSaleActive ? 'bg-emerald-500/10 text-emerald-300 border border-emerald-500/40' : 'bg-white/5 text-white/60 border border-white/10' }}">
                    <span class="w-1.5 h-1.5 rounded-full {{ $flashSaleActive ? 'bg-emerald-400 animate-pulse' : 'bg-white/30' }}"></span>
                    {{ $flashSaleActive ? 'Active' : 'Inactive' }}
                </div>

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 text-xs font-medium text-white/70">
                    <div class="bg-white/5 rounded-2xl p-4 border border-white/10">
                        <p class="text-[10px] font-bold uppercase tracking-widest text-white/40 mb-1">Starts</p>
                        <p class="text-sm font-semibold">
                            @if($flashSaleStartInput)
                                {{ \Carbon\Carbon::parse($flashSaleStartInput)->format('d M Y, h:i A') }}
                            @else
                                Not scheduled
                            @endif
                        </p>
                    </div>
                    <div class="bg-white/5 rounded-2xl p-4 border border-white/10">
                        <p class="text-[10px] font-bold uppercase tracking-widest text-white/40 mb-1">Ends</p>
                        <p class="text-sm font-semibold">
                            @if($flashSaleEndInput)
                                {{ \Carbon\Carbon::parse($flashSaleEndInput)->format('d M Y, h:i A') }}
                            @else
                                Not scheduled
                            @endif
                        </p>
                    </div>
                </div>

                <p class="text-[11px] text-white/40 leading-relaxed">
                    Homepage countdown and flash sale widgets will automatically use this schedule and status.
                </p>
            </div>
        </div>

        <div class="bg-white rounded-3xl border border-gray-100 shadow-sm p-6">
            <form action="{{ route('admin.products.flash_sale.schedule') }}" method="POST" class="space-y-4">
                @csrf
                <div class="flex items-center justify-between gap-4">
                    <div>
                        <p class="text-sm font-semibold text-gray-900">Flash Sale status</p>
                        <p class="text-xs text-gray-500">Toggle to instantly enable or pause flash sale across the site.</p>
                    </div>
                    <label class="inline-flex items-center cursor-pointer">
                        <input type="checkbox" name="is_active" value="1" class="sr-only peer" {{ $flashSaleActive ? 'checked' : '' }}>
                        <span class="w-11 h-6 flex items-center bg-gray-200 rounded-full p-1 duration-300 ease-in-out peer-checked:bg-emerald-500">
                            <span class="bg-white w-4 h-4 rounded-full shadow transform duration-300 ease-in-out peer-checked:translate-x-5"></span>
                        </span>
                    </label>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-xs font-semibold text-gray-500 uppercase tracking-widest mb-1.5">Start date & time</label>
                        <input
                            type="datetime-local"
                            name="start_at"
                            value="{{ $flashSaleStartInput }}"
                            class="w-full px-4 py-2.5 rounded-xl border border-gray-200 text-sm focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-400"
                        >
                    </div>
                    <div>
                        <label class="block text-xs font-semibold text-gray-500 uppercase tracking-widest mb-1.5">End date & time</label>
                        <input
                            type="datetime-local"
                            name="end_at"
                            value="{{ $flashSaleEndInput }}"
                            class="w-full px-4 py-2.5 rounded-xl border border-gray-200 text-sm focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-400"
                        >
                    </div>
                </div>

                <div class="flex items-center justify-between gap-4 pt-2">
                    <p class="text-xs text-gray-500">
                        Leave fields empty to keep countdown using daily auto-reset timer.
                    </p>
                    <button type="submit" class="inline-flex items-center gap-2 px-5 py-2.5 rounded-xl bg-zinc-900 text-white text-xs font-bold tracking-wide hover:bg-emerald-600 transition-all shadow-md shadow-zinc-200">
                        <i class="fas fa-save text-[11px]"></i>
                        Save Schedule
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="bg-white rounded-3xl shadow-sm border border-gray-100 overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-100 bg-gray-50/60">
            <div class="flex items-center gap-2 text-sm text-gray-600">
                <i class="fas fa-bolt text-amber-500"></i>
                Showing flash sale products
            </div>
        </div>

        <div class="p-6">
            @if($products->count())
                <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-5">
                    @foreach($products as $product)
                    <div class="group border border-gray-100 rounded-2xl overflow-hidden bg-white hover:shadow-md transition-all">
                        <div class="relative aspect-square bg-gray-50 flex items-center justify-center overflow-hidden">
                            @if($product->image)
                                <img class="h-full w-full object-cover group-hover:scale-105 transition-transform duration-500" src="{{ Str::startsWith($product->image, ['http://', 'https://']) ? $product->image : Storage::url($product->image) }}" alt="{{ $product->name }}">
                            @else
                                <i class="fas fa-box text-4xl text-gray-200"></i>
                            @endif
                            <div class="absolute top-3 right-3 h-7 px-2 rounded-lg bg-red-500 text-white text-[10px] font-bold inline-flex items-center gap-1">
                                <i class="fas fa-bolt text-[9px]"></i>
                                Flash
                            </div>
                            @if(!$product->is_active)
                                <div class="absolute bottom-3 left-3 px-2 py-1 text-[10px] font-semibold rounded bg-gray-900 text-white/90">Inactive</div>
                            @endif
                        </div>
                        <div class="p-4">
                            <div class="text-sm font-semibold text-gray-900 line-clamp-2">{{ $product->name }}</div>
                            <div class="mt-1 text-xs text-gray-500">{{ $product->category->name ?? 'Uncategorized' }}</div>
                            <div class="mt-2 flex items-baseline gap-2">
                                @if($product->discount_price)
                                    <span class="text-sm font-semibold text-emerald-600">{{ $currencySymbol }}{{ number_format($product->discount_price, 2) }}</span>
                                    <span class="text-xs text-gray-400 line-through">{{ $currencySymbol }}{{ number_format($product->price, 2) }}</span>
                                @else
                                    <span class="text-sm font-semibold text-gray-900">{{ $currencySymbol }}{{ number_format($product->price, 2) }}</span>
                                @endif
                            </div>
                            <div class="mt-3 flex items-center justify-between">
                                <span class="text-xs text-gray-500">{{ $product->stock }} {{ $product->unit }}</span>
                                <div class="flex items-center gap-2">
                                    <a href="{{ route('admin.products.edit', $product) }}" class="h-9 w-9 flex items-center justify-center text-gray-400 hover:text-brand hover:bg-emerald-50 rounded-xl transition-all" title="Edit">
                                        <i class="fas fa-edit text-sm"></i>
                                    </a>
                                    <form method="POST" action="{{ route('admin.products.flash_sale.detach') }}" class="inline-block">
                                        @csrf
                                        <input type="hidden" name="product_ids[]" value="{{ $product->id }}">
                                        <button type="submit" class="h-9 w-9 flex items-center justify-center text-gray-400 hover:text-red-600 hover:bg-red-50 rounded-xl transition-all" title="Remove from Flash Sale">
                                            <i class="fas fa-minus-circle text-sm"></i>
                                        </button>
                                    </form>
                                    <form action="{{ route('admin.products.destroy', $product) }}" method="POST" class="inline-block" onsubmit="return confirm('Are you sure you want to delete this product?');">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="h-9 w-9 flex items-center justify-center text-gray-400 hover:text-red-500 hover:bg-red-50 rounded-xl transition-all" title="Delete">
                                            <i class="fas fa-trash-alt text-sm"></i>
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    @endforeach
                </div>
            @else
                <div class="px-8 py-24 text-center">
                    <div class="flex flex-col items-center justify-center max-w-md mx-auto">
                        <div class="h-20 w-20 bg-gray-50 rounded-2xl flex items-center justify-center mb-6 border border-gray-100">
                            <i class="fas fa-bolt text-3xl text-red-300"></i>
                        </div>
                        <h3 class="text-xl font-semibold text-gray-900 mb-1">No flash sale products</h3>
                        <p class="text-gray-500 text-sm leading-relaxed mb-8">Add from existing products to build your Flash Sale list.</p>
                        <a href="{{ route('admin.products.flash_sale.select') }}" class="inline-flex items-center gap-2 btn-brand text-white px-6 py-3 rounded-xl text-sm font-semibold transition-all shadow-md shadow-brand/20">
                            <i class="fas fa-plus text-[10px]"></i>
                            Add From Products
                        </a>
                    </div>
                </div>
            @endif
        </div>

        @if($products->hasPages())
        <div class="px-6 py-5 border-t border-gray-100 bg-gray-50/50">
            {{ $products->links() }}
        </div>
        @endif
    </div>
@endsection
