<?php<?php

// Debug contact form processing// Simple debug file to test contact form functionality

error_reporting(E_ALL);session_start();

ini_set('display_errors', 1);

echo "<h2>🔧 Contact Form Debug Information</h2>";

echo "<h1>Contact Form Debug Tool</h1>";

// Check if form was submitted

// Test database connectionif ($_SERVER['REQUEST_METHOD'] === 'POST') {

echo "<h2>1. Database Connection Test</h2>";    echo "<div style='background: #e8f5e8; padding: 15px; border-radius: 8px; margin: 10px 0;'>";

include 'config.php';    echo "<h3>✅ Form Submission Detected!</h3>";

    echo "<strong>POST Data:</strong><br>";

if ($conn) {    echo "<pre>";

    echo "<p style='color: green;'>✓ Database connected successfully</p>";    print_r($_POST);

    echo "<p>Database: " . mysqli_get_server_info($conn) . "</p>";    echo "</pre>";

} else {    echo "</div>";

    echo "<p style='color: red;'>✗ Database connection failed: " . mysqli_connect_error() . "</p>";    

    exit;    if (isset($_POST['submit_contact'])) {

}        echo "<div style='background: #e8f5e8; padding: 15px; border-radius: 8px; margin: 10px 0;'>";

        echo "<h3>✅ Submit Button Clicked!</h3>";

// Test contacts table        echo "</div>";

echo "<h2>2. Contacts Table Test</h2>";    }

$check_table = "SHOW TABLES LIKE 'contacts'";} else {

$result = mysqli_query($conn, $check_table);    echo "<div style='background: #fff3cd; padding: 15px; border-radius: 8px; margin: 10px 0;'>";

    echo "<h3>ℹ️ No Form Submission Detected</h3>";

if (mysqli_num_rows($result) > 0) {    echo "<p>This page will show debug info when the contact form is submitted.</p>";

    echo "<p style='color: green;'>✓ Contacts table exists</p>";    echo "</div>";

    }

    // Show table structure

    $structure = mysqli_query($conn, "DESCRIBE contacts");// Check session data

    echo "<h3>Table Structure:</h3>";if (isset($_SESSION['contact_form'])) {

    echo "<table border='1' style='border-collapse: collapse;'>";    echo "<div style='background: #d4edda; padding: 15px; border-radius: 8px; margin: 10px 0;'>";

    echo "<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th></tr>";    echo "<h3>📋 Session Data Found:</h3>";

    while ($row = mysqli_fetch_assoc($structure)) {    echo "<pre>";

        echo "<tr>";    print_r($_SESSION['contact_form']);

        foreach ($row as $value) {    echo "</pre>";

            echo "<td>" . htmlspecialchars($value) . "</td>";    echo "</div>";

        }}

        echo "</tr>";

    }// Check if WhatsApp message exists

    echo "</table>";if (isset($_SESSION['whatsapp_message'])) {

} else {    echo "<div style='background: #d1ecf1; padding: 15px; border-radius: 8px; margin: 10px 0;'>";

    echo "<p style='color: red;'>✗ Contacts table does not exist</p>";    echo "<h3>📱 WhatsApp Message Found:</h3>";

    echo "<p>Creating contacts table...</p>";    echo "<p>Message exists in session and ready for sharing!</p>";

        echo "<a href='https://wa.me/918905245890?text=" . $_SESSION['whatsapp_message'] . "' target='_blank' style='background: #25d366; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>Test WhatsApp Link</a>";

    $create_table = "CREATE TABLE contacts (    echo "</div>";

        id INT(11) NOT NULL AUTO_INCREMENT,}

        name VARCHAR(100) NOT NULL,

        email VARCHAR(100) NOT NULL,echo "<br><a href='contact.php' style='background: #007bff; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px;'>← Back to Contact Form</a>";

        phone VARCHAR(20),?>
        subject VARCHAR(100) NOT NULL,
        message TEXT NOT NULL,
        status ENUM('unread', 'read', 'replied') DEFAULT 'unread',
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id)
    )";
    
    if (mysqli_query($conn, $create_table)) {
        echo "<p style='color: green;'>✓ Contacts table created successfully</p>";
    } else {
        echo "<p style='color: red;'>✗ Failed to create contacts table: " . mysqli_error($conn) . "</p>";
    }
}

