<?php

require 'db_config.php'; 

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $scanned_id = trim($_POST['barcode']);
    $response = '';

    // Fetch user data
    $stmt = $conn->prepare("SELECT fullname, meals_allocated, meals_remaining FROM users WHERE id = ?");
    $stmt->bind_param("i", $scanned_id);
    $stmt->execute();
    $stmt->bind_result($fullname, $allocated, $remaining);
    if ($stmt->fetch()) {
        if ($remaining > 0) {
            // Deduct meal
            $remaining -= 1;
            $update = $conn->prepare("UPDATE users SET meals_remaining = ? WHERE id = ?");
            $update->bind_param("ii", $remaining, $scanned_id);
            $update->execute();
            $response = "✅ Meal deducted successfully!";
        } else {
            $response = "❌ No meals remaining.";
        }
    } else {
        $response = "❌ User not found.";
    }
    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Meal Deduction</title>
    <style>
        body {
            font-family: Arial;
            padding: 20px;
        }
        input[type="text"] {
            font-size: 20px;
            padding: 8px;
            width: 100%;
        }
        .response {
            margin-top: 15px;
            font-weight: bold;
        }
        table {
            margin-top: 20px;
            width: 100%;
            border-collapse: collapse;
        }
        th, td {
            padding: 12px;
            border: 1px solid #ccc;
            text-align: left;
        }
        .scan-btns button {
            padding: 10px 15px;
            margin: 5px;
        }
        @media (max-width: 600px) {
            input[type="text"] {
                font-size: 18px;
            }
            table th, table td {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>

<h2>Meal Deduction Scan</h2>

<!-- Scan Mode Switch -->
<div class="scan-btns">
    <form action="attendance_scanner.php" method="get" style="display:inline;">
        <button type="submit">Attendance</button>
    </form>
    <form action="meal_deductions.php" method="get" style="display:inline;">
        <button type="submit">Meals</button>
    </form>
    <form action="promotional_collections.php" method="get" style="display:inline;">
        <button type="submit">Promo</button>
    </form>
</div>

<!-- Scan Input Form -->
<form method="POST" autocomplete="off">
    <label>Scan User ID:</label>
    <input type="text" name="barcode" id="barcode" autofocus onblur="this.focus()">
</form>

<?php if ($_SERVER["REQUEST_METHOD"] === "POST"): ?>
    <div class="response"><?= $response ?></div>

    <?php if (isset($fullname)): ?>
        <table>
            <tr>
                <th>Full Name</th>
                <th>Total Meals Allocated</th>
                <th>Meals Remaining</th>
                <th>Status</th>
            </tr>
            <tr>
                <td><?= htmlspecialchars($fullname) ?></td>
                <td><?= $allocated ?></td>
                <td><?= $remaining ?></td>
                <td><?= $response ?></td>
            </tr>
        </table>
    <?php endif; ?>
<?php endif; ?>

<script>
// Keep input focused for PC scanner
document.getElementById("barcode").focus();
</script>

</body>
</html>
