<?php
// Certificate verification page for direct QR code access
include('includes/config.php');

$registration_no = isset($_GET['reg']) ? mysqli_real_escape_string($conn, $_GET['reg']) : '';
$certificate_data = null;
$error_message = '';
$table_name = 'delhi_birth'; // Default table

// Check if it's from CRS system or HHHHH system
if (!empty($registration_no)) {
    // First try delhi_birth table (CRS system)
    $sql = "SELECT *, 'delhi_birth' as source_table FROM delhi_birth WHERE registration_no='$registration_no'";
    $result = mysqli_query($conn, $sql);
    
    if ($result && mysqli_num_rows($result) > 0) {
        $certificate_data = mysqli_fetch_assoc($result);
        $table_name = 'delhi_birth';
    } else {
        // Try birth_certificate table (HHHHH system)
        $sql = "SELECT *, 'birth_certificate' as source_table FROM birth_certificate WHERE registration_no='$registration_no'";
        $result = mysqli_query($conn, $sql);
        
        if ($result && mysqli_num_rows($result) > 0) {
            $certificate_data = mysqli_fetch_assoc($result);
            $table_name = 'birth_certificate';
        } else {
            $error_message = 'Certificate not found with this registration number.';
        }
    }
}

if (isset($_POST['verify'])) {
    $reg_no = mysqli_real_escape_string($conn, trim($_POST['reg_no']));
    header("Location: verify_certificate.php?reg=" . urlencode($reg_no));
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Certificate Verification - Delhi Birth Portal</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body { 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); 
            min-height: 100vh; 
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .verify-container {
            max-width: 900px;
            margin: 30px auto;
            background: #ffffff;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.2);
            overflow: hidden;
        }
        .verify-header {
            background: linear-gradient(135deg, #1e3a8a 0%, #3730a3 100%);
            color: #fff;
            padding: 40px;
            text-align: center;
        }
        .verify-header i { font-size: 60px; margin-bottom: 20px; opacity: 0.9; }
        .verify-header h2 { margin: 0; font-weight: 800; font-size: 28px; }
        .verify-header p { margin: 10px 0 0 0; opacity: 0.9; font-size: 16px; }
        .verify-body { padding: 40px; }
        .search-card {
            background: #f8fafc;
            border-radius: 16px;
            padding: 30px;
            margin-bottom: 30px;
            border: 2px solid #e2e8f0;
        }
        .cert-details {
            background: #ffffff;
            border: 2px solid #e5e7eb;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0,0,0,0.05);
        }
        .verified-badge {
            background: linear-gradient(135deg, #10b981 0%, #059669 100%);
            color: #fff;
            padding: 15px 30px;
            border-radius: 50px;
            font-weight: 800;
            display: inline-flex;
            align-items: center;
            font-size: 16px;
            margin-bottom: 25px;
            box-shadow: 0 4px 15px rgba(16,185,129,0.3);
        }
        .verified-badge i { margin-right: 10px; font-size: 20px; }
        .error-badge {
            background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
            color: #fff;
            padding: 15px 30px;
            border-radius: 50px;
            font-weight: 800;
            display: inline-flex;
            align-items: center;
            font-size: 16px;
            margin-bottom: 25px;
            box-shadow: 0 4px 15px rgba(239,68,68,0.3);
        }
        .error-badge i { margin-right: 10px; font-size: 20px; }
        .detail-row {
            display: flex;
            padding: 15px 0;
            border-bottom: 1px solid #f1f5f9;
        }
        .detail-row:last-child { border-bottom: none; }
        .detail-label {
            font-weight: 700;
            color: #374151;
            font-size: 14px;
            min-width: 180px;
            display: flex;
            align-items: center;
        }
        .detail-label i { margin-right: 8px; color: #6b7280; }
        .detail-value {
            color: #1f2937;
            font-size: 16px;
            font-weight: 600;
            flex: 1;
        }
        .btn-search {
            background: linear-gradient(135deg, #1e3a8a 0%, #3730a3 100%);
            color: #fff;
            border: none;
            padding: 15px 30px;
            border-radius: 12px;
            font-weight: 700;
            font-size: 16px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(30,58,138,0.3);
        }
        .btn-search:hover {
            transform: translateY(-2px);
            color: #fff;
            box-shadow: 0 6px 25px rgba(30,58,138,0.4);
        }
        .form-control {
            padding: 15px;
            font-size: 16px;
            border: 2px solid #e5e7eb;
            border-radius: 12px;
            font-family: 'Courier New', monospace;
            font-weight: 600;
        }
        .form-control:focus {
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59,130,246,0.1);
        }
        .badge-mcd {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%);
            color: white;
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 700;
        }
        .system-badge {
            background: linear-gradient(135deg, #8b5cf6 0%, #7c3aed 100%);
            color: white;
            padding: 6px 12px;
            border-radius: 15px;
            font-size: 11px;
            font-weight: 700;
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <div class="verify-container">
        <div class="verify-header">
            <i class="fas fa-shield-alt"></i>
            <h2>Birth Certificate Verification</h2>
            <p>Municipal Corporation of Delhi - Instant Certificate Verification Portal</p>
        </div>
        
        <div class="verify-body">
            <!-- Search Form -->
            <div class="search-card">
                <form method="post" action="">
                    <h5 style="margin-bottom: 25px; color: #1f2937; font-weight: 700;">
                        <i class="fas fa-search" style="color: #3b82f6;"></i> Enter Registration Number
                    </h5>
                    <div class="input-group">
                        <input type="text" name="reg_no" class="form-control" 
                               placeholder="Enter registration number (e.g. MCDOLIR-0126-1104990)" 
                               value="<?php echo htmlspecialchars($registration_no); ?>" required>
                        <div class="input-group-append">
                            <button type="submit" name="verify" class="btn-search">
                                <i class="fas fa-search"></i> Verify Certificate
                            </button>
                        </div>
                    </div>
                    <small style="color: #6b7280; margin-top: 10px; display: block;">
                        <i class="fas fa-info-circle"></i> You can scan the QR code on the certificate or enter the registration number manually
                    </small>
                </form>
            </div>

            <!-- Results -->
            <?php if (!empty($registration_no)): ?>
                <?php if ($certificate_data): ?>
                    <div class="verified-badge">
                        <i class="fas fa-check-circle"></i> 
                        Certificate Successfully Verified
                        <span class="system-badge"><?php echo strtoupper(str_replace('_', ' ', $table_name)); ?></span>
                    </div>
                    
                    <div class="cert-details">
                        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px; padding-bottom: 20px; border-bottom: 2px solid #e5e7eb;">
                            <h4 style="margin: 0; color: #1e3a8a; font-weight: 800;">
                                <i class="fas fa-certificate"></i> Birth Certificate Details
                            </h4>
                            <span class="badge-mcd">MCD VERIFIED</span>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-hashtag"></i> Registration Number
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['registration_no']); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-user"></i> Full Name
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['name']); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-venus-mars"></i> Gender
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['gender']); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-calendar-alt"></i> Date of Birth
                            </div>
                            <div class="detail-value"><?php echo date('d F Y', strtotime($certificate_data['date_of_birth'])); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-male"></i> Father's Name
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['father_name']); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-female"></i> Mother's Name
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['mother_name']); ?></div>
                        </div>
                        
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-map-marker-alt"></i> Place of Birth
                            </div>
                            <div class="detail-value"><?php echo htmlspecialchars($certificate_data['place_of_birth']); ?></div>
                        </div>
                        
                        <?php if (isset($certificate_data['date_of_registration'])): ?>
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-calendar-check"></i> Registration Date
                            </div>
                            <div class="detail-value"><?php echo date('d F Y', strtotime($certificate_data['date_of_registration'])); ?></div>
                        </div>
                        <?php endif; ?>
                        
                        <?php if (isset($certificate_data['date_of_issue'])): ?>
                        <div class="detail-row">
                            <div class="detail-label">
                                <i class="fas fa-calendar-plus"></i> Issue Date
                            </div>
                            <div class="detail-value"><?php echo date('d F Y', strtotime($certificate_data['date_of_issue'])); ?></div>
                        </div>
                        <?php endif; ?>
                        
                        <div style="margin-top: 30px; padding-top: 25px; border-top: 2px solid #e5e7eb; text-align: center;">
                            <div style="background: #ecfdf5; border: 1px solid #10b981; border-radius: 12px; padding: 20px;">
                                <p style="color: #065f46; margin: 0; font-weight: 600;">
                                    <i class="fas fa-shield-alt" style="color: #10b981;"></i> 
                                    This certificate is authentic and issued by Municipal Corporation of Delhi. 
                                    No physical seal or signature required for verification.
                                </p>
                            </div>
                        </div>
                    </div>
                <?php else: ?>
                    <div class="error-badge">
                        <i class="fas fa-exclamation-triangle"></i> 
                        Certificate Not Found
                    </div>
                    <div style="background: #fef2f2; border: 1px solid #f87171; border-radius: 12px; padding: 20px;">
                        <h6 style="color: #dc2626; margin-bottom: 10px;">
                            <i class="fas fa-times-circle"></i> Verification Failed
                        </h6>
                        <p style="color: #991b1b; margin: 0;"><?php echo htmlspecialchars($error_message); ?></p>
                        <p style="color: #991b1b; margin: 10px 0 0 0; font-size: 14px;">
                            Please check the registration number and try again. Make sure you're using the complete registration number from your certificate.
                        </p>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <!-- Instructions -->
                <div style="background: #fffbeb; border: 1px solid #f59e0b; border-radius: 12px; padding: 25px;">
                    <h6 style="color: #92400e; margin-bottom: 20px; font-weight: 700;">
                        <i class="fas fa-lightbulb"></i> How to Verify Your Certificate
                    </h6>
                    <div class="row">
                        <div class="col-md-6">
                            <ul style="color: #92400e; margin: 0; padding-left: 20px;">
                                <li style="margin-bottom: 8px;">Scan the QR code on your birth certificate</li>
                                <li style="margin-bottom: 8px;">Or manually enter the registration number</li>
                                <li style="margin-bottom: 8px;">Get instant verification results</li>
                            </ul>
                        </div>
                        <div class="col-md-6">
                            <ul style="color: #92400e; margin: 0; padding-left: 20px;">
                                <li style="margin-bottom: 8px;">Available 24/7 for verification</li>
                                <li style="margin-bottom: 8px;">No registration required</li>
                                <li style="margin-bottom: 8px;">Instant and secure verification</li>
                            </ul>
                        </div>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>