<?php
$pageTitle = '商家推荐';
require_once 'header.php';
require_once 'config.php';

// 获取酒店（从 custom_details 中动态查找婚宴酒店分类ID）
try {
    $stmt = $pdo->query("SELECT cd.id, cd.title, cd.content, cd.sort_order 
        FROM custom_details cd 
        JOIN custom_categories cc ON cd.category_id = cc.id 
        WHERE cc.name = '婚宴酒店' AND cd.sort_order <= 15 
        ORDER BY cd.sort_order");
    $hotels = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $hotels = [];
}

// 获取其他商家（从 vendors 表）
try {
    $stmt = $pdo->query("SELECT * FROM vendors ORDER BY sort_order");
    $otherVendors = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $otherVendors = [];
}

// 合并所有商家类型用于筛选
$vendorTypes = ['酒店', '婚庆', '婚车', '摄影', '化妆', '礼服'];
?>

<main class="site-main">

    <section class="page-banner">
        <div class="container">
            <h1>🏪 商家推荐</h1>
            <p>精选阳泉本地优质婚庆商家，涵盖酒店、婚庆、婚车、摄影、化妆、礼服等全方位服务</p>
        </div>
    </section>

    <section class="vendors-list">
        <div class="container">
            
            <!-- 商家筛选 -->
            <div class="vendor-filters">
                <button class="filter-btn active" data-filter="all">全部</button>
                <?php foreach ($vendorTypes as $type): ?>
                <button class="filter-btn" data-filter="<?php echo $type; ?>"><?php echo $type; ?></button>
                <?php endforeach; ?>
            </div>

            <!-- 商家列表 -->
            <div class="vendor-grid">
                <!-- 酒店（从知识库 custom_details 读取） -->
                <?php foreach ($hotels as $hotel): 
                    // 解析酒店信息
                    preg_match('/地址：(.+?) \| /', $hotel['content'], $m); $addr = $m[1] ?? '';
                    preg_match('/最大容纳：(.+?) \| /', $hotel['content'], $m); $cap = $m[1] ?? '';
                    preg_match('/餐标：(.+?) \| /', $hotel['content'], $m); $price = $m[1] ?? '';
                    preg_match('/特色：(.+?)$/', $hotel['content'], $m); $feat = $m[1] ?? '';
                    // Extract phone if present
                    $phone = '';
                    if (preg_match('/电话：(.+?)$/', $hotel['content'], $m)) $phone = $m[1];
                    
                    // 判断区域
                    $sort = $hotel['sort_order'];
                    if ($sort <= 5) $region = '开发区';
                    elseif ($sort <= 9) $region = '城区';
                    elseif ($sort <= 11) $region = '近郊景区';
                    else $region = '平定';
                ?>
                <div class="vendor-card" data-type="酒店">
                    <div class="vendor-header">
                        <span class="vendor-type">酒店 · <?php echo $region; ?></span>
                        <span class="vendor-detail-badge"><?php echo htmlspecialchars($cap); ?></span>
                    </div>
                    <h3><?php echo htmlspecialchars($hotel['title']); ?></h3>
                    <p class="vendor-description"><?php echo htmlspecialchars($feat); ?></p>
                    <div class="vendor-info">
                        <p><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($addr); ?></p>
                        <p><i class="fas fa-tag"></i> <?php echo htmlspecialchars($price); ?></p>
                        <?php if ($phone): ?>
                        <p><i class="fas fa-phone-alt"></i> <?php echo htmlspecialchars($phone); ?></p>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>

                <!-- 其他商家（从 vendors 表读取） -->
                <?php foreach ($otherVendors as $v): ?>
                <div class="vendor-card" data-type="<?php echo $v['type']; ?>">
                    <div class="vendor-header">
                        <span class="vendor-type"><?php echo $v['type']; ?></span>
                        <span class="vendor-rating">⭐ <?php echo $v['rating']; ?></span>
                    </div>
                    <h3><?php echo htmlspecialchars($v['name']); ?></h3>
                    <p class="vendor-description"><?php echo htmlspecialchars($v['description']); ?></p>
                    <div class="vendor-info">
                        <?php if ($v['address']): ?>
                        <p><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($v['address']); ?></p>
                        <?php endif; ?>
                        <?php if ($v['phone']): ?>
                        <p><i class="fas fa-phone-alt"></i> <?php echo htmlspecialchars($v['phone']); ?></p>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>

            <?php if (empty($hotels) && empty($otherVendors)): ?>
            <p class="empty-state">暂无商家数据，请先在数据库中初始化。</p>
            <?php endif; ?>
        </div>
    </section>

</main>

<script>
// 筛选功能
document.querySelectorAll('.filter-btn').forEach(function(btn) {
    btn.addEventListener('click', function() {
        document.querySelectorAll('.filter-btn').forEach(function(b) { b.classList.remove('active'); });
        this.classList.add('active');
        var filter = this.getAttribute('data-filter');
        document.querySelectorAll('.vendor-card').forEach(function(card) {
            if (filter === 'all' || card.getAttribute('data-type') === filter) {
                card.style.display = 'block';
            } else {
                card.style.display = 'none';
            }
        });
    });
});
</script>

<?php require_once 'footer.php'; ?>