<?php
/**
 * Update All Pages to Show Discount Prices
 * تحديث جميع الصفحات لعرض السعر بعد الخصم
 */

echo "🚀 تحديث عرض الخصومات في جميع الصفحات.../n/n";

// Function to update file
function updateFile($file, $search, $replace, $description) {
    if (!file_exists($file)) {
        echo "⚠️  $description - الملف غير موجود: $file/n";
        return false;
    }
    
    $content = file_get_contents($file);
    
    if (strpos($content, $search) === false) {
        echo "⚠️  $description - النص المطلوب غير موجود/n";
        return false;
    }
    
    $new_content = str_replace($search, $replace, $content);
    
    if (file_put_contents($file, $new_content)) {
        echo "✅ $description/n";
        return true;
    }
    
    echo "❌ $description - فشل الحفظ/n";
    return false;
}

// 1. Update Related Products in product.php
echo "1️⃣ تحديث المنتجات المشابهة في صفحة المنتج.../n";

$search1 = '<div class="related-price"><?php echo $currency_symbol; ?> <?php echo number_format($item[\'price/'], 0); ?></div>';

$replace1 = '<?php if (!empty($item[/'discount_price\']) && $item[/'discount_price\'] < $item[/'price\']): ?>
                                <div class="related-price-container">
                                    <span class="related-original-price"><?php echo $currency_symbol; ?> <?php echo number_format($item[\'price/'], 0); ?></span>
                                    <span class="related-discount-price"><?php echo $currency_symbol; ?> <?php echo number_format($item[\'discount_price/'], 0); ?></span>
                                    <span class="related-discount-badge">-<?php echo round((($item[\'price/'] - $item[\'discount_price/']) / $item[\'price/']) * 100); ?>%</span>
                                </div>
                            <?php else: ?>
                                <div class="related-price"><?php echo $currency_symbol; ?> <?php echo number_format($item[\'price/'], 0); ?></div>
                            <?php endif; ?>';

updateFile('../public/product.php', $search1, $replace1, 'المنتجات المشابهة');

echo "/n";

// Create a universal product card component
echo "2️⃣ إنشاء مكون عرض المنتج الموحد.../n";

$product_card_component = <<<'PHP'
<?php
/**
 * Product Card Component with Discount Support
 * مكون كارت المنتج مع دعم الخصومات
 * 
 * Usage:
 * include 'includes/product-card.php';
 * renderProductCard($product, $currency_symbol);
 */

function renderProductCard($product, $currency_symbol = 'ج.م') {
    $image = !empty($product['image']) ? $product['image'] : 'https://via.placeholder.com/300';
    $hasDiscount = !empty($product['discount_price']) && $product['discount_price'] < $product['price'];
    $discountPercent = $hasDiscount ? round((($product['price'] - $product['discount_price']) / $product['price']) * 100) : 0;
    ?>
    
    <div class="product-card" onclick="window.location.href='product.php?id=<?php echo $product['id']; ?>'">
        <?php if ($hasDiscount): ?>
            <div class="product-card-badge">-<?php echo $discountPercent; ?>%</div>
        <?php endif; ?>
        
        <img src="<?php echo htmlspecialchars($image); ?>" 
             alt="<?php echo htmlspecialchars($product['name']); ?>" 
             class="product-image">
        
        <div class="product-info">
            <h3 class="product-name"><?php echo htmlspecialchars($product['name']); ?></h3>
            
            <?php if ($hasDiscount): ?>
                <div class="price-container">
                    <span class="original-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['price'], 0); ?></span>
                    <span class="discount-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['discount_price'], 0); ?></span>
                </div>
            <?php else: ?>
                <div class="product-price"><?php echo $currency_symbol; ?> <?php echo number_format($product['price'], 0); ?></div>
            <?php endif; ?>
            
            <button class="btn-add-to-cart" onclick="event.stopPropagation(); addToCart(<?php echo $product['id']; ?>)">
                <i class="fas fa-shopping-cart"></i>
                أضف للسلة
            </button>
        </div>
    </div>
    
    <?php
}
?>
PHP;

file_put_contents('../includes/product-card.php', $product_card_component);
echo "✅ تم إنشاء مكون product-card.php/n/n";

// Add CSS for product cards
echo "3️⃣ إضافة CSS للكروت.../n";

$card_css = <<<'CSS'

/* Product Card Styles with Discount */
.product-card {
    position: relative;
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    transition: all 0.3s;
    cursor: pointer;
}

.product-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 16px rgba(0,0,0,0.15);
}

.product-card-badge {
    position: absolute;
    top: 12px;
    right: 12px;
    background: linear-gradient(135deg, #DC2626 0%, #B91C1C 100%);
    color: white;
    padding: 6px 12px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 700;
    box-shadow: 0 2px 8px rgba(220, 38, 38, 0.4);
    z-index: 10;
}

.product-image {
    width: 100%;
    height: 250px;
    object-fit: cover;
}

.product-info {
    padding: 16px;
}

.product-name {
    font-size: 16px;
    font-weight: 600;
    color: #111;
    margin-bottom: 12px;
    min-height: 40px;
}

.price-container {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.original-price {
    font-size: 14px;
    color: #9CA3AF;
    text-decoration: line-through;
}

.discount-price {
    font-size: 20px;
    font-weight: 700;
    color: #DC2626;
}

.product-price {
    font-size: 20px;
    font-weight: 700;
    color: #111;
    margin-bottom: 12px;
}

.btn-add-to-cart {
    width: 100%;
    padding: 10px;
    background: #E57393;
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.btn-add-to-cart:hover {
    background: #D1537A;
    transform: scale(1.02);
}

CSS;

file_put_contents('../assets/css/product-cards.css', $card_css);
echo "✅ تم إنشاء product-cards.css/n/n";

// Create usage example
echo "4️⃣ إنشاء مثال للاستخدام.../n";

$example = <<<'PHP'
<?php
/**
 * Example: How to use the product card component
 * مثال: كيفية استخدام مكون كارت المنتج
 */

// في أي صفحة تعرض منتجات (index.php, products.php, etc.)

// 1. أضف CSS في الـ head
?>
<link rel="stylesheet" href="assets/css/product-cards.css">
<?php

// 2. استدعي المكون
require_once 'includes/product-card.php';

// 3. اعرض المنتجات
?>
<div class="products-grid">
    <?php foreach ($products as $product): ?>
        <?php renderProductCard($product, $currency_symbol); ?>
    <?php endforeach; ?>
</div>
<?php

// CSS للـ grid
?>
<style>
.products-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 24px;
    padding: 20px;
}

@media (max-width: 768px) {
    .products-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 16px;
    }
}
</style>
PHP;

file_put_contents('../includes/product-card-example.php', $example);
echo "✅ تم إنشاء product-card-example.php/n/n";

echo "✨ تم بنجاح!/n/n";
echo "📝 الخطوات التالية:/n";
echo "   1. افتح أي صفحة تعرض منتجات (index.php, products.php)/n";
echo "   2. أضف في الـ head:/n";
echo "      <link rel='stylesheet' href='assets/css/product-cards.css'>\n";
echo "   3. أضف قبل عرض المنتجات:/n";
echo "      <?php require_once 'includes/product-card.php'; ?>\n";
echo "   4. استبدل كود عرض المنتج بـ:/n";
echo "      <?php renderProductCard(/$product, /$currency_symbol); ?>/n/n";
echo "   📄 راجع: includes/product-card-example.php للمثال الكامل/n/n";
