<?php
include_once 'config/database.php';
include_once 'models/Student.php';

$database = new Database();
$db = $database->getConnection();

$student = new Student($db);

// Get student ID from query parameter
$certificate_id = $_GET['id'] ?? $_GET['certificate_id'] ?? null;

if ($certificate_id) {
    // Find student by certificate ID
    $query = "SELECT s.*, c.certificate_type, c.issue_date, c.qr_code_path 
              FROM students s 
              LEFT JOIN certificates c ON s.id = c.student_id 
              WHERE s.certificate_id = ? OR s.id = ? 
              LIMIT 1";
    $stmt = $db->prepare($query);
    $stmt->execute([$certificate_id, $certificate_id]);
    $student_data = $stmt->fetch(PDO::FETCH_ASSOC);
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verify Certificate - Gateway ICT Polytechnic Saapade</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #1a237e, #283593);
            color: white;
            min-height: 100vh;
            padding: 20px;
        }
        .verification-card {
            background: white;
            color: #333;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.3);
            overflow: hidden;
        }
        .header-section {
            background: #1a237e;
            color: white;
            padding: 30px;
            text-align: center;
        }
        .student-photo-large {
            width: 200px;
            height: 250px;
            border: 5px solid #1a237e;
            border-radius: 10px;
            object-fit: cover;
            margin: 0 auto;
        }
        .status-valid {
            background: #4CAF50;
            color: white;
            padding: 10px 20px;
            border-radius: 25px;
            font-weight: bold;
        }
        .status-invalid {
            background: #f44336;
            color: white;
            padding: 10px 20px;
            border-radius: 25px;
            font-weight: bold;
        }
        .info-card {
            border-left: 4px solid #1a237e;
            background: #f8f9fa;
            padding: 15px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-10">
                <div class="verification-card">
                    <div class="header-section">
                        <h1>Gateway ICT Polytechnic Saapade</h1>
                        <p class="lead">Certificate Verification System</p>
                    </div>
                    
                    <div class="p-5">
                        <?php if ($student_data): ?>
                            <div class="row">
                                <div class="col-md-4 text-center">
                                    <img src="<?php echo $student_data['photo_path'] ?: 'https://via.placeholder.com/200x250/1a237e/ffffff?text=No+Photo'; ?>" 
                                         alt="<?php echo $student_data['full_name']; ?>" 
                                         class="student-photo-large mb-3">
                                    <div class="status-valid d-inline-block">
                                        <i class="fas fa-check-circle me-2"></i>VALID CERTIFICATE
                                    </div>
                                </div>
                                <div class="col-md-8">
                                    <h2 class="text-primary"><?php echo $student_data['full_name']; ?></h2>
                                    
                                    <div class="info-card">
                                        <strong>Student ID:</strong> <?php echo $student_data['student_id']; ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Program:</strong> <?php echo $student_data['program']; ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Department:</strong> <?php echo $student_data['department']; ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Grade:</strong> <?php echo $student_data['grade']; ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Graduation Date:</strong> <?php echo date('F j, Y', strtotime($student_data['graduation_date'])); ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Certificate ID:</strong> <?php echo $student_data['certificate_id']; ?>
                                    </div>
                                    
                                    <div class="info-card">
                                        <strong>Certificate Type:</strong> <?php echo $student_data['certificate_type'] ?: 'National Diploma'; ?>
                                    </div>
                                    
                                    <div class="mt-4">
                                        <small class="text-muted">
                                            <i class="fas fa-info-circle me-1"></i>
                                            This certificate was verified on <?php echo date('F j, Y \a\t g:i A'); ?>
                                        </small>
                                    </div>
                                </div>
                            </div>
                        <?php else: ?>
                            <div class="text-center py-5">
                                <div class="status-invalid d-inline-block mb-3">
                                    <i class="fas fa-times-circle me-2"></i>INVALID CERTIFICATE
                                </div>
                                <h3 class="text-danger">Certificate Not Found</h3>
                                <p>The certificate ID "<?php echo htmlspecialchars($certificate_id); ?>" could not be found in our records.</p>
                                <p>Please check the certificate ID and try again, or contact the institution for verification.</p>
                                <a href="index.php" class="btn btn-primary mt-3">
                                    <i class="fas fa-home me-2"></i>Return to Home
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="text-center mt-4">
                    <small class="text-light">
                        &copy; <?php echo date('Y'); ?> Gateway ICT Polytechnic Saapade. All rights reserved.
                    </small>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/js/all.min.js"></script>
</body>
</html>