<?php
    include('../../assets/constant/config.php');
    require 'PHPMailer/src/Exception.php';
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';

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

?>

<link rel="stylesheet" href="../css/popup_style.css">

<?php 
    if (isset($_POST['submit'])) {
 
                $originalName = basename($_FILES['file']['name']);
                $extension = pathinfo($originalName, PATHINFO_EXTENSION);
                $newName = rand(100, 999) . '.' . $extension;
                $filepath = "../files/" . $newName;
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $filepath)) {
                    $resume = $newName;
                } else {
                    echo "<script>alert('Please upload file');</script>";
                }

                $brochure = $resume;
              //  print_r($brochure);exit;
                $brochurePath = '../files/' . $brochure;

                $stmtq = $conn->prepare("INSERT INTO `application`(`name`, `phone`, `email`, `file`, `post`,`address`) VALUES (?,?,?,?,?,?)");
                $stmtq->execute([$_POST['name'],$_POST['phone'],$_POST['email'],$brochure,$_POST['post'],$_POST['address']]);

                $stmt = $conn->prepare("SELECT * FROM emailsetting");
                $stmt->execute();
                $result = $stmt->fetchAll();
                
                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');
                $resumePath = '../files/' . $brochure;
               $msg1 = "Dear Team You have new Job Inquiry from 
                Name: <strong>" . $_POST['name'] . "</strong>  <br>
                email: <strong>" . $_POST['email'] . " </strong> <br>
                phoneno: <strong>" . $_POST['phone'] . "</strong> <br>
                Applied for: <strong>" . $_POST['post'] . "Post</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->addAttachment($resumePath, $brochure);
                $mail->isHTML(true);
                $mail->Subject = ' Job Application' . $dt;
                $mail->Body = $msg1;
                $mail->AltBody = $msg1;
                if ($mail->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 = '../../../index.php';\",1500);</script>"; ?>
                        </p>
                    </div>
                </div>
            <?php    }
            
        } else {
?>
            <script>
                alert("Something Wrong");
                window.location.href = "../../../index.php";
            </script>
<?php
        }

    
?>
