( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ HEX
HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux mail.thebrand.ai 6.8.0-107-generic #107-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 13 19:51:50 UTC 2026 x86_64
User: www-data (33)
PHP: 8.3.6
Disabled: NONE
Upload Files
File: /var/www/html/tmpr/../tmpr/../tmpr/../tmpr/../looksy/debug_save_complete.php
<?php
// Include configuration to ensure consistent session handling
require_once 'config/config.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo "<h2>Please log in first</h2>";
    echo "<a href='/looksy/login.php'>Login</a>";
    exit;
}

echo "<h1>Complete Auto-Save Debug Test</h1>";
echo "<p>User ID: " . $_SESSION['user_id'] . "</p>";

// Test 1: Database Connection and Table
echo "<h2>Test 1: Database Connection and Wardrobe Table</h2>";
try {
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($conn->connect_error) {
        throw new Exception("Connection failed: " . $conn->connect_error);
    }
    echo "<p style='color: green;'>✅ Database connection successful</p>";
    
    $result = $conn->query("SHOW TABLES LIKE 'wardrobe'");
    if ($result && $result->num_rows > 0) {
        echo "<p style='color: green;'>✅ Wardrobe table exists</p>";
    } else {
        echo "<p style='color: red;'>❌ Wardrobe table missing</p>";
    }
    
    $conn->close();
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Database error: " . $e->getMessage() . "</p>";
}

// Test 2: Database Class
echo "<h2>Test 2: Database Class</h2>";
try {
    // Autoload classes
    spl_autoload_register(function($className) {
        $classFile = 'includes/classes/' . $className . '.php';
        if (file_exists($classFile)) {
            require_once $classFile;
            return true;
        }
        return false;
    });
    
    $database = Database::getInstance();
    $conn = $database->getConnection();
    echo "<p style='color: green;'>✅ Database class working</p>";
    echo "<p>Connection charset: " . $conn->character_set_name() . "</p>";
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Database class error: " . $e->getMessage() . "</p>";
}

// Test 3: Direct API Call
echo "<h2>Test 3: Direct API Call</h2>";
$testData = [
    'action' => 'save_outfit',
    'outfit_name' => 'Debug Test ' . date('H:i:s'),
    'model_id' => '1',
    'model_data' => '{"id": 1, "name": "Test Model"}',
    'garments_data' => '[{"id": 1, "name": "Test Garment"}]',
    'background_id' => '1',
    'background_data' => '{"id": 1, "name": "Test Background"}',
    'fitting_result_image' => 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
    'notes' => 'Debug test'
];

// Simulate the API call
$_POST = $testData;

echo "<h3>Simulating API Call:</h3>";
echo "<pre>" . print_r($testData, true) . "</pre>";

// Capture the API response
ob_start();
try {
    include 'api/virtual_dress.php';
} catch (Exception $e) {
    echo "API Error: " . $e->getMessage();
}
$apiResponse = ob_get_clean();

echo "<h3>API Response:</h3>";
echo "<pre>" . htmlspecialchars($apiResponse) . "</pre>";

// Test JSON decode
$jsonData = json_decode($apiResponse, true);
if ($jsonData === null) {
    echo "<p style='color: red;'>❌ API response is not valid JSON</p>";
    echo "<p>JSON Error: " . json_last_error_msg() . "</p>";
} else {
    echo "<p style='color: green;'>✅ API response is valid JSON</p>";
    echo "<pre>" . print_r($jsonData, true) . "</pre>";
}

