<?php
/**
 * 2026企业CMS - 默认模板产品列表页（支持多条件筛选）
 */
$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" style="display:flex;gap:30px;">
        <!-- 侧边栏筛选 -->
        <div class="sidebar" style="width:280px;">
            <!-- 分类筛选 -->
            <div class="widget">
                <h3 class="widget-title">产品分类</h3>
                <ul style="list-style:none;">
                    <li style="padding:8px 0;border-bottom:1px solid #eee;">
                        <a href="<?php echo cms_url('products'); ?>" style="<?php echo !$category?'font-weight:bold;color:#2c82e0;':''; ?>">全部产品</a>
                    </li>
                    <?php foreach ($categories as $cat): ?>
                    <li style="padding:8px 0;border-bottom:1px solid #eee;">
                        <a href="<?php echo cms_url('products', ['category' => $cat['slug']]); ?>" style="<?php echo htmlspecialchars($category, ENT_QUOTES, "UTF-8")==$cat['slug']?'font-weight:bold;color:#2c82e0;':''; ?>"><?php echo xss_clean($cat['name']); ?></a>
                    </li>
                    <?php endforeach; ?>
                </ul>
            </div>
            
            <!-- 多条件筛选 -->
            <?php if ($filter_attributes): ?>
            <div class="widget filter-widget">
                <h3 class="widget-title">筛选条件</h3>
                <form method="get" action="<?php echo cms_url('products'); ?>" id="filterForm">
                    <!-- 保留分类参数 -->
                    <?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-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':''; ?>>
                                        全部
                                    </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':''; ?>>
                                            <?php echo htmlspecialchars($opt); ?>
                                        </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':''; ?>>
                                            <?php echo htmlspecialchars($opt); ?>
                                        </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:''; ?>" style="width:48%;display:inline-block;">
                                    <span style="display:inline-block;width:4%;text-align:center;">-</span>
                                    <input type="number" name="price_max" placeholder="最高价" value="<?php echo $price_max>0?$price_max:''; ?>" style="width:48%;display:inline-block;">
                                </div>
                                
                            <?php elseif ($attr['type'] == 'text'): ?>
                                <!-- 文本输入 -->
                                <input type="text" 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" 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-secondary btn-sm">重置</a>
                    </div>
                </form>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- 产品列表 -->
        <div style="flex:1;">
            <h2 class="section-title" style="text-align:left;">产品中心</h2>
            
            <!-- 当前筛选条件显示 -->
            <?php if ($current_filters || $price_min>0 || $price_max>0): ?>
                <div class="current-filters">
                    <span>当前筛选：</span>
                    <?php foreach ($current_filters as $slug => $value): ?>
                        <?php
                        // 获取属性名称
                        $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; ?>
                            <a href="?<?php echo http_build_query(array_diff_key($_GET, [$param_name => ''])); ?>">×</a>
                        </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:'∞'; ?>
                            <a href="?<?php echo http_build_query(array_diff_key($_GET, ['price_min' => '', 'price_max' => ''])); ?>">×</a>
                        </span>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
            
            <?php if (empty($products)): ?>
            <div style="text-align:center;padding:60px;color:#999;">暂无产品</div>
            <?php else: ?>
            <div class="grid">
                <?php foreach ($products as $p): ?>
                <div class="card">
                    <a href="<?php echo cms_url('product', ['slug' => $p['slug']]); ?>">
                        <div class="card-img" style="height:200px;background:#f5f5f5;display:flex;align-items:center;justify-content:center;">
                            <?php if ($p['cover_image']): ?>
                            <img src="<?php echo $p['cover_image']; ?>" alt="<?php echo xss_clean($p['title']); ?>" style="width:100%;height:100%;object-fit:cover;">
                            <?php else: ?>
                            <span style="font-size:48px;color:#ccc;">📦</span>
                            <?php endif; ?>
                        </div>
                        <div class="card-body">
                            <div class="card-title" style="font-size:16px;"><?php echo xss_clean($p['title']); ?></div>
                            <div class="card-desc" style="font-size:13px;"><?php echo xss_clean(str_cut($p['summary'],60)); ?></div>
                            <div class="card-meta" style="display:flex;justify-content:space-between;align-items:center;margin-top:10px;">
                                <?php if ($p['price']): ?>
                                <span style="color:#e74c3c;font-weight:bold;font-size:18px;">¥<?php echo $p['price']; ?></span>
                                <?php endif; ?>
                                <span style="color:#999;font-size:12px;">已售<?php echo $p['sales']; ?></span>
                            </div>
                        </div>
                    </a>
                </div>
                <?php endforeach; ?>
            </div>
            
            <!-- 分页 -->
            <?php if ($totalPage > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?><a href="<?php echo cms_url('products', ['page' => $page-1]); ?>">上一页</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]); ?>">下一页</a><?php endif; ?>
            </div>
            <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- 筛选样式 -->
<style>
.filter-widget {
    margin-top: 20px;
    padding: 15px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}

.filter-group {
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid #eee;
}

.filter-group:last-of-type {
    border-bottom: none;
}

.filter-title {
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 10px;
    color: #333;
}

.filter-options {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.filter-option {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    cursor: pointer;
}

.filter-option input[type="radio"],
.filter-option input[type="checkbox"] {
    margin: 0;
}

.filter-price {
    display: flex;
    align-items: center;
    gap: 5px;
}

.filter-price input {
    padding: 5px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 13px;
}

.filter-actions {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}

.current-filters {
    margin-bottom: 20px;
    padding: 10px 15px;
    background: #f8f9fa;
    border-radius: 4px;
    font-size: 13px;
}

.filter-tag {
    display: inline-block;
    padding: 3px 8px;
    margin: 3px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 3px;
}

.filter-tag a {
    margin-left: 5px;
    color: #999;
    text-decoration: none;
}

.filter-tag a:hover {
    color: red;
}
</style>
