<?php
session_start();
include "admin/config/db.php";
include_once("site-models/online_orders.class.php"); 

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cart = json_decode($_POST['cart_data'], true);
    $total = floatval($_POST['total_amount']);

    if (!$cart || count($cart) === 0) {
        die("Cart is empty.");
    }

    $order = new ResOnlineOrders(
        null,
        null,
        $_SESSION['user_name'] ?? 'Guest',
        $_SESSION['phone_no'] ?? '',
        $_SESSION['address'] ?? '',
        $total,
        1 // order_status_id
    );

    $result = $order->createOnlineOrder($cart);

    echo $result; // debug: should show "Data saved successfully" or error
}
?>
