<?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: false, deleteModalOpen: false, deleteId: null, editData: {id: '', name: ''} }">
 <?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">Expense Categories</h1>
 <p class="text-[10px] sm:text-xs font-medium text-gray-400 mt-0.5">Manage structure for institute overheads</p>
 </div>
 <div class="flex flex-col sm:flex-row items-center gap-3">
 <a href="<?php echo url('/admin/expenses'); ?>" class="w-full sm:w-auto px-5 py-3 bg-white border border-gray-200 text-gray-600 text-xs font-semibold rounded-xl hover:bg-gray-50 transition-all flex items-center justify-center gap-2">
 <i class="fa-solid fa-arrow-left text-sm"></i>
 <span>Back to Expenses</span>
 </a>
 <button @click="editData = {id: '', name: ''}; showModal = true" class="w-full sm:w-auto px-5 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">
 <i class="fa-solid fa-plus text-sm"></i>
 <span>Add Category</span>
 </button>
 </div>
 </div>

 <!-- 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">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 text-center">Usage</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($categories)): ?>
 <tr><td colspan="4" class="px-5 py-12 text-center text-xs font-bold text-gray-400">No categories found</td></tr>
 <?php else: ?>
 <?php foreach($categories as $index => $category): ?>
 <tr class="group hover:bg-gray-50 transition-colors">
 <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-3">
 <div class="h-8 w-8 rounded-lg bg-blue-600/5 flex items-center justify-center text-blue-600 group-hover:bg-blue-600 group-hover:text-white transition-all">
 <i class="fa-solid fa-layer-group text-[10px]"></i>
 </div>
 <span class="text-xs font-bold text-gray-900 group-hover:text-blue-600 transition-colors"><?php echo $category['name']; ?></span>
 </div>
 </td>
 <td class="px-6 py-5 text-center border-l border-gray-100/50">
 <span class="px-3 py-1 bg-gray-50 text-gray-500 text-[9px] font-bold rounded-lg border border-gray-200 uppercase tracking-tight">
 <?php echo $category['usage_count']; ?> Records
 </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">
 <button @click="editData = <?php echo htmlspecialchars(json_encode($category)); ?>; showModal = true" 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>
 </button>
 <button @click="deleteId = <?php echo $category['id']; ?>; deleteModalOpen = true" 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($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-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">
 <i class="fa-solid fa-tags text-lg"></i>
 </div>
 <div>
 <h3 class="text-sm font-bold text-gray-900 truncate"><?php echo $category['name']; ?></h3>
 </div>
 </div>

 <div class="flex items-center justify-end pt-4 border-t border-gray-50 gap-2">
 <button @click="editData = <?php echo htmlspecialchars(json_encode($category)); ?>; showModal = true" 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>
 </button>
 <button @click="deleteId = <?php echo $category['id']; ?>; deleteModalOpen = true" 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>
 <?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"></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 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-lg font-bold text-gray-900 leading-tight" x-text="editData.id ? 'Update Category' : 'New Category'"></h3>
 <p class="text-[10px] font-medium text-gray-400 mt-1 tracking-wide uppercase">Define a name for overhead classification</p>
 </div>
 <button @click="showModal = false" class="h-10 w-10 flex items-center justify-center rounded-xl bg-gray-50 text-gray-400 hover:text-gray-600 transition-all border border-gray-200">
 <i class="fa-solid fa-xmark text-lg"></i>
 </button>
 </div>

 <form action="<?php echo url('/admin/expense-categories/save'); ?>" method="POST" class="space-y-6">
 <input type="hidden" name="id" x-model="editData.id">
 <div class="space-y-2">
 <label class="text-[10px] font-bold text-gray-400 ml-0.5 uppercase tracking-widest">Category Name</label>
 <input name="name" x-model="editData.name" type="text" 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. Utility Bills">
 </div>

 <div class="pt-6 flex flex-col sm:flex-row items-center justify-end gap-3">
 <button type="button" @click="showModal = false" class="w-full sm:w-auto 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">Discard</button>
 <button type="submit" class="w-full sm:w-auto px-6 py-2.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 x-text="editData.id ? 'Confirm Changes' : 'Create Category'"></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">Remove Category?</h3>
 <p class="text-[11px] font-medium text-gray-400 leading-relaxed mt-1">Are you sure you want to delete this category? All associated expense records will 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/expense-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'; ?>
