<?php
session_start();
include "../include/config.php"; 

$message = "";
$result = null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $verification_code = trim($_POST['verification_code']);

    if (!empty($verification_code)) {
        // Join certificates with students table to get student info
        $stmt = $conn->prepare("
            SELECT c.*, s.studentname, s.image, s.program, s.intake 
            FROM certificates c
            INNER JOIN students s ON c.studentid = s.studentid
            WHERE c.document_id = ? OR c.security_code = ?
            LIMIT 1
        ");
        $stmt->bind_param("ss", $verification_code, $verification_code);
        $stmt->execute();
        $res = $stmt->get_result();

        if ($res->num_rows > 0) {
            $result = $res->fetch_assoc();
            // Calculate expiry date 21 days from generated_on
            $expiry_date = date("Y-m-d", strtotime($result['generated_on'] . ' +21 days'));
        } else {
            $message = "No certificate found with this code.";
        }
    } else {
        $message = "Please enter a verification code.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Certificate Verification</title>
   <!-- Bootstrap 4 CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">

<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #d03f4d;
            --secondary-color: #146c43;
            --danger-color: #dc3545;
            --light-color: #f7f9fc;
            --dark-color: #343a40;
            --border-radius: 12px;
            --box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            --transition: all 0.3s ease;
        }
        
        body {
            background: var(--light-color);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: var(--dark-color);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }
        
        .container {
            flex: 1;
            padding-top: 2rem;
            padding-bottom: 2rem;
        }
        
        .page-title {
            color: var(--primary-color);
            font-weight: 700;
            margin-bottom: 1.5rem;
            text-align: center;
            font-size: 2.2rem;
        }
        
        .card-verification {
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            border: none;
            overflow: hidden;
            margin-bottom: 2rem;
            transition: var(--transition);
        }
        
        .card-verification:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.15);
        }
        
        .student-image {
            width: 120px;
            height: 120px;
            object-fit: cover;
            border-radius: 50%;
            border: 3px solid var(--primary-color);
            margin: 0 auto 1rem;
            display: block;
        }
        
        .certificate-header {
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: #fff;
            font-weight: bold;
            font-size: 1.2rem;
            text-align: center;
            padding: 15px;
            position: relative;
        }
        
        .certificate-header::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            width: 0;
            height: 0;
            border-left: 10px solid transparent;
            border-right: 10px solid transparent;
            border-top: 10px solid var(--secondary-color);
        }
        
        .info-row {
            margin-bottom: 12px;
            display: flex;
            flex-wrap: wrap;
            border-bottom: 1px dashed #e9ecef;
            padding-bottom: 8px;
        }
        
        .info-label {
            font-weight: 600;
            color: var(--dark-color);
            min-width: 140px;
            margin-right: 10px;
        }
        
        .info-value {
            flex: 1;
            color: #495057;
        }
        
        .expiry-text {
            font-weight: bold;
            color: var(--danger-color);
            background-color: rgba(220, 53, 69, 0.1);
            padding: 8px 12px;
            border-radius: 6px;
            margin-top: 5px;
        }
        
        .verification-form {
            background: white;
            padding: 2rem;
            border-radius: var(--border-radius);
            box-shadow: var(--box-shadow);
            margin-bottom: 2rem;
        }
        
        .form-label {
            font-weight: 600;
            color: var(--dark-color);
            margin-bottom: 0.5rem;
        }
        
        .btn-verify {
            background:  var(--primary-color);
            border: none;
            padding: 12px;
            font-weight: 600;
            font-size: 1.1rem;
            transition: var(--transition);
        }
        
        .btn-verify:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(25, 135, 84, 0.3);
                        background:  var(--primary-color);

        }
        
        .alert {
            border-radius: var(--border-radius);
            border: none;
            font-weight: 500;
        }
        
        .authentic-badge {
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
            padding: 10px 20px;
            border-radius: 50px;
            display: inline-block;
            margin-top: 1rem;
            font-weight: 600;
            box-shadow: 0 4px 10px rgba(40, 167, 69, 0.3);
        }
        
        .footer {
            background-color: var(--dark-color);
            color: white;
            text-align: center;
            padding: 1.5rem 0;
            margin-top: auto;
        }
        
        /* Responsive adjustments */
        @media (max-width: 768px) {
            .page-title {
                font-size: 1.8rem;
            }
            
            .student-image {
                width: 100px;
                height: 100px;
            }
            
            .info-row {
                flex-direction: column;
            }
            
            .info-label {
                min-width: auto;
                margin-bottom: 5px;
            }
            
            .verification-form {
                padding: 1.5rem;
            }
        }
        
        @media (max-width: 576px) {
            .container {
                padding-left: 15px;
                padding-right: 15px;
            }
            
            .page-title {
                font-size: 1.5rem;
            }
            
            .certificate-header {
                font-size: 1rem;
                padding: 12px;
            }
            
            .student-image {
                width: 90px;
                height: 90px;
            }
            
            .btn-verify {
                font-size: 1rem;
                padding: 10px;
            }
            
            .authentic-badge {
                font-size: 0.9rem;
                padding: 8px 16px;
            }
        }
        
        @media (max-width: 400px) {
            .page-title {
                font-size: 1.3rem;
            }
            
            .verification-form {
                padding: 1rem;
            }
            
            .info-label, .info-value {
                font-size: 0.9rem;
            }
        }
        
        /* Animation for results */
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .fade-in {
            animation: fadeIn 0.5s ease forwards;
        }
        
        /* Loading spinner */
        .spinner-border {
            width: 1.5rem;
            height: 1.5rem;
            display: none;
        }
        
        /* Print styles */
        @media print {
            .verification-form, .footer, .btn {
                display: none;
            }
            
            .card-verification {
                box-shadow: none;
                border: 1px solid #ddd;
            }
        }
    </style>
