<?php
    // NOTE: All database and session logic has been removed,
    // but we retain the logic that includes site config variables
    require_once 'get_setting.php'; 

    $application_message = '';
    $application_status = ''; // 'success' or 'error'

    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['submit_application'])) {
        $to_email = "info@reliparcel.com"; 
        $subject = "New Job Application: " . htmlspecialchars($_POST['job_title'] ?? 'General Application');
        
        $name = htmlspecialchars($_POST['fullName']);
        $applicant_email = htmlspecialchars($_POST['email']);
        $phone = htmlspecialchars($_POST['phone'] ?? 'N/A');
        $location = htmlspecialchars($_POST['location'] ?? 'N/A');
        $job_applied_for = htmlspecialchars($_POST['job_title'] ?? 'General Application');
        $applicant_message = htmlspecialchars($_POST['message'] ?? 'No cover letter provided.');

        // 1. Build Email Body
        $email_body = "You have received a new job application.\n\n";
        $email_body .= "Job Applied For: " . $job_applied_for . "\n";
        $email_body .= "Full Name: " . $name . "\n";
        $email_body .= "Email: " . $applicant_email . "\n";
        $email_body .= "Phone: " . $phone . "\n";
        $email_body .= "Location: " . $location . "\n\n";
        $email_body .= "Message/Cover Letter:\n" . $applicant_message . "\n\n";
        $email_body .= "--- Note: Resume attached below ---";

        // 2. Handle File Upload (Resume)
        $file_uploaded = false;
        $attachment = '';
        $filename = '';
        
        if (isset($_FILES['resume']) && $_FILES['resume']['error'] == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES['resume']['tmp_name'];
            $filename = basename($_FILES['resume']['name']);
            $file_type = $_FILES['resume']['type'];
            $file_size = $_FILES['resume']['size'];
            
            // Check file size (e.g., limit to 5MB)
            if ($file_size > 5000000) {
                 $application_status = 'error';
                 $application_message = "File is too large. Maximum file size is 5MB.";
            } else {
                // Read the file content
                $content = file_get_contents($tmp_name);
                $content = chunk_split(base64_encode($content));
                $file_uploaded = true;
            }
        } else if (!isset($_FILES['resume']) || $_FILES['resume']['error'] == UPLOAD_ERR_NO_FILE) {
             $application_status = 'error';
             $application_message = "Error: Please upload a resume file.";
        } else {
             $application_status = 'error';
             $application_message = "An error occurred during file upload. Error code: " . $_FILES['resume']['error'];
        }


        // 3. Assemble and Send Mail (only if file upload was successful)
        if ($application_status !== 'error') {
            
            // Generate unique boundary for multipart email
            $mime_boundary = "==Multipart_Boundary_x" . md5(time()) . "x";
            
            // Headers for file attachment
            $headers = "From: " . $name . " <" . $applicant_email . ">\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-Type: multipart/mixed; boundary=\"$mime_boundary\"\r\n";
            
            // Multipart Email Construction
            $message = "--$mime_boundary\r\n";
            $message .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
            $message .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
            $message .= $email_body . "\r\n"; // Add the plain text body
            
            // Attachment section
            if ($file_uploaded) {
                $message .= "--$mime_boundary\r\n";
                $message .= "Content-Type: $file_type; name=\"$filename\"\r\n";
                $message .= "Content-Disposition: attachment; filename=\"$filename\"\r\n";
                $message .= "Content-Transfer-Encoding: base64\r\n\r\n";
                $message .= $content . "\r\n";
            }
            $message .= "--$mime_boundary--\r\n"; // End of message

            // Send the email (Disabled)
            if (true || mail($to_email, $subject, $message, $headers)) {
                $application_status = 'success';
                $application_message = "Thank you, " . $name . "! Your application has been successfully submitted.";
            } else {
                $application_status = 'error';
                $application_message = "Failed to send the application email. Please check your server mail configuration or contact us directly.";
            }
        }
    }
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title><?php echo htmlspecialchars($site_settings['site_title'] ?? 'ReliParcel Express'); ?> | Careers</title> 
        <meta name="description" content="Explore career opportunities in transportation, operations, customs brokerage, and technology with a leading global logistics provider.">
        
        <?php if (isset($use_favicon) && $use_favicon): ?>
        <link rel="icon" type="<?php echo $favicon_mime; ?>" href="<?php echo $favicon_path; ?>">
        <?php else: ?>
        <link rel="icon" type="image/x-icon" href="../assets/images/favicon.ico">
        <?php endif; ?>
        
        <link href="assets/css/style.css" rel="stylesheet">
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
        <link rel="stylesheet" href="assets/css/font-awesome-pro.css">
        <link rel="stylesheet" href="assets/css/gtranslate.css">
        <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
        
        <style>
            /* RETAINED STYLING */
            .text-crimson {
                color: #6d0000 !important; /* RETAINED: Brand color */
            }
            /* Custom styling for Careers Page */
            .job-card {
                transition: transform 0.3s ease, box-shadow 0.3s ease;
            }
            .job-card:hover {
                transform: translateY(-5px);
                box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
            }
            .value-prop-icon {
                font-size: 2.5rem;
                color: #6d0000;
            }
            .card-header-job {
                background-color: #f7f7f7;
                border-bottom: 1px solid #eee;
            }
        </style>
    </head>
    <body>
        
        <?php include "nav.php" ?>
        
        <header class="hero-gradient-bg text-white text-center pt-5 pb-5 shadow-lg">
            <div class="container mt-5 pt-5">
                <h1 class="display-6 fw-bolder mb-3">CAREERS: DRIVE YOUR FUTURE WITH US</h1>
                <p class="lead mb-0 fs-5">
                    Join a world-class team committed to speed, efficiency, and global connectivity. Your next journey starts here.
                </p>
            </div>
        </header>
        
        <?php if ($application_message): ?>
        <div class="container mt-4">
            <div class="alert alert-<?php echo $application_status == 'success' ? 'success' : 'danger'; ?> text-center fade show" role="alert">
                <i class="bi bi-<?php echo $application_status == 'success' ? 'check-circle-fill' : 'exclamation-triangle-fill'; ?> me-2"></i>
                <?php echo $application_message; ?>
            </div>
        </div>
        <?php endif; ?>
        <section id="our-values" class="py-5 bg-light">
            <div class="container">
                <h2 class="text-center fw-bolder mb-5 text-dark">Why Choose a Career in Logistics with <?php echo htmlspecialchars($site_settings['site_title'] ?? 'ReliParcel'); ?>?</h2>
                <div class="row text-center">
                    
                    <div class="col-md-4 mb-4">
                        <i class="bi bi-truck value-prop-icon mb-3"></i>
                        <h4 class="fw-semibold">Global Impact</h4>
                        <p class="text-muted">Be the link that connects businesses and families worldwide, managing critical supply chains.</p>
                    </div>

                    <div class="col-md-4 mb-4">
                        <i class="bi bi-cash-stack value-prop-icon mb-3"></i>
                        <h4 class="fw-semibold">Competitive Benefits</h4>
                        <p class="text-muted">We offer comprehensive health plans, generous paid time off, and competitive salary packages.</p>
                    </div>
                    
                    <div class="col-md-4 mb-4">
                        <i class="bi bi-graph-up-arrow value-prop-icon mb-3"></i>
                        <h4 class="fw-semibold">Growth & Training</h4>
                        <p class="text-muted">Access ongoing professional development, mentorship programs, and clear paths for advancement.</p>
                    </div>

                </div>
            </div>
        </section>

        <section id="job-openings" class="py-5">
            <div class="container">
                <h2 class="text-center fw-bolder mb-4 text-crimson">Current Opportunities</h2>
                <p class="text-center lead mb-5 text-muted">Filter by department or location to find the perfect role.</p>

                <div class="row">
                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card shadow-sm h-100 job-card">
                            <div class="card-header card-header-job">
                                <h5 class="mb-0 fw-bold">Operations Coordinator</h5>
                            </div>
                            <div class="card-body">
                                <p class="card-text text-muted">
                                    <i class="bi bi-geo-alt-fill me-1"></i> Location: New York, NY (Hybrid)<br>
                                    <i class="bi bi-briefcase-fill me-1"></i> Department: Express Services
                                </p>
                                <p class="small">Manage daily courier routing, optimize manifest creation, and liaise between drivers and customers to ensure service delivery targets are met.</p>
                                <button type="button" class="btn btn-outline-dark btn-sm stretched-link apply-job-btn" 
                                        data-bs-toggle="modal" data-bs-target="#applicationModal" 
                                        data-job-title="Operations Coordinator">
                                    View Details & Apply
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card shadow-sm h-100 job-card">
                            <div class="card-header card-header-job">
                                <h5 class="mb-0 fw-bold">Commercial Truck Driver (CDL)</h5>
                            </div>
                            <div class="card-body">
                                <p class="card-text text-muted">
                                    <i class="bi bi-geo-alt-fill me-1"></i> Location: Los Angeles, CA (Full-Time)<br>
                                    <i class="bi bi-briefcase-fill me-1"></i> Department: Freight Transport
                                </p>
                                <p class="small">Responsible for safe, on-time movement of LTL and FCL freight. Requires valid CDL and clean driving record.</p>
                                <button type="button" class="btn btn-outline-dark btn-sm stretched-link apply-job-btn" 
                                        data-bs-toggle="modal" data-bs-target="#applicationModal" 
                                        data-job-title="Commercial Truck Driver (CDL)">
                                    View Details & Apply
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="col-md-6 col-lg-4 mb-4">
                        <div class="card shadow-sm h-100 job-card">
                            <div class="card-header card-header-job">
                                <h5 class="mb-0 fw-bold">Customs Brokerage Specialist</h5>
                            </div>
                            <div class="card-body">
                                <p class="card-text text-muted">
                                    <i class="bi bi-geo-alt-fill me-1"></i> Location: Remote (Full-Time)<br>
                                    <i class="bi bi-briefcase-fill me-1"></i> Department: Global Trade
                                </p>
                                <p class="small">Ensure compliance with international trade regulations, process duties/taxes, and manage complex import/export documentation.</p>
                                <button type="button" class="btn btn-outline-dark btn-sm stretched-link apply-job-btn" 
                                        data-bs-toggle="modal" data-bs-target="#applicationModal" 
                                        data-job-title="Customs Brokerage Specialist">
                                    View Details & Apply
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="col-12 mt-4 text-center">
                        <div class="p-3 bg-light rounded shadow-sm">
                             <p class="mb-0">Don't see your perfect fit? Send us your resume for future opportunities.</p>
                             <a href="mailto:info@reliparcel.com" class="btn btn-md btn-crimson mt-2" style="background-color: #6d0000; color: white;">Submit General Application</a>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <section id="application-form-cta" class="py-5 hero-gradient-bg text-white">
            <div class="container text-center">
                <h2 class="fw-bolder mb-3">Ready to Start Your Career?</h2>
                <p class="lead mb-4">Use our simple form to apply in minutes.</p>
                
                <button type="button" class="btn btn-warning fw-bold text-dark rounded-pill ms-lg-3 shadow-lg btn-skew" 
                        data-bs-toggle="modal" data-bs-target="#applicationModal"
                        data-job-title="General Application">
                   <span class="btn-skew-text"> <i class="bi bi-send-fill me-2"></i> Apply Online Now</span>
                </button>
            </div>
        </section>


        <?php include "footer.php" ?>
        
        <div class="modal fade" id="applicationModal" tabindex="-1" aria-labelledby="applicationModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable">
                <div class="modal-content">
                    <div class="modal-header" style="background-color: #f7f7f7;">
                        <h5 class="modal-title fw-bold text-crimson" id="applicationModalLabel">
                            <i class="bi bi-person-rolodex me-2"></i> Job Application Form
                        </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p class="mb-3">
                            Applying for: <strong id="job-title-display">General Application</strong>
                        </p>
                        <hr>
                        <form id="simpleApplicationForm" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST" enctype="multipart/form-data">
                            
                            <input type="hidden" name="job_title" id="job-title-input" value="General Application">

                            <div class="row mb-3">
    <div class="col-md-6">
        <label for="fullName" class="form-label">Full Name <span class="text-danger">*</span></label>
        <input type="text" class="form-control" id="fullName" name="fullName" required placeholder="Enter your full name">
    </div>
    <div class="col-md-6">
        <label for="email" class="form-label">Email Address <span class="text-danger">*</span></label>
        <input type="email" class="form-control" id="email" name="email" required placeholder="you@example.com">
    </div>
