<?php
/**
 * DarkMatter Request Access Form
 * Saves access requests 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'] ?? '');
    $role = trim($_POST['role'] ?? '');
    $use_case = trim($_POST['use_case'] ?? '');
    $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 darkmatter_access_requests (
                id INT AUTO_INCREMENT PRIMARY KEY,
                name VARCHAR(255) NOT NULL,
                email VARCHAR(255) NOT NULL,
                company VARCHAR(255),
                role VARCHAR(100),
                use_case VARCHAR(100),
                comments TEXT,
                ip_address VARCHAR(45),
                user_agent TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            )");
            
            // Insert request
            $stmt = $pdo->prepare("INSERT INTO darkmatter_access_requests (name, email, company, role, use_case, comments, ip_address, user_agent) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
            $stmt->execute([
                $name,
                $email,
                $company,
                $role,
                $use_case,
                $comments,
                $_SERVER['REMOTE_ADDR'] ?? '',
                $_SERVER['HTTP_USER_AGENT'] ?? ''
            ]);
            
            // Send email notification
            $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' => 'Not provided',
                        'subject' => 'DarkMatter Access Request',
                        'message' => "Role: $role\nUse Case: $use_case\n\nComments: $comments",
                        'form_type' => 'DarkMatter Access Request',
                        '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! Your access request has been submitted. We\'ll be in touch soon.';
            $messageType = 'success';
            
            // Clear form
            $name = $email = $company = $role = $use_case = $comments = '';
            
        } catch (PDOException $e) {
            $message = 'Sorry, there was an error processing your request. Please try again later.';
            $messageType = 'error';
            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>Request Access | #ProjectDarkMatter</title>
    <meta name="description" content="Request early access to DarkMatter - South Africa's premier AI-powered legal intelligence platform.">
    <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+Outlined: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: {
                        sans: ['Quicksand', 'sans-serif'],
                    },
                    colors: {
                        navy: {
                            800: '#262626',
                            900: '#171717',
                        }
                    }
                }
            }
        }
    </script>
    <style>
        body { font-family: 'Quicksand', sans-serif; font-weight: 400; }
        
        .form-input {
            background: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.15);
            border-radius: 12px;
            padding: 14px 18px;
            color: white;
            font-size: 16px;
            width: 100%;
            transition: all 0.3s ease;
        }
        
        .form-input:focus {
            outline: none;
            border-color: rgba(255, 255, 255, 0.4);
            background: rgba(255, 255, 255, 0.08);
        }
        
        .form-input::placeholder {
            color: rgba(255, 255, 255, 0.4);
        }
        
        .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(255,255,255,0.5)'%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;
            appearance: none;
            padding-right: 40px;
        }
        
        .form-select option {
            background: #171717;
            color: white;
        }
        
        .hero-pattern {
            background-color: #171717;
            background-image: radial-gradient(#262626 1px, transparent 1px);
            background-size: 30px 30px;
        }
        
        /* DarkMatter split text */
        .darkmatter-split {
            display: inline-block;
            font-weight: 700;
        }
        .darkmatter-split-dark {
            color: white;
        }
    </style>
