<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

$id = intval($_GET['id']);
$payment = $conn->query("SELECT * FROM payments WHERE id=$id")->fetch_assoc();
$guests = $conn->query("SELECT * FROM guests ORDER BY name");

if(!$payment){
    echo "<div class='alert alert-danger'>Payment not found.</div>";
    exit;
}

// Handle update
if(isset($_POST['update_payment'])){
    $guest_id = intval($_POST['guest_id']);
    $amount = floatval($_POST['amount']);
    $method = $_POST['method'];
    $type = $_POST['type'];
    $description = $_POST['description'];
    $payment_date = $_POST['payment_date'];

    $stmt = $conn->prepare("UPDATE payments SET guest_id=?, amount=?, method=?, type=?, description=?, payment_date=? WHERE id=?");
    $stmt->bind_param("idssssi", $guest_id, $amount, $method, $type, $description, $payment_date, $id);
    if($stmt->execute()){
        echo "<div class='alert alert-success'>Payment updated successfully.</div>";
        $payment = $conn->query("SELECT * FROM payments WHERE id=$id")->fetch_assoc();
    } else {
        echo "<div class='alert alert-danger'>Error: ".$stmt->error."</div>";
    }
}
?>

<div class="container mt-4">
      <!-- ================= WELCOME MESSAGE ================= -->
    <div class="welcome-msg">
        <i class="fa fa-user-circle"></i> Welcome, <strong><?= htmlspecialchars($username) ?></strong>!
    </div>
<h4>Edit Payment</h4>
<hr>

<form method="POST" class="row g-3">
    <div class="col-md-4">
        <select name="guest_id" class="form-control" required>
            <?php while($g=$guests->fetch_assoc()): ?>
                <option value="<?= $g['id'] ?>" <?= $payment['guest_id']==$g['id']?'selected':'' ?>><?= $g['name'] ?></option>
            <?php endwhile; ?>
        </select>
    </div>
    <div class="col-md-2">
        <input type="number" step="0.01" name="amount" class="form-control" value="<?= $payment['amount'] ?>" required>
    </div>
    <div class="col-md-3">
        <select name="method" class="form-control">
            <option <?= $payment['method']=='Cash'?'selected':'' ?>>Cash</option>
            <option <?= $payment['method']=='Bank'?'selected':'' ?>>Bank</option>
            <option <?= $payment['method']=='Mobile Money'?'selected':'' ?>>Mobile Money</option>
        </select>
    </div>
    <div class="col-md-3">
        <select name="type" class="form-control">
            <option <?= $payment['type']=='Room'?'selected':'' ?>>Room</option>
            <option <?= $payment['type']=='Laundry'?'selected':'' ?>>Laundry</option>
            <option <?= $payment['type']=='Room Service'?'selected':'' ?>>Room Service</option>
            <option <?= $payment['type']=='Restaurant'?'selected':'' ?>>Restaurant</option>
            <option <?= $payment['type']=='Bar'?'selected':'' ?>>Bar</option>
        </select>
    </div>
    <div class="col-md-4 mt-2">
        <input type="text" name="description" class="form-control" value="<?= $payment['description'] ?>" placeholder="Description">
    </div>
    <div class="col-md-4 mt-2">
        <input type="datetime-local" name="payment_date" class="form-control" value="<?= date('Y-m-d\TH:i', strtotime($payment['payment_date'])) ?>">
    </div>
    <div class="col-md-2 mt-2">
        <button type="submit" name="update_payment" class="btn btn-primary w-100">Update Payment</button>
    </div>
</form>
</div>

<?php include 'includes/footer.php'; ?>
