<?php
session_start();
require_once __DIR__ . '/../../includes/auth.php';
require_once __DIR__ . '/../../classes/Database.php';
require_once __DIR__ . '/../../classes/Expense.php';


$database = new Database();
$conn = $database->getConnection();


$auth = new Auth($conn);


if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit();
}
$auth->enforceSuperAdminPrivileges($_SESSION['user_id']);


$expense = new Expense($conn);


$categories = $expense->getAllCategories();


if (isset($_GET['delete']) && isset($_GET['id'])) {
    $categoryId = (int)$_GET['id'];
    if ($expense->deleteCategory($categoryId)) {
        $successMessage = "Category deleted successfully.";

        $categories = $expense->getAllCategories();
    } else {
        $errorMessage = "Failed to delete category. It may be in use by existing expenses.";
    }
}
?>

<?php include __DIR__ . '/../../includes/header.php'; ?>
<style>
    .action-buttons .btn {
        margin-right: 5px;
    }
    .card {
        margin-bottom: 20px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    }
    .btn-action {
        margin-right: 5px;
    }
</style>
    <div class="container mt-4">
        <div class="row mb-3">
            <div class="col-12">
                <h2>Manage Expense Categories</h2>
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-12 d-flex justify-content-between align-items-center">
                <a href="manage_expenses.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Back to Expenses
                </a>
                <a href="add_category.php" class="btn btn-primary">
                    <i class="fas fa-plus"></i> Add New Category
                </a>
            </div>
        </div>

        <?php if (isset($successMessage)): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo htmlspecialchars($successMessage); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>

        <?php if (isset($errorMessage)): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo htmlspecialchars($errorMessage); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        <?php endif; ?>


        <div class="card">
            <div class="card-header">
                <h5>Expense Categories</h5>
            </div>
            <div class="card-body">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Category Name</th>
                            <th>Description</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($categories->rowCount() === 0): ?>
                            <tr>
                                <td colspan="4" class="text-center">No categories found.</td>
                            </tr>
                        <?php else: ?>
                            <?php while ($category = $categories->fetch(PDO::FETCH_ASSOC)): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($category['category_id']); ?></td>
                                    <td><?php echo htmlspecialchars($category['category_name']); ?></td>
                                    <td><?php echo htmlspecialchars($category['description'] ?? 'N/A'); ?></td>
                                    <td class="action-buttons">
                                        <a href="add_category_type.php?id=<?php echo $category['category_id']; ?>" class="btn btn-sm btn-primary btn-action">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <a href="javascript:void(0);" onclick="confirmDelete(<?php echo $category['category_id']; ?>)" class="btn btn-sm btn-danger btn-action">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        function confirmDelete(id) {
            if (confirm('Are you sure you want to delete this category?')) {
                window.location.href = `?delete=1&id=${id}`;
            }
        }
    </script>
<?php include __DIR__ . '/../../includes/footer.php'; ?>