<?php
require_once __DIR__ . '/../library/auth.php'; 

$school_id = $_SESSION['school_id'];
$current_session_id = get_current_session_id($pdo);
allow_roles(['admin', 'accountant']);

// ======================================================
// VALIDATE ID
// ======================================================

if (!isset($_GET['id'])) {

    header("Location: view_expense.php");
    exit;
}

$expense_id = intval($_GET['id']);


// ======================================================
// FETCH EXPENSE DATA
// ======================================================

$stmt = $pdo->prepare("
    SELECT *
    FROM expenses
    WHERE id = ?
AND school_id = ?
AND session_id = ?
");

$stmt->execute([
    $expense_id,
    $school_id,
    $current_session_id
]);

$expense = $stmt->fetch(PDO::FETCH_ASSOC);


// Expense Not Found
if (!$expense) {

    header("Location: view_expense.php?msg=❌ Expense Not Found!");
    exit;
}


// ======================================================
// FETCH 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 = 'Edit Expense';

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

?>

<div class="content-wrapper">

    <section class="content-header">

        <h1>Edit Expense</h1>

    </section>


    <section class="content">

        <div class="card">

            <div class="card-body">

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

                    <input
                        type="hidden"
                        name="expense_id"
                        value="<?= $expense['id'] ?>"
                    >


                    <div class="row">

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

                            <label>Category</label>

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

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

                                    <option
                                        value="<?= $cat['id'] ?>"
                                        <?= $expense['category_id'] == $cat['id'] ? 'selected' : '' ?>
                                    >

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

                                    </option>

                                <?php endforeach; ?>

                            </select>

                        </div>


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

                            <label>Description</label>

                            <input
                                type="text"
                                name="description"
                                class="form-control"
                                value="<?= htmlspecialchars($expense['description']) ?>"
                                required
                            >

                        </div>


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

                            <label>Amount (₹)</label>

                            <input
                                type="number"
                                name="amount"
                                class="form-control"
                                value="<?= $expense['amount'] ?>"
                                step="0.01"
                                min="0"
                                required
                            >

                        </div>

                    </div>


                    <div class="row mt-3">

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

                            <label>Payment Mode</label>

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

                                <option
                                    value="Cash"
                                    <?= $expense['payment_mode'] == 'Cash' ? 'selected' : '' ?>
                                >
                                    Cash
                                </option>

                                <option
                                    value="UPI"
                                    <?= $expense['payment_mode'] == 'UPI' ? 'selected' : '' ?>
                                >
                                    UPI
                                </option>

                                <option
                                    value="Cheque"
                                    <?= $expense['payment_mode'] == 'Cheque' ? 'selected' : '' ?>
                                >
                                    Cheque
                                </option>

                                <option
                                    value="Bank Transfer"
                                    <?= $expense['payment_mode'] == 'Bank Transfer' ? 'selected' : '' ?>
                                >
                                    Bank Transfer
                                </option>

                            </select>

                        </div>


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

                            <label>Date</label>

                            <input
                                type="date"
                                name="payment_date"
                                class="form-control"
                                value="<?= $expense['payment_date'] ?>"
                                required
                            >

                        </div>


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

                            <button
                                type="submit"
                                name="update_expense"
                                class="btn btn-warning btn-block"
                            >

                                Update

                            </button>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

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