<?php
// Load Composer's autoloader
require 'vendor/autoload.php';

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

var_dump($_SERVER['REQUEST_METHOD']);
// Function to send an email
function sendMail($toEmail, $subject, $messageBody, $uploaded_file) {
    // Create a new PHPMailer instance
    $mail = new PHPMailer(true);

    try {
        $loader='true';
        // SMTP Configuration
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com'; // Replace with your SMTP host
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@bimcafe.in'; // Replace with your SMTP username
        $mail->Password   = 'Fah00237';  // PES // Replace with your SMTP password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use TLS or PHPMailer::ENCRYPTION_SMTPS for SSL
        $mail->Port       = 587; // Replace with your SMTP port (usually 587 for TLS)
 

        // Sender and recipient settings
        $mail->setFrom('info@bimcafe.in', 'Bimcafe'); // Replace with your "From" address
        $mail->addAddress($toEmail); // Add recipient email

        // Email content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $messageBody;

        // Attach files
        if (!empty($uploaded_file) && file_exists($uploaded_file)) {
            $mail->addAttachment($uploaded_file);
        }

        // Send the email
        $mail->send();
        return true;
    } catch (Exception $e) {
        return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
    }
} 

if ($_SERVER["REQUEST_METHOD"] === "POST" ) {  

    if (!empty($_FILES['file']['name'])) {
        $upload_dir = 'uploads/';
        $max_size = 5 * 1024 * 1024; // 5MB limit
        $errors = [];
    
        $file_name = basename($_FILES['file']['name']);
        $file_size = $_FILES['file']['size'];
        $tmp_name = $_FILES['file']['tmp_name'];
        $target_path = $upload_dir . $file_name;
    
        // Validate file size only
        if ($file_size > $max_size) {
            echo "$file_name exceeds the 5MB file size limit.";
        } else {
            if (move_uploaded_file($tmp_name, $target_path)) {
                $uploaded_file = $target_path;
            } else {
                echo "Failed to upload $file_name.";
            }
        }
    }
    
    
    
    
    // Prepare email
    $toEmail = 'lakshmipriya@techpullers.com';  
    $subject = "Career Submission - " . $_POST['name'];

    
    $body    = ' 
        <!DOCTYPE html>
        <html lang="en"> 
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                .button,.email-footer,.email-header{text-align:center}body{font-family:Arial,sans-serif;background-color:#f6f6f6;margin:0;padding:0}.email-container{max-width:600px;margin:20px auto;background-color:#fff;border-radius:8px;box-shadow:0 4px 8px rgba(0,0,0,.1);overflow:hidden}.button:hover,.email-header{background-color:#084258}.email-header{color:#fff;padding:20px}.email-body{padding:20px}.email-footer{background-color:#f1f1f1;color:#666;padding:10px;font-size:12px}.table{width:100%;border-collapse:collapse}.table td,.table th{text-align:left;padding:10px;border-bottom:1px solid #eee}.table th{background-color:#f7f7f7;font-weight:700}.button{display:inline-block;margin-top:20px;padding:10px 20px;color:#fff;background-color:#14a800;text-decoration:none;border-radius:4px}
            </style>
        </head>
        
        <body>
            <div class="email-container">
                <div class="email-header">
                    <h1>Career Form Submission</h1>
                </div>
                <div class="email-body">
                <p>Hello Admin,</p>
                <p>You have received a new message from the carrer form. Below are the details:</p>
            
               <table border="1" width="100%" cellpadding="5" cellspacing="5">
                    <tr>
                        <td width="30%">Name</td>
                        <td width="70%">' . htmlspecialchars($_POST["name"]) . '</td>
                    </tr> 
                    <tr>
                        <td width="30%">Email Address</td>
                        <td width="70%">' . htmlspecialchars($_POST["email"]) . '</td>
                    </tr>
                    <tr>
                        <td width="30%">Area of Interest</td>
                        <td width="70%">' . htmlspecialchars($_POST['phone_dial_code'] . ' ' . $_POST['phone']) . '</td>
                    </tr>
                    <tr>
                        <td width="30%">Position</td>
                        <td width="70%">' . htmlspecialchars($_POST["position"]) . '</td>
                    </tr> 
                </table>
            
                <p>To reply to this email, you can use the sender\'s email address provided above.</p>
                    <a href="mailto:'. $email .'" class="button">Reply to Sender</a>
                </div>
                
            </div>
        </body>
        
        </html> 
    ';

    $result = sendMail($toEmail, $subject, $body, $uploaded_file);
    // echo $result;
    
    if ($result) {
        echo "<script>window.location.href='career-success.php'</script>";
    } else {
        echo 'Failed to send email.';
    }

        
} 
?>