@php
    $notifikasiKadaluarsa = \App\Models\ObatMasuk::where('tanggal_kadaluarsa', '<=', now()->addMonths(4))
        ->where('tanggal_kadaluarsa', '>', now()) // Exclude already expired (red)
        ->orderBy('tanggal_kadaluarsa', 'asc')
        ->get();
@endphp

<div id="notificationModal" class="hidden fixed inset-0 z-50 overflow-y-auto">
    <!-- Overlay -->
    <div class="fixed inset-0 bg-black/50 transition-opacity" onclick="document.getElementById('notificationModal').classList.add('hidden')"></div>
    
    <!-- Modal -->
    <div class="fixed inset-0 flex items-center justify-center p-4">
        <div class="bg-white rounded-xl shadow-2xl w-full max-w-md flex flex-col" style="max-height: 80vh;">
            <!-- Header -->
            <div class="bg-[#4A538F] px-6 py-4 flex items-center justify-between rounded-t-xl flex-shrink-0">
                <h3 class="text-lg font-semibold text-white flex items-center gap-2">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
                    </svg>
                    Notifikasi Obat Kadaluarsa
                </h3>
                <button onclick="document.getElementById('notificationModal').classList.add('hidden')" class="text-white/80 hover:text-white">
                    <svg class="w-5 h-5" 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"/>
                    </svg>
                </button>
            </div>

            <!-- Content -->
            <div class="overflow-y-auto flex-1">
                @forelse($notifikasiKadaluarsa as $item)
                    @php
                        $sisaHari = (int) now()->diffInDays($item->tanggal_kadaluarsa, false);
                    @endphp
                    <div class="px-6 py-4 border-b border-[#E5E7F2] hover:bg-[#F4F6FF] transition-colors">
                        <div class="flex items-start gap-3">
                            <div class="p-2 rounded-full {{ $sisaHari <= 0 ? 'bg-[#DC2626] text-white' : ($sisaHari <= 30 ? 'bg-[#FFEDD5] text-[#C2410C]' : 'bg-[#FEF9C3] text-[#A16207]') }}">
                                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
                                </svg>
                            </div>
                            <div class="flex-1">
                                <p class="font-medium text-[#2F347A]">{{ $item->nama_obat ?? 'N/A' }}</p>
                                <p class="text-sm text-[#7A7FAE]">
                                    Kadaluarsa: {{ $item->tanggal_kadaluarsa->format('d M Y') }}
                                </p>
                                <p class="text-sm {{ $sisaHari <= 0 ? 'text-[#DC2626]' : ($sisaHari <= 30 ? 'text-[#C2410C]' : 'text-[#A16207]') }} font-medium">
                                    {{ $sisaHari <= 0 ? 'Sudah Kadaluarsa (' . $sisaHari . ' hari)' : $sisaHari . ' hari lagi' }}
                                </p>
                                <p class="text-xs text-[#7A7FAE] mt-1">
                                    {{ $item->created_at->diffForHumans() }}
                                </p>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="px-6 py-12 text-center text-[#7A7FAE]">
                        <svg class="w-12 h-12 mx-auto mb-4 text-[#E5E7F2]" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <p>Tidak ada obat yang akan kadaluarsa dalam 4 bulan ke depan</p>
                    </div>
                @endforelse
            </div>

            <!-- Footer -->
            @if($notifikasiKadaluarsa->count() > 0)
                <div class="px-6 py-4 bg-[#F4F6FF] border-t border-[#E5E7F2] rounded-b-xl flex-shrink-0">
                    <a href="{{ route('kadaluarsa.index') }}" 
                       class="block text-center text-sm text-[#4A538F] hover:text-[#2F347A] font-medium py-1"
                       onclick="document.getElementById('notificationModal').classList.add('hidden')">
                        Lihat Semua →
                    </a>
                </div>
            @endif
        </div>
    </div>
</div>
