<?php
session_start();
if (!isset($_SESSION['username'])) {
    echo "<script>window.top.location.href='index.php';</script>"; // Redirect parent window to login page
    exit();
}

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "prodb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Fetch product data
$sql = "SELECT pr_Name, qty_Onhand FROM products_table";
$result = $conn->query($sql);

$pr_Name = [];
$qty_Onhand = [];

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $pr_Name[] = $row['pr_Name'];
        $qty_Onhand[] = $row['qty_Onhand'];
    }
} else {
    echo "No data found";
    exit;
}

$conn->close();

// Convert PHP arrays to JavaScript arrays
$product_names_json = json_encode($pr_Name);
$product_quantities_json = json_encode($qty_Onhand);
?>
