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

$product_id = get_the_ID();
$price = Dima_Product::get_price($product_id);
$stock = Dima_Product::get_stock($product_id);
$currency = get_option('dima_currency', 'تومان');
$is_in_stock = ($stock === '' || intval($stock) > 0);
?>

<div class="dima-quick-view" dir="rtl">
    
    <div class="quick-view-layout">
        
        <!-- تصویر -->
        <div class="quick-view-image">
            <?php if (has_post_thumbnail()): ?>
                <?php the_post_thumbnail('large'); ?>
            <?php else: ?>
                <div class="no-image">📦</div>
            <?php endif; ?>
        </div>
        
        <!-- اطلاعات -->
        <div class="quick-view-info">
            <h2 class="quick-view-title"><?php the_title(); ?></h2>
            
            <?php if (has_excerpt()): ?>
                <div class="quick-view-excerpt">
                    <?php the_excerpt(); ?>
                </div>
            <?php endif; ?>
            
            <div class="quick-view-price">
                <span class="price-value"><?php echo number_format($price); ?></span>
                <span class="price-currency"><?php echo esc_html($currency); ?></span>
            </div>
            
            <div class="quick-view-stock">
                <?php if ($is_in_stock): ?>
                    <span class="stock-badge in-stock">✓ موجود در انبار</span>
                <?php else: ?>
                    <span class="stock-badge out-of-stock">✗ ناموجود</span>
                <?php endif; ?>
            </div>
            
            <div class="quick-view-actions">
                <?php if ($is_in_stock): ?>
                    <button class="btn btn-primary btn-large add-to-cart-btn" data-product-id="<?php echo esc_attr($product_id); ?>">
                        🛒 افزودن به سبد خرید
                    </button>
                <?php else: ?>
                    <button class="btn btn-disabled btn-large" disabled>ناموجود</button>
                <?php endif; ?>
                
                <a href="<?php the_permalink(); ?>" class="btn btn-outline btn-large">
                    مشاهده جزئیات ←
                </a>
            </div>
        </div>
        
    </div>
    
</div>

<style>
.dima-quick-view {
    padding: 20px;
    font-family: 'Vazir', Tahoma, sans-serif;
}

.quick-view-layout {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    max-width: 900px;
    margin: 0 auto;
}

@media (max-width: 768px) {
    .quick-view-layout {
        grid-template-columns: 1fr;
    }
}

.quick-view-image {
    border-radius: 16px;
    overflow: hidden;
    background: #f9fafb;
    aspect-ratio: 1/1;
}

.quick-view-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

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

.quick-view-info {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.quick-view-title {
    font-size: 24px;
    font-weight: 800;
    color: #1f2937;
    margin: 0;
}

.quick-view-excerpt {
    color: #4b5563;
    line-height: 1.6;
    font-size: 14px;
}

.quick-view-price {
    display: flex;
    align-items: baseline;
    gap: 10px;
}

.price-value {
    font-size: 28px;
    font-weight: 900;
    color: #6366f1;
}

.price-currency {
    font-size: 16px;
    color: #6b7280;
}

.stock-badge {
    display: inline-block;
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
}

.in-stock {
    background: #d1fae5;
    color: #065f46;
}

.out-of-stock {
    background: #fee2e2;
    color: #991b1b;
}

.quick-view-actions {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 24px;
    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, #6366f1, #a855f7);
    color: white;
}

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

.btn-outline {
    background: white;
    color: #6366f1;
    border: 2px solid #6366f1;
}

.btn-outline:hover {
    background: #6366f1;
    color: white;
}

.btn-large {
    padding: 14px 28px;
    font-size: 15px;
    flex: 1;
}

.btn-disabled {
    background: #e5e7eb;
    color: #9ca3af;
    cursor: not-allowed;
}
</style>

<script>
jQuery(document).ready(function($) {
    $('.add-to-cart-btn').on('click', 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() {
                        // بستن مودال
                        if (typeof DimaQuickView !== 'undefined') {
                            DimaQuickView.close();
                        }
                    }, 1500);
                }
            }
        });
    });
});
</script>