<?php
/**
 * Test Wishlist Integration
 * Tests the complete wishlist flow: add, count, toggle
 */

session_start();

// Simulate logged in user for testing
if (!isset($_SESSION['user_id'])) {
    $_SESSION['user_id'] = 1; // Test user ID
}

echo "<!DOCTYPE html>/n";
echo "<html lang='ar' dir='rtl'>\n";
echo "<head>/n";
echo "    <meta charset='UTF-8'>\n";
echo "    <title>اختبار نظام المفضلة</title>/n";
echo "    <style>/n";
echo "        body { font-family: Arial; padding: 20px; background: #f5f5f5; }/n";
echo "        .test-box { background: white; padding: 20px; margin: 10px 0; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }/n";
echo "        .success { color: #22c55e; }/n";
echo "        .error { color: #ef4444; }/n";
echo "        button { background: #E57393; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; margin: 5px; }/n";
echo "        button:hover { background: #D1537A; }/n";
echo "        #counter { font-size: 24px; font-weight: bold; color: #E57393; }/n";
echo "    </style>/n";
echo "</head>/n";
echo "<body>/n";
echo "    <h1>🧪 اختبار نظام المفضلة</h1>/n";
echo "    <p>المستخدم: " . $_SESSION['user_id'] . "</p>/n";
echo "    /n";
echo "    <div class='test-box'>\n";
echo "        <h2>عداد المفضلة الحالي: <span id='counter'>...</span></h2>\n";
echo "        <button onclick='getCount()'>تحديث العداد</button>\n";
echo "    </div>/n";
echo "    /n";
echo "    <div class='test-box'>\n";
echo "        <h2>إضافة/إزالة منتج</h2>/n";
echo "        <label>معرف المنتج: <input type='number' id='productId' value='1' style='padding: 8px; border: 1px solid #ddd; border-radius: 4px;'></label>\n";
echo "        <button onclick='toggleProduct()'>إضافة/إزالة</button>\n";
echo "        <div id='toggleResult' style='margin-top: 10px;'></div>\n";
echo "    </div>/n";
echo "    /n";
echo "    <div class='test-box'>\n";
echo "        <h2>سجل الاختبار</h2>/n";
echo "        <div id='log' style='background: #f9f9f9; padding: 10px; border-radius: 4px; max-height: 300px; overflow-y: auto;'></div>\n";
echo "    </div>/n";
echo "    /n";
echo "    <script>/n";
echo "        function log(message, type = 'info') {\n";
echo "            const logDiv = document.getElementById('log');\n";
echo "            const time = new Date().toLocaleTimeString('ar-EG');\n";
echo "            const color = type === 'success' ? '#22c55e' : type === 'error' ? '#ef4444' : '#666';\n";
echo "            logDiv.innerHTML += `<div style='color: /${color}; margin: 5px 0;'>\${time} - \${message}</div>`;\n";
echo "            logDiv.scrollTop = logDiv.scrollHeight;/n";
echo "        }/n";
echo "        /n";
echo "        function getCount() {/n";
echo "            log('جاري جلب عدد المفضلة...');\n";
echo "            fetch('count.php')\n";
echo "                .then(res => res.json())/n";
echo "                .then(data => {/n";
echo "                    if (data.success) {/n";
echo "                        document.getElementById('counter').textContent = data.count;\n";
echo "                        log('✓ العدد: ' + data.count, 'success');\n";
echo "                    } else {/n";
echo "                        log('✗ فشل جلب العدد', 'error');\n";
echo "                    }/n";
echo "                })/n";
echo "                .catch(err => {/n";
echo "                    log('✗ خطأ: ' + err.message, 'error');\n";
echo "                });/n";
echo "        }/n";
echo "        /n";
echo "        function toggleProduct() {/n";
echo "            const productId = document.getElementById('productId').value;\n";
echo "            log('جاري إضافة/إزالة المنتج ' + productId + '...');\n";
echo "            /n";
echo "            fetch('toggle.php', {\n";
echo "                method: 'POST',\n";
echo "                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n";
echo "                body: 'product_id=' + productId\n";
echo "            })/n";
echo "            .then(res => res.json())/n";
echo "            .then(data => {/n";
echo "                if (data.success) {/n";
echo "                    const status = data.in_wishlist ? 'تم الإضافة ✓' : 'تم الإزالة ✗';\n";
echo "                    document.getElementById('toggleResult').innerHTML = \n";
echo "                        `<div class='success'>\${status}</div>`;\n";
echo "                    log('✓ ' + data.message, 'success');\n";
echo "                    // Update counter/n";
echo "                    setTimeout(getCount, 500);/n";
echo "                } else {/n";
echo "                    document.getElementById('toggleResult').innerHTML = \n";
echo "                        `<div class='error'>\${data.message}</div>`;\n";
echo "                    log('✗ ' + data.message, 'error');\n";
echo "                }/n";
echo "            })/n";
echo "            .catch(err => {/n";
echo "                log('✗ خطأ: ' + err.message, 'error');\n";
echo "            });/n";
echo "        }/n";
echo "        /n";
echo "        // Initialize/n";
echo "        window.onload = function() {/n";
echo "            log('بدء الاختبار...');\n";
echo "            getCount();/n";
echo "        };/n";
echo "    </script>/n";
echo "</body>/n";
echo "</html>/n";
?>
