<?php
/**
 * VCB-AI Infrastructure Interest Registration
 * Saves leads to MariaDB
 */

// Database configuration
$db_host = 'raven.aserv.co.za';
$db_port = 3306;
$db_name = 'vcbaioh8w6o6_vcb';
$db_user = 'vcbaioh8w6o6_svc';
$db_pass = '@nB6%#Z6!dvKMB';

$message = '';
$messageType = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $company = trim($_POST['company'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $interest = trim($_POST['interest'] ?? '');
    $comments = trim($_POST['comments'] ?? '');
    
    // Validation
    if (empty($name) || empty($email)) {
        $message = 'Please fill in all required fields.';
        $messageType = 'error';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $message = 'Please enter a valid email address.';
        $messageType = 'error';
    } else {
        try {
            // Connect to database
            $pdo = new PDO(
                "mysql:host=$db_host;port=$db_port;dbname=$db_name;charset=utf8mb4",
                $db_user,
                $db_pass,
                [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
            );
            
            // Create table if not exists
            $pdo->exec("CREATE TABLE IF NOT EXISTS infrastructure_leads (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                email VARCHAR(255) NOT NULL,
                company VARCHAR(255),
                phone VARCHAR(50),
                interest VARCHAR(100),
                comments TEXT,
                ip_address VARCHAR(45),
                user_agent TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )");
            
            // Insert lead
            $stmt = $pdo->prepare("INSERT INTO infrastructure_leads (name, email, company, phone, interest, comments, ip_address, user_agent) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([
                $name,
                $email,
                $company,
                $phone,
                $interest,
                $comments,
                $_SERVER['REMOTE_ADDR'] ?? '',
                $_SERVER['HTTP_USER_AGENT'] ?? ''
            ]);

            // --- PHP mail() notification (primary) ---
            $notifyTo = 'hello@vcb-ai.online';
            $mailSubject = '[VCB Interest] ' . ($interest ?: 'General') . ' — from ' . $name;
            $mailBody  = "New Interest Registration\r\n";
            $mailBody .= "=========================\r\n";
            $mailBody .= "Name:     $name\r\n";
            $mailBody .= "Email:    $email\r\n";
            $mailBody .= "Company:  " . ($company ?: 'Not provided') . "\r\n";
            $mailBody .= "Phone:    " . ($phone ?: 'Not provided') . "\r\n";
            $mailBody .= "Interest: " . ($interest ?: 'Not provided') . "\r\n\r\n";
            $mailBody .= "Comments:\r\n$comments\r\n\r\n";
            $mailBody .= "IP: " . ($_SERVER['REMOTE_ADDR'] ?? 'unknown') . "\r\n";
            $mailHeaders  = "From: noreply@vcb-ai.online\r\n";
            $mailHeaders .= "Reply-To: $email\r\n";
            $mailHeaders .= "X-Mailer: PHP/" . phpversion();
            @mail($notifyTo, $mailSubject, $mailBody, $mailHeaders);
            $configStmt = $pdo->prepare("SELECT key_name, key_value FROM config_keys WHERE is_active = 1");
            $configStmt->execute();
            $config = [];
            while ($row = $configStmt->fetch(PDO::FETCH_ASSOC)) {
                $config[$row['key_name']] = $row['key_value'];
            }
            
            if (!empty($config['emailjs_service_id'])) {
                $emailData = [
                    'service_id' => $config['emailjs_service_id'],
                    'template_id' => $config['emailjs_template_id'],
                    'user_id' => $config['emailjs_public_key'],
                    'accessToken' => $config['emailjs_private_key'],
                    'template_params' => [
                        'name' => $name,
                        'email' => $config['admin_email'],
                        'from_name' => $name,
                        'from_email' => $email,
                        'company' => $company ?: 'Not provided',
                        'phone' => $phone ?: 'Not provided',
                        'subject' => 'Infrastructure Interest: ' . ($interest ?: 'General'),
                        'message' => "Interest: $interest\n\nComments: $comments",
                        'form_type' => 'Infrastructure Interest',
                        'reply_to' => $email
                    ]
                ];
                
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, 'https://api.emailjs.com/api/v1.0/email/send');
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($emailData));
                curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_exec($ch);
                curl_close($ch);
            }
            
            $message = 'Thank you for your interest! We\'ll be in touch soon.';
            $messageType = 'success';
            
            // Clear form
            $name = $email = $company = $phone = $interest = $comments = '';
            
        } catch (PDOException $e) {
            $message = 'Sorry, there was an error processing your request. Please try again later.';
            $messageType = 'error';
            // Log error (don't show to user)
            error_log('DB Error: ' . $e->getMessage());
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en" class="scroll-smooth">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register Interest | VCB-AI Sovereign Infrastructure</title>
    <meta name="description" content="Register your interest in VCB-AI's sovereign AI infrastructure solutions for South Africa.">
    <link rel="icon" type="image/png" href="/logoWtext-transparent-BlackBack.png">
    
    <!-- Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Sharp:opsz,wght,FILL,GRAD@24,400,0,0&display=swap" rel="stylesheet">
    
    <!-- Tailwind -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    fontFamily: {
                        'display': ['Quicksand', 'sans-serif'],
                        'body': ['Quicksand', 'sans-serif'],
                    },
                    colors: {
                        'vcb': {
                            '900': '#0a0a0a',
                            '800': '#1a1a1a',
                            '700': '#2a2a2a',
                        },
                        'brand': {
                            'amber': '#f59e0b'
                        }
                    }
                }
            }
        }
    </script>
    <style>
        * { font-family: 'Quicksand', sans-serif; font-weight: 400; }
        
        /* Form inputs - light theme */
        .form-input {
            background: white;
            border: 1px solid #e5e7eb;
            border-radius: 12px;
            padding: 14px 18px;
            color: #1f2937;
            font-size: 16px;
            width: 100%;
            transition: all 0.3s ease;
        }
        
        .form-input:focus {
            outline: none;
            border-color: #f59e0b;
            background: white;
            box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.1);
        }
        
        .form-input::placeholder {
            color: #9ca3af;
        }
        
        .form-select {
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='rgba(107,114,128,0.8)'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 20px;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            padding-right: 40px;
        }
        
        .form-select option {
            background: white;
            color: #1f2937;
        }
        
        /* Text gradient */
        .text-gradient {
            background: linear-gradient(135deg, #f59e0b 0%, #d97706 50%, #f59e0b 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        /* Light grid background */
        .grid-bg {
            background-image: 
                linear-gradient(rgba(0,0,0,0.02) 1px, transparent 1px),
                linear-gradient(90deg, rgba(0,0,0,0.02) 1px, transparent 1px);
            background-size: 50px 50px;
        }
    </style>
</head>
<body class="bg-gray-50 text-gray-900 min-h-screen grid-bg">
    
    <!-- Header -->
    <header class="p-6 sm:p-8 border-b border-gray-200 bg-white">
        <nav class="max-w-4xl mx-auto flex items-center justify-between">
            <a href="/" class="flex items-center gap-3 group">
                <img src="/logo-transparent-WhiteBack.png" alt="VCB Logo" class="h-40 sm:h-64 w-auto">
            </a>
            <a href="/infrastructure.html" class="flex items-center gap-2 px-4 py-2 text-gray-600 hover:text-gray-900 border border-gray-300 hover:border-gray-400 rounded-full transition-all text-sm font-medium">
                <span class="material-symbols-sharp text-lg">arrow_back</span>
                Back
            </a>
        </nav>
    </header>
    
    <!-- Main Content -->
    <main class="flex-1 py-12 px-6">
        <div class="max-w-2xl mx-auto">
            
            <!-- Header -->
            <div class="text-center mb-12">
                <div class="inline-flex items-center justify-center w-16 h-16 rounded-2xl bg-gradient-to-br from-amber-100 to-amber-200 border border-amber-300 mb-6">
                    <span class="material-symbols-sharp text-3xl text-amber-600">rocket_launch</span>
                </div>
                
                <h1 class="text-3xl sm:text-4xl font-bold mb-4">
                    <span class="text-gray-900">Register Your</span>
                    <span class="text-gradient"> Interest</span>
                </h1>
                
                <p class="text-gray-500 max-w-md mx-auto">
                    Be among the first to access VCB-AI's sovereign infrastructure when we launch in South Africa.
                </p>
            </div>
            
            <?php if ($message): ?>
            <!-- Message -->
            <div class="mb-8 p-4 rounded-xl border <?php echo $messageType === 'success' ? 'bg-emerald-500/10 border-emerald-500/30 text-emerald-400' : 'bg-red-500/10 border-red-500/30 text-red-400'; ?>">
                <div class="flex items-center gap-3">
                    <span class="material-symbols-sharp"><?php echo $messageType === 'success' ? 'check_circle' : 'error'; ?></span>
                    <p class="font-medium"><?php echo htmlspecialchars($message); ?></p>
                </div>
            </div>
            <?php endif; ?>
            
            <!-- Form -->
            <form method="POST" class="space-y-6">
                
                <!-- Name & Email -->
                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-2">Full Name *</label>
                        <input type="text" name="name" required 
                               value="<?php echo htmlspecialchars($name ?? ''); ?>"
                               placeholder="John Smith"
                               class="form-input">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-2">Email Address *</label>
                        <input type="email" name="email" required 
                               value="<?php echo htmlspecialchars($email ?? ''); ?>"
                               placeholder="john@company.co.za"
                               class="form-input">
                    </div>
                </div>
                
                <!-- Company & Phone -->
                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-2">Company</label>
                        <input type="text" name="company" 
                               value="<?php echo htmlspecialchars($company ?? ''); ?>"
                               placeholder="Your Company (Pty) Ltd"
                               class="form-input">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-600 mb-2">Phone Number</label>
                        <input type="tel" name="phone" 
                               value="<?php echo htmlspecialchars($phone ?? ''); ?>"
                               placeholder="+27 82 123 4567"
                               class="form-input">
                    </div>
                </div>
                
                <!-- Interest Area -->
                <div>
                    <label class="block text-sm font-medium text-gray-600 mb-2">Area of Interest</label>
                    <select name="interest" class="form-input form-select">
                        <option value="">Select an option...</option>
                        <option value="gpu-compute" <?php echo ($interest ?? '') === 'gpu-compute' ? 'selected' : ''; ?>>GPU Compute / AI Training</option>
                        <option value="inference" <?php echo ($interest ?? '') === 'inference' ? 'selected' : ''; ?>>AI Inference / Production</option>
                        <option value="data-sovereignty" <?php echo ($interest ?? '') === 'data-sovereignty' ? 'selected' : ''; ?>>Data Sovereignty / POPIA</option>
                        <option value="enterprise" <?php echo ($interest ?? '') === 'enterprise' ? 'selected' : ''; ?>>Enterprise AI Solutions</option>
                        <option value="research" <?php echo ($interest ?? '') === 'research' ? 'selected' : ''; ?>>Research / Academia</option>
                        <option value="government" <?php echo ($interest ?? '') === 'government' ? 'selected' : ''; ?>>Government / Public Sector</option>
                        <option value="other" <?php echo ($interest ?? '') === 'other' ? 'selected' : ''; ?>>Other</option>
                    </select>
                </div>
                
                <!-- Comments -->
                <div>
                    <label class="block text-sm font-medium text-gray-600 mb-2">Tell us more about your needs</label>
                    <textarea name="comments" rows="4" 
                              placeholder="Describe your use case, expected workloads, or any questions..."
                              class="form-input resize-none"><?php echo htmlspecialchars($comments ?? ''); ?></textarea>
                </div>
                
                <!-- Submit -->
                <div class="pt-4">
                    <button type="submit" 
                            class="w-full flex items-center justify-center gap-3 px-8 py-4 bg-gradient-to-r from-amber-500 to-amber-600 text-black font-bold uppercase tracking-wider rounded-xl hover:shadow-xl hover:shadow-amber-500/30 transition-all duration-300">
                        <span class="material-symbols-sharp">send</span>
                        Submit Interest
                    </button>
                </div>
                
                <!-- Privacy Note -->
                <p class="text-center text-xs text-gray-500 pt-2">
                    By submitting, you agree to our <a href="/LEGAL.html" class="text-amber-600 hover:text-amber-700 underline">Privacy Policy</a>. 
                    Your data is stored securely in South Africa.
                </p>
            </form>
            
            <!-- Trust Badges -->
            <div class="mt-12 pt-8 border-t border-gray-200">
                <div class="flex flex-wrap items-center justify-center gap-6 text-sm text-gray-500">
                    <div class="flex items-center gap-2">
                        <span class="material-symbols-sharp text-emerald-500 text-lg">verified</span>
                        <span>POPIA Compliant</span>
                    </div>
                    <div class="flex items-center gap-2">
                        <span class="material-symbols-sharp text-amber-500 text-lg">lock</span>
                        <span>Secure Storage</span>
                    </div>
                    <div class="flex items-center gap-2">
                        <span class="material-symbols-sharp text-blue-500 text-lg">location_on</span>
                        <span>SA Data Centre</span>
                    </div>
                </div>
            </div>
        </div>
    </main>
    
    <!-- Footer -->
    <footer class="p-6 sm:p-8 bg-black border-t border-white/10">
        <div class="max-w-4xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-4">
            <div class="flex items-center gap-4">
                <img src="/logoWtext-transparent-BlackBack.png" alt="VCB Logo" class="h-32 w-auto">
            </div>
            <p class="text-sm text-white/60">© 2026 VCB-AI (Pty) Ltd. All rights reserved.</p>
            <div class="flex items-center gap-6 text-sm text-white/60">
                <a href="/LEGAL.html" class="hover:text-white transition-colors">Legal</a>
                <a href="/" class="hover:text-white transition-colors">Home</a>
            </div>
        </div>
    </footer>
</body>
</html>
