<?php
$pmcpa_phone = (isset($_GET['pmcpa_phone']) && $_GET['pmcpa_phone'] != '') ? $_GET['pmcpa_phone'] : '';

// Define query string parameters as variables
$token  = 'fe3bd34a-1b71-44b9-8d15-9b12d59b70b8';
$accid  = 'gma';
$action = 'AvailableInboundAgents';
$did    = '9542845268';

// Build the query string
$queryParams = http_build_query([
    'token'  => $token,
    'accid'  => $accid,
    'Action' => $action,
    'DID'    => $did,
    'Callid' => $pmcpa_phone
]);

// Base API URL
$baseUrl = 'https://api.chasedatacorp.com/HttpImport/CallOperations.php';

// Full URL with query string
$url = $baseUrl . '?' . $queryParams;

// Initialize cURL
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);            // Set full URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response as string
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disable SSL verification (only for testing)

// Execute request
$response = curl_exec($ch);

// Check for errors
if (curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
} else {
    // echo "API Response:\n";
    // echo $response;
}

// Close cURL
curl_close($ch);
$result = json_decode($response, true);
$data = [
    "msg" => $result['AvailableInboundAgents'] . " Agents Available",
];

header('Content-Type: application/json');
echo json_encode($data);
