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

<?php
// Start session
session_start();

// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    echo '<div class="alert alert-danger">Unauthorized access</div>';
    exit;
}

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

// Check if enrollment ID is provided
if (!isset($_GET['application_id']) || empty($_GET['application_id'])) {
    echo '<div class="alert alert-danger">Invalid request</div>';
    exit;
}

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

// Get user ID for this enrollment
$query = "SELECT user_id FROM enrollments WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo '<div class="alert alert-warning">Enrollment not found or user has no documents</div>';
    exit;
}

$row = $result->fetch_assoc();
$user_id = $row['user_id'];

// Get all documents for this user
$query = "SELECT * FROM student_documents WHERE user_id = ? ORDER BY upload_date DESC";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();

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

// Define status badges
$status_badges = [
    'pending' => 'warning',
    'verified' => 'success',
    'rejected' => 'danger'
];

// Display documents
if ($result && $result->num_rows > 0) {
    echo '<div class="list-group">';
    
    while ($row = $result->fetch_assoc()) {
        $doc_type = $doc_types[$row['document_type']] ?? $row['document_type'];
        $status_badge = $status_badges[$row['status']] ?? 'secondary';
        
        echo '<div class="list-group-item list-group-item-action">';
        echo '<div class="d-flex w-100 justify-content-between">';
        echo '<h6 class="mb-1">' . htmlspecialchars($doc_type) . '</h6>';
        echo '<small>' . date('M d, Y', strtotime($row['upload_date'])) . '</small>';
        echo '</div>';
        
        echo '<div class="mb-1">';
        echo '<span class="badge badge-' . $status_badge . '">' . ucfirst($row['status']) . '</span> ';
        
        // Document preview with file type icon based on extension
        $file_path = isset($row['document_path']) ? $row['document_path'] : (isset($row['file_path']) ? $row['file_path'] : '');
        $file_extension = pathinfo($file_path, PATHINFO_EXTENSION);
        $file_icon = 'far fa-file';
        
        if (in_array(strtolower($file_extension), ['jpg', 'jpeg', 'png', 'gif'])) {
            $file_icon = 'far fa-file-image';
        } elseif (in_array(strtolower($file_extension), ['pdf'])) {
            $file_icon = 'far fa-file-pdf';
        } elseif (in_array(strtolower($file_extension), ['doc', 'docx'])) {
            $file_icon = 'far fa-file-word';
        }
        
        echo '<a href="../' . htmlspecialchars($file_path) . '" target="_blank" class="btn btn-sm btn-info">';
        echo '<i class="' . $file_icon . ' mr-1"></i> View Document</a>';
        
        // Show file name if available
        if (!empty($row['original_filename'])) {
            echo ' <small class="text-muted ml-2">' . htmlspecialchars($row['original_filename']) . '</small>';
        }
        
        echo '</div>';
        
        // Admin notes if available
        if (!empty($row['admin_notes'])) {
            echo '<div class="small bg-light p-2 rounded mt-1"><strong>Admin Notes:</strong> ' . htmlspecialchars($row['admin_notes']) . '</div>';
        }
        
        // Action buttons
        echo '<div class="mt-2 d-flex justify-content-end">';
        if ($row['status'] !== 'verified') {
            echo '<button type="button" class="btn btn-sm btn-success mr-2 verify-doc-btn" data-document-id="' . $row['id'] . '" data-status="verified" data-doc-type="' . htmlspecialchars($doc_type) . '">';
            echo '<i class="fas fa-check-circle mr-1"></i> Verify</button>';
        }
        if ($row['status'] !== 'rejected') {
            echo '<button type="button" class="btn btn-sm btn-danger reject-doc-btn" data-document-id="' . $row['id'] . '" data-status="rejected" data-doc-type="' . htmlspecialchars($doc_type) . '">';
            echo '<i class="fas fa-times-circle mr-1"></i> Reject</button>';
        }
        
        // Reset status button if document has been verified or rejected
        if ($row['status'] !== 'pending') {
            echo '<button type="button" class="btn btn-sm btn-secondary ml-2 reset-doc-btn" data-document-id="' . $row['id'] . '" data-status="pending" data-doc-type="' . htmlspecialchars($doc_type) . '">';
            echo '<i class="fas fa-undo mr-1"></i> Reset</button>';
        }
        
        echo '</div>';
        
        echo '</div>';
    }
    
    echo '</div>';
    
    // Add JavaScript for document verification buttons
    echo '<script>
    $(document).ready(function() {
        $(".verify-doc-btn, .reject-doc-btn, .reset-doc-btn").click(function() {
            var documentId = $(this).data("document-id");
            var status = $(this).data("status");
            var docType = $(this).data("doc-type");
            var btn = $(this);
            var adminNotes = "";
            
            if (status === "rejected") {
                // Prompt for rejection reason
                adminNotes = prompt("Please provide a reason for rejecting this " + docType + ":");
                if (adminNotes === null) return; // User canceled
            }
            
            // Show loading state
            var originalBtnHtml = btn.html();
            btn.html(\'<i class="fas fa-spinner fa-spin"></i> Processing...\');
            btn.prop("disabled", true);
            
            // Send AJAX request
            $.ajax({
                url: "ajax/verify_document.php",
                type: "POST",
                data: {
                    document_id: documentId,
                    status: status,
                    admin_notes: adminNotes
                },
                success: function(response) {
                    if (response === "success") {
                        // Refresh the document list
                        location.reload();
                    } else {
                        alert("Error: " + response);
                        btn.html(originalBtnHtml);
                        btn.prop("disabled", false);
                    }
                },
                error: function() {
                    alert("Server error occurred");
                    btn.html(originalBtnHtml);
                    btn.prop("disabled", false);
                }
            });
        });
    });
    </script>';
} else {
    echo '<div class="alert alert-info">No documents found for this student</div>';
    
    // Add upload document button
    echo '<div class="text-center mt-3">';
    echo '<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#uploadDocumentModal" data-user-id="' . $user_id . '">';
    echo '<i class="fas fa-upload mr-1"></i> Upload Document for this Student';
    echo '</button>';
    echo '</div>';
}
?>