<?php
/**
 * قالب محصولات شگفت‌انگیز Dima Shop
 * نسخه: 1.0.0
 */
if (!defined('ABSPATH')) exit;

$limit = isset($atts['limit']) ? intval($atts['limit']) : 8;
$flash_sales = Dima_Flash_Sale::get_instance()->get_active_sales($limit);
$currency = get_option('dima_currency', 'تومان');

if (empty($flash_sales)) {
    echo '<div class="no-flash-sales">هیچ محصول شگفت‌انگیزی موجود نیست.</div>';
    return;
}
?>

<div class="dima-flash-sale-section" dir="rtl">
    
    <div class="flash-sale-header">
        <h2>🔥 شگفت‌انگیز امروز</h2>
        <div class="flash-sale-timer" id="flash-timer">
            <span class="timer-label">زمان باقی‌مانده:</span>
            <div class="timer-display">
                <span class="timer-unit" id="timer-hours">00</span>:
                <span class="timer-unit" id="timer-minutes">00</span>:
                <span class="timer-unit" id="timer-seconds">00</span>
            </div>
        </div>
    </div>
    
    <div class="flash-sale-grid">
        <?php foreach ($flash_sales as $sale): 
            $product = get_post($sale->product_id);
            if (!$product) continue;
            
            $original_price = Dima_Product::get_price($sale->product_id);
            $discount_percent = $original_price > 0 ? round((($original_price - $sale->sale_price) / $original_price) * 100) : 0;
            $stock_remaining = $sale->stock_limit > 0 ? $sale->stock_limit - $sale->sold_count : 0;
            $stock_percent = $sale->stock_limit > 0 ? ($sale->sold_count / $sale->stock_limit) * 100 : 0;
        ?>
        <div class="flash-sale-card" data-end-time="<?php echo esc_attr($sale->end_time); ?>">
            
            <div class="flash-sale-badges">
                <span class="badge discount-badge">-<?php echo $discount_percent; ?>٪</span>
                <?php if ($stock_remaining <= 5 && $stock_remaining > 0): ?>
                    <span class="badge urgent-badge">فقط <?php echo $stock_remaining; ?> عدد!</span>
                <?php endif; ?>
            </div>
            
            <a href="<?php echo esc_url(get_permalink($sale->product_id)); ?>" class="flash-sale-image">
                <?php if (has_post_thumbnail($sale->product_id)): ?>
                    <?php echo get_the_post_thumbnail($sale->product_id, 'medium'); ?>
                <?php else: ?>
                    <div class="no-image">📦</div>
                <?php endif; ?>
            </a>
            
            <div class="flash-sale-info">
                <h3 class="flash-sale-title">
                    <a href="<?php echo esc_url(get_permalink($sale->product_id)); ?>">
                        <?php echo esc_html($product->post_title); ?>
                    </a>
                </h3>
                
                <div class="flash-sale-prices">
                    <span class="original-price"><?php echo number_format($original_price); ?></span>
                    <span class="sale-price"><?php echo number_format($sale->sale_price); ?> <?php echo esc_html($currency); ?></span>
                </div>
                
                <?php if ($sale->stock_limit > 0): ?>
                <div class="flash-sale-progress">
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: <?php echo $stock_percent; ?>%"></div>
                    </div>
                    <span class="progress-text">فروش: <?php echo intval($sale->sold_count); ?> / <?php echo intval($sale->stock_limit); ?></span>
                </div>
                <?php endif; ?>
                
                <button class="btn btn-primary btn-small add-to-cart-btn" data-product-id="<?php echo esc_attr($sale->product_id); ?>">
                    🛒 افزودن به سبد
                </button>
            </div>
            
        </div>
        <?php endforeach; ?>
    </div>
    
</div>

<style>
.dima-flash-sale-section {
    max-width: 1200px;
    margin: 0 auto;
    padding: 30px 20px;
    font-family: 'Vazir', Tahoma, sans-serif;
}

.flash-sale-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e5e7eb;
    flex-wrap: wrap;
    gap: 15px;
}

.flash-sale-header h2 {
    font-size: 28px;
    color: #1f2937;
    margin: 0;
}

.flash-sale-timer {
    display: flex;
    align-items: center;
    gap: 10px;
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: white;
    padding: 10px 20px;
    border-radius: 12px;
}

.timer-label {
    font-size: 14px;
    font-weight: 600;
}

