<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Certificate Verification</title>
    <link rel="icon" type="image/x-icon" href="logo.png">
    <style>
        .logo-container {
            text-align: center;
            margin-bottom: 1.5rem;
        }
        .logo {
            height: 80px;
            margin-bottom: 1rem;
        }
        .certificate-verify-container {
            max-width: 500px;
            margin: 2rem auto;
            padding: 2rem;
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.08);
            border: 1px solid #e0e0e0;
        }
        .form-title {
            text-align: center;
            color: #008000;
            margin-bottom: 1.5rem;
        }
        .form-group {
            margin-bottom: 1.5rem;
        }
        .form-label {
            display: block;
            margin-bottom: 0.5rem;
            font-weight: 600;
            color: #333;
        }
        .form-input {
            width: 100%;
            padding: 0.75rem;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 1rem;
            transition: border-color 0.3s;
        }
        .form-input:focus {
            border-color: #3498db;
            outline: none;
        }
        .form-hint {
            display: block;
            margin-top: 0.5rem;
            color: #666;
            font-size: 0.85rem;
        }
        .submit-btn {
            width: 100%;
            padding: 0.75rem;
            background-color: #008000;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 1rem;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .submit-btn:hover {
            background-color: #009000;
        }
    </style>
    <!-- SweetAlert2 CDN -->
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body>

    <div class="certificate-verify-container">
        <div class="logo-container">
            <img src="logo.png" alt="St John Ambulance Logo" class="logo">
        </div>
        <h2 class="form-title">Certificate Verification</h2>
        <form method="POST" class="certificate-form">
            <div class="form-group">
                <label for="refcode" class="form-label">Certificate Serial Number</label>
                <input type="text" id="refcode" name="refcode" class="form-input" placeholder="Enter your certificate number" required>
                <small class="form-hint">Enter the 12-digit code found on your certificate</small>
            </div>
            <button type="submit" class="submit-btn">Verify Certificate</button>
        </form>
    </div>

    
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $refcode = $_POST['refcode'];

    // API Endpoint (Update with your actual domain)
    $apiUrl = "https://st.briananikayi.com/api/verifycert/" . urlencode($refcode);

    // Initialize cURL
    $ch = curl_init($apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $result = json_decode($response, true);

    if ($status === 200 && $result['status'] === 'success') {
        $name = htmlspecialchars($result['data']['full_names'] ?? 'N/A');
        $validity = htmlspecialchars($result['data']['experydate'] ?? 'N/A');
        $validity = isset($result['data']['experydate'])? date('d-m-Y', strtotime($result['data']['experydate'])) : 'N/A';
        $course = htmlspecialchars($result['data']['course_name'] ?? 'N/A');
        echo "<script>
            Swal.fire({
                icon: 'success',
                title: 'Certificate is Valid',
                html: '<b>Name:</b> {$name}<br><b>Course:</b> {$course}<br><b>Valid till</b> {$validity}'
            });
        </script>";
    } elseif ($status === 403 && $result['status'] === 'expired') {
        echo "<script>
            Swal.fire({
                icon: 'warning',
                title: 'Certificate Expired',
                text: 'The certificate exists but is no longer valid.'
            });
        </script>";
    } else {
        echo "<script>
            Swal.fire({
                icon: 'error',
                title: 'Not Found',
                text: 'Certificate with that serial number was not found.'
            });
        </script>";
    }
}
?>

    </body>
</html>