<?php require_once __DIR__ . '/../layouts/header.php'; ?>
<?php require_once __DIR__ . '/../layouts/sidebar.php'; ?>

<div class="flex-1 flex flex-col min-w-0 h-full overflow-hidden" x-data="{ deleteModalOpen: false, deleteId: null, confirmDelete(id) { this.deleteId = id; this.deleteModalOpen = true; } }">
 <?php require_once __DIR__ . '/../layouts/topbar.php'; ?>

 <main class="flex-1 relative overflow-y-auto py-4 sm:py-6 px-3 sm:px-6 lg:px-8 bg-gray-50 custom-scrollbar">
 <div class="max-w-6xl mx-auto space-y-4 pb-6">
 <!-- Page Header -->
 <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4 px-1 py-2">
 <div>
 <h1 class="text-xl sm:text-2xl font-bold text-gray-900 tracking-tight">Our Students</h1>
 <p class="text-[10px] sm:text-xs font-medium text-gray-400 mt-0.5">Manage student profiles and academic records</p>
 </div>
 <a href="<?php echo url('admin/students/create'); ?>" class="w-full sm:w-auto px-6 py-3 bg-blue-600 text-white text-xs font-semibold rounded-xl hover:bg-blue-700 transition-all flex items-center justify-center gap-2 active:scale-95 -600/20">
 <i class="fa-solid fa-user-plus text-sm"></i>
 <span>Register Student</span>
 </a>
 </div>

 <!-- Search and Filters -->
 <form action="<?php echo url('admin/students'); ?>" method="GET" class="bg-white border border-gray-200 rounded-2xl p-4 flex flex-col sm:flex-row gap-4 items-center">
 <div class="relative w-full sm:w-96">
 <i class="fa-solid fa-magnifying-glass absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 text-xs"></i>
 <input name="search" type="text" value="<?php echo htmlspecialchars($params['search']); ?>" class="w-full pl-10 pr-4 py-3 bg-white border border-gray-200 rounded-xl text-xs font-medium text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none" placeholder="Search by name, ID or phone...">
 </div>
 </form>

 <!-- Desktop View (Table) -->
 <div class="hidden md:block bg-white border border-gray-200 rounded-2xl overflow-hidden">
 <div class="overflow-x-auto">
 <table class="w-full text-left border-collapse">
 <thead>
 <tr class="bg-gray-50/80 border-b border-gray-200">
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest w-16 text-center">SL</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Student Info</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Enrolled Courses</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Contact</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest text-center border-l border-gray-100/50">Status</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest text-right border-l border-gray-100/50">Actions</th>
 </tr>
 </thead>
 <tbody class="divide-y divide-gray-100">
 <?php if(empty($students)): ?>
 <tr>
 <td colspan="6" class="px-6 py-12 text-center text-gray-400 text-xs font-bold">No students found</td>
 </tr>
 <?php else: ?>
 <?php foreach($students as $index => $student): ?>
 <tr class="hover:bg-gray-50/30 transition-colors group">
 <td class="px-6 py-4 text-center">
 <span class="text-[10px] font-bold text-gray-400"><?php echo str_pad($index + 1, 2, '0', STR_PAD_LEFT); ?></span>
 </td>
 <td class="px-6 py-4 border-l border-gray-100/50">
 <div class="flex items-center gap-3">
 <?php if($student['photo']): ?>
 <img src="<?php echo url($student['photo']); ?>" class="w-10 aspect-[4/5] rounded-lg object-cover border border-gray-200">
 <?php else: ?>
 <div class="w-10 aspect-[4/5] rounded-lg bg-blue-600/10 text-blue-600 flex items-center justify-center font-bold text-sm">
 <?php echo substr($student['name'], 0, 1); ?>
 </div>
 <?php endif; ?>
 <div class="flex flex-col">
 <span class="text-xs font-bold text-gray-900 group-hover:text-blue-600 transition-colors"><?php echo $student['name']; ?></span>
 <span class="text-[9px] font-bold text-gray-400 mt-0.5 uppercase tracking-tighter"><?php echo $student['student_id']; ?></span>
 </div>
 </div>
 </td>
 <td class="px-6 py-4 border-l border-gray-100/50">
 <div class="flex flex-wrap gap-1">
 <?php if(!empty($student['admissions'])): ?>
 <?php foreach($student['admissions'] as $admission): ?>
 <span class="text-[8px] font-bold px-1.5 py-0.5 bg-blue-600/5 text-blue-600 rounded border border-blue-600/10"><?php echo $admission['course_name']; ?></span>
 <?php endforeach; ?>
 <?php else: ?>
 <span class="text-[8px] font-bold px-1.5 py-0.5 bg-gray-50 text-gray-400 rounded border border-gray-100 italic">No Admission</span>
 <?php endif; ?>
 </div>
 </td>
 <td class="px-6 py-4 border-l border-gray-100/50">
 <div class="flex flex-col">
 <span class="text-xs font-bold text-gray-700"><?php echo $student['phone']; ?></span>
 <span class="text-[9px] font-medium text-gray-400 mt-0.5">Joined <?php echo date('d/m/Y', strtotime($student['created_at'])); ?></span>
 </div>
 </td>
 <td class="px-6 py-4 text-center border-l border-gray-100/50">
 <span class="px-2 py-1 rounded-lg text-[10px] font-bold <?php echo $student['status'] === 'Active' ? 'bg-emerald-50 text-emerald-600 border border-emerald-100' : 'bg-red-50 text-red-600 border border-red-100'; ?>">
 <?php echo $student['status']; ?>
 </span>
 </td>
 <td class="px-6 py-4 text-right border-l border-gray-100/50">
 <div class="flex items-center justify-end gap-2">
 <a href="<?php echo url('admin/students/view?id=' . $student['id']); ?>" class="h-8 w-8 flex items-center justify-center rounded-lg bg-blue-50 text-blue-500 hover:bg-blue-500 hover:text-white transition-all border border-blue-100" title="View Profile">
 <i class="fa-solid fa-eye text-xs"></i>
 </a>
 <a href="<?php echo url('admin/students/edit?id=' . $student['id']); ?>" class="h-8 w-8 flex items-center justify-center rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition-all" title="Edit Profile">
 <i class="fa-solid fa-pen-to-square text-xs"></i>
 </a>
 <button @click="confirmDelete(<?php echo $student['id']; ?>)" class="h-9 w-9 flex items-center justify-center rounded-xl bg-red-50 text-red-600 border border-red-100 hover:bg-red-600 hover:text-white transition-all active:scale-95" title="Delete Record">
 <i class="fa-solid fa-trash-can text-xs"></i>
 </button>
 </div>
 </td>
 </tr>
 <?php endforeach; ?>
 <?php endif; ?>
 </tbody>
 </table>
 </div>
 </div>

 <!-- Mobile View (Cards) -->
 <div class="md:hidden space-y-4">
 <?php foreach($students as $index => $student): ?>
 <div class="bg-white border border-gray-200 rounded-2xl p-4 space-y-4 relative overflow-hidden">
 <div class="absolute top-0 right-0 px-3 py-1 bg-gray-50 border-b border-l border-gray-100 rounded-bl-xl">
 <span class="text-[9px] font-bold text-gray-400">#<?php echo str_pad($index + 1, 2, '0', STR_PAD_LEFT); ?></span>
 </div>
 <div class="flex items-center justify-between">
 <div class="flex items-center gap-3">
 <?php if($student['photo']): ?>
 <img src="<?php echo url($student['photo']); ?>" class="w-12 aspect-[4/5] rounded-xl object-cover border border-gray-100">
 <?php else: ?>
 <div class="w-12 aspect-[4/5] rounded-xl bg-blue-600/10 text-blue-600 flex items-center justify-center font-bold text-sm">
 <?php echo substr($student['name'], 0, 1); ?>
 </div>
 <?php endif; ?>
 <div class="flex flex-col">
 <span class="text-sm font-bold text-gray-900"><?php echo $student['name']; ?></span>
 <span class="text-[10px] font-bold text-gray-400 uppercase tracking-tighter"><?php echo $student['student_id']; ?></span>
 </div>
 </div>
 <span class="px-2 py-1 rounded-lg text-[9px] font-black uppercase tracking-widest <?php echo $student['status'] === 'Active' ? 'bg-emerald-50 text-emerald-600 border border-emerald-100' : 'bg-red-50 text-red-600 border border-red-100'; ?>">
 <?php echo $student['status']; ?>
 </span>
 </div>

 <div class="flex flex-col gap-2 pt-2 border-t border-gray-50">
 <div class="flex items-center justify-between text-[10px]">
 <span class="font-bold text-gray-400 uppercase tracking-widest">Phone</span>
 <span class="font-bold text-gray-700"><?php echo $student['phone']; ?></span>
 </div>
 <div class="flex items-center justify-between text-[10px]">
 <span class="font-bold text-gray-400 uppercase tracking-widest">Enrolled</span>
 <div class="flex flex-wrap justify-end gap-1">
 <?php if(!empty($student['admissions'])): ?>
 <?php foreach($student['admissions'] as $admission): ?>
 <span class="px-1.5 py-0.5 bg-blue-600/5 text-blue-600 rounded border border-blue-600/10 font-bold"><?php echo $admission['course_name']; ?></span>
 <?php endforeach; ?>
 <?php else: ?>
 <span class="italic text-gray-400">None</span>
 <?php endif; ?>
 </div>
 </div>
 </div>

 <div class="flex items-center gap-2 pt-3 border-t border-gray-50">
 <a href="<?php echo url('admin/students/view?id=' . $student['id']); ?>" class="flex-1 h-11 flex items-center justify-center gap-2 rounded-xl bg-blue-50 text-blue-600 font-bold text-[10px] uppercase tracking-widest border border-blue-100 active:bg-blue-100 transition-colors">
 <i class="fa-solid fa-eye text-sm"></i>
 View
 </a>
 <a href="<?php echo url('admin/students/edit?id=' . $student['id']); ?>" class="flex-1 h-11 flex items-center justify-center gap-2 rounded-xl bg-blue-600 text-white font-bold text-[10px] uppercase tracking-widest hover:bg-blue-700 active:scale-[0.98] transition-all">
 <i class="fa-solid fa-pen-to-square text-sm"></i>
 Edit
 </a>
 <button @click="confirmDelete(<?php echo $student['id']; ?>)" class="h-11 w-11 flex items-center justify-center rounded-xl bg-red-50 text-red-600 border border-red-100 hover:bg-red-600 hover:text-white transition-all active:scale-95">
 <i class="fa-solid fa-trash-can text-xs"></i>
 </button>
 </div>
 </div>
 <?php endforeach; ?>
 </div>
 </div>
 </main>

 <!-- Delete Modal Overlay -->
 <div x-show="deleteModalOpen" class="fixed inset-0 z-[110] overflow-y-auto" x-cloak>
 <div class="flex items-center justify-center min-h-screen px-4">
 <div class="fixed inset-0 bg-gray-900/40 backdrop-blur-sm" @click="deleteModalOpen = false"></div>
 <div class="bg-white rounded-3xl p-6 max-w-[440px] w-full relative z-[120] border border-gray-100">
 <div class="flex items-start gap-5">
 <div class="w-12 h-12 bg-red-50 text-red-500 rounded-2xl flex items-center justify-center flex-none text-lg">
 <i class="fa-solid fa-trash-can"></i>
 </div>
 <div class="flex-1">
 <h3 class="text-lg font-black text-gray-900">Delete Student?</h3>
 <p class="text-[11px] font-medium text-gray-400 leading-relaxed mt-1">This action will permanently remove all associated data including profile, enrollments, and login access. Cannot be undone.</p>
 <div class="flex justify-end gap-2 mt-6">
 <button @click="deleteModalOpen = false" class="px-5 py-2.5 bg-white border border-gray-200 text-gray-500 text-[10px] font-bold rounded-xl hover:bg-gray-50 transition-all">Cancel</button>
 <a :href="'<?php echo url('/admin/students/delete'); ?>?id=' + deleteId" class="px-5 py-2.5 bg-red-500 text-white text-[10px] font-bold rounded-xl hover:bg-red-600 transition-all active:scale-95 flex items-center justify-center">Yes, Delete</a>
 </div>
 </div>
 </div>
 </div>
 </div>
 </div>
</div>

<?php require_once __DIR__ . '/../layouts/footer.php'; ?>