</head>
<body class="bg-navy-900 text-white min-h-screen hero-pattern">
    
    <!-- Header -->
    <header class="p-6 sm:p-8 border-b border-neutral-800">
        <nav class="max-w-4xl mx-auto flex items-center justify-between">
            <a href="/project-darkmatter.html" class="flex items-center gap-3">
                <img src="/darkmatter-logo.svg" alt="DarkMatter" class="h-10 sm:h-12 w-auto" onerror="this.style.display='none'">
                <span class="text-lg font-bold text-white">#ProjectDarkMatter</span>
            </a>
            <a href="/project-darkmatter.html" class="flex items-center gap-2 px-4 py-2 text-neutral-400 hover:text-white border border-neutral-700 hover:border-neutral-500 rounded-full transition-all text-sm font-medium">
                <span class="material-symbols-outlined text-lg">arrow_back</span>
                Back
            </a>
        </nav>
    </header>
    
    <!-- Main Content -->
    <main class="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-white/10 border border-white/20 mb-6">
                    <span class="material-symbols-outlined text-3xl text-white">rocket_launch</span>
                </div>
                
                <h1 class="text-3xl sm:text-4xl font-bold mb-4">
                    Request Access
                </h1>
                
                <p class="text-neutral-400 max-w-md mx-auto">
                    Join the waitlist for South Africa's premier AI-powered legal intelligence platform.
                </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-outlined"><?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="bg-white/5 rounded-2xl p-8 border border-white/10">
                <div 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-neutral-300 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-neutral-300 mb-2">Email Address *</label>
                            <input type="email" name="email" required 
                                   value="<?php echo htmlspecialchars($email ?? ''); ?>"
                                   placeholder="john@lawfirm.co.za"
                                   class="form-input">
                        </div>
                    </div>
                    
                    <!-- Company & Role -->
                    <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                        <div>
                            <label class="block text-sm font-medium text-neutral-300 mb-2">Company / Firm</label>
                            <input type="text" name="company" 
                                   value="<?php echo htmlspecialchars($company ?? ''); ?>"
                                   placeholder="Smith & Associates"
                                   class="form-input">
                        </div>
                        <div>
                            <label class="block text-sm font-medium text-neutral-300 mb-2">Your Role</label>
                            <select name="role" class="form-input form-select">
                                <option value="">Select...</option>
                                <option value="attorney" <?php echo ($role ?? '') === 'attorney' ? 'selected' : ''; ?>>Attorney / Advocate</option>
                                <option value="paralegal" <?php echo ($role ?? '') === 'paralegal' ? 'selected' : ''; ?>>Paralegal / Legal Assistant</option>
                                <option value="legal-counsel" <?php echo ($role ?? '') === 'legal-counsel' ? 'selected' : ''; ?>>In-House Legal Counsel</option>
                                <option value="law-student" <?php echo ($role ?? '') === 'law-student' ? 'selected' : ''; ?>>Law Student</option>
                                <option value="researcher" <?php echo ($role ?? '') === 'researcher' ? 'selected' : ''; ?>>Legal Researcher</option>
                                <option value="business" <?php echo ($role ?? '') === 'business' ? 'selected' : ''; ?>>Business Professional</option>
                                <option value="other" <?php echo ($role ?? '') === 'other' ? 'selected' : ''; ?>>Other</option>
                            </select>
                        </div>
                    </div>
                    
                    <!-- Use Case -->
                    <div>
                        <label class="block text-sm font-medium text-neutral-300 mb-2">Primary Use Case</label>
                        <select name="use_case" class="form-input form-select">
                            <option value="">What will you use DarkMatter for?</option>
                            <option value="case-research" <?php echo ($use_case ?? '') === 'case-research' ? 'selected' : ''; ?>>Case Law Research</option>
                            <option value="legal-drafting" <?php echo ($use_case ?? '') === 'legal-drafting' ? 'selected' : ''; ?>>Legal Document Drafting</option>
                            <option value="compliance" <?php echo ($use_case ?? '') === 'compliance' ? 'selected' : ''; ?>>Compliance & Regulatory</option>
                            <option value="contract-review" <?php echo ($use_case ?? '') === 'contract-review' ? 'selected' : ''; ?>>Contract Review</option>
                            <option value="due-diligence" <?php echo ($use_case ?? '') === 'due-diligence' ? 'selected' : ''; ?>>Due Diligence</option>
                            <option value="general" <?php echo ($use_case ?? '') === 'general' ? 'selected' : ''; ?>>General Legal Questions</option>
                            <option value="other" <?php echo ($use_case ?? '') === 'other' ? 'selected' : ''; ?>>Other</option>
                        </select>
                    </div>
                    
                    <!-- Comments -->
                    <div>
                        <label class="block text-sm font-medium text-neutral-300 mb-2">Anything else we should know?</label>
                        <textarea name="comments" rows="3" 
                                  placeholder="Tell us about your specific needs or 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-white text-navy-900 font-bold uppercase tracking-wider rounded-xl hover:bg-neutral-100 transition-all duration-300">
                            <span class="material-symbols-outlined">send</span>
                            Submit Request
                        </button>
                    </div>
                </div>
            </form>
            
            <!-- Features -->
            <div class="mt-12 grid grid-cols-3 gap-4 text-center">
                <div class="p-4">
                    <span class="material-symbols-outlined text-2xl text-white mb-2">verified</span>
                    <p class="text-xs text-neutral-400">POPIA Compliant</p>
                </div>
                <div class="p-4">
                    <span class="material-symbols-outlined text-2xl text-white mb-2">shield</span>
                    <p class="text-xs text-neutral-400">Zero Log Policy</p>
                </div>
                <div class="p-4">
                    <span class="material-symbols-outlined text-2xl text-white mb-2">location_on</span>
                    <p class="text-xs text-neutral-400">100% SA Hosted</p>
                </div>
            </div>
        </div>
    </main>
    
    <!-- Footer -->
    <footer class="p-6 sm:p-8 border-t border-neutral-800">
        <div class="max-w-4xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-4 text-sm text-neutral-500">
            <p>© 2026 #ProjectDarkMatter. All rights reserved.</p>
            <div class="flex items-center gap-6">
                <a href="/LEGAL.html" class="hover:text-white transition-colors">Legal</a>
                <a href="/project-darkmatter.html" class="hover:text-white transition-colors">Back to DarkMatter</a>
            </div>
        </div>
    </footer>
</body>
</html>