.timer-display {
    display: flex;
    gap: 5px;
    font-family: monospace;
    font-size: 18px;
    font-weight: 700;
}

.timer-unit {
    background: rgba(255,255,255,0.2);
    padding: 4px 8px;
    border-radius: 6px;
    min-width: 32px;
    text-align: center;
}

.flash-sale-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 20px;
}

.flash-sale-card {
    background: white;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    border: 2px solid #fee2e2;
    position: relative;
    transition: all 0.3s;
}

.flash-sale-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(239, 68, 68, 0.15);
}

.flash-sale-badges {
    position: absolute;
    top: 10px;
    right: 10px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    z-index: 10;
}

.badge {
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 700;
    color: white;
}

.discount-badge {
    background: linear-gradient(135deg, #ef4444, #dc2626);
}

.urgent-badge {
    background: linear-gradient(135deg, #f59e0b, #d97706);
}

.flash-sale-image {
    display: block;
    aspect-ratio: 1/1;
    overflow: hidden;
    background: #f9fafb;
}

.flash-sale-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.3s;
}

.flash-sale-card:hover .flash-sale-image img {
    transform: scale(1.05);
}

.no-image {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 60px;
}

.flash-sale-info {
    padding: 15px;
}

.flash-sale-title {
    font-size: 15px;
    margin: 0 0 10px 0;
    line-height: 1.4;
}

.flash-sale-title a {
    color: #1f2937;
    text-decoration: none;
}

.flash-sale-title a:hover {
    color: #ef4444;
}

.flash-sale-prices {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

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

.sale-price {
    font-size: 18px;
    font-weight: 800;
    color: #ef4444;
}

.flash-sale-progress {
    margin-bottom: 12px;
}

.progress-bar {
    height: 6px;
    background: #fee2e2;
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 5px;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #ef4444, #f59e0b);
    border-radius: 3px;
    transition: width 0.3s;
}

.progress-text {
    font-size: 12px;
    color: #6b7280;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 20px;
    border-radius: 10px;
    font-family: inherit;
    font-weight: 600;
    font-size: 14px;
    cursor: pointer;
    border: none;
    text-decoration: none;
    transition: all 0.2s;
}

.btn-primary {
    background: linear-gradient(135deg, #ef4444, #dc2626);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(239, 68, 68, 0.3);
}

.btn-small {
    padding: 8px 16px;
    font-size: 13px;
    width: 100%;
}

.no-flash-sales {
    text-align: center;
    padding: 50px 20px;
    color: #6b7280;
    font-size: 16px;
}
</style>

<script>
jQuery(document).ready(function($) {
    // تایمر شگفت‌انگیز
    function updateTimer() {
        var endTime = $('.flash-sale-card').first().data('end-time');
        if (!endTime) return;
        
        var end = new Date(endTime).getTime();
        var now = new Date().getTime();
        var diff = end - now;
        
        if (diff <= 0) {
            $('#flash-timer').html('<span style="color:#ef4444; font-weight:700;">⏰ زمان به پایان رسید!</span>');
            return;
        }
        
        var hours = Math.floor(diff / (1000 * 60 * 60));
        var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((diff % (1000 * 60)) / 1000);
        
        $('#timer-hours').text(String(hours).padStart(2, '0'));
        $('#timer-minutes').text(String(minutes).padStart(2, '0'));
        $('#timer-seconds').text(String(seconds).padStart(2, '0'));
    }
    
    updateTimer();
    setInterval(updateTimer, 1000);
    
    // افزودن به سبد
    $(document).on('click', '.add-to-cart-btn', function(e) {
        e.preventDefault();
        var $btn = $(this);
        var productId = $btn.data('product-id');
        var originalText = $btn.html();
        
        $btn.prop('disabled', true).html('⏳ در حال افزودن...');
        
        $.ajax({
            url: dima_shop.ajax_url,
            type: 'POST',
            data: {
                action: 'dima_add_to_cart',
                product_id: productId,
                quantity: 1,
                nonce: dima_shop.nonce
            },
            success: function(response) {
                if (response.success) {
                    $btn.html('✓ اضافه شد!');
                    $('.cart-count-badge').text(response.data.cart_count);
                    setTimeout(function() {
                        $btn.html(originalText).prop('disabled', false);
                    }, 2000);
                }
            }
        });
    });
});
</script>