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

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

$id = (int)($_GET['id'] ?? 0);

if ($id <= 0) {

    header("Location: view_income.php?msg=Invalid income id");
    exit;
}

// ======================================================
// FETCH INCOME
// ======================================================

$stmt = $pdo->prepare("
    SELECT

        i.*,

        ic.name AS category_name

    FROM income i

    LEFT JOIN income_categories ic
        ON ic.id = i.category_id

    WHERE i.id = ?
    AND i.school_id = ?

    LIMIT 1
");

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

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

// ======================================================

if (!$income) {

    header("Location: view_income.php?msg=Income record not found");
    exit;
}

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

$stmt = $pdo->prepare("
    SELECT

        id,
        name

    FROM income_categories

    WHERE school_id = ?

    ORDER BY name ASC
");

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

$categories = $stmt->fetchAll(PDO::FETCH_ASSOC);
// ======================================================

$page_title = 'Edit Income 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>Edit Income Entry</h1>

                </div>

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

                    <a
                        href="view_income.php"
                        class="btn btn-secondary"
                    >

                        Back To Records

                    </a>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <?php if(isset($_GET['msg'])): ?>

                <div class="alert alert-danger">

                    <?= htmlspecialchars($_GET['msg']) ?>

                </div>

            <?php endif; ?>

            <!-- FORM -->

            <div class="card">

                <div class="card-header bg-warning">

                    <h5>

                        Update Income Entry

                    </h5>

                </div>

                <div class="card-body">

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

                        <input
                            type="hidden"
                            name="action"
                            value="edit"
                        >

                        <input
                            type="hidden"
                            name="income_id"
                            value="<?= $income['id'] ?>"
                        >

                        <div class="row">

                            <!-- CATEGORY -->

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

                                <div class="form-group">

                                    <label>

                                        Category

                                    </label>

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

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

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

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

</option>

                                        <?php endforeach; ?>

                                    </select>

                                </div>

                            </div>

                            <!-- DESCRIPTION -->

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

                                <div class="form-group">

                                    <label>

                                        Description

                                    </label>

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

                                </div>

                            </div>

                            <!-- AMOUNT -->

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

                                <div class="form-group">

                                    <label>

                                        Amount

                                    </label>

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

                                </div>

                            </div>

                        </div>

                        <!-- SECOND ROW -->

                        <div class="row mt-3">

                            <!-- PAYMENT MODE -->

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

                                <div class="form-group">

                                    <label>

                                        Payment Mode

                                    </label>

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

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

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

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

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

                                    </select>

                                </div>

                            </div>

                            <!-- DATE -->

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

                                <div class="form-group">

                                    <label>

                                        Payment Date

                                    </label>

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

                                </div>

                            </div>

                            <!-- BUTTON -->

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

                                <div class="form-group">

                                    <label>&nbsp;</label>

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

                                        Update Income

                                    </button>

                                </div>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

        </div>

    </section>

</div>

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