<?php
include_once("include/main.inc.php");
$admin_sql = mysqli_query($dbconn, "select * from admin_details");
$admin_res = mysqli_fetch_array($admin_sql);
$referer = $_SERVER['HTTP_REFERER'];
$ip = $_SERVER['REMOTE_ADDR'];
$mobileNumber = "";
if (isset($_POST['submit-enquiry'])) {
    $name = !empty($_POST['names']) ? $_POST['names'] : '';
    $email = !empty($_POST['emails']) ? $_POST['emails'] : '';
    $mobileNumber = (!empty($_POST['mobiles'])) ? $_POST['mobiles'] : '';
    $location = !empty($_POST['locations']) ? $_POST['locations'] : '';
    $message = !empty($_POST['messages']) ? $_POST['messages'] : '';
    if (strpos($message, '#') !== false || strpos($message, '%') !== false || strpos($message, '@') !== false || strpos($message, '&') !== false || strpos($message, '~') !== false || strpos($message, 'https') !== false || strpos($message, 'http') !== false || strpos($message, 'www') !== false || strpos($message, '?') !== false) {
        $errMsg = 'URL(s) and special characters are not allowed!';
    } else {

        if ($name != '' && $email != '' && $mobileNumber != '' && $location != '' && $message != '' && $ip != '') {

            //$qry=mysql_query($sql_ens);	
            //reCAPTCHA validation
            if (isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {

                $secret = SECRET_KEY;
                //get verify response data
                $data = array('secret' => $secret, 'response' => $_POST['g-recaptcha-response']);
                $verify = curl_init();
                curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
                curl_setopt($verify, CURLOPT_POST, true);
                curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
                curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
                $response = curl_exec($verify); //var_dump($response);
                $response = json_decode($response);

                if (!$response->success) {
                    $errMsg = 'Please click on the reCAPTCHA box.';
                }

                if ($response->success) {

                    $sql_ens = "insert into tbl_enquiry set en_name='$name', en_email='$email', en_mobile='$mobileNumber', en_country='$location', en_comment='$message', en_url='$referer', en_ip='$ip', en_date=now()";
                    $qry = mysqli_query($dbconn, $sql_ens);

                    $subject = utf8_encode('New Leads via Whatsapp from ' . ' - ' . $admin_res['subject']);
                    
            //         $htmlContent = "
            // Customer Details:

            // Url: " . $referer . "

            // Name: " . $name . "

            // Email: " . $email . "

            // Mobile: " . $mobileNumber . "

            // Location:" . $location . "

            // Message: " . $message . "
            // ";
  $htmlContent =  '<table border="0" width="100%">
        <tbody>
  <tr>
    <td colspan="2">
      <strong>Dear Admin</strong></td>
  </tr>
  <tr>
    <td colspan="2">
      Enquiry Received with following info :</td>
  </tr>
  <tr>
    <td colspan="2">
      &nbsp;</td>
  </tr>
  <tr>
    <td width="26%">
      <strong>Post URL : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $referer . '</span></td>
  </tr>
   <tr>
    <td width="26%">
      <strong>Name : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $name . '</span></td>
  </tr>

  <tr>
    <td width="26%">
      <strong>Email Id : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $email . '</span></td>
  </tr>

  <tr>
    <td width="26%">
      <strong>Contact No : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $mobileNumber . '</span></td>
  </tr>

  <tr>
    <td>
      <strong>Location : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $location . '</span></td>
  </tr>
  <tr>
    <td>
      <strong>Message : </strong></td>
    <td>
      <span style="margin-top: 15px;">' . $message . '</span></td>
  </tr>

  

</tbody>
       
        </table>';
                       $to = $admin_res['email'];
            $to2 = $admin_res['cc'];
            $to3 = $admin_res['bcc'];  
        // $to3 = 'vicky.wpsranchi@gmail.com';


                    // $headers = 'From: ' . 'Webpulseindia.com' . ' enquiry@webpulseindia.com' . "\r\n" .
                    //     'Reply-To:  ' . $name . '<' . $email . '>' . "\r\n" .
                    //     'X-Mailer: PHP/' . phpversion();
                    // @mail($to, $subject, $htmlContent, $headers);
                    // @mail($to2, $subject, $htmlContent, $headers);
                    // @mail($to3, $subject, $htmlContent, $headers);
                      if($to!='')
         {
         smtp_mailer($to, $email, $name, $subject, $htmlContent);
         }
         if($to2!='')
         {
         smtp_mailer($to2, $email, $name, $subject, $htmlContent);
         }
   
         smtp_mailer($to3, $email, $name, $subject, $htmlContent);
                    $succMsg = 'Thank you for your enquiry. Our team will contact you soon.';
                }
            } else {
                $errMsg = 'Robot verification failed, please try again.';
            }
        } else {
            $errMsg = 'All the fields are Mandatory!';
        }
    }
}
?>
<?php if (isset($_POST['submit-enquiry'])) { ?>
    <script type="text/javascript">
        setTimeout(function() {
            $('#myModal').modal('show');
        }, 300);
    </script>
<?php } ?>


<div class="modal fade" id="whatsapp" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Message on WhatsApp</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form method="post" id="contact_id" >
                    <div id="whatsapp_response"></div>
                    <div class="row">
                        <div class="col-xxl-12 col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="form-group">
                                <input class="form-control" type="text" name="wmname" id="wmname" placeholder="Full Name*" value="" onkeypress="return ValidateAlpha(event)" required>
                            </div>
                        </div>
                        <div class="col-xxl-12 col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <div class="form-group">
                                <input class="form-control" type="tel" name="wmobile" id="wmobile" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" maxlength="10" value="" placeholder="Mobile Number*" required>
                            </div>
                        </div>
                        <div class="col-xxl-12 col-xl-12 col-lg-12 col-md-12 col-sm-12 col-12">
                            <button type="submit" name="whatsEnq" id="whatsEnq" class="read_more_1 bg_btn_1">Submit</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title">Quick Enquiry</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <form id="quick_enquiry" method="post">
                    
                    <?php if (!empty($errMsg)) : ?><div class="alert alert-danger"><?php echo $errMsg; ?></div>
                    <?php endif; ?>
                    <?php if (!empty($succMsg)) : ?><div class="alert alert-success"><?php echo $succMsg; ?></div>
                    <?php endif; ?>
                    <div class="form-group">
                        <input class="form-control" type="text" name="names" onkeypress="return ValidateAlpha(event)" placeholder="Full Name*" value="" required>
                    </div>

                    <div class="form-group">
                        <input class="form-control" type="text" name="emails" value="" placeholder="Email ID*" required>
                    </div>

                    <div class="form-group">
                        <input class="form-control" type="text" name="mobiles" value="" placeholder="Mobile Number*" onkeyup="if (/\D/g.test(this.value)) this.value = this.value.replace(/\D/g,'')" maxlength="10" required>
                    </div>

                    <div class="form-group">
                        <input class="form-control" type="text" name="locations" onkeypress="return ValidateAlpha(event)" value="" placeholder="Your Location*" required>
                    </div>

                    <div class="form-group">
                        <textarea class="form-control" name="messages" placeholder="Your Message*" required></textarea>
                    </div>

                    <div class="form-group" style="margin-bottom: 0;">
                        <div class="g-recaptcha" data-sitekey="<?= SITE_KEY; ?>"></div>
                    </div>

                    <button type="submit" name="submit-enquiry" class="read_more_1 bg_btn_1">Submit</button>
                </form>
            </div>
        </div>
    </div>
</div>


<!-- Product Popup -->
<div class="modal fade" id="productID" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title">Send Enquiry</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xxl-7 col-xl-7 col-lg-7 col-md-7 col-sm-7 col-12">
                        <div class="product_popup">
                            <div class="product_popup_image">
                                <img class="img-responsive catImg" src="<?=site_url;?>/designer/images/no-image.jpg" alt="No image" title="No image">
                            </div>
                        </div>
                    </div>
                    <div class="col-xxl-5 col-xl-5 col-lg-5 col-md-5 col-sm-5 col-12">

                        <form name="contact-form" method="post" id="product_pop_form">
                        <div id="mail-status1"></div>
                            <div class="form-group">
                                <input class="form-control" type="text" name="catuser_name" id="catuser_name" onkeypress="return ValidateAlpha(event)" placeholder="Full Name*" value="" pattern="[a-z A-Z]+" required>
                            </div>

                            <div class="form-group">
                                <input class="form-control" type="email" name="catemail_address" id="catemail_address" placeholder=" Email ID*" value="" required>
                            </div>

                            <div class="form-group">
                                <input class="form-control" type="tel" name="catphone_no" id="catphone_no" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" maxlength="10" placeholder=" Phone No*" value="" required>
                            </div>

                            <div class="form-group">
                                <input class="form-control" type="text" name="catlocation" id="catlocation" onkeypress="return ValidateAlpha(event)" placeholder="Location*" value="" pattern="[a-z A-Z]+" autocomplete="off" required>
                            </div>
                            <div class="form-group">
                                <textarea class="form-control" name="caten_message" id="caten_message" placeholder=" Message*" required></textarea>
                            </div>

                            <div class="form-group" style="margin-bottom: 0;">
                                <div class="g-recaptcha" data-sitekey="<?= SITE_KEY; ?>"></div>
                            </div>

                            <div class="form-group">
                                <button type="submit" name="submit" id="cat_submit" class="read_more_1 bg_btn_1">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="callback" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title">Get Callback Quickly</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xxl-5 col-xl-5 col-lg-5 col-md-5 col-sm-5 col-12">
                        <div class="product_popup">
                            <div class="product_popup_image">
                                <img src="designer/images/image4.jpg" alt="Product" title="Product">
                            </div>
                            <h6>Category Heading One</h6>
                        </div>
                    </div>
                    <div class="col-xxl-7 col-xl-7 col-lg-7 col-md-7 col-sm-7 col-12">

                        <form name="contact-form" method="post" id="callback_form">
                            
                            <div class="form-group">
                                <input class="form-control" type="text" name="name" placeholder="Full Name*" onkeypress="return ValidateAlpha(event)" value="" pattern="[a-z A-Z]+" required>
                            </div>

                            <div class="form-group">
                                <input class="form-control" type="tel" name="mobile" placeholder=" Phone No*" maxlength="10" value="" required>
                            </div>
                            <p>We will contact you on this number</p>

                            <div class="form-group">
                                <button type="submit" class="read_more_1 bg_btn_1">Submit</button>
                            </div>
                        </form>

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#cat_submit').click(function(e) {
            e.preventDefault();
            if ($("#catuser_name").val() != '' && $("#catemail_address").val() != '' && $("#catphone_no")
                .val() != '' && $("#caten_message").val() != '' && $("#catlocation").val() != '') {
                jQuery.ajax({
                    url: "<?= site_url; ?>/ajaxEnquirycat.php",
                    data: $('#product_pop_form').serialize(),
                    type: "POST",
                    success: function(data) {
                        var data = data.trim();
                        if (data == 'success') {
                            $("#mail-status1").html(
                                '<div class="alert alert-success alert-dismissible show">Your Enquiry has been sent successfully.  Our Team will contact you shortly!<button type="button" class="close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>'
                            );
                            $('#product_pop_form').trigger('reset');

                        } else {
                            $("#mail-status1").html(data).css('color', 'red');
                        }
                    },
                    error: function() {}
                });
            } else {
                $("#mail-status1").html(
                    '<div class="alert alert-danger alert-dismissible show"><strong>Asterisk(*) Marked fields are Mandatory!<button type="button" class="close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>'
                );
            }

        })

    })
