<?php
/**
 * قالب محصول تکی Dima Shop
 * نسخه: 2.1.0 - با Header و Footer
 */
if (!defined('ABSPATH')) exit;

// شروع Loop
if (have_posts()) :
    while (have_posts()) : the_post();
        
        $product_id = get_the_ID();
        $price = Dima_Product::get_price($product_id);
        $stock = Dima_Product::get_stock($product_id);
        $sku = Dima_Product::get_sku($product_id);
        $currency = get_option('dima_currency', 'تومان');
        $is_in_stock = ($stock === '' || intval($stock) > 0);
        
        $categories = wp_get_post_terms($product_id, 'dima_product_cat');
        $attributes = wp_get_post_terms($product_id, 'dima_attribute');
        
        $rating = get_post_meta($product_id, '_dima_avg_rating', true);
        $review_count = get_post_meta($product_id, '_dima_review_count', true);
        
        $gallery = class_exists('Dima_Gallery') ? Dima_Gallery::get_gallery($product_id) : array();
        $wishlist = Dima_Wishlist::get_instance();
        $is_in_wishlist = $wishlist->is_in_wishlist($product_id);
        
        $flash = class_exists('Dima_Flash_Sale') ? Dima_Flash_Sale::get_instance()->get_flash_price($product_id) : null;
        $flash_price = $flash ? $flash->sale_price : null;

get_header(); ?>

