Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : //home/vishqocm/pcib.in/admin/ajax/download_application_new.php

<?php
// Start session
session_start();

// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    header('HTTP/1.1 403 Forbidden');
    echo 'Unauthorized access';
    exit;
}

// Include database configuration
require_once '../../admin/database/db_config.php';

// Check if application ID is provided
if (!isset($_GET['id']) || empty($_GET['id'])) {
    header('HTTP/1.1 400 Bad Request');
    echo 'Invalid request: Missing application ID';
    exit;
}

$application_id = intval($_GET['id']);

// Get application details - NO START_DATE or END_DATE
$app_query = "SELECT ea.*, u.first_name, u.last_name, u.email, u.phone, 
                      c.title as course_title, c.price, c.discount_price, c.duration
               FROM enrollment_applications ea
               JOIN users u ON ea.user_id = u.id
               JOIN courses c ON ea.course_id = c.id
               WHERE ea.id = ?";
               
$stmt = $conn->prepare($app_query);
$stmt->bind_param("i", $application_id);
$stmt->execute();
$app_result = $stmt->get_result();
$application = $app_result->fetch_assoc();

if (!$application) {
    header('HTTP/1.1 404 Not Found');
    echo 'Application not found';
    exit;
}

// Get student documents
$docs_query = "SELECT * FROM student_documents WHERE user_id = ? ORDER BY document_type";
$stmt = $conn->prepare($docs_query);
$stmt->bind_param("i", $application['user_id']);
$stmt->execute();
$docs_result = $stmt->get_result();
$documents = [];

while ($doc = $docs_result->fetch_assoc()) {
    $documents[] = $doc;
}

// Document type labels
$doc_types = [
    'id_proof' => 'ID Proof',
    'educational_certificate' => 'Educational Certificate',
    'photograph' => 'Photograph',
    'other' => 'Other Document'
];

// Status labels
$status_labels = [
    'pending' => 'Pending',
    'payment_pending' => 'Payment Pending',
    'approved' => 'Approved',
    'rejected' => 'Rejected',
    'completed' => 'Completed'
];

// Try to parse personal details JSON if exists
$personal_details = [];
if (!empty($application['personal_details'])) {
    $personal_details = json_decode($application['personal_details'], true) ?: [];
}

// Set headers for PDF output
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="Application_' . $application_id . '.pdf"');

