<?php
/**
 * CloudForge Theme - Product Detail
 */
$pageDescription = $product['summary'] ?: strip_tags(mb_substr($product['content'], 0, 200, 'UTF-8'));
$pageKeywords = $product['seo_keywords'];
$breadcrumbItems = [
    ['title' => '产品中心', 'url' => cms_url('products')],
    ['title' => $product['title']]
];
?>
<?php echo breadcrumb($breadcrumbItems); ?>

<div class="main-content">
    <div class="container container--sidebar">
        <div class="content-main">
            <div class="product-detail">
                <!-- Product Header: Image + Info -->
                <div class="product-detail-header">
                    <div class="product-detail-gallery">
                        <div class="product-detail-main-image">
                            <?php if ($product['cover_image']): ?>
                            <img src="<?php echo $product['cover_image']; ?>" alt="<?php echo xss_clean($product['title']); ?>">
                            <?php else: ?>
                            <div class="product-detail-placeholder">
                                <svg width="64" height="64" viewBox="0 0 64 64" fill="none" opacity="0.2"><rect x="10" y="16" width="44" height="36" rx="3" stroke="currentColor" stroke-width="1.5"/><path d="M10 26h44" stroke="currentColor" stroke-width="1.5"/></svg>
                            </div>
                            <?php endif; ?>
                        </div>
                        <?php if ($product['gallery']): ?>
                        <?php $gallery = json_decode($product['gallery'], true); ?>
                        <?php if ($gallery): ?>
                        <div class="product-detail-thumbs">
                            <?php foreach ($gallery as $img): ?>
                            <div class="product-detail-thumb">
                                <img src="<?php echo $img; ?>" alt="" loading="lazy">
                            </div>
                            <?php endforeach; ?>
                        </div>
                        <?php endif; ?>
                        <?php endif; ?>
                    </div>

                    <div class="product-detail-info">
                        <h1 class="product-detail-title"><?php echo xss_clean($product['title']); ?></h1>

                        <?php if ($product['price']): ?>
                        <div class="product-detail-price">
                            <span class="price-current">¥<?php echo $product['price']; ?></span>
                            <?php if ($product['original_price'] && $product['original_price'] > $product['price']): ?>
                            <span class="price-original">¥<?php echo $product['original_price']; ?></span>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>

                        <dl class="product-detail-attrs">
                            <?php if ($product['sku']): ?>
                            <div class="attr-row"><dt>商品编号</dt><dd><?php echo xss_clean($product['sku']); ?></dd></div>
                            <?php endif; ?>
                            <?php if ($product['stock'] !== null): ?>
                            <div class="attr-row"><dt>库存</dt><dd><?php echo $product['stock']; ?> <?php echo xss_clean($product['unit'] ?: '件'); ?></dd></div>
                            <?php endif; ?>
                            <div class="attr-row"><dt>销量</dt><dd><?php echo $product['sales']; ?></dd></div>
                            <div class="attr-row"><dt>浏览量</dt><dd><?php echo $product['views']; ?></dd></div>
                        </dl>

                        <div class="product-detail-actions">
                            <a href="<?php echo cms_url('contact'); ?>" class="btn btn--primary btn--lg">
                                <span>立即咨询</span>
                                <svg width="16" height="16" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
                            </a>
                            <button class="btn btn--outline btn--lg" onclick="navigator.clipboard.writeText(window.location.href); this.querySelector('span').textContent='已复制';">
                                <span>分享链接</span>
                            </button>
                        </div>
                    </div>
                </div>

                <!-- Product Content -->
                <div class="product-detail-content">
                    <div class="section-header">
                        <span class="section-label">// DETAILS</span>
                        <h3 class="section-title">产品详情</h3>
                    </div>
                    <div class="article-content">
                        <?php echo $product['content']; ?>
                    </div>
                </div>
            </div>
        </div>

        <aside class="sidebar">
            <div class="widget">
                <h3 class="widget-title">// 热门产品</h3>
                <ul class="widget-list widget-list--ranked">
                    <?php $hotProducts = db_query("SELECT `title`,`slug` FROM `products` WHERE `status`=1 AND `is_hot`=1 ORDER BY `sort_order` ASC LIMIT 5"); ?>
                    <?php foreach ($hotProducts as $idx => $hp): ?>
                    <li>
                        <span class="rank-num"><?php echo sprintf('%02d', $idx + 1); ?></span>
                        <a href="<?php echo cms_url('product', ['slug' => $hp['slug']]); ?>"><?php echo xss_clean($hp['title']); ?></a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <div class="widget">
                <h3 class="widget-title">// 推荐产品</h3>
                <ul class="widget-list">
                    <?php $recProducts = db_query("SELECT `title`,`slug` FROM `products` WHERE `status`=1 AND `is_recommend`=1 LIMIT 5"); ?>
                    <?php foreach ($recProducts as $rp): ?>
                    <li><a href="<?php echo cms_url('product', ['slug' => $rp['slug']]); ?>"><?php echo xss_clean($rp['title']); ?></a></li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </aside>
    </div>
</div>
