<?php
// PHPMailer
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

include ("inc/connection.php");

?>
<!DOCTYPE html>
<html lang="en">

<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">

  <title>Contact Us - Namdar Realty, Inc.</title>

  <!-- Bootstrap core CSS -->
  <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

  <!-- Custom fonts for this template -->
  <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
  <link href='https://fonts.googleapis.com/css?family=Kaushan+Script' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,100,300,700' rel='stylesheet' type='text/css'>

  <!-- Custom styles for this template -->
  <link href="css/agency.css" rel="stylesheet">

<!-- Recaptcha -->
  <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  <script>
   function enableBtn(){
     document.getElementById("sendMessageButton").disabled = false;
   }
  </script>
<?php
    $siteKey = '6Ldnae0UAAAAACNvC_2foO6RDnackiivFw5IwEMn';
    $secret = '6Ldnae0UAAAAAGgajY8BiaHfHNY1jPeEgaJM0Dy8';
?>

</head>

<body id="page-top">

<?php include("inc/header.php"); ?>

  <!-- Contact -->
  <section id="contact">
    <div class="container">
<?php

$varValid = "";
$errMsg = "";
$succMsg = "";

if (isset($_POST["sendMessageButton"])){

  // recapthca
  if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
  {
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success)
        {
            $succMsg = 'Your contact request have submitted successfully.';
        }
        else
        {
            $errMsg = '<p style="padding:10px 0; font-weight:bold; color:red;">Please click on the reCAPTCHA box.</p>';
        }
   }

  if ($succMsg) {
    $varDate = date("F j, Y");
    $subj = "Web Contact Form from " . $_POST["email"];
    $emess = "<table style=\"width:700px;\">";
    $emess.= "<tr><td width=\"200\">Date : </td><td width=\"500\">" . $varDate . "</td></tr>";
    $emess.= "<tr><td>Name: </td><td>" . $_POST["name"] . "</td></tr>";
    $emess.= "<tr><td>Company Name: </td><td>" . $_POST["company"] . "</td></tr>";
    $emess.= "<tr><td>Phone: </td><td>" . $_POST["phone"] . "</td></tr>";
    $emess.= "<tr><td>e-mail: </td><td>" . $_POST["email"] . "</td></tr>";
    $emess.= "<tr><td>Message: </td><td>" . $_POST["message"] . "</td></tr>";
    $emess.= "</table>";
    
    //Load Composer's autoloader
    require 'inc/PHPMailer/vendor/autoload.php';


    // email confirmation
    $SendToEmail = "anamdar@namdarrealty.com";
    // $SendToEmail = "fnamdar@nexium.com";
    $SenderEmail = "webmail@namdarrealty.com";
    
    $mail = new PHPMailer(true);                              // Passing `true` enables exceptions
    try {
      //Enable SMTP debugging
      // 0 = off (for production use)
      // 1 = client messages
      // 2 = client and server messages
      $mail->SMTPDebug = 0;             // Enable verbose debug output
      
      $mail->isSMTP();                  // Set mailer to use SMTP
      $mail->SMTPAuth = true;             // Enable SMTP authentication
      $mail->Host = 'smtp.ionos.com';         // Specify main and backup SMTP servers
      $mail->Username = 'webmail@namdarrealty.com';   // SMTP username
      $mail->Password = '#NR26web';       // SMTP password
      $mail->SMTPSecure = 'tls';            // Enable TLS encryption, `ssl` also accepted
      $mail->Port = 587;                // TCP port to connect to
    
      $mail->setFrom($SenderEmail, 'NamdarRealty.com Contact');
      $myemail = explode(",",$SendToEmail);
      foreach($myemail as $address){
        $mail->addAddress($address, 'Namdar Realty Webmail');     // Add a recipient
      }
      
      $mail->isHTML(true);                                  // Set email format to HTML
      
      $mail->Subject = $subj;
      $mail->Body    = $emess;

      $mail->send();
    } catch (Exception $e) {
      $varValid = false;
    }


    $sqlText = "INSERT INTO WebContact(Date, Name, Company, Phone, Email, Message) VALUES ('". date("Y-m-d H:i:s") ."', '". mysqli_real_escape_string($connect,$_POST["name"]) . "', '". mysqli_real_escape_string($connect,$_POST["company"]) . "', '". mysqli_real_escape_string($connect,$_POST["phone"]) . "', '". mysqli_real_escape_string($connect,$_POST["email"]) . "', '". mysqli_real_escape_string($connect,$_POST["message"]) . "')";

    if (!mysqli_query($connect,$sqlText))
      die('Error: ' . mysqli_error() . $sqlText);


    echo "<p style=\"margin:10px 0 200px 0;\">Thank you for contacting us. One of our representatives will contact you regarding your inquiry.</p>" ;
  }
} else {
?>
      <div class="row my-5">
        <div class="col-lg-12 text-center">
          <h2 class="section-heading text-uppercase">Contact Us</h2>
        </div>
      </div>
      <div class="row pb-5">
        <div class="col-lg-12">
          <form id="contactForm" name="sentMessage" method="post">
            <div class="row text-white">
              <div class="col-md-6">
                <p align="center"><strong>Namdar Realty, Inc.</strong><br>
                  7 West 45th Street, # 1604<br>
                  New York, NY 10036<br>
                  Tel. 516-338-8343</p>
                <p align="center"><a class="text-white" href="mailto:anamdar@NamdarRealty.com">anamdar@NamdarRealty.com</a></p>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                  <input class="form-control" id="name" name="name" type="text" value="<?=$_POST["name"]?>" placeholder="Your Name *" required>
                  <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                  <input class="form-control" id="company" name="company" type="text" value="<?=$_POST["company"]?>" placeholder="Your Company">
                </div>
                <div class="form-group">
                  <input class="form-control" id="email" name="email" type="email" value="<?=$_POST["email"]?>" placeholder="Your Email *" required>
                  <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                  <input class="form-control" id="phone" name="phone" type="tel" value="<?=$_POST["phone"]?>" placeholder="Your Phone *" required>
                  <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                  <textarea class="form-control" id="message" name="message" placeholder="Your Message *" required><?=$_POST["message"]?></textarea>
                  <p class="help-block text-danger"></p>
                </div>
                <p class="g-recaptcha" data-sitekey="<?=$siteKey?>" data-callback="enableBtn"></p>
                <?=$errMsg?>
              </div>
              <div class="clearfix"></div>
              <div class="col-lg-12 text-center">
                <div id="success"></div>
                <button id="sendMessageButton" name="sendMessageButton" class="btn btn-warning btn-xl text-uppercase" type="submit" disabled>Send Message</button>
              </div>
            </div>
          </form>
        </div>
      </div>
<?php
}
?>
    </div>
  </section>

<?php include("inc/footer.php"); ?>

  <!-- Bootstrap core JavaScript -->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Plugin JavaScript -->
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for this template -->
  <script src="js/agency.min.js"></script>

</body>

</html>
