<link rel="stylesheet" href="https://getbootstrap.com/docs/4.3/dist/css/bootstrap.min.css">
<div style="width: 60%; margin: 0 auto; margin-top: 100px;">
    <h2>ASB State Ping</h2><br><br>
    <form action="" method="post">
        <div class="row">
            <div class="form-group col-md-6">
                <?php
                $us_states = [
                    'AL' => 'Alabama',
                    'AK' => 'Alaska',
                    'AZ' => 'Arizona',
                    'AR' => 'Arkansas',
                    'CA' => 'California',
                    'CO' => 'Colorado',
                    'CT' => 'Connecticut',
                    'DE' => 'Delaware',
                    'FL' => 'Florida',
                    'GA' => 'Georgia',
                    'HI' => 'Hawaii',
                    'ID' => 'Idaho',
                    'IL' => 'Illinois',
                    'IN' => 'Indiana',
                    'IA' => 'Iowa',
                    'KS' => 'Kansas',
                    'KY' => 'Kentucky',
                    'LA' => 'Louisiana',
                    'ME' => 'Maine',
                    'MD' => 'Maryland',
                    'MA' => 'Massachusetts',
                    'MI' => 'Michigan',
                    'MN' => 'Minnesota',
                    'MS' => 'Mississippi',
                    'MO' => 'Missouri',
                    'MT' => 'Montana',
                    'NE' => 'Nebraska',
                    'NV' => 'Nevada',
                    'NH' => 'New Hampshire',
                    'NJ' => 'New Jersey',
                    'NM' => 'New Mexico',
                    'NY' => 'New York',
                    'NC' => 'North Carolina',
                    'ND' => 'North Dakota',
                    'OH' => 'Ohio',
                    'OK' => 'Oklahoma',
                    'OR' => 'Oregon',
                    'PA' => 'Pennsylvania',
                    'RI' => 'Rhode Island',
                    'SC' => 'South Carolina',
                    'SD' => 'South Dakota',
                    'TN' => 'Tennessee',
                    'TX' => 'Texas',
                    'UT' => 'Utah',
                    'VT' => 'Vermont',
                    'VA' => 'Virginia',
                    'WA' => 'Washington',
                    'WV' => 'West Virginia',
                    'WI' => 'Wisconsin',
                    'WY' => 'Wyoming'
                ];

                ?>
                <label for="Phone_number">States</label>
                <select name="state" id="state" class="form-control">
                    <option value="">Select State</option>
                    <?php foreach ($us_states as $abbr => $name): ?>
                        <option value="<?= $abbr ?>" <?= (isset($_GET['state']) && $_GET['state'] == $abbr) ? 'selected' : '' ?>><?= $name ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-6 text-left" style="margin-top: 30px;">
                <button type="submit" class="btn btn-primary">Confirm Submit</button>
            </div>
        </div>
    </form>
    <?php
    if ($_POST) {
        include "MySqliDb.php";
        if ($_SERVER['HTTP_HOST'] == 'mcc-forms.dev') {
            // DB credentials.
            define('DB_HOST', '127.0.0.1');
            define('DB_USER', 'root');
            define('DB_PASS', 'ServBay.dev');
            define('DB_NAME', 'masterco_forms_data');
        } else {
            // DB credentials.
            define('DB_HOST', 'localhost');
            define('DB_USER', 'masterco_forms_data');
            define('DB_PASS', 'masterco_forms_data');
            define('DB_NAME', 'masterco_forms_data');
        }
        //$db = new db(DB_HOST, DB_USER, DB_PASS, DB_NAME);

        // ------------------------------------------------------------------------------------

        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.convoso.com/v1/user-activity/search?auth_token=h1e7b21sdieg7ll7nik3lo2eydeup9jx&campaign_id=&queue_id=9313&user_id=&filter_by_skill_options=' . $_POST['state'] . '&exclude_remote_users=Y',
            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_HTTPHEADER => array(
                'loginId: 010E969C27A85E8FC3F5F107B1487433C66983BB61E0'
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        $jsonRes = json_decode($response, true);
        // print_r($jsonRes);
        $status = $jsonRes['success'];
        if ($jsonRes['success'] == 1) {
            echo '
                <div class="alert alert-success" role="alert">
                    State: ' . $us_states[$_POST['state']] . ' >> <strong>' . $jsonRes['data']['available_agents'] . '</strong> Agents Available out of <strong>' . $jsonRes['data']['logged_in_agents'] . '</strong> Logged Agents.
                </div>';
        } else {
            echo '
                <div class="alert alert-danger" role="alert">
                    <code>' . $response . '</code>
                </div>';
        }
    }
    ?>
</div>