</div>

<div class="row mb-3">
    <div class="col-md-6">
        <label for="phone" class="form-label">Phone Number</label>
        <input type="tel" class="form-control" id="phone" name="phone" placeholder="e.g., +1 234 567 890">
    </div>
    <div class="col-md-6">
        <label for="location" class="form-label">Current Location (City, State/Country)</label>
        <input type="text" class="form-control" id="location" name="location" placeholder="City, State or Country">
    </div>
</div>

                            
                            <div class="mb-3">
                                <label for="resume" class="form-label">Upload Resume/CV (PDF, DOCX) <span class="text-danger">*</span></label>
                                <input class="form-control" type="file" id="resume" name="resume" accept=".pdf,.doc,.docx" required>
                                <div class="form-text">Max file size: 5MB.</div>
                            </div>

                            <div class="mb-3">
                                <label for="message" class="form-label">Cover Letter/Brief Message (Optional)</label>
                                <textarea class="form-control" id="message" name="message" rows="3"></textarea>
                            </div>
                            
                            <div class="form-check mb-3">
                                <input class="form-check-input" type="checkbox" value="agreed" id="privacyCheck" required>
                                <label class="form-check-label small" for="privacyCheck">
                                    I confirm that I have read and agree to the <a href="privacy_policy.php" target="_blank" class="text-crimson">Privacy Policy</a> regarding the submission of my personal data.
                                </label>
                            </div>
                            
                            <div class="modal-footer justify-content-start">
                                <button type="submit" name="submit_application" class="btn btn-crimson text-white fw-bold" style="background-color: #6d0000;">
                                    <i class="bi bi-box-arrow-in-right me-2"></i> Submit Application
                                </button>
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
        <?php include "custom_script.php" ?>

        <script>
            document.addEventListener('DOMContentLoaded', function() {
                var applicationModal = document.getElementById('applicationModal');
                
                // Function to update the modal's hidden and visible job title fields
                var updateModalJobTitle = function(button) {
                    var jobTitle = button.getAttribute('data-job-title');
                    
                    var modalTitleDisplay = applicationModal.querySelector('#job-title-display');
                    var modalTitleInput = applicationModal.querySelector('#job-title-input');

                    if (modalTitleDisplay && modalTitleInput) {
                        modalTitleDisplay.textContent = jobTitle;
                        modalTitleInput.value = jobTitle;
                    }
                };

                // Add listener to all buttons that trigger the modal
                applicationModal.addEventListener('show.bs.modal', function (event) {
                    var button = event.relatedTarget; 
                    updateModalJobTitle(button);
                });
                
                // If the form submitted an error, the page reloaded. We need to check if the modal should reopen.
                <?php if ($application_status == 'error'): ?>
                    // Display SweetAlert for error
                    Swal.fire({
                      icon: 'error',
                      title: 'Application Error',
                      text: '<?php echo $application_message; ?>',
                      confirmButtonColor: '#6d0000'
                    });
                    
                    // You might want to re-open the modal here, but that requires storing the job title
                    // in a session or hidden field across the submission, which complicates the script.
                    // For simplicity, we just show the error alert.
                <?php elseif ($application_status == 'success'): ?>
                    // Display SweetAlert for success
                    Swal.fire({
                      icon: 'success',
                      title: 'Application Submitted!',
                      text: '<?php echo $application_message; ?>',
                      confirmButtonColor: '#6d0000'
                    });
                <?php endif; ?>
            });
        </script>
    </body>
</html>