<?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['submit'])) {
    
 


    

      
    

        
            $stmt = $conn->prepare("INSERT INTO `blog_comment`(`b_id`,`name`,  `message`) VALUES (?,?,?)");
           

            $stmt->execute([$_POST['b_id'],$_POST['name'],$_POST['message']]);
           
            // 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'];
            }

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

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

            $mail = new PHPMailer(true);

            $mail->isSMTP();
            $mail->Host       = $smtp_server;
            $mail->SMTPAuth   = true;
            $mail->Username   = $smtp_username;
            $mail->Password   = $smtp_password;
            $mail->SMTPSecure = $smtp_enc;
            $mail->Port       = $smtp_port;

            $mail->setFrom($smtp_username);
            $mail->addAddress($email);

            $mail->isHTML(true);
            $mail->Subject = 'Cordillera ' . $dt;
            $mail->Body    = $msg1;
            $mail->AltBody = $msg1;

            if ($mail->send())
            // email end

            {

?>
                <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 = '../../../blog.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 = '../../../blog.php';\",1500);</script>"; ?>
                    </p>
                </div>
            </div>
<?php
        }
    
}


?>