<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>Display Name</h2><br><br>
    <?php
    if ($_POST) {
        //header('Content-Type: application/json; charset=utf-8');
        $POSTValues = $_POST;
        $POSTValues['lp_campaign_id'] = "68b08ae99dedf";
        $POSTValues['lp_campaign_key'] = "TwGzfqpPF9RQhYCk4LKg";
        $POSTValues['lp_response'] = "JSON";
        $POSTValues['caller_id'] = "1" . $POSTValues['phone_number'];


        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://track.edmleadnetwork.com/call-preping.do?' . http_build_query($POSTValues),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            //CURLOPT_POSTFIELDS => ($POSTValues),
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        $response = curl_exec($curl);
        $error = curl_error($curl);
        $errno = curl_errno($curl);
        curl_close($curl);

        if (0 !== $errno) {
            throw new RuntimeException($error, $errno);
        }

        $result = json_decode($response);
        //    echo "<pre><hr>Results: "; print_r( $result );
        //    print_r($POSTValues);
        //    echo "</pre>";

        if ($result->success) {
            echo '<div class="alert alert-success" role="alert">Your form request is Accepted...
            <br>Ping ID: ' . $result->ping_id . '
            <br>Payout: ' . $result->payout . '
            <br>Duration: ' . $result->duration . '
            <br>Number: ' . $result->number . '
            <br>Status: ' . $result->status . '
            <br>Message: ' . $result->message . '
            </div>';
        } else {
            echo '<div class="alert alert-danger" role="alert">Your form request is NOT Accepted... 
            <br>Ping ID: ' . $result->ping_id . '
            <br>Status: ' . $result->status . '
            <br>Message: ' . $result->message . '
            </div>';
        }
    }
    ?>
    <form action="" method="post">
        <div class="row">
            <?php
            $inputs = array();
            $inputs[] = array('name' => 'first_name', 'required' => true);
            $inputs[] = array('name' => 'last_name', 'required' => true);
            $inputs[] = array('name' => 'phone_number', 'required' => true);
            $inputs[] = array('name' => 'email_address', 'required' => true);
            $inputs[] = array('name' => 'address', 'required' => true);
            $inputs[] = array('name' => 'city', 'required' => true);
            $inputs[] = array('name' => 'state', 'required' => true);
            $inputs[] = array('name' => 'zip_code', 'required' => true);

            foreach ($inputs as $input) {
                $title = ucwords(str_replace("_", " ", $input['name']));
                $type = 'text';
                if (isset($input['type']) && $input['type'] == 'dropdown')
                    $type = 'dropdown';

                if (isset($input['type']) && $input['type'] == 'dropdown') {
            ?>
                    <div class="form-group col-md-6">
                        <label for="exampleInput<?= ucwords($input['name']) ?>"><?= $title ?> <?= ((isset($input['required'])) ? '<span style="color: red;">*</span>' : '') ?></label>
                        <select class="form-control" id="<?= ucwords($input['name']) ?>"
                            name="<?= ($input['name']) ?>" <?= ((isset($input['required'])) ? 'required' : '') ?>>
                            <?php
                            foreach ($input['options'] as $option) {
                                echo '<option value="' . ($option) . '">' . ucwords($option) . '</option>';
                            } ?>
                        </select>
                    </div>
                <?php
                } else { ?>
                    <div class="form-group col-md-6">
                        <label for="exampleInput<?= ucwords($input['name']) ?>">
                            <?= $title ?> <?= ((isset($input['required'])) ? '<span style="color: red;">*</span>' : '') ?>
                        </label>
                        <input type="<?= $type ?>"
                            class="form-control <?= (($input['name'] == 'phone_1') ? ' capture_number ' : '') ?>"
                            id="<?= ucwords($input['name']) ?>"
                            name="<?= ($input['name']) ?>"
                            <?= ((isset($input['required'])) ? 'required' : '') ?>
                            value="<?= ((isset($_GET[$input['name']])) ? $_GET[$input['name']] : '') ?>"
                            aria-describedby="<?= $title ?> Help"
                            placeholder="Enter <?= $title ?>">
                    </div> <?php
                        }
                    }
                            ?>
            <div class="col-md-12 text-right">
                <button type="submit" class="btn btn-primary mb-2">Confirm Submit</button>
            </div>
        </div>
    </form>
</div>