// Add Shortcode for Certificate Verification
function certificate_verification_shortcode() {
    ob_start(); ?>
    <div class="container">
        <h2>Certificate Verification</h2>
        <form id="verification-form">
            <label for="certificate_number">Certificate Number:</label>
            <input type="text" id="certificate_number" name="certificate_number" placeholder="Enter certificate number" required>

            <label for="dob">Date of Birth:</label>
            <input type="date" id="dob" name="dob" required>

            <button type="submit">Verify Certificate</button>
        </form>

        <div id="popup" class="popup">
            <div class="popup-content">
                <span class="close" onclick="closePopup()">&times;</span>
                <div id="popup-message"></div>
            </div>
        </div>
    </div>
    <script>
        document.getElementById("verification-form").addEventListener("submit", async function(event) {
            event.preventDefault();
            const formData = new FormData(event.target);

            const response = await fetch("<?php echo admin_url('admin-ajax.php'); ?>", {
                method: "POST",
                body: new URLSearchParams({
                    action: "verify_certificate",
                    certificate_number: formData.get("certificate_number"),
                    dob: formData.get("dob"),
                }),
            });

            const result = await response.json();
            const popup = document.getElementById("popup");
            const message = document.getElementById("popup-message");

            if (result.success) {
                message.innerHTML = `<strong>Certificate Verified!</strong><br>Name: ${result.name}<br>Course: ${result.course}<br>Issued Date: ${result.issued_date}`;
            } else {
                message.innerHTML = `<span style="color: red;">${result.message}</span>`;
            }
            popup.style.display = "flex";
        });

        function closePopup() {
            document.getElementById("popup").style.display = "none";
        }
    </script>
    <style>
        .container { max-width: 400px; margin: auto; text-align: center; padding: 20px; border: 1px solid #ddd; border-radius: 10px; }
        .popup { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); justify-content: center; align-items: center; }
        .popup-content { background: white; padding: 20px; border-radius: 5px; }
    </style>
    <?php
    return ob_get_clean();
}
add_shortcode('certificate_verification', 'certificate_verification_shortcode');

// AJAX Handler for Certificate Verification
function verify_certificate_ajax_handler() {
    global $wpdb;

    $certificate_number = sanitize_text_field($_POST['certificate_number']);
    $dob = sanitize_text_field($_POST['dob']);
    $table_name = "certificates";

    // Debugging: Log the input data
    error_log("Certificate Number: $certificate_number");
    error_log("Date of Birth: $dob");

    $result = $wpdb->get_row($wpdb->prepare(
        "SELECT * FROM $table_name WHERE certificate_number = %s AND date_of_birth = %s",
        $certificate_number,
        $dob
    ));

    // Debugging: Log the query result
    if ($result) {
        error_log("Query Result: " . print_r($result, true));
        wp_send_json_success([
            'name' => $result->name,
            'course' => $result->course_name,
            'issued_date' => $result->issued_date,
        ]);
    } else {
        error_log("No certificate found for $certificate_number with DOB $dob");
        wp_send_json_error(['message' => 'No certificate found with the provided details.']);

    }
}
add_action('wp_ajax_verify_certificate', 'verify_certificate_ajax_handler');
add_action('wp_ajax_nopriv_verify_certificate', 'verify_certificate_ajax_handler');
