<?php

include '../config/connect.php';

// Define your API key
$valid_api_key = '12345abcde67890fghijklmnopqrstuv';

// Check if the API key is present and valid
if (isset($_GET['api_key']) && $_GET['api_key'] === $valid_api_key) {
    
    $id = '3'; // You can replace this with $_GET['id'] to get the ID from the query string

    $stmt = $pdo->prepare("SELECT saldo AS nominal, bulan AS waktu FROM v_jumlah_transaksi_bulanan WHERE id_data_server = :id ");
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->execute();

    // Initialize an array to store results
    $dataArray = [];

    // Fetch all rows of data
    while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $dataArray[] = $data; // Append each row to the array
    }

    // Check if any data was found
    if (count($dataArray) > 0) {
        // Return the data as JSON
        echo json_encode($dataArray);
    } else {
        // If no data found, return an error message
        echo json_encode(['error' => 'Data not found']);
    }
    
} else {
    // If API key is missing or invalid
    echo json_encode(['error' => 'Invalid or missing API key']);
}

?>
