<?php
// verify_certificate.php

// Start session if needed
session_start();

// Set content type
header('Content-Type: text/html; charset=utf-8');

// Function to safely get GET parameters
function getParam($paramName, $default = '') {
    return isset($_GET[$paramName]) ? htmlspecialchars(urldecode($_GET[$paramName]), ENT_QUOTES, 'UTF-8') : $default;
}

// Get parameters with fallbacks
$full_name = getParam('full_name', '');
$matric_no = getParam('matric_no', '');
$program = getParam('program', '');
$certificate_id = getParam('certificate_id', '');
$grade = getParam('grade', '');
$certificate_type = getParam('certificate_type', 'National Diploma');
$student_id = getParam('student_id', '');
$graduation_date = getParam('graduation_date', '');

// Alternative: Check for base64 encoded data
if (empty($full_name) && isset($_GET['data'])) {
    $encodedData = $_GET['data'];
    $decodedData = base64_decode($encodedData);
    
    if ($decodedData !== false) {
        $verificationData = json_decode($decodedData, true);
        if (is_array($verificationData)) {
            $full_name = isset($verificationData['full_name']) ? htmlspecialchars($verificationData['full_name'], ENT_QUOTES, 'UTF-8') : '';
            $matric_no = isset($verificationData['matric_no']) ? htmlspecialchars($verificationData['matric_no'], ENT_QUOTES, 'UTF-8') : '';
            $program = isset($verificationData['program']) ? htmlspecialchars($verificationData['program'], ENT_QUOTES, 'UTF-8') : '';
            $certificate_id = isset($verificationData['certificate_id']) ? htmlspecialchars($verificationData['certificate_id'], ENT_QUOTES, 'UTF-8') : '';
            $grade = isset($verificationData['grade']) ? htmlspecialchars($verificationData['grade'], ENT_QUOTES, 'UTF-8') : '';
            $certificate_type = isset($verificationData['certificate_type']) ? htmlspecialchars($verificationData['certificate_type'], ENT_QUOTES, 'UTF-8') : 'National Diploma';
            $student_id = isset($verificationData['student_id']) ? htmlspecialchars($verificationData['student_id'], ENT_QUOTES, 'UTF-8') : '';
            $graduation_date = isset($verificationData['graduation_date']) ? htmlspecialchars($verificationData['graduation_date'], ENT_QUOTES, 'UTF-8') : '';
        }
    }
}

// Handle missing parameters - redirect to main verification page
if (empty($full_name) && empty($certificate_id)) {
    // Check if this is a direct access (no parameters)
    if (empty($_GET)) {
        header('Location: https://www.appdeveloper.net.ng/certificate_verify/');
        exit();
    }
    // If some parameters exist but critical ones are missing, show error
    $hasIncompleteData = !empty($_GET) && (empty($full_name) || empty($certificate_id));
}

// More details link
$moreDetailsLink = "https://www.appdeveloper.net.ng/certificate_verify/";

// Get current URL for QR code
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$currentUrl = $protocol . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$qrUrl = "https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=" . urlencode($currentUrl);

// Format graduation date if available
$formatted_graduation_date = '';
if (!empty($graduation_date) && $graduation_date !== '0000-00-00') {
    $date = DateTime::createFromFormat('Y-m-d', $graduation_date);
    if ($date) {
        $formatted_graduation_date = $date->format('F j, Y');
    }
}

