<?php
$pendingCount = 0;
foreach ($restaurants as $r) {
    if ($r['approval_status'] === 'pending') $pendingCount++;
}
?>
<div class="space-y-6">
    <!-- Header -->
    <div class="flex items-center justify-between">
        <div>
            <h2 class="text-xl font-bold text-white">Pending Registrations</h2>
            <p class="text-slate-400 text-sm mt-1">Review and approve new restaurant registration requests.</p>
        </div>
        <?php if ($pendingCount > 0): ?>
        <span class="px-3 py-1.5 rounded-full text-xs font-extrabold uppercase bg-yellow-500/15 text-yellow-400 border border-yellow-500/25">
            <?php echo $pendingCount; ?> Pending
        </span>
        <?php endif; ?>
    </div>

    <?php if (empty($restaurants)): ?>
    <div class="rounded-2xl p-12 text-center" style="background:rgba(255,255,255,0.04); border:1px solid rgba(255,255,255,0.07)">
        <i class="fas fa-check-circle text-4xl text-emerald-500/40 mb-4"></i>
        <p class="text-slate-400 font-medium">No pending registrations at this time.</p>
    </div>
    <?php else: ?>
    <div class="space-y-4">
        <?php foreach ($restaurants as $r):
            $admin = $r['admin_user'];
            $isCallback = $r['approval_status'] === 'callback';
            $borderColor = $isCallback ? 'border-yellow-500/20' : 'border-white/5';
            $bgColor = $isCallback ? 'bg-yellow-500/[0.03]' : 'bg-white/[0.02]';
        ?>
        <div x-data="{ showNotes: false }" class="rounded-2xl overflow-hidden border <?php echo $borderColor; ?> <?php echo $bgColor; ?>">
            <div class="p-5">
                <div class="flex flex-col lg:flex-row lg:items-start justify-between gap-4">
                    <!-- Restaurant Info -->
                    <div class="flex-1 space-y-3">
                        <div class="flex items-center gap-3">
                            <div class="w-10 h-10 rounded-xl flex items-center justify-center flex-shrink-0" style="background:linear-gradient(135deg,#8b5cf6,#3b82f6)">
                                <i class="fas fa-store text-white text-sm"></i>
                            </div>
                            <div>
                                <h3 class="text-white font-bold text-sm"><?php echo htmlspecialchars($r['name']); ?></h3>
                                <div class="flex items-center gap-2 mt-0.5">
                                    <span class="px-2 py-0.5 rounded text-3xs font-extrabold uppercase border <?php echo $isCallback ? 'bg-yellow-500/10 border-yellow-500/20 text-yellow-400' : 'bg-blue-500/10 border-blue-500/20 text-blue-400'; ?>">
                                        <?php echo $r['approval_status']; ?>
                                    </span>
                                    <span class="text-slate-500 text-xs"><?php echo date('M d, Y h:i A', strtotime($r['created_at'])); ?></span>
                                </div>
                            </div>
                        </div>

                        <div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 gap-3 text-xs">
                            <div>
                                <span class="text-slate-500 block text-3xs uppercase font-semibold tracking-wider mb-1">Restaurant Email</span>
                                <span class="text-slate-300 font-medium"><?php echo htmlspecialchars($r['email'] ?? '—'); ?></span>
                            </div>
                            <div>
                                <span class="text-slate-500 block text-3xs uppercase font-semibold tracking-wider mb-1">Phone</span>
                                <span class="text-slate-300 font-medium"><?php echo htmlspecialchars($r['phone'] ?? '—'); ?></span>
                            </div>
                            <div>
                                <span class="text-slate-500 block text-3xs uppercase font-semibold tracking-wider mb-1">Address</span>
                                <span class="text-slate-300 font-medium"><?php echo htmlspecialchars($r['address'] ?? '—'); ?></span>
                            </div>
                        </div>

                        <?php if ($admin): ?>
                        <div class="p-3 rounded-xl bg-white/[0.03] border border-white/5">
                            <span class="text-slate-500 text-3xs uppercase font-semibold tracking-wider block mb-2"><i class="fas fa-user-shield text-purple-400 mr-1"></i>Admin Account</span>
                            <div class="flex flex-wrap gap-x-6 gap-y-1 text-xs">
                                <span class="text-slate-300"><i class="fas fa-user text-slate-600 mr-1"></i><?php echo htmlspecialchars($admin['name']); ?></span>
                                <span class="text-slate-300"><i class="fas fa-envelope text-slate-600 mr-1"></i><?php echo htmlspecialchars($admin['email']); ?></span>
                                <?php if (!empty($admin['phone'])): ?>
                                <span class="text-slate-300"><i class="fas fa-phone text-slate-600 mr-1"></i><?php echo htmlspecialchars($admin['phone']); ?></span>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php endif; ?>

                        <?php if ($isCallback && !empty($r['admin_notes'])): ?>
                        <div class="p-3 rounded-xl bg-yellow-500/[0.06] border border-yellow-500/15">
                            <span class="text-yellow-400 text-3xs uppercase font-semibold tracking-wider block mb-1"><i class="fas fa-sticky-note mr-1"></i>Admin Notes</span>
                            <p class="text-slate-300 text-xs"><?php echo nl2br(htmlspecialchars($r['admin_notes'])); ?></p>
                        </div>
                        <?php endif; ?>
                    </div>

                    <!-- Action Buttons -->
                    <div class="flex flex-wrap lg:flex-col items-center gap-2 lg:self-start flex-shrink-0">
                        <form action="/superadmin/registrations/approve" method="POST" class="inline">
                            <input type="hidden" name="restaurant_id" value="<?php echo $r['id']; ?>">
                            <button type="submit" onclick="return confirm('Approve this registration? A welcome email with login credentials will be sent.')" class="text-xs font-semibold py-2 px-4 rounded-xl text-white bg-emerald-600 hover:bg-emerald-700 active:scale-[0.98] transition flex items-center gap-1.5 shadow-lg shadow-emerald-600/20 w-full justify-center">
                                <i class="fas fa-check"></i> Approve
                            </button>
                        </form>

                        <button @click="showNotes = !showNotes" class="text-xs font-semibold py-2 px-4 rounded-xl text-yellow-400 bg-yellow-500/10 hover:bg-yellow-500/20 active:scale-[0.98] border border-yellow-500/30 transition flex items-center gap-1.5 w-full justify-center">
                            <i class="fas fa-phone-alt"></i> Call Back
                        </button>

                        <form action="/superadmin/registrations/delete" method="POST" class="inline w-full">
                            <input type="hidden" name="restaurant_id" value="<?php echo $r['id']; ?>">
                            <button type="submit" onclick="return confirm('Delete this registration permanently? This cannot be undone.')" class="text-xs font-semibold py-2 px-4 rounded-xl text-red-400 bg-red-500/10 hover:bg-red-500/20 active:scale-[0.98] border border-red-500/30 transition flex items-center gap-1.5 w-full justify-center">
                                <i class="fas fa-trash-alt"></i> Delete
                            </button>
                        </form>
                    </div>
                </div>

                <!-- Callback Notes Form (inline) -->
                <div x-show="showNotes" x-transition class="mt-4 p-4 rounded-xl border border-yellow-500/20 bg-yellow-500/[0.04]" style="display:none">
                    <form action="/superadmin/registrations/callback" method="POST" class="space-y-3">
                        <input type="hidden" name="restaurant_id" value="<?php echo $r['id']; ?>">
                        <div>
                            <label class="block text-xs text-yellow-400 font-semibold mb-1"><i class="fas fa-pen mr-1"></i>Callback Notes</label>
                            <textarea name="admin_notes" rows="3" class="form-input text-xs" placeholder="Add notes about this callback..."><?php echo htmlspecialchars($r['admin_notes'] ?? ''); ?></textarea>
                        </div>
                        <div class="flex gap-2">
                            <button type="submit" class="text-xs font-semibold py-2 px-4 rounded-xl text-white bg-yellow-600 hover:bg-yellow-700 transition">
                                <i class="fas fa-save mr-1"></i>Save & Mark Callback
                            </button>
                            <button type="button" @click="showNotes = false" class="text-xs py-2 px-4 rounded-xl text-slate-400 hover:text-white hover:bg-white/8 transition">Cancel</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <?php endforeach; ?>
    </div>
    <?php endif; ?>
</div>
