<x-layouts.portal title="Create Deal" page-title="Build New Offer">

<div class="mb-8">
    <a href="{{ route('portal.deals.index') }}" class="text-[10px] font-black text-zinc-400 uppercase tracking-widest hover:text-zinc-900 flex items-center group transition-colors">
        <svg class="w-3 h-3 mr-2 group-hover:-translate-x-1 transition-transform" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M15 19l-7-7 7-7"></path></svg>
        Back to Deals
    </a>
</div>

<form action="{{ route('portal.deals.store') }}" method="POST" x-data="dealForm()">
    @csrf
    
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        <!-- Basic Info -->
        <div class="lg:col-span-1 space-y-6">
            <div class="bg-white p-6 border border-zinc-200 rounded-sm shadow-xs">
                <h3 class="text-[10px] font-black text-zinc-900 uppercase tracking-widest mb-6 border-b border-zinc-50 pb-4">General Information</h3>
                
                <div class="space-y-4">
                    <div>
                        <label class="block text-[10px] font-black text-zinc-500 uppercase tracking-widest mb-2">Deal Name</label>
                        <input type="text" name="name" required placeholder="e.g. Wednesday Special" 
                            class="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-200 rounded-sm text-xs font-bold focus:outline-hidden focus:border-amber-500 focus:bg-white transition-all">
                    </div>
                    
                    <div>
                        <label class="block text-[10px] font-black text-zinc-500 uppercase tracking-widest mb-2">Fixed Price (₹)</label>
                        <input type="number" name="price" step="0.01" required placeholder="0.00" 
                            class="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-200 rounded-sm text-xs font-bold focus:outline-hidden focus:border-amber-500 focus:bg-white transition-all">
                    </div>
                    
                    <div>
                        <label class="block text-[10px] font-black text-zinc-500 uppercase tracking-widest mb-2">Description</label>
                        <textarea name="description" rows="3" placeholder="Explain the deal to waiters..." 
                            class="w-full px-4 py-2.5 bg-zinc-50 border border-zinc-200 rounded-sm text-xs font-bold focus:outline-hidden focus:border-amber-500 focus:bg-white transition-all"></textarea>
                    </div>

                    <div class="pt-4 border-t border-zinc-50">
                        <label class="block text-[10px] font-black text-zinc-500 uppercase tracking-widest mb-3">Available Days</label>
                        <div class="grid grid-cols-2 gap-2">
                            @foreach(['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] as $day)
                                <label class="flex items-center space-x-2 p-2 bg-zinc-50 border border-zinc-100 rounded-sm cursor-pointer hover:border-amber-500 transition-all group">
                                    <input type="checkbox" name="available_days[]" value="{{ $day }}" 
                                        class="w-3 h-3 text-amber-600 rounded-xs border-zinc-300 focus:ring-amber-500/20">
                                    <span class="text-[9px] font-black uppercase text-zinc-500 group-hover:text-amber-600 transition-colors">{{ $day }}</span>
                                </label>
                            @endforeach
                        </div>
                        <p class="mt-2 text-[8px] font-bold text-zinc-400 uppercase tracking-tighter">If none selected, deal shows daily</p>
                    </div>
                </div>
            </div>

            <div class="bg-amber-600 p-6 rounded-sm shadow-lg shadow-amber-900/20 text-white">
                <h4 class="text-[10px] font-black uppercase tracking-widest mb-2">Chef's Note</h4>
                <p class="text-[10px] font-bold text-amber-100 leading-relaxed">
                    This deal will "explode" into individual KOT items for the kitchen. Make sure to choose the correct items for each slot!
                </p>
            </div>

            <button type="submit" class="w-full py-4 bg-zinc-900 text-white text-[10px] font-black uppercase tracking-[0.2em] rounded-sm shadow-xl shadow-zinc-900/20 hover:bg-emerald-600 transition-all active:scale-95">
                Save & Launch Deal
            </button>
        </div>

        <!-- Deal Slots -->
        <div class="lg:col-span-2 space-y-6">
            <div class="bg-white p-6 border border-zinc-200 rounded-sm shadow-xs min-h-full">
                <div class="flex justify-between items-center mb-8 border-b border-zinc-50 pb-4">
                    <h3 class="text-[10px] font-black text-zinc-900 uppercase tracking-widest">Deal Slots (The Steps)</h3>
                    <button type="button" @click="addSlot()" class="text-[9px] font-black text-amber-600 uppercase tracking-widest hover:text-amber-700 flex items-center">
                        <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M12 6v6m0 0v6m0-6h6m-6 0H6"></path></svg>
                        Add Another Slot
                    </button>
                </div>

                <div class="space-y-8">
                    <template x-for="(slot, index) in slots" :key="index">
                        <div class="p-6 bg-zinc-50 border border-zinc-100 rounded-sm relative group animate-in fade-in slide-in-from-bottom-4 duration-300">
                            <button type="button" @click="removeSlot(index)" x-show="slots.length > 1" 
                                class="absolute top-4 right-4 text-zinc-300 hover:text-rose-500 transition-colors">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path></svg>
                            </button>

                            <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                                <div class="md:col-span-1">
                                    <label class="block text-[9px] font-black text-zinc-400 uppercase tracking-widest mb-2" x-text="'Step ' + (index + 1) + ' Name'"></label>
                                    <input type="text" :name="'slots['+index+'][name]'" required placeholder="e.g. Choose Pizza" 
                                        class="w-full px-4 py-2 bg-white border border-zinc-200 rounded-sm text-[10px] font-bold focus:outline-hidden focus:border-amber-500 transition-all">
                                </div>
                                
                                <div class="md:col-span-3">
                                    <label class="block text-[9px] font-black text-zinc-400 uppercase tracking-widest mb-2">Available Items for this Slot</label>
                                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 max-h-80 overflow-y-auto no-scrollbar p-1">
                                        @foreach($items as $item)
                                            <div class="p-3 bg-white border border-zinc-200 rounded-sm">
                                                @if($item->variants->count() > 0)
                                                    <div class="flex items-center mb-3 pb-2 border-b border-zinc-50">
                                                        <span class="text-[9px] font-black text-zinc-900 uppercase tracking-tight">{{ $item->name }}</span>
                                                        <span class="ml-2 px-1.5 py-0.5 bg-zinc-100 text-[7px] font-black text-zinc-500 rounded-xs uppercase">Choose Size</span>
                                                    </div>
                                                    <div class="space-y-2 ml-2">
                                                        @foreach($item->variants as $variant)
                                                            <label class="flex items-center cursor-pointer group">
                                                                <input type="checkbox" :name="'slots['+index+'][selections][]'" value="{{ $item->id }}:{{ $variant->id }}" 
                                                                    class="w-3 h-3 text-amber-600 rounded-xs border-zinc-300 focus:ring-amber-500/20">
                                                                <span class="ml-2 text-[8px] font-bold text-zinc-500 uppercase group-hover:text-amber-600 transition-colors">
                                                                    {{ $variant->name }} - ₹{{ number_format($variant->dine_in_price, 2) }}
                                                                </span>
                                                            </label>
                                                        @endforeach
                                                    </div>
                                                @else
                                                    <label class="flex items-center cursor-pointer group">
                                                        <input type="checkbox" :name="'slots['+index+'][selections][]'" value="{{ $item->id }}" 
                                                            class="w-3 h-3 text-amber-600 rounded-xs border-zinc-300 focus:ring-amber-500/20">
                                                        <span class="ml-2 text-[9px] font-black text-zinc-900 uppercase tracking-tight group-hover:text-amber-600 transition-colors">{{ $item->name }}</span>
                                                    </label>
                                                @endif
                                            </div>
                                        @endforeach
                                    </div>
                                </div>
                            </div>
                        </div>
                    </template>
                </div>
            </div>
        </div>
    </div>
</form>

<script>
    function dealForm() {
        return {
            slots: [
                { name: 'Slot 1' }
            ],
            addSlot() {
                this.slots.push({ name: 'Slot ' + (this.slots.length + 1) });
            },
            removeSlot(index) {
                this.slots.splice(index, 1);
            }
        }
    }
</script>

</x-layouts.portal>
