<?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($editingCategory) ? '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">Categories Master</h1>
 <p class="text-[10px] sm:text-xs font-medium text-gray-400">Manage course categories and groupings</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 Category</span>
 </button>
 </div>

 <!-- Search and Filters -->
 <form action="<?php echo url('admin/categories'); ?>" 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 categories...">
 </div>
 
 <div class="flex items-center gap-3 w-full sm:w-auto">
 <div class="relative w-full sm:w-40">
 <select name="filter_status" 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="1"<?php echo $params['filter_status'] === '1' ? 'selected' : ''; ?>>Active</option>
 <option value="0"<?php echo $params['filter_status'] === '0' ? '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">Category Name</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50">Description</th>
 <th class="px-6 py-4 text-[10px] font-bold text-gray-500 uppercase tracking-widest border-l border-gray-100/50 text-center">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($categories)): ?>
 <tr>
 <td colspan="5" class="px-6 py-12 text-center">
 <div class="flex flex-col items-center">
 <i class="fa-solid fa-layer-group text-gray-200 text-4xl mb-4"></i>
 <p class="text-xs font-bold text-gray-400">No categories found</p>
 </div>
 </td>
 </tr>
 <?php else: ?>
 <?php foreach($categories as $index => $category): ?>
 <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">
 <span class="text-xs font-semibold text-gray-900 group-hover:text-blue-600 transition-colors"><?php echo $category['name']; ?></span>
 </td>
 <td class="px-6 py-4 border-l border-gray-100/50">
 <p class="text-[10px] font-medium text-gray-400 mt-0.5 line-clamp-1"><?php echo $category['description'] ?: 'No description'; ?></p>
 </td>
 <td class="px-6 py-4 border-l border-gray-100/50 text-center">
 <a href="<?php echo url('admin/categories/toggle-status?id=' . $category['id']); ?>" class="relative inline-flex h-5 w-9 items-center rounded-full transition-colors focus:outline-none <?php echo $category['is_active'] ? 'bg-blue-600' : 'bg-gray-200'; ?>">
 <span class="inline-block h-3 w-3 transform rounded-full bg-white transition-transform <?php echo $category['is_active'] ? 'translate-x-5' : 'translate-x-1'; ?>"></span>
 </a>
 </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/categories?edit_id=' . $category['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 $category['id']; ?>)" class="h-8 w-8 flex items-center justify-center rounded-lg bg-red-50 text-red-500 hover:bg-red-500 hover:text-white transition-all border border-red-100">
 <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($categories as $index => $category): ?>
 <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 flex-col">
 <span class="text-sm font-bold text-gray-900 leading-none"><?php echo $category['name']; ?></span>
 <span class="text-[10px] font-medium text-gray-400 mt-2 truncate max-w-[200px]"><?php echo $category['description'] ?: 'No description'; ?></span>
 </div>
 <a href="<?php echo url('admin/categories/toggle-status?id=' . $category['id']); ?>" class="relative inline-flex h-5 w-9 items-center rounded-full transition-colors focus:outline-none <?php echo $category['is_active'] ? 'bg-blue-600' : 'bg-gray-200'; ?>">
 <span class="inline-block h-3 w-3 transform rounded-full bg-white transition-transform <?php echo $category['is_active'] ? 'translate-x-5' : 'translate-x-1'; ?>"></span>
 </a>
 </div>
 <div class="flex items-center justify-end gap-2 pt-1">
 <a href="<?php echo url('admin/categories?edit_id=' . $category['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 $category['id']; ?>)" class="h-10 w-10 flex items-center justify-center rounded-xl bg-red-50 text-red-500 border border-red-100">
 <i class="fa-solid fa-trash-can text-xs"></i>
 </button>
 </div>
 </div>
 <?php endforeach; ?>
 </div>
 </div>
 </main>

 <!-- Modal (Add/Edit) -->
 <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" @click="showModal = false; window.location.href='<?php echo url('admin/categories'); ?>'"></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 sm:max-w-lg sm:w-full">
 <div class="bg-white px-6 py-6 sm:p-10">
 <div class="flex items-center justify-between mb-8 border-b border-gray-50 pb-6">
 <h3 class="text-lg font-bold text-gray-900"><?php echo isset($editingCategory) ? 'Edit Category' : 'Add New Category'; ?></h3>
 <button @click="showModal = false; window.location.href='<?php echo url('admin/categories'); ?>'" class="h-10 w-10 flex items-center justify-center rounded-xl 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-lg"></i>
 </button>
 </div>

 <form action="<?php echo url('admin/categories/save'); ?>" method="POST" class="space-y-6">
 <?php if(isset($editingCategory)): ?>
 <input type="hidden" name="id" value="<?php echo $editingCategory['id']; ?>">
 <?php endif; ?>
 
 <div class="space-y-1.5">
 <label class="text-[9px] font-semibold text-gray-400 ml-0.5 uppercase tracking-tighter">Category Name</label>
 <input name="name" type="text" value="<?php echo $editingCategory['name'] ?? ''; ?>" required 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" placeholder="e.g. Science">
 </div>

 <div class="space-y-1.5">
 <label class="text-[9px] font-semibold text-gray-400 ml-0.5 uppercase tracking-tighter">Description</label>
 <textarea name="description" rows="3" 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 resize-none" placeholder="Short description..."><?php echo $editingCategory['description'] ?? ''; ?></textarea>
 </div>

 <div class="flex items-center justify-between p-3 border border-gray-200 rounded-xl bg-gray-50/30" x-data="{ isActive: <?php echo (isset($editingCategory) && !$editingCategory['is_active']) ? 'false' : 'true'; ?> }">
 <span class="text-[10px] font-bold text-gray-600 uppercase tracking-tighter">Active Status</span>
 <input type="hidden" name="is_active" :value="isActive ? '1' : '0'">
 <button type="button" @click="isActive = !isActive" class="relative inline-flex h-4 w-7 items-center rounded-full transition-colors focus:outline-none" :class="isActive ? 'bg-blue-600' : 'bg-gray-200'">
 <span class="inline-block h-2 w-2 transform rounded-full bg-white transition-transform" :class="isActive ? 'translate-x-4' : 'translate-x-1'"></span>
 </button>
 </div>

 <div class="pt-6 flex items-center justify-end gap-3">
 <button type="button" @click="showModal = false; window.location.href='<?php echo url('admin/categories'); ?>'" class="px-6 py-2.5 bg-white border border-gray-200 text-gray-600 text-[11px] font-bold rounded-xl hover:bg-gray-50 transition-all">Cancel</button>
 <button type="submit" class="px-6 py-2.5 bg-blue-600 text-white text-[11px] font-bold rounded-xl hover:bg-blue-700 transition-all active:scale-95">
 <?php echo isset($editingCategory) ? 'Update' : 'Create'; ?> Category
 </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 Category?</h3>
 <p class="text-[11px] font-medium text-gray-400 leading-relaxed mt-1">This action is permanent and cannot be undone. Any courses linked to this category 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/categories/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'; ?>
