<?php include('../../assets/constant/config.php'); ?>
<link rel="stylesheet" href="../css/popup_style.css">

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
?>

<?php

//contact email

if (isset($_POST['contact'])) {
    
// print_r($_POST['captcha']); exit;
   
          if ($_POST['text'] !== $_POST['captcha']) {
        echo "<div class='popup popup--icon -error js_error-popup popup--visible'>
                <div class='popup__background'></div>
                <div class='popup__content'>
                    <h3 class='popup__content__title'>Error</h3>
                    <p>Incorrect CAPTCHA. Please try again.</p>
                    <p>
                        <script>setTimeout(\"location.href = '../../../contact.php';\",1500);</script>
                    </p>
                </div>
              </div>";
        exit;
    }else{
            $stmt = $conn->prepare("INSERT INTO `contactus`(`name`, `email`,`phone`, `subject`,`message`) VALUES ('".$_POST['name']."','".$_POST['email']."','".$_POST['phone']."','".$_POST['subject']."','".$_POST['message']."')");
           

            $stmt->execute();
           
            // print_r($stmt);
            // exit;

            // email start
            $stmt = $conn->prepare("SELECT * FROM emailsetting");
            $stmt->execute();
            $result = $stmt->fetchAll();
            //print_r($result);
            foreach ($result as $row) {

                $smtp_server = $row['smtp_server'];
                $smtp_password = $row['smtp_password'];
                $smtp_enc = $row['smtp_type'];
                $smtp_username = $row['smtp_username'];
                $smtp_port = $row['stmp_port'];
                $email = $row['email'];
 $email1 = $row['email1'];
              
            }

            $dt = date('Y-m-d H:i:s');

            $msg1 = "Dear Admin, <br>
    You Have new message from <br> 
    Name: <strong>" . $_POST['name'] . "</strong>  <br>
    email: <strong>" . $_POST['email'] . " </strong> <br>
    Subject: <strong>" . $_POST['subject'] . " </strong> <br>
    Phone: <strong>" . $_POST['phone'] . "</strong> <br>
     message: <strong>" . $_POST['message'] . "</strong> <br>
   
     ";

          $mail = new PHPMailer();
            $mail->Encoding = "base64";
            $mail->SMTPAuth = true;
            $mail->Host       = $smtp_server;
            $mail->Port       = $smtp_port;
            $mail->Username   = $smtp_username;
            $mail->Password   = $smtp_password;
            $mail->SMTPSecure = $smtp_enc;
            $mail->isSMTP();
            $mail->IsHTML(true);
            $mail->CharSet = "UTF-8";
            $mail->From = "noreply@ova.ngo";
            $mail->addAddress($email);
              $mail->addAddress($email1);
            $mail->Subject = 'Bharatiya Open Volunteer Association (OVA) ' . $dt;
            $mail->Body    = $msg1;
            $mail->AltBody = $msg1;

            if ($mail->send())
                        {

 $msg2 = "Dear " . htmlspecialchars($_POST['name']) . ",<br><br>
Thank you for contacting us. We have received your query and will get back to you within the next few days.<br><br>
Best regards,<br>
The Team OVA ";


          $mail1 = new PHPMailer();
            $mail1->Encoding = "base64";
            $mail1->SMTPAuth = true;
            $mail1->Host       = $smtp_server;
            $mail1->Port       = $smtp_port;
            $mail1->Username   = $smtp_username;
            $mail1->Password   = $smtp_password;
            $mail1->SMTPSecure = $smtp_enc;
            $mail1->isSMTP();
            $mail1->IsHTML(true);
            $mail1->CharSet = "UTF-8";
            $mail1->From = "noreply@ova.ngo";
            $mail1->addAddress($_POST['email']);
            $mail1->Subject = 'Bharatiya Open Volunteer Association (OVA) ' . $dt;
            $mail1->Body    = $msg2;
            $mail1->AltBody = $msg2;

            $mail1->send();


?>
                <div class="popup popup--icon -success js_success-popup popup--visible">
                    <div class="popup__background"></div>
                    <div class="popup__content">
                        <h3 class="popup__content__title">
                            Success
                        </h3>
                        <p> We have successfully received your Message and will get Back to you as soon as possible.</p>
                        <p>
                            <!-- <a href="login.php"><button class="button button--success" data-for="js_success-popup">OK</button></a> -->
                            <?php echo "<script>setTimeout(\"location.href = '../../../contact.php';\",1500);</script>"; ?>
                        </p>
                    </div>
                </div>
            <?php
            }
         }
         }else {
            // echo "<script>alert('Something Went Wrong');
            //     window.location = (\"contact-us.php\")
            //     </script>";

            ?>
            <div class="popup popup--icon -error js_error-popup popup--visible">
                <div class="popup__background"></div>
                <div class="popup__content">
                    <h3 class="popup__content__title">
                        Error
                    </h3>
                    <p> Something Went Wrong.</p>
                    <p>
                        <!-- <a href="login.php"><button class="button button--success" data-for="js_success-popup">OK</button></a> -->
                        <?php echo "<script>setTimeout(\"location.href = '../../../contact.php';\",1500);</script>"; ?>
                    </p>
                </div>
            </div>
<?php
        }




?>