<?php
session_start();
require_once '../config/database.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
header('Location: ../login.php');
exit;
}
$user_id = $_SESSION['user_id'];
// Check if user has an active application
$stmt = $conn->prepare("SELECT id, course_id, status FROM enrollment_applications WHERE user_id = ? AND status IN ('pending', 'document_verification', 'payment_pending') ORDER BY created_at DESC LIMIT 1");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
// No active application found
header('Location: index.php?error=no_active_application');
exit;
}
$application = $result->fetch_assoc();
$application_id = $application['id'];
$course_id = $application['course_id'];
$application_status = $application['status'];
// Get course details
$stmt = $conn->prepare("SELECT title FROM courses WHERE id = ?");
$stmt->bind_param("i", $course_id);
$stmt->execute();
$course_result = $stmt->get_result();
$course = $course_result->fetch_assoc();
// Get user details
$stmt = $conn->prepare("SELECT first_name, last_name, email FROM users WHERE id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user_result = $stmt->get_result();
$user = $user_result->fetch_assoc();
// Get uploaded documents
$stmt = $conn->prepare("SELECT document_type, status, file_path, updated_at FROM student_documents WHERE user_id = ? AND status != 'rejected' ORDER BY updated_at DESC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$documents_result = $stmt->get_result();
$uploaded_documents = [];
while ($doc = $documents_result->fetch_assoc()) {
$uploaded_documents[$doc['document_type']] = $doc;
}
// Define required documents
$required_documents = [
'id_proof' => 'ID Proof (National ID/Passport)',
'educational_certificate' => 'Educational Certificate',
'photo' => 'Recent Passport Size Photo',
'address_proof' => 'Address Proof'
];
// Calculate document completion percentage
$uploaded_count = count($uploaded_documents);
$total_documents = count($required_documents);
$completion_percentage = ($total_documents > 0) ? round(($uploaded_count / $total_documents) * 100) : 0;
// Get page title
$page_title = "Document Upload";
include_once '../includes/header.php';
?>
<div class="container my-5">
<div class="row">
<div class="col-md-8 mx-auto">
<div class="card shadow">
<div class="card-header bg-primary text-white">
<h4 class="mb-0">Document Upload</h4>
</div>
<div class="card-body">
<div class="alert alert-info">
<p><strong>Application for:</strong> <?php echo htmlspecialchars($course['title']); ?></p>
<p><strong>Status:</strong>
<span class="badge bg-<?php
echo ($application_status == 'pending') ? 'warning' :
(($application_status == 'document_verification') ? 'info' :
(($application_status == 'payment_pending') ? 'primary' : 'secondary'));
?>">
<?php echo ucfirst(str_replace('_', ' ', $application_status)); ?>
</span>
</p>
<p class="mb-0"><strong>Application ID:</strong> <?php echo $application_id; ?></p>
</div>
<div class="progress mt-4 mb-3">
<div class="progress-bar progress-bar-striped bg-success" role="progressbar" style="width: <?php echo $completion_percentage; ?>%"
aria-valuenow="<?php echo $completion_percentage; ?>" aria-valuemin="0" aria-valuemax="100">
<?php echo $completion_percentage; ?>%
</div>
</div>
<p class="text-center mb-4"><strong>Document Completion: </strong> <?php echo $uploaded_count; ?> of <?php echo $total_documents; ?> required documents uploaded</p>
<div class="row">
<div class="col-md-12">
<div class="alert alert-warning">
<i class="fas fa-exclamation-triangle"></i> Please upload all required documents to proceed with your application. All documents must be verified by the admin.
</div>
</div>
</div>
<div class="accordion" id="documentsAccordion">
<?php foreach ($required_documents as $doc_type => $doc_name): ?>
<div class="accordion-item">
<h2 class="accordion-header" id="heading_<?php echo $doc_type; ?>">
<button class="accordion-button <?php echo isset($uploaded_documents[$doc_type]) ? '' : 'collapsed'; ?>" type="button"
data-bs-toggle="collapse" data-bs-target="#collapse_<?php echo $doc_type; ?>"
aria-expanded="<?php echo isset($uploaded_documents[$doc_type]) ? 'true' : 'false'; ?>"
aria-controls="collapse_<?php echo $doc_type; ?>">
<?php echo $doc_name; ?>
<?php if (isset($uploaded_documents[$doc_type])): ?>
<span class="ms-auto badge bg-<?php
echo ($uploaded_documents[$doc_type]['status'] == 'pending') ? 'warning' :
(($uploaded_documents[$doc_type]['status'] == 'verified') ? 'success' : 'danger');
?>">
<?php echo ucfirst($uploaded_documents[$doc_type]['status']); ?>
</span>
<?php else: ?>
<span class="ms-auto badge bg-secondary">Not Uploaded</span>
<?php endif; ?>
</button>
</h2>
<div id="collapse_<?php echo $doc_type; ?>" class="accordion-collapse collapse <?php echo isset($uploaded_documents[$doc_type]) ? 'show' : ''; ?>"
aria-labelledby="heading_<?php echo $doc_type; ?>" data-bs-parent="#documentsAccordion">
<div class="accordion-body">
<?php if (isset($uploaded_documents[$doc_type])): ?>
<div class="mb-3">
<p><strong>Uploaded on:</strong> <?php echo date('F j, Y, g:i a', strtotime($uploaded_documents[$doc_type]['updated_at'])); ?></p>
<?php if (preg_match('/\.(jpg|jpeg|png|gif)$/i', $uploaded_documents[$doc_type]['file_path'])): ?>
<div class="text-center mb-3">
<img src="../<?php echo htmlspecialchars($uploaded_documents[$doc_type]['file_path']); ?>" class="img-fluid img-thumbnail" style="max-height: 200px;">
</div>
<?php else: ?>
<div class="text-center mb-3">
<a href="../<?php echo htmlspecialchars($uploaded_documents[$doc_type]['file_path']); ?>" target="_blank" class="btn btn-sm btn-outline-primary">
<i class="fas fa-file-pdf"></i> View Document
</a>
</div>
<?php endif; ?>
<?php if ($uploaded_documents[$doc_type]['status'] != 'verified'): ?>
<form class="document-upload-form" data-document-type="<?php echo $doc_type; ?>">
<div class="mb-3">
<label for="document_file_<?php echo $doc_type; ?>" class="form-label">Upload new version</label>
<input class="form-control" type="file" id="document_file_<?php echo $doc_type; ?>" name="document_file" accept=".pdf,.jpg,.jpeg,.png" required>
<div class="form-text">Accepted formats: PDF, JPG, JPEG, PNG. Max size: 5MB</div>
</div>
<button type="submit" class="btn btn-primary">Replace Document</button>
</form>
<?php else: ?>
<div class="alert alert-success">
<i class="fas fa-check-circle"></i> This document has been verified and cannot be modified.
</div>
<?php endif; ?>
</div>
<?php else: ?>
<form class="document-upload-form" data-document-type="<?php echo $doc_type; ?>">
<div class="mb-3">
<label for="document_file_<?php echo $doc_type; ?>" class="form-label">Upload document</label>
<input class="form-control" type="file" id="document_file_<?php echo $doc_type; ?>" name="document_file" accept=".pdf,.jpg,.jpeg,.png" required>
<div class="form-text">Accepted formats: PDF, JPG, JPEG, PNG. Max size: 5MB</div>
</div>
<button type="submit" class="btn btn-primary">Upload Document</button>
</form>
<?php endif; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="mt-4 text-center">
<?php if ($completion_percentage == 100 && $application_status == 'document_verification'): ?>
<p class="text-success"><i class="fas fa-check-circle"></i> All documents have been uploaded. Please wait for admin verification.</p>
<?php elseif ($application_status == 'payment_pending'): ?>
<a href="payment.php" class="btn btn-success">Proceed to Payment</a>
<?php endif; ?>
<a href="index.php" class="btn btn-secondary">Back to Dashboard</a>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const documentForms = document.querySelectorAll('.document-upload-form');
documentForms.forEach(form => {
form.addEventListener('submit', function(e) {
e.preventDefault();
const documentType = this.getAttribute('data-document-type');
const fileInput = this.querySelector('input[type="file"]');
if (!fileInput.files.length) {
alert('Please select a file to upload');
return;
}
const formData = new FormData();
formData.append('document_type', documentType);
formData.append('document_file', fileInput.files[0]);
// Show loading state
const submitButton = this.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
submitButton.disabled = true;
submitButton.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Uploading...';
fetch('ajax/upload_document.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Show success message
alert('Document uploaded successfully!');
// Reload page to show updated document status
window.location.reload();
} else {
// Show error message
alert('Upload failed: ' + data.message);
// Reset button
submitButton.disabled = false;
submitButton.textContent = originalText;
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred. Please try again.');
// Reset button
submitButton.disabled = false;
submitButton.textContent = originalText;
});
});
});
// Check document status periodically when documents are all uploaded
<?php if ($completion_percentage == 100 && $application_status == 'document_verification'): ?>
function checkDocumentStatus() {
fetch('ajax/check_document_status.php?user_id=<?php echo $user_id; ?>')
.then(response => response.json())
.then(data => {
if (data.success && data.needs_refresh) {
window.location.reload();
}
})
.catch(error => console.error('Error checking status:', error));
}
// Check every 30 seconds
setInterval(checkDocumentStatus, 30000);
<?php endif; ?>
});
</script>
<?php include_once '../includes/footer.php'; ?>