<?php
include "config.php"; // DB connection

// Validate inputs
if (isset($_GET['id']) && isset($_GET['action'])) {
    $id = (int) $_GET['id'];
    $action = $_GET['action'];

    // Decide status and message based on action
    if ($action === "confirm") {
        $status = "Confirmed";
        $msg = "Order confirmed successfully!";
        $type = "success"; // green alert
    } elseif ($action === "cancel") {
        $status = "Cancelled";
        $msg = "Order cancelled successfully!";
        $type = "danger"; // red alert
    } else {
        $status = "processing";
    }

    if ($status) {
        $query = "UPDATE orders SET status = ? WHERE id = ?";
        $stmt = mysqli_prepare($con, $query);
        mysqli_stmt_bind_param($stmt, "si", $status, $id);

        if (mysqli_stmt_execute($stmt)) {
            // Redirect back with message + type
            header("Location: dashboard.php?msg=" . urlencode($msg) . "&type=" . $type);
            exit();
        } else {
            echo "Error updating record: " . mysqli_error($con);
        }
    } else {
        echo "Invalid action!";
    }
} else {
    echo "Invalid request!";
}
?>