<?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($editingTeacher) ? 'true' : 'false'; ?>,
 showViewModal: <?php echo isset($viewingTeacher) ? 'true' : 'false'; ?>,
 deleteModalOpen: false,
 deleteId: null,
 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-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">Teachers Master</h1>
 <p class="text-[10px] sm:text-xs font-medium text-gray-400">Manage your faculty and teaching staff</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-plus text-sm"></i>
 <span>Add New Teacher</span>
 </button>
 </div>

 <!-- Search and Filters -->
 <form action="<?php echo url('admin/teachers'); ?>" 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-2.5 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, email, phone or subject...">
 </div>
 
 <div class="flex items-center gap-3 w-full sm:w-auto">
 <div class="relative w-full sm:w-48">
 <select name="status_filter" onchange="this.form.submit()" class="w-full px-4 py-2.5 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 Status</option>
 <option value="Active"<?php echo $params['status_filter'] === 'Active' ? 'selected' : ''; ?>>Active</option>
 <option value="Inactive"<?php echo $params['status_filter'] === 'Inactive' ? 'selected' : ''; ?>>Inactive</option>
 </select>
 <div class="absolute inset-y-0 right-0 pr-3 flex items-center pointer-events-none text-gray-400 border-l border-gray-100 ml-2 pl-2">
 <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 border-l border-gray-100/50">Teacher Details</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Specialization & 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($teachers)): ?>
 <tr>
 <td colspan="5" class="px-6 py-12 text-center">
 <div class="flex flex-col items-center">
 <i class="fa-solid fa-users-slash text-gray-200 text-4xl mb-4"></i>
 <p class="text-xs font-bold text-gray-400">No teachers found</p>
 </div>
 </td>
 </tr>
 <?php else: ?>
 <?php foreach($teachers as $index => $teacher): ?>
 <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-5 border-l border-gray-100/50">
 <div class="flex items-center gap-4">
 <div class="h-10 w-10 bg-blue-600/10 rounded-xl flex items-center justify-center text-blue-600 flex-none font-bold text-xs uppercase">
 <?php echo substr($teacher['name'], 0, 1); ?>
 </div>
 <div class="flex flex-col">
 <span class="text-xs font-bold text-gray-900 group-hover:text-blue-600 transition-colors"><?php echo $teacher['name']; ?></span>
 <span class="text-[10px] font-medium text-gray-400 mt-0.5">ID: #TCH-<?php echo str_pad($teacher['id'], 4, '0', STR_PAD_LEFT); ?></span>
 </div>
 </div>
 </td>
 <td class="px-6 py-5 border-l border-gray-100/50">
 <div class="flex flex-col gap-1.5">
 <span class="px-2 py-0.5 bg-blue-50 text-blue-600 rounded-lg text-[9px] font-bold uppercase tracking-wide w-fit">
 <?php echo $teacher['subject'] ?? 'General'; ?>
 </span>
 <div class="flex items-center gap-3">
 <span class="text-[10px] font-medium text-gray-500 flex items-center gap-1">
 <i class="fa-solid fa-phone text-[9px] text-gray-400"></i>
 <?php echo $teacher['phone']; ?>
 </span>
 </div>
 </div>
 </td>
 <td class="px-6 py-5 text-center border-l border-gray-100/50">
 <span class="px-2 py-0.5 rounded-full text-[9px] font-bold <?php echo $teacher['status'] === 'Active' ? 'bg-emerald-50 text-emerald-500 border-emerald-100' : 'bg-red-50 text-red-500 border-red-100'; ?> border">
 <?php echo $teacher['status']; ?>
 </span>
 </td>
 <td class="px-6 py-5 text-right border-l border-gray-100/50">
 <div class="flex items-center justify-end gap-2">
 <a href="<?php echo url('admin/teachers?view_id=' . $teacher['id']); ?>" class="h-8 w-8 flex items-center justify-center rounded-lg bg-emerald-600/10 text-emerald-600 hover:bg-emerald-600 hover:text-white transition-all border border-emerald-600/20">
 <i class="fa-solid fa-eye text-xs"></i>
 </a>
 <a href="<?php echo url('admin/teachers?edit_id=' . $teacher['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 $teacher['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($teachers as $index => $teacher): ?>
 <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 justify-between">
 <div class="flex items-center gap-3">
 <div class="h-10 w-10 bg-blue-600/10 rounded-xl flex items-center justify-center text-blue-600 font-bold text-sm uppercase">
 <?php echo substr($teacher['name'], 0, 1); ?>
 </div>
 <div class="flex flex-col">
 <span class="text-sm font-bold text-gray-900 leading-none"><?php echo $teacher['name']; ?></span>
 <span class="text-[10px] font-medium text-gray-400 mt-1">ID: #TCH-<?php echo str_pad($teacher['id'], 4, '0', STR_PAD_LEFT); ?></span>
 </div>
 </div>
 <span class="text-[9px] font-bold px-2 py-0.5 rounded-full <?php echo $teacher['status'] === 'Active' ? 'bg-emerald-50 text-emerald-500 border-emerald-100' : 'bg-red-50 text-red-500 border-red-100'; ?> border"><?php echo $teacher['status']; ?></span>
 </div>
 <div class="space-y-3 border-t border-gray-50 pt-3">
 <div class="flex items-center justify-between">
 <p class="text-[8px] font-bold text-gray-400 uppercase tracking-widest">Specialization</p>
 <span class="px-2 py-0.5 bg-blue-50 text-blue-600 rounded-lg text-[9px] font-bold uppercase tracking-wide">
 <?php echo $teacher['subject'] ?? 'General'; ?>
 </span>
 </div>
 <div class="flex flex-col gap-2">
 <div class="flex items-center justify-between w-full px-3 py-2 bg-gray-50/50 rounded-xl border border-gray-100">
 <span class="text-[9px] font-bold text-gray-400 uppercase">Phone</span>
 <span class="text-[10px] font-bold text-gray-900"><?php echo $teacher['phone']; ?></span>
 </div>
 </div>
 </div>
 <div class="flex items-center justify-end gap-2 pt-2 border-t border-gray-50">
 <a href="<?php echo url('admin/teachers?view_id=' . $teacher['id']); ?>" class="h-10 w-10 flex items-center justify-center rounded-xl bg-emerald-600/10 text-emerald-600 border border-emerald-600/20">
 <i class="fa-solid fa-eye text-xs"></i>
 </a>
 <a href="<?php echo url('admin/teachers?edit_id=' . $teacher['id']); ?>" class="h-10 w-10 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 $teacher['id']; ?>)" class="h-10 w-10 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>

 <!-- 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/teachers'); ?>'"></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-2xl sm:w-full">
 <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($editingTeacher) ? 'Update' : 'Register'; ?> Teacher</h3>
 <p class="text-[11px] font-medium text-gray-400 mt-1 tracking-wide uppercase">Manage faculty profile and specialization</p>
 </div>
 <button @click="showModal = false; window.location.href='<?php echo url('admin/teachers'); ?>'" 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/teachers/save'); ?>" method="POST" class="space-y-8">
 <?php if(isset($editingTeacher)): ?>
 <input type="hidden" name="id" value="<?php echo $editingTeacher['id']; ?>">
 <?php endif; ?>

 <div class="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6">
 <div class="space-y-2 md:col-span-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Full Name</label>
 <input name="name" type="text" value="<?php echo $editingTeacher['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. Prof. Amitava Das">
 </div>

 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Phone Number</label>
 <input name="phone" type="text" value="<?php echo $editingTeacher['phone'] ?? ''; ?>" 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. 9876543210">
 </div>

 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Email Address</label>
 <input name="email" type="email" value="<?php echo $editingTeacher['email'] ?? ''; ?>" 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. teacher@example.com">
 </div>

 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Specialization</label>
 <input name="subject" type="text" value="<?php echo $editingTeacher['subject'] ?? ''; ?>" 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. Mathematics">
 </div>

 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Employment Status</label>
 <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="Active"<?php echo (isset($editingTeacher) && $editingTeacher['status'] == 'Active') ? 'selected' : ''; ?>>Active</option>
 <option value="Inactive"<?php echo (isset($editingTeacher) && $editingTeacher['status'] == 'Inactive') ? 'selected' : ''; ?>>Inactive</option>
 </select>
 </div>
 </div>

 <div class="pt-8 flex flex-col sm:flex-row items-center justify-end gap-3">
 <button type="button" @click="showModal = false; window.location.href='<?php echo url('admin/teachers'); ?>'" 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 -600/20 active:scale-95">
 <i class="fa-solid fa-check-double text-[10px]"></i>
 <span><?php echo isset($editingTeacher) ? 'Confirm Changes' : 'Complete Registration'; ?></span>
 </button>
 </div>
 </form>
 </div>
 </div>
 </div>
 </div>

 <!-- View Modal -->
 <div x-show="showViewModal" 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="showViewModal = false; window.location.href='<?php echo url('admin/teachers'); ?>'"></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-xl sm:w-full">
 <?php if($viewingTeacher): ?>
 <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 class="flex items-center gap-5">
 <div class="h-16 w-16 bg-blue-600/10 rounded-2xl flex items-center justify-center text-blue-600 font-bold text-2xl uppercase">
 <?php echo substr($viewingTeacher['name'], 0, 1); ?>
 </div>
 <div>
 <h3 class="text-xl font-bold text-gray-900 leading-tight"><?php echo $viewingTeacher['name']; ?></h3>
 <div class="flex items-center gap-2 mt-1">
 <span class="text-[11px] font-medium text-gray-400 uppercase tracking-wide"><?php echo $viewingTeacher['subject'] ?? 'General'; ?></span>
 <span class="h-1 w-1 rounded-full bg-gray-300"></span>
 <span class="text-[10px] font-bold text-blue-600">ID: #TCH-<?php echo str_pad($viewingTeacher['id'], 4, '0', STR_PAD_LEFT); ?></span>
 </div>
 </div>
 </div>
 <button @click="showViewModal = false; window.location.href='<?php echo url('admin/teachers'); ?>'" 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>

 <div class="space-y-8">
 <div class="space-y-4">
 <p class="text-[10px] font-bold text-gray-400 uppercase tracking-[0.2em]">Contact Details</p>
 <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
 <div class="p-4 bg-gray-50/50 rounded-2xl border border-gray-100">
 <p class="text-[9px] font-bold text-gray-400 uppercase mb-1">Mobile</p>
 <p class="text-sm font-bold text-gray-900"><?php echo $viewingTeacher['phone']; ?></p>
 </div>
 <div class="p-4 bg-gray-50/50 rounded-2xl border border-gray-100">
 <p class="text-[9px] font-bold text-gray-400 uppercase mb-1">Email</p>
 <p class="text-sm font-bold text-gray-900 truncate"><?php echo $viewingTeacher['email'] ?? 'N/A'; ?></p>
 </div>
 </div>
 </div>

 <div class="p-4 bg-gray-50 rounded-2xl border border-gray-100 flex items-center justify-between">
 <div class="flex items-center gap-2">
 <span class="h-2 w-2 rounded-full <?php echo $viewingTeacher['status'] === 'Active' ? 'bg-emerald-500' : 'bg-red-500'; ?>"></span>
 <span class="text-[10px] font-bold text-gray-600 uppercase tracking-widest"><?php echo $viewingTeacher['status']; ?> Staff</span>
 </div>
 <span class="text-[9px] font-medium text-gray-400 uppercase">Joined: <?php echo date('d/m/Y', strtotime($viewingTeacher['created_at'])); ?></span>
 </div>
 </div>

 <div class="mt-12 pt-6 border-t border-gray-50 flex justify-end">
 <button @click="showViewModal = false; window.location.href='<?php echo url('admin/teachers'); ?>'" class="px-10 py-3 bg-gray-900 text-white text-[11px] font-bold uppercase tracking-widest rounded-xl hover:bg-gray-800 transition-all active:scale-95">Close Profile</button>
 </div>
 </div>
 <?php endif; ?>
 </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 Teacher?</h3>
 <p class="text-[11px] font-medium text-gray-400 leading-relaxed mt-1">This action is permanent and cannot be undone. All assigned batches for this teacher might be affected.</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/teachers/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'; ?>