// Include mPDF library if available, otherwise use plain text
if (file_exists('../../vendor/autoload.php')) {
    require_once '../../vendor/autoload.php';
    
    try {
        // Create mPDF instance
        $mpdf = new \Mpdf\Mpdf([
            'mode' => 'utf-8',
            'format' => 'A4',
            'margin_top' => 15,
            'margin_bottom' => 15,
            'margin_left' => 15,
            'margin_right' => 15
        ]);
        
        // Set document information
        $mpdf->SetTitle('Enrollment Application #' . $application_id);
        $mpdf->SetAuthor('Popular Computer');
        
        // Build HTML content
        $html = '
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.5; }
            h1 { color: #336699; font-size: 18px; text-align: center; margin-bottom: 20px; }
            h2 { color: #336699; font-size: 16px; margin-top: 20px; border-bottom: 1px solid #eee; padding-bottom: 5px; }
            .section { margin-bottom: 20px; }
            table { width: 100%; border-collapse: collapse; }
            table.details td, table.details th { padding: 6px; border: 1px solid #ddd; }
            table.details th { background-color: #f2f2f2; width: 30%; text-align: left; }
            .header { text-align: center; margin-bottom: 30px; }
            .logo { text-align: center; margin-bottom: 10px; }
            .status-pending { color: #ff9900; }
            .status-approved, .status-completed, .status-verified { color: #00aa00; }
            .status-rejected { color: #cc0000; }
            .status-payment_pending { color: #0066cc; }
            .footer { text-align: center; font-size: 11px; margin-top: 30px; }
        </style>
        
        <div class="header">
            <div class="logo">POPULAR COMPUTER</div>
            <h1>ENROLLMENT APPLICATION #' . $application_id . '</h1>
        </div>
        
        <div class="section">
            <h2>Application Information</h2>
            <table class="details">
                <tr>
                    <th>Application ID</th>
                    <td>' . $application_id . '</td>
                </tr>
                <tr>
                    <th>Application Date</th>
                    <td>' . date('F d, Y', strtotime($application['application_date'])) . '</td>
                </tr>
                <tr>
                    <th>Status</th>
                    <td class="status-' . $application['status'] . '">' . ($status_labels[$application['status']] ?? 'Unknown') . '</td>
                </tr>
                <tr>
                    <th>Verification Token</th>
                    <td>' . htmlspecialchars($application['verification_token']) . '</td>
                </tr>
            </table>
        </div>
        
        <div class="section">
            <h2>Student Information</h2>
            <table class="details">
                <tr>
                    <th>Name</th>
                    <td>' . htmlspecialchars($application['first_name'] . ' ' . $application['last_name']) . '</td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td>' . htmlspecialchars($application['email']) . '</td>
                </tr>
                <tr>
                    <th>Phone</th>
                    <td>' . htmlspecialchars($application['phone'] ?? 'N/A') . '</td>
                </tr>
            </table>
        </div>
        
        <div class="section">
            <h2>Course Information</h2>
            <table class="details">
                <tr>
                    <th>Course</th>
                    <td>' . htmlspecialchars($application['course_title']) . '</td>
                </tr>
                <tr>
                    <th>Duration</th>
                    <td>' . htmlspecialchars($application['duration'] ?? 'N/A') . ' weeks</td>
                </tr>
                <tr>
                    <th>Price</th>
                    <td>₹' . number_format(($application['discount_price'] > 0 && $application['discount_price'] < $application['price']) ? 
                           $application['discount_price'] : $application['price'], 2) . '</td>
                </tr>';
        
        $html .= '
            </table>
        </div>';
        
        // Personal details section
        if (!empty($personal_details)) {
            $html .= '
            <div class="section">
                <h2>Personal Details</h2>
                <table class="details">';
            
            foreach ($personal_details as $key => $value) {
                $label = ucfirst(str_replace('_', ' ', $key));
                $html .= '
                    <tr>
                        <th>' . htmlspecialchars($label) . '</th>
                        <td>' . htmlspecialchars($value) . '</td>
                    </tr>';
            }
            
            $html .= '
                </table>
            </div>';
        }
        
        // Documents section
        if (!empty($documents)) {
            $html .= '
            <div class="section">
                <h2>Submitted Documents</h2>
                <table class="details">
                    <tr>
                        <th>Document Type</th>
                        <th>Upload Date</th>
                        <th>Status</th>
                    </tr>';
            
            foreach ($documents as $doc) {
                $doc_type = $doc_types[$doc['document_type']] ?? $doc['document_type'];
                $html .= '
                    <tr>
                        <td>' . htmlspecialchars($doc_type) . '</td>
                        <td>' . date('M d, Y', strtotime($doc['upload_date'])) . '</td>
                        <td class="status-' . $doc['status'] . '">' . ucfirst($doc['status']) . '</td>
                    </tr>';
            }
            
            $html .= '
                </table>
            </div>';
        }
        
        // Admin notes section if exists
        if (!empty($application['admin_notes'])) {
            $html .= '
            <div class="section">
                <h2>Admin Notes</h2>
                <p>' . nl2br(htmlspecialchars($application['admin_notes'])) . '</p>
            </div>';
        }
        
        $html .= '
        <div class="footer">
            This document was generated on ' . date('F d, Y H:i:s') . '.<br>
            Popular Computer - Enrollment Application Record
        </div>';
        
        // Write HTML to PDF
        $mpdf->WriteHTML($html);
        
        // Output PDF as download
        $mpdf->Output('Application_' . $application_id . '.pdf', 'D');
    }
    catch (Exception $e) {
        // If mPDF fails, fall back to simple HTML
        echo 'Error generating PDF: ' . $e->getMessage();
    }
}
else {
    // Simple HTML output if mPDF not available
    echo '<!DOCTYPE html>
    <html>
    <head>
        <title>Enrollment Application #' . $application_id . '</title>
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.5; padding: 20px; }
            h1 { color: #336699; font-size: 24px; }
            h2 { color: #336699; font-size: 18px; margin-top: 20px; }
            .section { margin-bottom: 20px; }
            table { width: 100%; border-collapse: collapse; }
            td, th { padding: 8px; border: 1px solid #ddd; }
            th { background-color: #f2f2f2; width: 30%; text-align: left; }
        </style>
    </head>
    <body>
        <h1>Enrollment Application #' . $application_id . '</h1>
        
        <div class="section">
            <h2>Application Information</h2>
            <table>
                <tr>
                    <th>Application ID</th>
                    <td>' . $application_id . '</td>
                </tr>
                <tr>
                    <th>Application Date</th>
                    <td>' . date('F d, Y', strtotime($application['application_date'])) . '</td>
                </tr>
                <tr>
                    <th>Status</th>
                    <td>' . ($status_labels[$application['status']] ?? 'Unknown') . '</td>
                </tr>
            </table>
        </div>
        
        <div class="section">
            <h2>Student Information</h2>
            <table>
                <tr>
                    <th>Name</th>
                    <td>' . htmlspecialchars($application['first_name'] . ' ' . $application['last_name']) . '</td>
                </tr>
                <tr>
                    <th>Email</th>
                    <td>' . htmlspecialchars($application['email']) . '</td>
                </tr>
                <tr>
                    <th>Phone</th>
                    <td>' . htmlspecialchars($application['phone'] ?? 'N/A') . '</td>
                </tr>
            </table>
        </div>
        
        <div class="section">
            <h2>Course Information</h2>
            <table>
                <tr>
                    <th>Course</th>
                    <td>' . htmlspecialchars($application['course_title']) . '</td>
                </tr>
                <tr>
                    <th>Price</th>
                    <td>₹' . number_format(($application['discount_price'] > 0 && $application['discount_price'] < $application['price']) ? 
                           $application['discount_price'] : $application['price'], 2) . '</td>
                </tr>
            </table>
        </div>
        
        <p>Please install mPDF for better PDF formatting.</p>
    </body>
    </html>';
}
?>