<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'accountant']);
// Add Category
if (isset($_POST['add_category'])) {
    $stmt = $pdo->prepare("INSERT INTO income_categories
(
    school_id,
    name
)
VALUES (?, ?)");
    $stmt->execute([
    $school_id,
    trim($_POST['name'])
]);
    header("Location: income_category.php?msg=✅ Category Added!");
    exit;
}

// Delete Category
if (isset($_GET['delete'])) {
    $stmt = $pdo->prepare("DELETE FROM income_categories
WHERE id = ?
AND school_id = ?");
    $stmt->execute([$_GET['delete']]);
    header("Location: income_category.php?msg=🗑️ Category Deleted!");
    exit;
}

// Edit Logic
$edit_data = null;
if (isset($_GET['edit'])) {
    $stmt = $pdo->prepare("SELECT *
FROM income_categories
WHERE id = ?
AND school_id = ?");
    $stmt->execute([
    $_GET['edit'],
    $school_id
]);
    $edit_data = $stmt->fetch(PDO::FETCH_ASSOC);
}

if (isset($_POST['update_category'])) {
    $stmt = $pdo->prepare("UPDATE income_categories

SET name = ?

WHERE id = ?
AND school_id = ?");
 $stmt->execute([
    trim($_POST['name']),
    $_POST['id'],
    $school_id
]);
    header("Location: income_category.php?msg=🔄 Category Updated!");
    exit;
}

$stmt = $pdo->prepare("
    SELECT *

    FROM income_categories

    WHERE school_id = ?

    ORDER BY id DESC
");

$stmt->execute([$school_id]);

$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
$page_title = 'Income Categories';
require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';
?>

<div class="content-wrapper">
    <section class="content-header"><h1>Manage Income Categories</h1></section>
    <section class="content">
        <div class="container-fluid">
            <?php if(isset($_GET['msg'])): ?>
                <div class="alert alert-success"><?= htmlspecialchars($_GET['msg']) ?></div>
            <?php endif; ?>

            <div class="row">
                <!-- Form Add/Edit -->
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header bg-primary text-white">
                            <h5><?= $edit_data ? 'Edit Category' : 'Add New Category' ?></h5>
                        </div>
                        <div class="card-body">
                            <form method="POST">
                                <?php if($edit_data): ?> <input type="hidden" name="id" value="<?= $edit_data['id'] ?>"> <?php endif; ?>
                                <div class="form-group">
                                    <label>Category Name</label>
                                    <input type="text" name="name" class="form-control" value="<?= $edit_data['name'] ?? '' ?>" required>
                                </div>
                                <button type="submit" name="<?= $edit_data ? 'update_category' : 'add_category' ?>" class="btn btn-success btn-block">
                                    <?= $edit_data ? 'Update' : 'Save' ?>
                                </button>
                                <?php if($edit_data): ?> <a href="income_category.php" class="btn btn-default btn-block">Cancel</a> <?php endif; ?>
                            </form>
                        </div>
                    </div>
                </div>

                <!-- View List -->
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-body">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Name</th>
                                        <th>Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach($categories as $cat): ?>
                                    <tr>
                                        <td><?= $cat['id'] ?></td>
                                        <td><?= $cat['name'] ?></td>
                                        <td>
                                            <a href="income_category.php?edit=<?= $cat['id'] ?>" class="btn btn-sm btn-warning"><i class="fa fa-edit"></i></a>
                                            <a href="income_category.php?delete=<?= $cat['id'] ?>" class="btn btn-sm btn-danger" onclick="return confirm('Sure?')"><i class="fa fa-trash"></i></a>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
</div>

<?php require_once __DIR__ . '/../include/footer.php'; ?>