<?php
/**
 * IBVTE - Public Certificate Verification
 */
require_once __DIR__ . '/config.php';
require_once __DIR__ . '/includes/db.php';

$id = intval($_GET['id'] ?? 0);
$db = DB();

$student = $db->fetchOne("
    SELECT s.*, 
           c.institution_name as college_name,
           crs.course_name,
           crs.course_code
    FROM students s
    LEFT JOIN users c ON s.institution_id = c.id
    LEFT JOIN courses crs ON s.course_id = crs.id
    WHERE s.id = ? AND s.result_declared = 1
", [$id]);

if (!$student) {
    $error = "Invalid certificate or verification ID.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Verify Certificate - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <style>
        body { background-color: #f4f7f6; min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
        .verification-card { max-width: 600px; width: 100%; background: white; border-radius: 20px; overflow: hidden; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .header-verify { background: linear-gradient(135deg, #3b0b7c 0%, #6c4ab6 100%); color: white; padding: 40px 20px; text-align: center; }
        .status-badge { display: inline-block; padding: 8px 20px; border-radius: 50px; font-weight: bold; margin-top: 15px; }
        .status-success { background: rgba(255,255,255,0.2); color: white; border: 1px solid rgba(255,255,255,0.4); }
        .status-error { background: #fef2f2; color: #b91c1c; border: 1px solid #fecaca; }
        .detail-row { display: flex; justify-content: space-between; padding: 15px 0; border-bottom: 1px solid #f0f0f0; }
        .detail-label { color: #6b7280; font-size: 14px; font-weight: 500; }
        .detail-value { color: #111827; font-weight: 700; text-align: right; }
    </style>
</head>
<body>
    <div class="verification-card">
        <?php if (isset($error)): ?>
            <div class="p-5 text-center">
                <i class="fas fa-exclamation-triangle fa-4x text-danger mb-4"></i>
                <h2 class="fw-bold mb-3">Verification Failed</h2>
                <p class="text-muted"><?= $error ?></p>
                <a href="index.php" class="btn btn-primary mt-3 px-4 rounded-pill">Back to Home</a>
            </div>
        <?php else: ?>
            <div class="header-verify">
                <i class="fas fa-check-circle fa-4x mb-3"></i>
                <h2 class="fw-bold mb-0">Certificate Verified</h2>
                <div class="status-badge status-success">Authentic IBVTE Record</div>
            </div>
            <div class="p-4">
                <div class="detail-row">
                    <span class="detail-label">Student Name</span>
                    <span class="detail-value"><?= htmlspecialchars($student['full_name']) ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Registration Number</span>
                    <span class="detail-value"><?= htmlspecialchars($student['registration_number']) ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Course Name</span>
                    <span class="detail-value"><?= htmlspecialchars($student['course_name']) ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Course Code</span>
                    <span class="detail-value"><?= htmlspecialchars($student['course_code']) ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Institution</span>
                    <span class="detail-value"><?= htmlspecialchars($student['college_name'] ?? 'Direct Admission') ?></span>
                </div>
                <div class="detail-row">
                    <span class="detail-label">Grade / Percentage</span>
                    <span class="detail-value"><?= number_format($student['percentage'], 2) ?>% (<?= ucfirst($student['division']) ?>)</span>
                </div>
                <div class="detail-row border-0">
                    <span class="detail-label">Result Date</span>
                    <span class="detail-value"><?= formatDate($student['result_declared_at']) ?></span>
                </div>
                
                <div class="text-center mt-4">
                    <p class="small text-muted mb-0">This is a system generated record and does not require physical signature.</p>
                    <p class="small text-muted">© <?= date('Y') ?> Indian Board of Vocational and Technical Education</p>
                </div>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>
