<?php
/**
 * CloudForge Theme - Product List (with multi-condition filtering)
 */
$pageDescription = '产品中心 - ' . SITE_NAME;
$breadcrumbItems = [['title' => '产品中心']];
$urlPattern = (get_setting('url_mode', 1) == 1) ? '?p=products&page=%d' : '/products/%d.html';
$pagination = paginate($total, $page, $pageSize, $urlPattern);

$categories = db_query("SELECT * FROM `product_categories` WHERE `status`=1 ORDER BY `sort_order` ASC");
$filter_attributes = db_query("SELECT * FROM `product_attributes` WHERE `status`=1 ORDER BY `sort_order` ASC");

$current_filters = [];
foreach ($filter_attributes as $attr) {
    $param_name = 'filter_' . $attr['slug'];
    if (isset($_GET[$param_name])) {
        $current_filters[$attr['slug']] = $_GET[$param_name];
    }
}
$price_min = isset($_GET['price_min']) ? floatval($_GET['price_min']) : 0;
$price_max = isset($_GET['price_max']) ? floatval($_GET['price_max']) : 0;
?>
<?php echo breadcrumb($breadcrumbItems); ?>

<div class="main-content">
    <div class="container container--sidebar-reverse">
        <!-- Filter Sidebar -->
        <aside class="sidebar sidebar--filter">
            <div class="widget">
                <h3 class="widget-title">// 产品分类</h3>
                <ul class="widget-list">
                    <li><a href="<?php echo cms_url('products'); ?>" class="<?php echo !$category ? 'is-active' : ''; ?>">全部产品</a></li>
                    <?php foreach ($categories as $cat): ?>
                    <li><a href="<?php echo cms_url('products', ['category' => $cat['slug']]); ?>" class="<?php echo htmlspecialchars($category ?? '', ENT_QUOTES, 'UTF-8') == $cat['slug'] ? 'is-active' : ''; ?>"><?php echo xss_clean($cat['name']); ?></a></li>
                    <?php endforeach; ?>
                </ul>
            </div>

            <?php if ($filter_attributes): ?>
            <div class="widget">
                <h3 class="widget-title">// 筛选条件</h3>
                <form method="get" action="<?php echo cms_url('products'); ?>" id="filterForm" class="filter-form">
                    <?php if ($category): ?>
                    <input type="hidden" name="category" value="<?php echo xss_clean($category); ?>">
                    <?php endif; ?>

                    <?php foreach ($filter_attributes as $attr): ?>
                    <div class="filter-group">
                        <h4 class="filter-group-title"><?php echo htmlspecialchars($attr['name']); ?></h4>
                        <?php $options = $attr['options'] ? json_decode($attr['options'], true) : []; ?>
                        <?php $param_name = 'filter_' . $attr['slug']; ?>

                        <?php if ($attr['type'] == 'select' || $attr['type'] == 'radio'): ?>
                        <div class="filter-options">
                            <label class="filter-option">
                                <input type="radio" name="<?php echo $param_name; ?>" value="" <?php echo !isset($current_filters[$attr['slug']]) ? 'checked' : ''; ?>>
                                <span>全部</span>
                            </label>
                            <?php foreach ($options as $opt): ?>
                            <label class="filter-option">
                                <input type="radio" name="<?php echo $param_name; ?>" value="<?php echo htmlspecialchars($opt); ?>" <?php echo (isset($current_filters[$attr['slug']]) && $current_filters[$attr['slug']] == $opt) ? 'checked' : ''; ?>>
                                <span><?php echo htmlspecialchars($opt); ?></span>
                            </label>
                            <?php endforeach; ?>
                        </div>
                        <?php elseif ($attr['type'] == 'multiselect' || $attr['type'] == 'checkbox'): ?>
                        <div class="filter-options">
                            <?php foreach ($options as $opt): ?>
                            <label class="filter-option">
                                <input type="checkbox" name="<?php echo $param_name; ?>[]" value="<?php echo htmlspecialchars($opt); ?>" <?php echo (isset($current_filters[$attr['slug']]) && is_array($current_filters[$attr['slug']]) && in_array($opt, $current_filters[$attr['slug']])) ? 'checked' : ''; ?>>
                                <span><?php echo htmlspecialchars($opt); ?></span>
                            </label>
                            <?php endforeach; ?>
                        </div>
                        <?php elseif ($attr['type'] == 'price_range'): ?>
                        <div class="filter-price">
                            <input type="number" name="price_min" placeholder="最低" value="<?php echo $price_min > 0 ? $price_min : ''; ?>">
                            <span class="filter-price-sep">—</span>
                            <input type="number" name="price_max" placeholder="最高" value="<?php echo $price_max > 0 ? $price_max : ''; ?>">
                        </div>
                        <?php elseif ($attr['type'] == 'text'): ?>
                        <input type="text" class="form-input" name="<?php echo $param_name; ?>" value="<?php echo xss_clean($current_filters[$attr['slug']] ?? ''); ?>" placeholder="输入<?php echo htmlspecialchars($attr['name']); ?>">
                        <?php elseif ($attr['type'] == 'number'): ?>
                        <input type="number" class="form-input" name="<?php echo $param_name; ?>" value="<?php echo xss_clean($current_filters[$attr['slug']] ?? ''); ?>" placeholder="输入数值">
                        <?php endif; ?>
                    </div>
                    <?php endforeach; ?>

                    <div class="filter-actions">
                        <button type="submit" class="btn btn--primary btn--sm">筛选</button>
                        <a href="<?php echo cms_url('products'); ?>" class="btn btn--ghost btn--sm">重置</a>
                    </div>
                </form>
            </div>
            <?php endif; ?>
        </aside>

        <!-- Product List -->
        <div class="content-main">
            <div class="section-header">
                <span class="section-label">// PRODUCTS</span>
                <h2 class="section-title">产品中心</h2>
            </div>

            <?php if ($current_filters || $price_min > 0 || $price_max > 0): ?>
            <div class="current-filters">
                <span class="current-filters-label">当前筛选:</span>
                <?php foreach ($current_filters as $slug => $value):
                    $attr_name = '';
                    foreach ($filter_attributes as $attr) {
                        if ($attr['slug'] == $slug) { $attr_name = $attr['name']; break; }
                    }
                ?>
                <span class="filter-tag">
                    <?php echo htmlspecialchars($attr_name); ?>:
                    <?php if (is_array($value)): ?>
                        <?php echo implode(', ', array_map('htmlspecialchars', $value)); ?>
                    <?php else: ?>
                        <?php echo htmlspecialchars($value); ?>
                    <?php endif; ?>
                </span>
                <?php endforeach; ?>
                <?php if ($price_min > 0 || $price_max > 0): ?>
                <span class="filter-tag">价格: ¥<?php echo $price_min; ?> — ¥<?php echo $price_max > 0 ? $price_max : '∞'; ?></span>
                <?php endif; ?>
                <a href="<?php echo cms_url('products'); ?>" class="filter-clear">清除全部</a>
            </div>
            <?php endif; ?>

            <?php if (empty($products)): ?>
            <div class="empty-state">
                <svg width="48" height="48" viewBox="0 0 48 48" fill="none" opacity="0.3"><path d="M8 18l16-8 16 8v18l-16 8-16-8V18z" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round"/><path d="M8 18l16 8 16-8M24 26v18" stroke="currentColor" stroke-width="1.5"/></svg>
                <p>暂无产品</p>
            </div>
            <?php else: ?>
            <div class="product-grid">
                <?php foreach ($products as $p): ?>
                <article class="product-card">
                    <a href="<?php echo cms_url('product', ['slug' => $p['slug']]); ?>" class="product-card-link">
                        <div class="product-card-image">
                            <?php if ($p['cover_image']): ?>
                            <img src="<?php echo $p['cover_image']; ?>" alt="<?php echo xss_clean($p['title']); ?>" loading="lazy">
                            <?php else: ?>
                            <div class="product-card-placeholder">
                                <svg width="40" height="40" viewBox="0 0 40 40" fill="none"><rect x="6" y="10" width="28" height="22" rx="2" stroke="currentColor" stroke-width="1.5"/><path d="M6 16h28" stroke="currentColor" stroke-width="1.5"/></svg>
                            </div>
                            <?php endif; ?>
                        </div>
                        <div class="product-card-body">
                            <h3 class="product-card-title"><?php echo xss_clean($p['title']); ?></h3>
                            <p class="product-card-desc"><?php echo xss_clean(str_cut($p['summary'], 60)); ?></p>
                            <div class="product-card-footer">
                                <?php if ($p['price']): ?>
                                <span class="product-price">¥<?php echo $p['price']; ?></span>
                                <?php endif; ?>
                                <?php if ($p['sales']): ?>
                                <span class="product-sales">已售 <?php echo $p['sales']; ?></span>
                                <?php endif; ?>
                            </div>
                        </div>
                    </a>
                </article>
                <?php endforeach; ?>
            </div>

            <?php if ($totalPage > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?><a href="<?php echo cms_url('products', ['page' => $page - 1]); ?>" class="page-prev">&larr; 上一页</a><?php endif; ?>
                <?php for ($i = max(1, $page - 2); $i <= min($totalPage, $page + 2); $i++): ?>
                    <?php if ($i == $page): ?><span class="current"><?php echo $i; ?></span>
                    <?php else: ?><a href="<?php echo cms_url('products', ['page' => $i]); ?>"><?php echo $i; ?></a>
                    <?php endif; ?>
                <?php endfor; ?>
                <?php if ($page < $totalPage): ?><a href="<?php echo cms_url('products', ['page' => $page + 1]); ?>" class="page-next">下一页 &rarr;</a><?php endif; ?>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
