<?php
if(!isset($_GET['id'])){
    header('Location: 505');
    exit;
}
include_once "global.php";

$pageTitle = 'Edit Expense - ';
include_once 'header.php';

if(isset($_GET["id"])){
    $expenseId = $_GET["id"];

    $sql = "SELECT * FROM `daily_expenses` WHERE `id` = ?";
    $expenseData = $dbF->getRow($sql, [$expenseId]);
}
?>

<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Edit Expense</h5>
        <form id="expenseEditForm" class="row g-3" method="post">
            <input type="hidden" name="expenseId" value="<?php echo $expenseId; ?>">
            <div class="col-md-6">
                <label for="title" class="form-label">Title <span>*</span></label>
                <input type="text" name="title" id="title" class="form-control" value="<?php echo $expenseData["title"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="category" class="form-label">Category <span>*</span></label>
                <select id="category" name="category" class="form-select" required>
                    <option selected="" value="">Select...</option>
                    <?php
                    $expenseCategories = $productClass->getExpenseCategories();
                    foreach ($expenseCategories as $expenseCategory) {
                        $categoryId = $expenseCategory["id"];
                        $name = $expenseCategory["category"];
                        $selected = ($categoryId == $expenseData["category"]) ? "selected" : "";
                        echo "<option value='$categoryId' $selected>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="paidto" class="form-label">Paid To <span>*</span></label>
                <input type="text" name="paidto" id="paidto" class="form-control" value="<?php echo $expenseData["paid_to"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="amount" class="form-label">Amount <span>*</span></label>
                <input type="number" name="amount" id="amount" class="form-control" value="<?php echo $expenseData["amount"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="date" class="form-label">Date <span>*</span></label>
                <input type="date" name="date" id="date" class="form-control" value="<?php echo $expenseData["date"]; ?>" required>
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-grd-primary px-4">Submit</button>
            </div>
        </form>
    </div>
</div>

<?php
    include_once 'footer.php';
?>