<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 Ping</h2><br><br>
    <form action="" method="post">
        <div class="row">
            <div class="form-group col-md-6">
                <label for="Phone_number">Phone Number</label>
                <input type="number" class="form-control" id="phone_number" name="phone_number" required="" value="<?= ((isset($_GET['phone'])) ? $_GET['phone'] : '') ?>"
                    aria-describedby="Phone_numberHelp" placeholder="Enter Phone_number">
            </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);

        // ------------------------------------------------------------------------------------
        $phone = $_POST['phone_number'];
        if ($phone != '' && strlen($phone) == 10) {



            $curl = curl_init();
            curl_setopt_array($curl, array(
                CURLOPT_URL => 'https://api.convoso.com/v1/dnc/insert?auth_token=cckxa5p7u54zbnxp7wvvoovep7qtm1ap&phone_number=' . $phone . '&phone_code=1&campaign_id=',
                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 ($status == 'success' && !isset($jsonRes['text'])) {
                $response_status = 'Ping Accepted';
                echo '
                <div class="alert alert-success" role="alert">
                    Number (' . $phone . ') Accepted
                </div>';
            } else {
                $response_status = $jsonRes['text'];
                echo '
                <div class="alert alert-danger" role="alert">
                    Number (' . $phone . '): ' . $jsonRes['text'] . '
                </div>';
            }

            $SQL = "
            INSERT INTO `asb_ping` (`phone`, `status`, `response`) 
            VALUES ('" . $phone . "', '" . $response_status . "', '" . $response . "');";
        } else {
            echo '<div class="alert alert-danger" role="alert">Invalid Number (' . $phone . ') </div>';
            $SQL = "
            INSERT INTO `asb_ping` (`phone`, `status`, `response`) 
            VALUES ('" . $phone . "', 'Invalid Phone', '');";
        }
        $db->query($SQL);
    }
    ?>
</div>