<?php
// Include DB connection
include './db.php';

// Start session
session_start();

// Check if the user is logged in
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'User not logged in']);
    exit();
}

$user_id = $_SESSION['user_id']; // Get user ID

// Query to get total price of the cart
$stmt = $conn->prepare("SELECT SUM(price) FROM cart WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$stmt->bind_result($total_price);
$stmt->fetch();
$stmt->close();

// Format total price
$formatted_total = number_format($total_price, 0, ',', '.');

// Return total price in JSON format
echo json_encode(['success' => true, 'total' => $formatted_total]);

$conn->close();
?>
