<?php
    $res = '';
    $num = '';
    if(isset($_POST['submit'])){
        if(strlen($_POST['phone_number']) != 10){
            $num = "10 charcters phone number is required.";
        }else{
            $headers = array(
                //'Accept: application/json',
                //'Content-Type: application/json',
            
                );
            $url = "https://errandscloser2.dialerhosting.com/vicidial/non_agent_api.php?source=APIDATA&user=api1&pass=ErrandsAdminAPI101&function=add_lead&list_id=101&first_name=".urlencode(trim($_POST['fname']))."&last_name=".urlencode(trim($_POST['lname']))."&address1=".urlencode(trim($_POST['address']))."&city=".urlencode(trim($_POST['city']))."&postal_code=".urlencode(trim($_POST['postCode']))."&state=".urlencode(trim($_POST['state']))."&phone_number=".urlencode(trim($_POST['phone_number']));
            
            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            //for debug only!
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            curl_close($ch);
            $resArr = explode(':', $result);
            if($resArr[0] == 'SUCCESS'){
                $res = 'Your lead was posted successfully.';
            }else{
                $res = 'There was some error posting your lead.';
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
    
    <script src="js/validator.min.js"></script>
</head>
<style>
    @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;700;900&display=swap');

*, body {
    font-family: 'Poppins', sans-serif;
    font-weight: 400;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
    -moz-osx-font-smoothing: grayscale;
}

html, body {
    height: 100%;
    background-color: #152733;
    overflow-x: hidden;
}


.form-holder {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      text-align: center;
      min-height: 100vh;
}

.form-holder .form-content {
    position: relative;
    text-align: center;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-justify-content: center;
    justify-content: center;
    -webkit-align-items: center;
    align-items: center;
    padding: 60px;
}

.form-content .form-items {
    border: 3px solid #fff;
    padding: 40px;
    display: inline-block;
    width: 100%;
    min-width: 540px;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
    text-align: left;
    -webkit-transition: all 0.4s ease;
    transition: all 0.4s ease;
}

.form-content h3 {
    color: #fff;
    text-align: left;
    font-size: 28px;
    font-weight: 600;
    margin-bottom: 5px;
}

.form-content h3.form-title {
    margin-bottom: 30px;
}

.form-content p {
    color: #fff;
    text-align: left;
    font-size: 17px;
    font-weight: 300;
    line-height: 20px;
    margin-bottom: 30px;
}


.form-content label, .was-validated .form-check-input:invalid~.form-check-label, .was-validated .form-check-input:valid~.form-check-label{
    color: #fff;
}

.form-content input[type=number], .form-content input[type=text], .form-content input[type=password], .form-content input[type=email], .form-content select {
    width: 100%;
    padding: 9px 20px;
    text-align: left;
    border: 0;
    outline: 0;
    border-radius: 6px;
    background-color: #fff;
    font-size: 15px;
    font-weight: 300;
    color: #8D8D8D;
    -webkit-transition: all 0.3s ease;
    transition: all 0.3s ease;
    margin-top: 16px;
}


.btn-primary{
    background-color: #6C757D;
    outline: none;
    border: 0px;
     box-shadow: none;
}

.btn-primary:hover, .btn-primary:focus, .btn-primary:active{
    background-color: #495056;
    outline: none !important;
    border: none !important;
     box-shadow: none;
}

.form-content textarea {
    position: static !important;
    width: 100%;
    padding: 8px 20px;
    border-radius: 6px;
    text-align: left;
    background-color: #fff;
    border: 0;
    font-size: 15px;
    font-weight: 300;
    color: #8D8D8D;
    outline: none;
    resize: none;
    height: 120px;
    -webkit-transition: none;
    transition: none;
    margin-bottom: 14px;
}

.form-content textarea:hover, .form-content textarea:focus {
    border: 0;
    background-color: #ebeff8;
    color: #8D8D8D;
}

.mv-up{
    margin-top: -9px !important;
    margin-bottom: 8px !important;
}

.invalid-feedback{
    color: #ff606e;
}
#warning-message{
    color: red;
}
.valid-feedback{
   color: #2acc80;
}
</style>

<body>

<div class="form-body">
        <div class="row">
            <div class="form-holder">
                <div class="form-content">
                    <div class="form-items">
                        <h3>Lead</h3>
                        <p><?php echo $res; ?></p>
                        <form class="requires-validation" method="post" id="lead-form">

                            <div class="col-md-12">
                               <input class="form-control" type="text" name="fname" placeholder="First Name" >
                            </div>
                            <div class="col-md-12">
                               <input class="form-control" type="text" name="lname" placeholder="Last Name" >
                            </div>

                            <div class="col-md-12">
                                <input class="form-control" type="text" name="state" placeholder="State" >
                            </div>

                           <div class="col-md-12">
                              <input class="form-control" type="text" name="city" placeholder="City" >
                           </div>

                           <div class="col-md-12">
                              <input class="form-control" type="number" name="postCode" placeholder="Postal Code" >
                           </div>

                           
                           <div class="col-md-12">
                              <input id="phone_number" class="form-control" type="number" name="phone_number" placeholder="Phone"  required>
                              <span id="warning-message"><?php echo $num; ?></span>
                            </div>

                           <div class="col-md-12" style="margin-top: 16px;">
                              <textarea class="form-control" name="address" placeholder="Address" ></textarea>
                           </div>                  

                            <div class="form-button mt-3">
                                <button name="submit" id="submit" type="submit" class="btn btn-primary">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <script type="text/javascript">
        
        var minLength = 10;
        var maxLength = 10;
        $('#phone_number').on('keydown keyup change', function(){
        var char = $(this).val();
        var charLength = $(this).val().length;
        if(charLength < minLength){
            $('#warning-message').text('Length is short, minimum '+minLength+' required.');
        }else if(charLength > maxLength){
            $('#warning-message').text('Length is not valid, maximum '+maxLength+' allowed.');
            $(this).val(char.substring(0, maxLength));
        }else{
            $('#warning-message').text('');
        }
    });
        (function () {
            'use strict'
            const forms = document.querySelectorAll('.requires-validation')
            Array.from(forms)
            .forEach(function (form) {
                form.addEventListener('submit', function (event) {
                if (!form.checkValidity()) {
                    event.preventDefault()
                    event.stopPropagation()
                }

                form.classList.add('was-validated')
                }, false)
            })
        })()

    </script>

</body>

</html>