</script>
<script>
$(document).ready(function(){
  $('#whatsEnq').click(function(e){
      
    
         e.preventDefault();
     var wname = $('#wmname').val();
     var wmobile = $('#wmobile').val();
     $.ajax({
          url: "<?=site_url;?>/whatsapp_enq.php",
          data:$('#contact_id').serialize(),
         type : "POST",
         success : function(data){
               
             if(data.trim() == 'success')
                 {
                    
                 $('#whatsapp_response').html('<div class="alert alert-success">Whatsapp Enquiry Submitted successfully</div>');
                    $('#whatsappEnq').trigger('reset');
                     
                     window.open("https://api.whatsapp.com/send?phone=917490023232&text=Hello, I visited your website and am interested in placing an inquiry. Looking forward to a prompt response.&source=&data=&app_absent=","_blank");
                 }
             else{
                
                 $('#whatsapp_response').html(data);
             }
         }
        
     })
          
      })
  })  
</script>

<script>
    $(document).ready(function() {
        $('#whatsEnq').click(function(e) {

            e.preventDefault();
            var wmobile = $('#wmobile').val();
            $.ajax({
                url: "<?= site_url; ?>/number_enq.php",
                data: $('#contact_id').serialize(),
                type: "POST",
                success: function(data) {
                    if (data == 'success') {
                        $('#whatsapp_response').html(
                            '<div class="alert alert-success">Whatsapp Enquiry Submitted successfully<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>'
                        );
                        $('#contact_id').trigger('reset');
                    } else {
                        $("#whatsapp_response").html(data).css('color', 'red');
                    }
                }

            })

        })
    })
</script>