</head>
<body>
    <?php include "../include/nav.php"; ?>
<div class="content">
<br><br>
<div class="container">
    <h1 class="page-title">Fee Clearance Certificate Verification</h1>

    <div class="row justify-content-center">
        <div class="col-lg-8 col-md-10">
            <form method="POST" class="verification-form">
                <div class="mb-3">
                    <label for="verification_code" class="form-label">
                        <i class="fas fa-search me-2"></i>Enter Verification Code or Document ID
                    </label>
                    <input type="text" name="verification_code" id="verification_code" class="form-control form-control-lg" placeholder="e.g. CLR-68D27819AA977" required>
                    <div class="form-text">Enter the unique code found on your certificate</div>
                </div>
                <button type="submit" class="btn btn-verify w-100 text-white">
                    <span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span>
                    Verify Certificate
                </button>
            </form>

            <?php if (!empty($message)): ?>
                <div class="alert alert-danger d-flex align-items-center" role="alert">
                    <i class="fas fa-exclamation-triangle me-2"></i>
                    <div><?= $message ?></div>
                </div>
            <?php endif; ?>

            <?php if ($result): ?>
                <div class="card card-verification fade-in">
                    <div class="certificate-header">
                        <i class="fas fa-certificate me-2"></i>Certificate Verified Successfully
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-4 text-center mb-3 mb-md-0">
                                <?php if (!empty($result['image'])): ?>
                                    <img src="../../students/img/<?= htmlspecialchars($result['image']) ?>" alt="Student Image" class="student-image">
                                <?php else: ?>
                                    <div class="student-image bg-light d-flex align-items-center justify-content-center">
                                        <i class="fas fa-user-graduate fa-3x text-muted"></i>
                                    </div>
                                <?php endif; ?>
                            </div>
                            <div class="col-md-8">
                                <div class="info-row">
                                    <span class="info-label">Student Name:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['studentname']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Student ID:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['studentid']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Program:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['program']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Intake:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['intake']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Document ID:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['document_id']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Security Code:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['security_code']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Generated On:</span>
                                    <span class="info-value"><?= htmlspecialchars($result['generated_on']) ?></span>
                                </div>
                                <div class="info-row">
                                    <span class="info-label">Expires On:</span>
                                    <span class="info-value expiry-text"><?= $expiry_date ?></span>
                                </div>
                                
                                <div class="text-center mt-4">
                                    <div class="authentic-badge">
                                        <i class="fas fa-shield-alt me-2"></i>This certificate is AUTHENTIC
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
               
            <?php endif; ?>
        </div>
    </div>
</div>


<script>
    // Form submission loading indicator
    document.querySelector('form').addEventListener('submit', function() {
        const submitBtn = this.querySelector('button[type="submit"]');
        const spinner = this.querySelector('.spinner-border');
        
        submitBtn.disabled = true;
        spinner.style.display = 'inline-block';
        submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-2" role="status" aria-hidden="true"></span> Verifying...';
    });
    
    // Share functionality
    document.getElementById('shareBtn')?.addEventListener('click', function() {
        if (navigator.share) {
            navigator.share({
                title: 'Certificate Verification Result',
                text: 'I verified my certificate using the Certificate Verification System.',
                url: window.location.href
            })
            .catch(error => console.log('Error sharing:', error));
        } else {
            // Fallback for browsers that don't support the Web Share API
            alert('Share this page URL: ' + window.location.href);
        }
    });
    
    // Auto-focus on input field
    document.getElementById('verification_code').focus();
</script>
</body>
</html>