// Test data insertion
echo "<h2>3. Data Insertion Test</h2>";
$test_data = [
    'name' => 'Debug Test User',
    'email' => 'debug@test.com',
    'phone' => '9999999999',
    'subject' => 'general',
    'message' => 'This is a debug test message - ' . date('Y-m-d H:i:s')
];

$insert_query = "INSERT INTO contacts (name, email, phone, subject, message) VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($conn, $insert_query);

if ($stmt) {
    mysqli_stmt_bind_param($stmt, "sssss", 
        $test_data['name'], 
        $test_data['email'], 
        $test_data['phone'], 
        $test_data['subject'], 
        $test_data['message']
    );
    
    if (mysqli_stmt_execute($stmt)) {
        $insert_id = mysqli_insert_id($conn);
        echo "<p style='color: green;'>✓ Test data inserted successfully (ID: $insert_id)</p>";
        
        // Verify insertion
        $verify = mysqli_query($conn, "SELECT * FROM contacts WHERE id = $insert_id");
        if ($verify && mysqli_num_rows($verify) > 0) {
            $row = mysqli_fetch_assoc($verify);
            echo "<h3>Inserted Data:</h3>";
            echo "<pre>" . print_r($row, true) . "</pre>";
        }
    } else {
        echo "<p style='color: red;'>✗ Failed to insert test data: " . mysqli_stmt_error($stmt) . "</p>";
    }
    mysqli_stmt_close($stmt);
} else {
    echo "<p style='color: red;'>✗ Failed to prepare statement: " . mysqli_error($conn) . "</p>";
}

// Test recent contacts
echo "<h2>4. Recent Contacts</h2>";
$recent = mysqli_query($conn, "SELECT * FROM contacts ORDER BY created_at DESC LIMIT 5");
if ($recent && mysqli_num_rows($recent) > 0) {
    echo "<table border='1' style='border-collapse: collapse; width: 100%;'>";
    echo "<tr><th>ID</th><th>Name</th><th>Email</th><th>Subject</th><th>Created</th><th>Status</th></tr>";
    while ($row = mysqli_fetch_assoc($recent)) {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . htmlspecialchars($row['name']) . "</td>";
        echo "<td>" . htmlspecialchars($row['email']) . "</td>";
        echo "<td>" . htmlspecialchars($row['subject']) . "</td>";
        echo "<td>" . $row['created_at'] . "</td>";
        echo "<td>" . $row['status'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p>No contacts found in database</p>";
}

// Test WhatsApp functionality
echo "<h2>5. WhatsApp Integration Test</h2>";
function testWhatsAppURL($phone, $message) {
    $formatted_phone = "91" . preg_replace('/[^0-9]/', '', $phone);
    $encoded_message = urlencode($message);
    return "https://wa.me/$formatted_phone?text=$encoded_message";
}

$test_phone = "9876543210";
$test_message = "Test WhatsApp message from Shubank contact form";
$whatsapp_url = testWhatsAppURL($test_phone, $test_message);

echo "<p>Test WhatsApp URL: <a href='$whatsapp_url' target='_blank'>$whatsapp_url</a></p>";
echo "<p style='color: green;'>✓ WhatsApp URL generation working</p>";

// Test file permissions
echo "<h2>6. File Permissions Test</h2>";
$files_to_check = ['contact.php', 'config.php', 'admin/admin-dashboard.php'];
foreach ($files_to_check as $file) {
    if (file_exists($file)) {
        $perms = fileperms($file);
        echo "<p>$file: " . substr(sprintf('%o', $perms), -4) . " ";
        if (is_readable($file)) {
            echo "<span style='color: green;'>✓ Readable</span>";
        } else {
            echo "<span style='color: red;'>✗ Not Readable</span>";
        }
        if (is_writable($file)) {
            echo " <span style='color: green;'>✓ Writable</span>";
        }
        echo "</p>";
    } else {
        echo "<p style='color: red;'>$file: File not found</p>";
    }
}

// Test PHP configuration
echo "<h2>7. PHP Configuration</h2>";
echo "<p>PHP Version: " . phpversion() . "</p>";
echo "<p>Max Upload Size: " . ini_get('upload_max_filesize') . "</p>";
echo "<p>Max Post Size: " . ini_get('post_max_size') . "</p>";
echo "<p>Error Reporting: " . error_reporting() . "</p>";

echo "<h2>Debug Complete</h2>";
echo "<p><a href='contact.php'>Go to Contact Form</a> | <a href='admin/admin-dashboard.php'>Go to Admin Dashboard</a></p>";
?>