<title>ACA RTB - Data Records</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
<?php

$username = "svc.api_user_gstate@startencore.com";
$password = "#Encore2715!";


function CreateSession($username, $password)
{
    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.five9.com/wssupervisor/SupervisorWebService',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_POSTFIELDS => '
        <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.supervisor.ws.five9.com/">
        <soapenv:Header/>
        <soapenv:Body>
            <ser:setSessionParameters>
                <viewSettings>
                    <forceLogoutSession>true</forceLogoutSession>
                    <statisticsRange>CurrentDay</statisticsRange>
                    <rollingPeriod>Minutes5</rollingPeriod>
                </viewSettings>
            </ser:setSessionParameters>
        </soapenv:Body>
        </soapenv:Envelope>',
        CURLOPT_HTTPHEADER => array(
            'Content-Type: text/xml'
        ),
        CURLOPT_USERPWD => $username . ":" . $password,
    ));

    $xmlString = curl_exec($curl);
    if (curl_errno($curl)) {
        echo "cURL Error: " . curl_error($curl);
    }

    curl_close($curl);

    // Parse XML
    $xml = simplexml_load_string($xmlString);

    $xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xml->registerXPathNamespace('ns1', 'http://service.supervisor.ws.five9.com/');
    $fault = $xml->xpath('//ns1:OperationsLimitExceededFault');
    curl_close($curl);
    if (!empty($fault)) { ?>
    <h3>Waiting for New Session</h3>
        <script>
            setTimeout(function() {
                location.reload();
            }, 1000 * 60 * 15); /// 15 mints
        </script>
        <?php
        exit;
    }
    
}

$states = [
    "Alabama" => "AL",
    "Alaska" => "AK",
    "Arizona" => "AZ",
    "Arkansas" => "AR",
    "California" => "CA",
    "Colorado" => "CO",
    "Connecticut" => "CT",
    "Delaware" => "DE",
    "Florida" => "FL",
    "Georgia" => "GA",
    "Hawaii" => "HI",
    "Idaho" => "ID",
    "Illinois" => "IL",
    "Indiana" => "IN",
    "Iowa" => "IA",
    "Kansas" => "KS",
    "Kentucky" => "KY",
    "Louisiana" => "LA",
    "Maine" => "ME",
    "Maryland" => "MD",
    "Massachusetts" => "MA",
    "Michigan" => "MI",
    "Minnesota" => "MN",
    "Mississippi" => "MS",
    "Missouri" => "MO",
    "Montana" => "MT",
    "Nebraska" => "NE",
    "Nevada" => "NV",
    "New Hampshire" => "NH",
    "New Jersey" => "NJ",
    "New Mexico" => "NM",
    "New York" => "NY",
    "North Carolina" => "NC",
    "North Dakota" => "ND",
    "Ohio" => "OH",
    "Oklahoma" => "OK",
    "Oregon" => "OR",
    "Pennsylvania" => "PA",
    "Rhode Island" => "RI",
    "South Carolina" => "SC",
    "South Dakota" => "SD",
    "Tennessee" => "TN",
    "Texas" => "TX",
    "Utah" => "UT",
    "Vermont" => "VT",
    "Virginia" => "VA",
    "Washington" => "WA",
    "West Virginia" => "WV",
    "Wisconsin" => "WI",
    "Wyoming" => "WY"
];


///////////////////////////////////////////

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.five9.com/wssupervisor/SupervisorWebService',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.supervisor.ws.five9.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getStatistics>
         <statisticType>ACDStatus</statisticType>
      </ser:getStatistics>
   </soapenv:Body>
</soapenv:Envelope>',
    CURLOPT_HTTPHEADER => array(
        'Content-Type: text/xml'
    ),
    CURLOPT_USERPWD => $username . ":" . $password,
));

$xmlString = curl_exec($curl);
// echo "<pre>";
// echo $xmlString;
// exit;
if (curl_errno($curl)) {
    echo "cURL Error: " . curl_error($curl);
}

curl_close($curl);

// Parse XML
$xml = simplexml_load_string($xmlString);

$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns1', 'http://service.supervisor.ws.five9.com/');
$fault = $xml->xpath('//ns1:OperationsLimitExceededFault');
if (!empty($fault)) {
    CreateSession($username, $password);
}

// Register namespaces
$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('ns2', 'http://service.supervisor.ws.five9.com/');

// Extract headers
$headers = $xml->xpath('//ns2:getStatisticsResponse/return/columns/values/data');
// print_r($headers);
// Extract rows
$rows = $xml->xpath('//ns2:getStatisticsResponse/return/rows/values');
echo '<div style="width: 90%; margin: 50px auto 100px;">';
// Build table
echo "<h5>ACD Status: Agents Ready For Calls</h5>";
// print_r($rows);
$StatesData = [];
foreach ($rows as $row) {
    $StateData = json_decode(json_encode($row->data), true);
    // echo $StateData[0]. " > " . $StateData[5]."<br>";
    if (array_key_exists($StateData[0], $states)) {
        $StatesData[$states[$StateData[0]]] = $StateData[5];
    }
}
$totalAgents = 0;
foreach ($StatesData as $state => $count) {
    echo "<span class='badge badge-" . (($count > 0) ? 'success' : 'danger') . " mr-2'><strong>$state:</strong> $count</span>";
    $totalAgents += $count;
}
if($totalAgents == 0){
    CreateSession($username, $password);
}
echo "<span class='badge badge-success mr-2'><strong>Total Agents:</strong> $totalAgents</span>";

echo '</div>';
?>
<script>
    setTimeout(function() {
        location.reload();
    }, 21000); // Refresh every 21 seconds
</script>