<?php

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

$school_id = $_SESSION['school_id'];

$current_session_id = get_current_session_id($pdo);

allow_roles(['admin', 'accountant']);


// ======================================================
// 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);


// ======================================================
// QUICK STATS
// ======================================================

$stmt = $pdo->prepare("
    SELECT SUM(amount)

    FROM income

    WHERE school_id = ?
    AND session_id = ?
    AND DATE(payment_date) = CURDATE()
");

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

$today_income = $stmt->fetchColumn() ?: 0;


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

$stmt = $pdo->prepare("
    SELECT SUM(amount)

    FROM income

    WHERE school_id = ?
    AND session_id = ?
");

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

$total_income = $stmt->fetchColumn() ?: 0;


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

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

                </div>

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

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

                        View Income Records

                    </a>

                </div>

            </div>

        </div>

    </section>

    <section class="content">

        <div class="container-fluid">

            <!-- MESSAGE -->

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

                <div class="alert alert-success">

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

                </div>

            <?php endif; ?>

            <!-- FORM -->

            <div class="card">

                <div class="card-header bg-primary text-white">

                    <h5>

                        New Income Entry

                    </h5>

                </div>

                <div class="card-body">

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

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

                        <div class="row">

                            <!-- CATEGORY -->

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

                                <div class="form-group">

                                    <label>

                                        Category

                                    </label>

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

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

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

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

                                                <?= 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"
                                        placeholder="Enter 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"
                                        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="">
                                            Select Mode
                                        </option>

                                        <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>

                            <!-- DATE -->

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

                                <div class="form-group">

                                    <label>

                                        Payment Date

                                    </label>

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

                                </div>

                            </div>

                            <!-- BUTTON -->

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

                                <div class="form-group">

                                    <label>&nbsp;</label>

                                    <button
                                        type="submit"
                                        name="save_income"
                                        class="btn btn-success btn-block"
                                    >

                                        Save Income

                                    </button>

                                </div>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

            <!-- STATS -->

            <div class="row">

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

                    <div class="card bg-success text-white">

                        <div class="card-body text-center">

                            <h3>

                                ₹<?= number_format($today_income, 2) ?>

                            </h3>

                            <p>

                                Today Income

                            </p>

                        </div>

                    </div>

                </div>

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

                    <div class="card bg-primary text-white">

                        <div class="card-body text-center">

                            <h3>

                                ₹<?= number_format($total_income, 2) ?>

                            </h3>

                            <p>

                                Total Income

                            </p>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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