<div class="dima-single-product-wrapper">
    
    <article id="product-<?php echo esc_attr($product_id); ?>" class="dima-single-product" itemscope itemtype="https://schema.org/Product">
        
        <!-- گالری تصاویر -->
        <div class="product-gallery-section">
            <div class="main-image-container">
                <?php if (has_post_thumbnail()): ?>
                    <img src="<?php echo esc_url(get_the_post_thumbnail_url($product_id, 'large')); ?>" 
                         alt="<?php echo esc_attr(get_the_title()); ?>" 
                         itemprop="image"
                         class="main-product-image"
                         id="main-product-image">
                <?php else: ?>
                    <div class="no-image-placeholder">
                        <span class="placeholder-icon">📦</span>
                    </div>
                <?php endif; ?>
                
                <div class="image-badges">
                    <?php if ($flash): 
                        $discount_percent = $price > 0 ? round((($price - $flash_price) / $price) * 100) : 0;
                    ?>
                        <span class="badge badge-sale">-<?php echo $discount_percent; ?>٪</span>
                    <?php endif; ?>
                    
                    <?php 
                    $post_date = get_the_date('U');
                    if (time() - $post_date < 7 * 86400): ?>
                        <span class="badge badge-new">جدید</span>
                    <?php endif; ?>
                </div>
            </div>
            
            <?php if (!empty($gallery) || has_post_thumbnail()): ?>
            <div class="thumbnails-container">
                <?php if (has_post_thumbnail()): ?>
                <button class="thumbnail-item active" 
                        data-image="<?php echo esc_url(get_the_post_thumbnail_url($product_id, 'large')); ?>">
                    <?php the_post_thumbnail('thumbnail'); ?>
                </button>
                <?php endif; ?>
                
                <?php foreach ($gallery as $img): ?>
                <button class="thumbnail-item" 
                        data-image="<?php echo esc_url($img['url']); ?>">
                    <img src="<?php echo esc_url($img['thumb_url']); ?>" alt="<?php echo esc_attr($img['alt']); ?>">
                </button>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- اطلاعات محصول -->
        <div class="product-info-section">
            
            <?php if (!empty($categories) && !is_wp_error($categories)): ?>
            <div class="product-categories">
                <?php foreach ($categories as $cat): 
                    $cat_data = Dima_Product::get_category_data($cat->term_id);
                ?>
                    <a href="<?php echo esc_url(get_term_link($cat)); ?>" 
                       class="category-tag" 
                       style="background: <?php echo esc_attr($cat_data['color']); ?>">
                        <?php echo esc_html($cat->name); ?>
                    </a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <h1 class="product-title" itemprop="name"><?php the_title(); ?></h1>
            
            <?php if ($rating && $review_count > 0): ?>
            <div class="product-rating-summary">
                <div class="stars-display">
                    <?php for ($i = 1; $i <= 5; $i++): ?>
                        <span class="star <?php echo $i <= round($rating) ? 'filled' : ''; ?>">★</span>
                    <?php endfor; ?>
                </div>
                <span class="rating-text">
                    <?php echo esc_html($rating); ?> از ۵ (<?php echo intval($review_count); ?> نظر)
                </span>
                <a href="#reviews-section" class="view-reviews-link">مشاهده نظرات</a>
            </div>
            <?php endif; ?>
            
            <?php if ($sku): ?>
            <div class="product-sku">
                <span class="sku-label">کد محصول:</span>
                <span class="sku-value" itemprop="sku"><?php echo esc_html($sku); ?></span>
            </div>
            <?php endif; ?>
            
            <div class="product-price-section" itemprop="offers" itemscope itemtype="https://schema.org/Offer">
                <meta itemprop="priceCurrency" content="IRR">
                <meta itemprop="availability" content="<?php echo $is_in_stock ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock'; ?>">
                
                <?php if ($flash_price): ?>
                    <div class="price-with-discount">
                        <span class="original-price"><del><?php echo number_format($price); ?></del></span>
                        <span class="discount-percent"><?php echo $discount_percent; ?>٪ تخفیف</span>
                    </div>
                    <div class="final-price">
                        <span class="price-label">قیمت:</span>
                        <span class="price-value" itemprop="price"><?php echo number_format($flash_price); ?></span>
                        <span class="price-currency"><?php echo esc_html($currency); ?></span>
                    </div>
                <?php else: ?>
                    <div class="final-price">
                        <span class="price-label">قیمت:</span>
                        <span class="price-value" itemprop="price"><?php echo number_format($price); ?></span>
                        <span class="price-currency"><?php echo esc_html($currency); ?></span>
                    </div>
                <?php endif; ?>
            </div>
            
            <div class="product-stock-status">
                <?php if ($is_in_stock): ?>
                    <span class="stock-badge in-stock">
                        <span class="stock-icon">✓</span>
                        موجود در انبار
                        <?php if ($stock !== '' && intval($stock) > 0): ?>
                            (<?php echo intval($stock); ?> عدد)
                        <?php endif; ?>
                    </span>
                <?php else: ?>
                    <span class="stock-badge out-of-stock">
                        <span class="stock-icon">✗</span>
                        ناموجود
                    </span>
                <?php endif; ?>
            </div>
            
            <?php if (has_excerpt()): ?>
            <div class="product-short-description" itemprop="description">
                <?php the_excerpt(); ?>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($attributes) && !is_wp_error($attributes)): ?>
            <div class="product-attributes">
                <h3 class="attributes-title">ویژگی‌ها:</h3>
                <ul class="attributes-list">
                    <?php foreach ($attributes as $attr): ?>
                        <li class="attribute-item">
                            <span class="attribute-icon">✓</span>
                            <span><?php echo esc_html($attr->name); ?></span>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>
            
            <div class="product-actions">
                <?php if ($is_in_stock): ?>
                    <button class="btn btn-primary add-to-cart-btn" data-product-id="<?php echo esc_attr($product_id); ?>">
                        🛒 افزودن به سبد خرید
                    </button>
                <?php else: ?>
                    <button class="btn btn-disabled" disabled>ناموجود</button>
                <?php endif; ?>
                
                <button class="btn btn-icon wishlist-btn <?php echo $is_in_wishlist ? 'active' : ''; ?>" 
                        data-product-id="<?php echo esc_attr($product_id); ?>">
                    <?php echo $is_in_wishlist ? '♥' : '♡'; ?>
                </button>
                
                <button class="btn btn-icon compare-btn" data-product-id="<?php echo esc_attr($product_id); ?>">
                    ⚖️
                </button>
            </div>
            
            <div class="product-share">
                <span class="share-label">اشتراک‌گذاری:</span>
                <div class="share-buttons">
                    <a href="https://t.me/share/url?url=<?php echo urlencode(get_permalink()); ?>" target="_blank" class="share-btn">📱</a>
                    <a href="https://wa.me/?text=<?php echo urlencode(get_the_title() . ' ' . get_permalink()); ?>" target="_blank" class="share-btn">💬</a>
                </div>
            </div>
        </div>
    </article>
    
    <!-- توضیحات کامل -->
    <?php if (get_the_content()): ?>
    <section class="dima-section">
        <div class="section-header">
            <h2>📝 توضیحات کامل محصول</h2>
        </div>
        <div class="description-content">
            <?php the_content(); ?>
        </div>
    </section>
    <?php endif; ?>
    
    <!-- نظرات -->
    <section id="reviews-section" class="dima-section">
        <div class="section-header">
            <h2>💬 نظرات کاربران (<?php echo intval($review_count); ?>)</h2>
        </div>
        
        <?php 
        $reviews = class_exists('Dima_Review') ? Dima_Review::get_instance()->get_product_reviews($product_id, 20) : array();
        ?>
        
        <?php if ($review_count > 0): ?>
        <div class="review-summary-box">
            <div class="review-avg-display">
                <div class="big-rating"><?php echo esc_html($rating ?: '۰'); ?></div>
                <div class="stars-display">
                    <?php for ($i = 1; $i <= 5; $i++): ?>
                        <span class="star <?php echo $i <= round($rating) ? 'filled' : ''; ?>">★</span>
                    <?php endfor; ?>
                </div>
                <div class="total-reviews"><?php echo intval($review_count); ?> نظر</div>
            </div>
            
            <div class="review-bars">
                <?php
                global $wpdb;
                for ($s = 5; $s >= 1; $s--):
                    $count = $wpdb->get_var($wpdb->prepare(
                        "SELECT COUNT(*) FROM {$wpdb->prefix}dima_reviews WHERE product_id = %d AND rating = %d AND status = 'approved'",
                        $product_id, $s
                    ));
                    $percent = $review_count > 0 ? ($count / $review_count) * 100 : 0;
                ?>
                <div class="review-bar-row">
                    <span class="bar-label"><?php echo $s; ?> ★</span>
                    <div class="bar-track">
                        <div class="bar-fill" style="width: <?php echo $percent; ?>%"></div>
                    </div>
                    <span class="bar-count"><?php echo intval($count); ?></span>
                </div>
                <?php endfor; ?>
            </div>
        </div>
        <?php endif; ?>
        
        <?php if (!empty($reviews)): ?>
        <div class="reviews-list">
            <?php foreach ($reviews as $review): ?>
            <div class="review-item">
                <div class="review-header">
                    <div class="review-author">
                        <div class="author-avatar"><?php echo mb_substr($review->author_name, 0, 1); ?></div>
                        <div class="author-info">
                            <div class="author-name"><?php echo esc_html($review->author_name); ?></div>
                            <?php if ($review->verified_purchase): ?>
                                <div class="verified-badge">✓ خریدار تایید شده</div>
                            <?php endif; ?>
                        </div>
                    </div>
                    <div class="review-meta">
                        <div class="review-stars">
                            <?php for ($i = 1; $i <= 5; $i++): ?>
                                <span class="star <?php echo $i <= $review->rating ? 'filled' : ''; ?>">★</span>
                            <?php endfor; ?>
                        </div>
                        <div class="review-date"><?php echo date_i18n('j F Y', strtotime($review->created_at)); ?></div>
                    </div>
                </div>
                <?php if ($review->title): ?>
                    <h4 class="review-title"><?php echo esc_html($review->title); ?></h4>
                <?php endif; ?>
                <p class="review-content"><?php echo esc_html($review->content); ?></p>
            </div>
            <?php endforeach; ?>
        </div>
        <?php else: ?>
            <div class="empty-reviews">
                <div class="empty-icon">💬</div>
                <h3>هنوز نظری ثبت نشده است</h3>
            </div>
        <?php endif; ?>
        
        <!-- فرم نظر -->
        <div class="review-form-section">
            <h3>✍️ نظر خود را بنویسید</h3>
            <form class="review-form" id="review-form">
                <div class="form-group">
                    <label>امتیاز شما:</label>
                    <div class="rating-input" id="rating-input">
                        <?php for ($i = 5; $i >= 1; $i--): ?>
                            <span class="rate-star" data-value="<?php echo $i; ?>">★</span>
                        <?php endfor; ?>
                    </div>
                    <input type="hidden" name="rating" id="rating-value" value="5">
                </div>
                
                <div class="form-group">
                    <label>عنوان نظر:</label>
                    <input type="text" name="title" placeholder="خلاصه نظر شما">
                </div>
                
                <div class="form-group">
                    <label>نظر شما: *</label>
                    <textarea name="content" required placeholder="تجربه خود را بنویسید..." rows="5"></textarea>
                </div>
                
                <input type="hidden" name="product_id" value="<?php echo esc_attr($product_id); ?>">
                <?php wp_nonce_field('dima_shop_nonce', 'nonce'); ?>
                
                <button type="submit" class="btn btn-primary">📤 ارسال نظر</button>
            </form>
        </div>
    </section>
    
    <?php endwhile;
endif; ?>

</div>

<?php get_footer(); ?>