<?php
// Database connection
require_once 'shikanisha.kts.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['trans_id'])) {
    $transId = $_POST['trans_id'];

    // Build the SQL query to fetch sales items for a specific transaction
    $sql = "SELECT product_name, quantity, selling_price, subtotal
            FROM sales
            WHERE trans_id = $transId";

    // Execute the query and fetch the results
    $result = $conn->query($sql);
    $data = [];

    if ($result) {
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
    }

    // Return the data as JSON
    header('Content-Type: application/json');
    echo json_encode($data);
} else {
    // Return an empty array if no data is found or request is invalid
    echo json_encode([]);
}
?>
