<?php
error_reporting(-1);

function buyers_available_server_auren($state, $phone)
{
    $curl = curl_init();
    $fields = [];
    if (isset($state) && $state != '') {
        $fields['state'] = $state;
    }
    if (isset($zipcode) && $zipcode != '') {
        $fields['zip_code'] = $zipcode;
    }
    if (isset($phone) && $phone != '') {
        $fields['phone'] = $phone;
        $fields['phone_number'] = $phone;
        $fields['phone_code'] = "1";
    }

    //echo json_encode($fields);
    if (count($fields) > 0) {
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://communication.onyxplatform.com/api/v1/external/organizations/1335/campaigns/86/sources/80/utilization',
            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 => json_encode($fields),
            CURLOPT_HTTPHEADER => array(
                'Authorization: comm__7DHLPJrx5lHOcQyPyfLcSu-zUGV7pWhhbrt5hJGglo',
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonRes = json_decode($response, true);
        //print_r($jsonRes);
        $available_workers_count = "0";
        if (isset($jsonRes['available_workers_count']) && $jsonRes['available_workers_count'] > 0) {
            $available_workers_count = $jsonRes['available_workers_count'];
        }
        if ($jsonRes["available"]) {

            $msg = $available_workers_count . " Agents are available";
        } else {
            $msg = $available_workers_count . " Agents are available";
        }
    } else {
        $msg =  "Please provide state or zipcode to check Agent Availability";
    }
    return $msg;
}



// echo ASBON();
// exit;
$auren_state        = (isset($_GET['auren_state']) && $_GET['auren_state'] != '') ? $_GET['auren_state'] : '';
$auren_zipcode      = (isset($_GET['auren_zipcode']) && $_GET['auren_zipcode'] != '') ? $_GET['auren_zipcode'] : '';
$auren_phone        = (isset($_GET['auren_phone']) && $_GET['auren_phone'] != '') ? $_GET['auren_phone'] : '';

$data = [
    "buyers_available_server_auren" => buyers_available_server_auren($auren_state, $auren_phone),
];

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