<?php
require_once __DIR__ . '/../library/auth.php'; 
$school_id = $_SESSION['school_id'];
allow_roles(['admin', 'accountant']);

// ======================================================
// FETCH EXPENSE CATEGORIES
// ======================================================

$stmt = $pdo->prepare("
    SELECT id, name
    FROM expense_categories
    WHERE school_id = ?
    ORDER BY name ASC
");

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

$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
$page_title = 'Add Expense Entry';
require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

    <section class="content-header">

        <div class="container-fluid">

            <div class="row mb-2">

                <div class="col-sm-6">
                    <h1>Add Expense Entry</h1>
                </div>

                <div class="col-sm-6 text-right">

                    <a href="expense_category.php" class="btn btn-warning">
                        <i class="fa fa-tags"></i> Manage Categories
                    </a>

                    <a href="view_expense.php" class="btn btn-info">
                        <i class="fa fa-list"></i> View All Expenses
                    </a>

                </div>

            </div>

        </div>

    </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="card">

                <div class="card-header bg-danger text-white">
                    <h5>
                        <i class="fa fa-minus-circle"></i>
                        New Expense Entry
                    </h5>
                </div>

                <div class="card-body">

                    <form method="POST" action="process.php">

                        <div class="row">

                            <div class="col-md-3">

                                <label>
                                    Category
                                    <span class="text-danger">*</span>
                                </label>

                                <select name="category_id" class="form-control" required>

                                    <option value="">
                                        Select Category
                                    </option>

                                    <?php foreach($categories as $cat): ?>

                                        <option value="<?= $cat['id'] ?>">

                                            <?= htmlspecialchars($cat['name']) ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>


                            <div class="col-md-5">

                                <label>
                                    Description
                                    <span class="text-danger">*</span>
                                </label>

                                <input
                                    type="text"
                                    name="description"
                                    class="form-control"
                                    required
                                    placeholder="Ex: Electricity Bill, Teacher Salary, etc."
                                >

                            </div>


                            <div class="col-md-4">

                                <label>
                                    Amount (₹)
                                    <span class="text-danger">*</span>
                                </label>

                                <input
                                    type="number"
                                    name="amount"
                                    class="form-control"
                                    min="0"
                                    step="0.01"
                                    required
                                >

                            </div>

                        </div>


                        <div class="row mt-3">

                            <div class="col-md-4">

                                <label>
                                    Payment Mode
                                    <span class="text-danger">*</span>
                                </label>

                                <select name="payment_mode" class="form-control" required>

                                    <option value="Cash">Cash</option>

                                    <option value="UPI">UPI</option>

                                    <option value="Cheque">Cheque</option>

                                    <option value="Bank Transfer">
                                        Bank Transfer
                                    </option>

                                </select>

                            </div>


                            <div class="col-md-4">

                                <label>
                                    Date
                                    <span class="text-danger">*</span>
                                </label>

                                <input
                                    type="date"
                                    name="payment_date"
                                    value="<?= date('Y-m-d') ?>"
                                    class="form-control"
                                    required
                                >

                            </div>


                            <div class="col-md-4 pt-4">

                                <button
                                    type="submit"
                                    name="save_expense"
                                    class="btn btn-danger btn-block"
                                >
                                    <i class="fa fa-save"></i>
                                    Save Expense
                                </button>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

        </div>

    </section>

</div>

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