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

<div class="flex-1 flex flex-col min-w-0 h-full overflow-hidden" x-data='{ 
 showModal: <?php echo isset($editingBatch) ? "true" : "false"; ?>,
 deleteModalOpen: false,
 deleteId: null,
 days: <?php echo isset($editingBatch) ? json_encode($editingBatch["days"]) : "[]"; ?>,
 toggleDay(day) {
 if (this.days.includes(day)) {
 this.days = this.days.filter(d => d !== day);
 } else {
 this.days.push(day);
 }
 },
 confirmDelete(id) {
 this.deleteId = id;
 this.deleteModalOpen = true;
 }
}'>
 <?php require_once '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-6 pb-6">
 <!-- 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">Batches</h1>
 <p class="text-[10px] sm:text-xs font-medium text-gray-400 mt-0.5">Manage schedules and student assignments</p>
 </div>
 <button @click="showModal = true" 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-calendar-plus text-sm"></i>
 <span>Start New Batch</span>
 </button>
 </div>

 <!-- Filters & Search -->
 <form action="<?php echo url('admin/batches'); ?>" method="GET" class="bg-white border border-gray-200 rounded-2xl p-4 space-y-4">
 <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4">
 <div class="relative group">
 <div class="absolute inset-y-0 left-0 pl-3.5 flex items-center pointer-events-none text-gray-400 group-focus-within:text-blue-600 transition-colors">
 <i class="fa-solid fa-magnifying-glass text-xs"></i>
 </div>
 <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 batch or teacher...">
 </div>

 <div class="relative">
 <select name="filter_course" onchange="this.form.submit()" class="w-full px-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 appearance-none cursor-pointer">
 <option value="">All Courses</option>
 <?php foreach($courses as $course): ?>
 <option value="<?php echo $course['name']; ?>"<?php echo $params['filter_course'] === $course['name'] ? 'selected' : ''; ?>><?php echo $course['name']; ?></option>
 <?php endforeach; ?>
 </select>
 <div class="absolute inset-y-0 right-0 pr-3.5 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-3">
 <i class="fa-solid fa-chevron-down text-[10px]"></i>
 </div>
 </div>

 <div class="relative sm:col-span-2 lg:col-span-1">
 <select name="filter_status" onchange="this.form.submit()" class="w-full px-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 appearance-none cursor-pointer">
 <option value="">All Statuses</option>
 <option value="Ongoing"<?php echo $params['filter_status'] === 'Ongoing' ? 'selected' : ''; ?>>Ongoing</option>
 <option value="Upcoming"<?php echo $params['filter_status'] === 'Upcoming' ? 'selected' : ''; ?>>Upcoming</option>
 <option value="Completed"<?php echo $params['filter_status'] === 'Completed' ? 'selected' : ''; ?>>Completed</option>
 </select>
 <div class="absolute inset-y-0 right-0 pr-3.5 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-3">
 <i class="fa-solid fa-chevron-down text-[10px]"></i>
 </div>
 </div>
 </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 min-w-[200px] border-l border-gray-100/50">Batch Info</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Schedule</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Students</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 w-24 border-l border-gray-100/50">Actions</th>
 </tr>
 </thead>
 <tbody class="divide-y divide-gray-100">
 <?php if(empty($batches)): ?>
 <tr><td colspan="6" class="px-6 py-12 text-center text-xs font-bold text-gray-400">No batches found</td></tr>
 <?php else: ?>
 <?php foreach($batches as $index => $batch): ?>
 <tr class="group hover:bg-gray-50 transition-colors">
 <td class="px-6 py-5 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-5 border-l border-gray-100/50">
 <div>
 <h3 class="text-xs font-bold text-gray-900 mb-0.5 group-hover:text-blue-600 transition-colors"><?php echo $batch['name']; ?></h3>
 <p class="text-[9px] font-bold text-blue-600 uppercase tracking-tight"><?php echo $batch['course_name']; ?></p>
 <div class="flex items-center gap-1.5 text-[10px] font-medium text-gray-400 mt-1">
 <i class="fa-solid fa-user-tie text-[9px]"></i>
 <span><?php echo $batch['teacher_name']; ?></span>
 </div>
 </div>
 </td>
 <td class="px-6 py-5 border-l border-gray-100/50">
 <div class="space-y-1">
 <div class="flex items-center gap-2 text-[10px] font-bold text-gray-700">
 <i class="fa-solid fa-calendar-days text-blue-600/40"></i>
 <span><?php echo $batch['days']; ?></span>
 </div>
 <div class="flex items-center gap-2 text-[10px] font-medium text-gray-400">
 <i class="fa-solid fa-clock text-gray-300"></i>
 <span><?php echo date('h:i A', strtotime($batch['start_time'])); ?> - <?php echo date('h:i A', strtotime($batch['end_time'])); ?></span>
 </div>
 </div>
 </td>
 <td class="px-6 py-5 border-l border-gray-100/50">
 <div class="flex items-center gap-3">
 <div class="flex-1 h-1.5 bg-gray-100 rounded-full overflow-hidden max-w-[80px] border border-gray-200">
 <div class="h-full bg-blue-600 rounded-full" style="width: <?php echo $batch['capacity'] > 0 ? ($batch['students_count'] / $batch['capacity']) * 100 : 0; ?>%"></div>
 </div>
 <span class="text-[10px] font-bold text-gray-900"><?php echo $batch['students_count']; ?><span class="text-gray-300 mx-0.5">/</span><?php echo $batch['capacity']; ?></span>
 </div>
 </td>
 <td class="px-6 py-5 text-center border-l border-gray-100/50">
 <span class="inline-flex items-center px-2 py-1 rounded-lg text-[9px] font-bold border <?php echo $batch['status'] === 'Ongoing' ? 'bg-emerald-50 text-emerald-600 border-emerald-200' : 'bg-amber-50 text-amber-600 border-amber-200'; ?>">
 <?php echo $batch['status']; ?>
 </span>
 </td>
 <td class="px-6 py-5 border-l border-gray-100/50 text-right">
 <div class="flex items-center justify-end gap-2">
 <a href="<?php echo url('admin/batches?edit_id=' . $batch['id']); ?>" class="h-8 w-8 flex items-center justify-center rounded-lg bg-blue-600/10 text-blue-600 hover:bg-blue-600 hover:text-white transition-all border border-blue-600/20">
 <i class="fa-solid fa-pen-to-square text-xs"></i>
 </a>
 <button @click="confirmDelete(<?php echo $batch['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">
 <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($batches as $index => $batch): ?>
 <div class="bg-white border border-gray-200 rounded-2xl p-5 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-start gap-4">
 <div class="h-12 w-12 bg-blue-600/10 rounded-2xl flex items-center justify-center text-blue-600 flex-none">
 <i class="fa-solid fa-users-rectangle text-xl"></i>
 </div>
 <div class="min-w-0 pr-8">
 <h3 class="text-sm font-bold text-gray-900 truncate"><?php echo $batch['name']; ?></h3>
 <p class="text-[10px] font-bold text-blue-600 uppercase tracking-tight"><?php echo $batch['course_name']; ?></p>
 <span class="inline-flex items-center mt-2 px-2 py-0.5 rounded-lg text-[9px] font-bold border <?php echo $batch['status'] === 'Ongoing' ? 'bg-emerald-50 text-emerald-600 border-emerald-200' : 'bg-amber-50 text-amber-600 border-amber-200'; ?>">
 <?php echo $batch['status']; ?>
 </span>
 </div>
 </div>
 <div class="grid grid-cols-2 gap-4 py-4 border-y border-gray-50">
 <div class="space-y-1">
 <p class="text-[8px] font-bold text-gray-400 uppercase tracking-widest">Schedule</p>
 <div class="flex items-center gap-1.5 text-[10px] font-bold text-gray-700">
 <i class="fa-solid fa-calendar-days text-blue-600/40 text-[9px]"></i>
 <span><?php echo $batch['days']; ?></span>
 </div>
 </div>
 <div class="space-y-1">
 <p class="text-[8px] font-bold text-gray-400 uppercase tracking-widest">Teacher</p>
 <div class="flex items-center gap-1.5 text-[10px] font-bold text-gray-700">
 <i class="fa-solid fa-user-tie text-blue-600/40 text-[9px]"></i>
 <span><?php echo $batch['teacher_name']; ?></span>
 </div>
 </div>
 </div>
 <div class="flex items-center justify-between pt-1">
 <div class="flex items-center gap-3 flex-1 max-w-[150px]">
 <div class="flex-1 h-1.5 bg-gray-100 rounded-full overflow-hidden border border-gray-200">
 <div class="h-full bg-blue-600 rounded-full" style="width: <?php echo $batch['capacity'] > 0 ? ($batch['students_count'] / $batch['capacity']) * 100 : 0; ?>%"></div>
 </div>
 <span class="text-[10px] font-bold text-gray-900"><?php echo $batch['students_count']; ?>/<?php echo $batch['capacity']; ?></span>
 </div>
 <div class="flex items-center gap-2">
 <a href="<?php echo url('admin/batches?edit_id=' . $batch['id']); ?>" class="h-9 w-9 flex items-center justify-center rounded-xl bg-blue-600/10 text-blue-600 border border-blue-600/20">
 <i class="fa-solid fa-pen-to-square text-xs"></i>
 </a>
 <button @click="confirmDelete(<?php echo $batch['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">
 <i class="fa-solid fa-trash-can text-xs"></i>
 </button>
 </div>
 </div>
 </div>
 <?php endforeach; ?>
 </div>
 </div>
 </main>

 <!-- Create/Edit Modal -->
 <div x-show="showModal" class="fixed inset-0 z-50 overflow-y-auto" x-cloak>
 <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
 <div class="fixed inset-0 bg-gray-900/40 backdrop-blur-sm transition-opacity" @click="showModal = false; window.location.href='<?php echo url('admin/batches'); ?>'"></div>
 <span class="hidden sm:inline-block sm:align-middle sm:h-screen">&#8203;</span>
 <div class="inline-block align-bottom bg-white rounded-3xl text-left overflow-hidden transform transition-all sm:my-8 sm:align-middle lg:max-w-4xl sm:w-full border-none">
 <div class="bg-white px-6 py-6 sm:p-10">
 <div class="flex items-center justify-between mb-10 border-b border-gray-50 pb-6">
 <div>
 <h3 class="text-xl font-bold text-gray-900 leading-tight"><?php echo isset($editingBatch) ? 'Update' : 'Configure' ?> Batch</h3>
 <p class="text-[11px] font-medium text-gray-400 mt-1 tracking-wide uppercase">Provide detailed schedule and teacher assignments</p>
 </div>
 <button @click="showModal = false; window.location.href='<?php echo url('admin/batches'); ?>'" class="h-12 w-12 flex items-center justify-center rounded-2xl bg-gray-50 text-gray-400 hover:text-gray-600 hover:bg-gray-100 transition-all border border-gray-200">
 <i class="fa-solid fa-xmark text-xl"></i>
 </button>
 </div>

 <form action="<?php echo url('admin/batches/save'); ?>" method="POST" class="space-y-8">
 <?php if(isset($editingBatch)): ?>
 <input type="hidden" name="id" value="<?php echo $editingBatch['id']; ?>">
 <?php endif; ?>

 <div class="grid grid-cols-1 lg:grid-cols-2 gap-x-12 gap-y-8">
 <div class="space-y-6">
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Batch Identity</label>
 <input name="name" type="text" value="<?php echo $editingBatch['name'] ?? ''; ?>" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none" placeholder="e.g. Morning Excellence A">
 </div>
 <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-1 gap-6">
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest text-blue-600">Academic Course</label>
 <div class="relative">
 <select name="course_id" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none appearance-none cursor-pointer">
 <option value="">Choose a course...</option>
 <?php foreach($courses as $course): ?>
 <option value="<?php echo $course['id']; ?>"<?php echo (isset($editingBatch) && $editingBatch['course_id'] == $course['id']) ? 'selected' : ''; ?>><?php echo $course['name']; ?></option>
 <?php endforeach; ?>
 </select>
 <div class="absolute inset-y-0 right-0 pr-4 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-4">
 <i class="fa-solid fa-chevron-down text-[10px]"></i>
 </div>
 </div>
 </div>
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Current Status</label>
 <div class="relative">
 <select name="status" class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none appearance-none cursor-pointer">
 <option value="Upcoming"<?php echo (isset($editingBatch) && $editingBatch['status'] == 'Upcoming') ? 'selected' : ''; ?>>Upcoming</option>
 <option value="Ongoing"<?php echo (isset($editingBatch) && $editingBatch['status'] == 'Ongoing') ? 'selected' : ''; ?>>Ongoing</option>
 <option value="Completed"<?php echo (isset($editingBatch) && $editingBatch['status'] == 'Completed') ? 'selected' : ''; ?>>Completed</option>
 </select>
 <div class="absolute inset-y-0 right-0 pr-4 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-4">
 <i class="fa-solid fa-chevron-down text-[10px]"></i>
 </div>
 </div>
 </div>
 </div>
 </div>

 <div class="space-y-6">
 <div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Assigned Faculty</label>
 <div class="relative">
 <select name="teacher_id" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none appearance-none cursor-pointer">
 <option value="">Select Teacher</option>
 <?php foreach($teachers_list as $teacher): ?>
 <option value="<?php echo $teacher['id']; ?>"<?php echo (isset($editingBatch) && $editingBatch['teacher_id'] == $teacher['id']) ? 'selected' : ''; ?>><?php echo $teacher['name']; ?></option>
 <?php endforeach; ?>
 </select>
 <div class="absolute inset-y-0 right-0 pr-4 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-4">
 <i class="fa-solid fa-chevron-down text-[10px]"></i>
 </div>
 </div>
 </div>
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Max Intake</label>
 <input name="capacity" type="number" value="<?php echo $editingBatch['capacity'] ?? ''; ?>" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none" placeholder="e.g. 30">
 </div>
 </div>
 <div class="grid grid-cols-2 gap-6">
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Start Time</label>
 <input name="start_time" type="time" value="<?php echo $editingBatch['start_time'] ?? ''; ?>" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none">
 </div>
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">End Time</label>
 <input name="end_time" type="time" value="<?php echo $editingBatch['end_time'] ?? ''; ?>" required class="w-full px-4 py-3.5 bg-white border border-gray-200 rounded-xl text-xs font-semibold text-gray-900 focus:ring-4 focus:ring-blue-600/5 focus:border-blue-600 transition-all outline-none">
 </div>
 </div>
 <div class="space-y-3">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Active Weekdays</label>
 <div class="flex flex-wrap gap-2.5">
 <?php foreach(['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] as $day): ?>
 <button type="button"
 @click="toggleDay('<?php echo $day; ?>')"
 class="h-11 w-11 flex items-center justify-center rounded-2xl text-xs font-bold transition-all border"
 :class="days.includes('<?php echo $day; ?>') ? 'bg-blue-600 border-blue-600 text-white -600/20' : 'bg-white border-gray-200 text-gray-400 hover:border-blue-300 hover:text-blue-600'">
 <?php echo in_array($day, ['Sat', 'Sun']) ? substr($day, 0, 2) : substr($day, 0, 1); ?>
 </button>
 <input type="checkbox" name="days[]" value="<?php echo $day; ?>" class="hidden" :checked="days.includes('<?php echo $day; ?>')">
 <?php endforeach; ?>
 </div>
 </div>
 </div>
 </div>

 <div class="pt-8 flex flex-col sm:flex-row items-center justify-end gap-3 border-t border-gray-50">
 <button type="button" @click="showModal = false; window.location.href='<?php echo url('admin/batches'); ?>'" class="w-full sm:w-auto px-8 py-3.5 bg-white border border-gray-200 text-gray-600 text-[11px] font-bold rounded-xl hover:bg-gray-50 transition-all">Discard</button>
 <button type="submit" class="w-full sm:w-auto px-8 py-3.5 bg-blue-600 text-white text-[11px] font-bold 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-check-double text-[10px]"></i>
 <span><?php echo isset($editingBatch) ? 'Confirm Changes' : 'Initialize Batch' ?></span>
 </button>
 </div>
 </form>
 </div>
 </div>
 </div>
 </div>

 <!-- 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 Batch?</h3>
 <p class="text-[11px] font-medium text-gray-400 leading-relaxed mt-1">This action is permanent. All schedule and student assignments for this batch will be removed.</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/batches/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 'layouts/footer.php'; ?>