// Test 4: Check Wardrobe Records
echo "<h2>Test 4: Check Wardrobe Records</h2>";
try {
    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $stmt = $conn->prepare("SELECT COUNT(*) as count FROM wardrobe WHERE user_id = ?");
    $stmt->bind_param('i', $_SESSION['user_id']);
    $stmt->execute();
    $result = $stmt->get_result();
    $row = $result->fetch_assoc();
    
    echo "<p>Total records for user " . $_SESSION['user_id'] . ": " . $row['count'] . "</p>";
    
    // Show recent records
    $stmt = $conn->prepare("SELECT * FROM wardrobe WHERE user_id = ? ORDER BY created_at DESC LIMIT 3");
    $stmt->bind_param('i', $_SESSION['user_id']);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        echo "<h3>Recent Records:</h3>";
        echo "<table border='1' style='border-collapse: collapse;'>";
        echo "<tr><th>ID</th><th>Name</th><th>Created</th><th>Notes</th></tr>";
        while ($record = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>" . $record['wardrobe_id'] . "</td>";
            echo "<td>" . $record['outfit_name'] . "</td>";
            echo "<td>" . $record['created_at'] . "</td>";
            echo "<td>" . $record['notes'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "<p>No records found</p>";
    }
    
    $conn->close();
} catch (Exception $e) {
    echo "<p style='color: red;'>❌ Error checking records: " . $e->getMessage() . "</p>";
}

// Test 5: JavaScript Auto-Save Simulation
echo "<h2>Test 5: JavaScript Auto-Save Simulation</h2>";
?>

<div id="test-form">
    <input type="text" id="outfit-name" value="JS Test <?php echo date('H:i:s'); ?>" placeholder="Outfit Name">
    <button onclick="testAutoSave()">Test Auto-Save</button>
</div>

<div id="debug-output"></div>

<script>
function testAutoSave() {
    const outfitName = document.getElementById('outfit-name').value;
    const debugOutput = document.getElementById('debug-output');
    
    debugOutput.innerHTML = '<h3>Testing Auto-Save...</h3>';
    
    const formData = new FormData();
    formData.append('action', 'save_outfit');
    formData.append('outfit_name', outfitName);
    formData.append('model_id', '1');
    formData.append('model_data', '{"id": 1, "name": "Test Model"}');
    formData.append('garments_data', '[{"id": 1, "name": "Test Garment"}]');
    formData.append('background_id', '1');
    formData.append('background_data', '{"id": 1, "name": "Test Background"}');
    formData.append('fitting_result_image', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==');
    formData.append('notes', 'JavaScript test');
    
    console.log('Sending request to /looksy/api/virtual_dress.php');
    
    fetch('/looksy/api/virtual_dress.php', {
        method: 'POST',
        body: formData
    })
    .then(response => {
        console.log('Response status:', response.status);
        console.log('Response headers:', response.headers);
        
        debugOutput.innerHTML += '<p>Response Status: ' + response.status + '</p>';
        
        if (!response.ok) {
            throw new Error('Network response was not ok: ' + response.status);
        }
        
        return response.text(); // Get as text first to see raw response
    })
    .then(text => {
        console.log('Raw response:', text);
        debugOutput.innerHTML += '<h4>Raw Response:</h4><pre>' + text + '</pre>';
        
        try {
            const data = JSON.parse(text);
            console.log('Parsed JSON:', data);
            debugOutput.innerHTML += '<h4>Parsed JSON:</h4><pre>' + JSON.stringify(data, null, 2) + '</pre>';
            
            if (data.success) {
                debugOutput.innerHTML += '<p style="color: green;">✅ Success: ' + data.message + '</p>';
            } else {
                debugOutput.innerHTML += '<p style="color: red;">❌ Error: ' + data.message + '</p>';
            }
        } catch (e) {
            console.error('JSON parse error:', e);
            debugOutput.innerHTML += '<p style="color: red;">❌ JSON Parse Error: ' + e.message + '</p>';
        }
    })
    .catch(error => {
        console.error('Fetch error:', error);
        debugOutput.innerHTML += '<p style="color: red;">❌ Fetch Error: ' + error.message + '</p>';
    });
}
</script>

<?php
echo "<h2>Debug Complete</h2>";
echo "<p>Check the console and output above for detailed information.</p>";
?>