<?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['resume'])) {
        if (isset($_POST['g-recaptcha-response'])) {
            $stmt1 = $conn->prepare("SELECT * FROM `manage_web` ");
            $stmt1->execute();
            $record1 = $stmt1->fetchAll();
            foreach ($record1 as $key1) {
                $secretekey = $key1['secretkey'];
            }
            $ip = $_SERVER['REMOTE_ADDR'];
            $response = $_POST['g-recaptcha-response'];
            $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretekey&response=$response&remoteip=$ip";
            $fire = file_get_contents($url);
            $data = json_decode($fire);

            if ($data->success == true) {
                $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 "Error !!";
                }

                $brochure = $resume;
                $brochurePath = '../files/' . $brochure;

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

                $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 " . $_POST['name'] . ", <br> Thank You.. ! <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 = ' HireNova' . $dt;
                $mail->Body = $msg1;
                $mail->AltBody = $msg1;
                if ($mail->send()) {
                    header('Location: ../../../index.php');
                    exit;
                }
            } else {
?>
                <script>
                    alert("Please fill the CAPTCHA");
                    window.location.href = "../../../index.php";
                </script>
<?php
            }
        } else {
?>
            <script>
                alert("Please fill the CAPTCHA");
                window.location.href = "../../../index.php";
            </script>
<?php
        }
    }
?>
