<?php
// ============================================================
// حفظ التصميم الحالي في قاعدة البيانات
// شغل هذا الملف مرة واحدة فقط
// ============================================================

include "session.php";
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/db.php';

// ============================================================
// إنشاء جدول التصميم إذا لم يكن موجوداً
// ============================================================
$pdo->exec("CREATE TABLE IF NOT EXISTS page_designs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    page_name VARCHAR(50) NOT NULL,
    element_id VARCHAR(100) NOT NULL,
    element_type VARCHAR(50),
    content TEXT,
    styles LONGTEXT,
    position INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY unique_page_element (page_name, element_id)
)");

echo "✅ جدول التصميم جاهز<br>";

// ============================================================
// حذف أي تصميم سابق
// ============================================================
$pdo->exec("DELETE FROM page_designs");
echo "✅ تم تنظيف الجدول<br>";

// ============================================================
// حفظ عناصر الصفحة الرئيسية
// ============================================================
$home_elements = [
    ['home', 'header-top', 'header', '', json_encode(['backgroundColor' => '#2C3E50', 'color' => '#ffffff', 'padding' => '10px 0'])],
    ['home', 'header-main', 'header', '', json_encode(['backgroundColor' => '#ffffff', 'boxShadow' => '0 2px 10px rgba(0,0,0,0.08)'])],
    ['home', 'logo', 'image', '', json_encode(['height' => '60px'])],
    ['home', 'search-button', 'button', '<i class="fas fa-search"></i>', json_encode(['backgroundColor' => '#2196F3', 'color' => '#ffffff', 'border' => 'none', 'padding' => '0 20px'])],
    ['home', 'nav-home', 'link', 'الرئيسية', json_encode(['color' => '#ffffff', 'padding' => '15px 25px'])],
    ['home', 'nav-orders', 'link', 'طلباتك', json_encode(['color' => '#ffffff', 'padding' => '15px 25px'])],
    ['home', 'nav-categories', 'link', 'الأقسام', json_encode(['color' => '#ffffff', 'padding' => '15px 25px'])],
    ['home', 'footer', 'footer', 'جميع الحقوق محفوظة', json_encode(['backgroundColor' => '#2C3E50', 'color' => '#ffffff', 'padding' => '30px 0', 'textAlign' => 'center'])]
];

// إضافة 4 بطاقات منتج
for ($i = 0; $i < 4; $i++) {
    $home_elements[] = ['home', "product-card-$i", 'product-card', '', json_encode(['backgroundColor' => '#ffffff', 'borderRadius' => '12px', 'border' => '1px solid #E0E0E0'])];
    $home_elements[] = ['home', "product-btn-$i", 'button', '<i class="fas fa-shopping-cart"></i> أضف للسلة', json_encode(['backgroundColor' => '#1a237e', 'color' => '#ffffff', 'border' => 'none', 'padding' => '12px', 'borderRadius' => '8px'])];
}

$home_elements[] = ['home', 'featured-button', 'button', 'عرض جميع المنتجات', json_encode(['backgroundColor' => '#2196F3', 'color' => '#ffffff', 'padding' => '15px 40px', 'borderRadius' => '8px'])];

// ============================================================
// حفظ عناصر صفحة المنتج
// ============================================================
$product_elements = [
    ['product', 'product-header-top', 'header', '', json_encode(['backgroundColor' => '#2C3E50', 'color' => '#ffffff', 'padding' => '10px 0'])],
    ['product', 'product-header-main', 'header', '', json_encode(['backgroundColor' => '#ffffff'])],
    ['product', 'product-logo', 'image', '', json_encode(['height' => '60px'])],
    ['product', 'product-main-image', 'image', '', json_encode(['maxWidth' => '100%', 'maxHeight' => '100%'])],
    ['product', 'product-title', 'text', 'اسم المنتج', json_encode(['fontSize' => '28px', 'color' => '#2C3E50', 'fontWeight' => 'bold'])],
    ['product', 'product-description', 'text', 'وصف المنتج هنا', json_encode(['color' => '#666', 'margin' => '15px 0'])],
    ['product', 'product-price', 'text', '0.00 دج', json_encode(['fontSize' => '32px', 'fontWeight' => '800', 'color' => '#2196F3'])],
    ['product', 'order-button', 'button', '<i class="fas fa-shopping-bag"></i> أطلب الآن', json_encode(['backgroundColor' => '#2196F3', 'color' => '#ffffff', 'border' => 'none', 'padding' => '15px 30px', 'borderRadius' => '8px', 'width' => '100%'])]
];

// ============================================================
// إدراج جميع العناصر في قاعدة البيانات
// ============================================================
$stmt = $pdo->prepare("INSERT INTO page_designs (page_name, element_id, element_type, content, styles) VALUES (?, ?, ?, ?, ?)");

$count = 0;
foreach ($home_elements as $element) {
    $stmt->execute($element);
    $count++;
}

foreach ($product_elements as $element) {
    $stmt->execute($element);
    $count++;
}

echo "✅ تم حفظ <strong>$count</strong> عنصر في قاعدة البيانات<br>";
echo "<hr>";
echo "<a href='design.php' style='display:inline-block; background:#2196F3; color:white; padding:15px 30px; text-decoration:none; border-radius:8px; margin-top:20px;'>الذهاب إلى المصمم الآن</a>";
?>