// Set page title
$pageTitle = empty($full_name) ? "Certificate Verification page" : "Certificate Verification - " . $full_name;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - Gateway Polytechnic Saapade</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .verification-card {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
            max-width: 1000px;
            width: 100%;
            animation: slideUp 0.6s ease-out;
        }
        
        .header-section {
            background: linear-gradient(135deg, #1a237e 0%, #283593 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .institution-logo {
            height: 80px;
            margin-bottom: 15px;
            filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
        }
        
        .content-section {
            padding: 40px;
        }
        
        .verified-badge {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            padding: 10px 25px;
            border-radius: 50px;
            display: inline-block;
            margin-bottom: 20px;
            font-weight: bold;
            box-shadow: 0 4px 15px rgba(40, 167, 69, 0.3);
        }
        
        .details-section {
            background: #f8f9fa;
            padding: 30px;
            border-radius: 15px;
            margin-bottom: 30px;
            border: 2px solid #e9ecef;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        
        .details-section h4 {
            color: #1a237e;
            border-bottom: 3px solid #1a237e;
            padding-bottom: 12px;
            margin-bottom: 25px;
            font-weight: 700;
        }
        
        .details-table {
            width: 100%;
            margin: 20px 0;
        }
        
        .details-table tr {
            border-bottom: 1px solid #dee2e6;
            transition: background-color 0.2s;
        }
        
        .details-table tr:hover {
            background-color: rgba(26, 35, 126, 0.03);
        }
        
        .details-table td {
            padding: 16px 12px;
            vertical-align: middle;
        }
        
        .details-table td:first-child {
            font-weight: 600;
            color: #1a237e;
            width: 35%;
            min-width: 200px;
            font-size: 16px;
        }
        
        .details-table td:last-child {
            color: #2c3e50;
            font-size: 16px;
            font-weight: 500;
        }
        
        .qr-section {
            text-align: center;
            margin: 30px 0;
            padding: 30px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 8px 25px rgba(0,0,0,0.08);
            border: 3px solid #1a237e;
        }
        
        .qr-code {
            width: 200px;
            height: 200px;
            margin: 15px auto;
            border: 8px solid white;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            border-radius: 10px;
            transition: transform 0.3s;
        }
        
        .qr-code:hover {
            transform: scale(1.05);
        }
        
        .more-details-section {
            text-align: center;
            margin: 40px 0 20px;
            padding: 30px;
            background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
            border-radius: 15px;
            border: 3px solid #1a237e;
            box-shadow: 0 10px 30px rgba(26, 35, 126, 0.15);
        }
        
        .link-container {
            max-width: 700px;
            margin: 25px auto;
        }
        
        .more-details-link {
            background: linear-gradient(135deg, #1a237e 0%, #283593 100%);
            color: white;
            text-decoration: none;
            border-radius: 12px;
            transition: all 0.3s;
            display: flex;
            align-items: center;
            padding: 22px 35px;
            gap: 20px;
            border: 2px solid rgba(255,255,255,0.2);
            text-align: left;
        }
        
        .more-details-link:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(26, 35, 126, 0.3);
            color: white;
            text-decoration: none;
        }
        
        .more-details-link i {
            font-size: 36px;
            background: rgba(255,255,255,0.1);
            padding: 18px;
            border-radius: 10px;
        }
        
        .more-details-link .link-text {
            flex-grow: 1;
        }
        
        .more-details-link .link-text div:first-child {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .more-details-link .link-text small {
            font-size: 14px;
            opacity: 0.9;
        }
        
        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        @keyframes fadeIn {
            from { opacity: 0; }
            to { opacity: 1; }
        }
        
        .fade-in {
            animation: fadeIn 0.8s ease-out;
        }
        
        .alert-warning-custom {
            background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%);
            border: 2px solid #ffc107;
            border-radius: 10px;
            padding: 20px;
        }
        
        @media (max-width: 768px) {
            .content-section {
                padding: 20px;
            }
            
            .more-details-link {
                flex-direction: column;
                text-align: center;
                padding: 20px;
                gap: 15px;
            }
            
            .more-details-link .link-text {
                text-align: center;
            }
            
            .more-details-link i {
                font-size: 30px;
                padding: 15px;
            }
            
            .qr-code {
                width: 180px;
                height: 180px;
            }
            
            .details-table td:first-child {
                width: 40%;
                min-width: 160px;
                font-size: 15px;
            }
            
            .details-table td {
                padding: 12px 8px;
            }
        }
    </style>
</head>
<body>
    <div class="verification-card">
        <div class="header-section">
            <img src="https://www.appdeveloper.net.ng/assets/NewLogo.svg" 
                 alt="Gateway Polytechnic Logo" 
                 class="institution-logo" 
                 onerror="this.src='https://via.placeholder.com/80x80/ffffff/1a237e?text=GIPS'">
            <h1 class="mb-2">Gateway (ICT) Polytechnic Saapade</h1>
            <p class="mb-0">Ogun State, Nigeria</p>
            <p class="mb-0 mt-2"><small><i class="fas fa-shield-alt me-1"></i>Official Certificate Verification System</small></p>
        </div>
        
        <div class="content-section">
            <?php if (isset($hasIncompleteData) && $hasIncompleteData): ?>
                <!-- Show error if incomplete data -->
                <div class="alert alert-danger fade-in">
                    <h4><i class="fas fa-exclamation-triangle me-2"></i>Incomplete Certificate Data</h4>
                    <p>The QR code contains incomplete certificate information. Please ensure you have scanned a valid certificate QR code.</p>
                    <p class="mb-0">Error: Missing critical certificate data (Name or Certificate ID)</p>
                    <hr>
                    <a href="<?php echo $moreDetailsLink; ?>" class="btn btn-primary mt-2">
                        <i class="fas fa-search me-2"></i>Go to Manual Verification
                    </a>
                </div>
            <?php elseif (empty($full_name) && empty($certificate_id)): ?>
                <!-- Show welcome message if no data -->
                <div class="text-center fade-in">
                    <div class="verified-badge">
                        <i class="fas fa-certificate me-2"></i>CERTIFICATE VERIFICATION PORTAL
                    </div>
                    <h2 class="mb-4">Welcome to Certificate Verification</h2>
                    <p class="text-muted mb-4">Scan a certificate QR code or use the link below for manual verification</p>
                    
                    <div class="more-details-section">
                        <h4 class="mb-4"><i class="fas fa-search me-2"></i>Manual Certificate Verification</h4>
                        <p class="mb-4 fs-5">Click the link below to verify certificates manually using certificate number or student details:</p>
                        
                        <div class="link-container">
                            <a href="<?php echo $moreDetailsLink; ?>" 
                               class="more-details-link" 
                               target="_blank">
                                <i class="fas fa-external-link-alt"></i>
                                <div class="link-text">
                                    <div>Certificate Verification Portal</div>
                                    <small>www.appdeveloper.net.ng/certificate_verify/</small>
                                </div>
                                <i class="fas fa-arrow-right"></i>
                            </a>
                        </div>
                        
                        <div class="alert alert-warning-custom mt-4">
                            <i class="fas fa-info-circle me-2"></i>
                            <span class="fw-bold">Note:</span> This portal is for verifying official certificates issued by Gateway (ICT) Polytechnic Saapade.
                        </div>
                    </div>
                </div>
            <?php else: ?>
                <!-- Show certificate data if available -->
                <div class="text-center">
                    <div class="verified-badge">
                        <i class="fas fa-shield-check me-2"></i>OFFICIAL CERTIFICATE VERIFIED
                    </div>
                    <h2 class="mb-3">Certificate Verified Successfully</h2>
                    <p class="text-muted mb-4">This certificate has been verified by Gateway (ICT) Polytechnic Saapade</p>
                </div>
                
                <!-- QR Code Section -->
                <div class="qr-section fade-in">
                    <h4><i class="fas fa-qrcode me-2"></i>Verification QR Code</h4>
                    <img src="<?php echo $qrUrl; ?>" alt="Verification QR Code" class="qr-code img-fluid">
                    <p class="mt-3"><small>Scan this QR code to view this verification result</small></p>
                </div>
                
                <!-- Student Details Section -->
                <div class="details-section fade-in">
                    <h4><i class="fas fa-user-graduate me-2"></i>Certificate Holder Details</h4>
                    <table class="details-table">
                        <tr>
                            <td>Full Name:</td>
                            <td id="studentName" class="fw-bold text-primary fs-5"><?php echo $full_name ?: 'Not Available'; ?></td>
                        </tr>
                        <tr>
                            <td>Matriculation Number:</td>
                            <td id="matricNumber"><?php echo $matric_no ?: 'Not Available'; ?></td>
                        </tr>
                        <tr>
                            <td>Program of Study:</td>
                            <td id="programName"><?php echo $program ?: 'Not Available'; ?></td>
                        </tr>
                        <tr>
                            <td>Certificate Type:</td>
                            <td id="certificateType"><?php echo $certificate_type; ?></td>
                        </tr>
                        <tr>
                            <td>Certificate Number:</td>
                            <td id="certificateNumber" class="fw-bold"><?php echo $certificate_id ?: 'Not Available'; ?></td>
                        </tr>
                        <tr>
                            <td>Grade Awarded:</td>
                            <td id="gradeAwarded"><?php echo $grade ?: 'Not Available'; ?></td>
                        </tr>
                        <?php if (!empty($formatted_graduation_date)): ?>
                        <tr>
                            <td>Graduation Date:</td>
                            <td id="graduationDate"><?php echo $formatted_graduation_date; ?></td>
                        </tr>
                        <?php endif; ?>
                        <tr>
                            <td>Student ID:</td>
                            <td id="studentID"><?php echo $student_id ?: 'Not Available'; ?></td>
                        </tr>
                        <tr>
                            <td>Verification Status:</td>
                            <td><span class="badge bg-success fs-6 p-2"><i class="fas fa-check-circle me-1"></i> Verified & Valid</span></td>
                        </tr>
                        <tr>
                            <td>Verification Date:</td>
                            <td id="verificationDate"><?php echo date('F j, Y'); ?></td>
                        </tr>
                        <tr>
                            <td>Verification Time:</td>
                            <td id="verificationTime"><?php echo date('g:i A'); ?></td>
                        </tr>
                    </table>
                    
                    <!-- More Details Link -->
                    <div class="more-details-section">
                        <h4 class="mb-4"><i class="fas fa-external-link-alt me-2"></i>Complete Certificate Details</h4>
                        <p class="mb-4 fs-5">For the complete official certificate with signatures and official seals:</p>
                        
                        <div class="link-container">
                            <a href="<?php echo $moreDetailsLink; ?>" 
                               class="more-details-link" 
                               target="_blank">
                                <i class="fas fa-certificate"></i>
                                <div class="link-text">
                                    <div>View Complete Certificate</div>
                                    <small>www.appdeveloper.net.ng/certificate_verify/</small>
                                </div>
                                <i class="fas fa-external-link-alt"></i>
                            </a>
                        </div>
                        
                        <div class="alert alert-warning-custom mt-4">
                            <i class="fas fa-info-circle me-2"></i>
                            <span class="fw-bold">Important:</span> The link above will open the official certificate portal with full details including registrar's signature and institutional seal.
                        </div>
                    </div>
                </div>
            <?php endif; ?>
            
            <!-- Action Buttons -->
            <?php if (!empty($full_name) || !empty($certificate_id)): ?>
            <div class="text-center mt-5 fade-in">
                <a href="javascript:window.print()" class="btn btn-outline-primary btn-lg me-3 mb-2">
                    <i class="fas fa-print me-2"></i>Print Verification
                </a>
                <a href="https://www.appdeveloper.net.ng/certificate_verify/" class="btn btn-primary btn-lg mb-2">
                    <i class="fas fa-search me-2"></i>Verify Another
                </a>
                <a href="https://www.appdeveloper.net.ng" class="btn btn-outline-secondary btn-lg mb-2">
                    <i class="fas fa-home me-2"></i>Back to Home
                </a>
            </div>
            <?php endif; ?>
        </div>
        
        <div class="footer-section text-center py-4" style="background: #f8f9fa; border-top: 1px solid #dee2e6;">
            <small class="text-muted">
                <i class="fas fa-shield-alt me-1"></i>Secure Verification System v2.0
                &copy; <?php echo date('Y'); ?> Gateway (ICT) Polytechnic Saapade. All rights reserved.
                <br>
                <i class="fas fa-phone me-1"></i> Contact: 0800-GATEWAY | 
                <i class="fas fa-envelope me-1 ms-2"></i> info@gpsaapade.edu.ng
            </small>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // Add fade-in effect to table rows if they exist
            const tableRows = document.querySelectorAll('.details-table tr');
            tableRows.forEach((row, index) => {
                row.style.opacity = '0';
                row.style.transform = 'translateX(-10px)';
                setTimeout(() => {
                    row.style.transition = 'opacity 0.5s, transform 0.5s';
                    row.style.opacity = '1';
                    row.style.transform = 'translateX(0)';
                }, 100 * index);
            });
            
            // Add click animation to the more details link
            const moreDetailsLinks = document.querySelectorAll('.more-details-link');
            moreDetailsLinks.forEach(link => {
                link.addEventListener('click', function() {
                    const originalHTML = this.innerHTML;
                    this.innerHTML = `
                        <i class="fas fa-spinner fa-spin"></i>
                        <div class="link-text">
                            <div>Redirecting...</div>
                            <small>Opening verification portal</small>
                        </div>
                        <i class="fas fa-spinner fa-spin"></i>
                    `;
                    
                    setTimeout(() => {
                        this.innerHTML = originalHTML;
                    }, 1500);
                });
            });
            
            // Auto-scroll to certificate details if data exists
            if (document.getElementById('studentName') && document.getElementById('studentName').textContent !== 'Not Available') {
                setTimeout(() => {
                    const detailsSection = document.querySelector('.details-section');
                    if (detailsSection) {
                        detailsSection.scrollIntoView({ behavior: 'smooth', block: 'start' });
                    }
                }, 500);
            }
            
            // Print functionality enhancement
            const printBtn = document.querySelector('[href="javascript:window.print()"]');
            if (printBtn) {
                printBtn.addEventListener('click', function() {
                    // Add a small delay to ensure content is ready
                    setTimeout(() => {
                        window.print();
                    }, 300);
                });
            }
        });
    </script>
</body>
</html>