<?php
require_once __DIR__ . '/db/config.php';

$pdo = db();
$settings_res = $pdo->query("SELECT `key`, `value` FROM website_settings")->fetchAll();
$settings = [];
foreach ($settings_res as $row) {
    $settings[$row['key']] = $row['value'];
}

$project_name = $settings['site_name'] ?? '智域科技';
$logo_path = $settings['site_logo'] ?? '/assets/pasted-20260226-073317-a8105f30.png';
$selected_category = $_GET['category'] ?? 'all';

// Fetch cases from DB
if ($selected_category === 'all') {
    $stmt = $pdo->prepare("SELECT * FROM cases WHERE is_visible = 1 ORDER BY sort_order ASC, created_at DESC");
    $stmt->execute();
} else {
    $stmt = $pdo->prepare("SELECT * FROM cases WHERE is_visible = 1 AND category = ? ORDER BY sort_order ASC, created_at DESC");
    $stmt->execute([$selected_category]);
}
$filtered_cases = $stmt->fetchAll();

$scope_items = [
    '海外资金盘', '点赞盘', '刷单盘', '商城盘', '云矿机', '新能源', 
    '投资理财', '反波胆', '微交易', '时间盘', '交易所', '各类小程序', 
    '机器人', '落地页定制', '远控系统', '授权系统', '在线客服', '获取通讯录'
];

// 手机端视图逻辑
$view = 'works';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作品案例中心 - <?= htmlspecialchars($project_name) ?></title>
    <link rel="icon" href="<?= $logo_path ?>" type="image/png">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
    <link href="/assets/css/custom.css?v=<?= time() ?>" rel="stylesheet">
    <style>
        .filter-btn {
            white-space: nowrap;
            padding: 8px 20px;
            border-radius: 50px;
            font-size: 0.85rem;
            font-weight: 600;
            border: 1px solid #eee;
            background: #fff;
            color: #666;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin-bottom: 10px;
        }
        .filter-btn:hover, .filter-btn.active {
            background: var(--primary-color, #0d6efd);
            color: #fff;
            border-color: var(--primary-color, #0d6efd);
            box-shadow: 0 4px 12px rgba(13, 110, 253, 0.2);
        }
        .filter-scroll {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            padding-bottom: 10px;
        }
        .filter-scroll::-webkit-scrollbar {
            height: 4px;
        }
        .filter-scroll::-webkit-scrollbar-thumb {
            background: #eee;
            border-radius: 10px;
        }
    </style>
</head>
<body class="bg-subtle">
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light bg-white border-bottom fixed-top scrolled">
        <div class="container">
            <a class="navbar-brand d-flex align-items-center gap-2" href="/">
                <img src="<?= $logo_path ?>" alt="<?= htmlspecialchars($project_name) ?>" >
                <span class="fw-bold text-dark"><?= htmlspecialchars($project_name) ?></span>
            </a>
            <a href="/" class="btn btn-outline-primary btn-sm ms-auto fw-bold"><i class="bi bi-house"></i> 返回首页</a>
        </div>
    </nav>

    <main class="py-5 mt-5">
        <div class="container py-5">
            <div class="text-center mb-5">
                <span class="section-subtitle">Portfolio Showcase</span>
                <h1 class="display-6 fw-bold mb-3">作品案例展示中心</h1>
                <p class="text-muted mx-auto" style="max-width: 700px;">
                    智域科技在多个垂直领域拥有深厚的技术积累。下方展示了我们为全球客户打造的部分精品项目案例，涵盖了从分布式架构到行业定制化方案的全方位交付成果。
                </p>
            </div>

            <!-- 分类过滤器 -->
            <div class="mb-5">
                <h6 class="fw-bold mb-3"><i class="bi bi-filter-left me-2"></i> 按业务范围筛选：</h6>
                <div class="filter-scroll d-flex gap-2">
                    <a href="?category=all" class="filter-btn <?= $selected_category == 'all' ? 'active' : '' ?>">全部案例</a>
                    <?php foreach ($scope_items as $item): ?>
                    <a href="?category=<?= urlencode($item) ?>" class="filter-btn <?= $selected_category == $item ? 'active' : '' ?>"><?= $item ?></a>
                    <?php endforeach; ?>
                </div>
            </div>

            <!-- 案例列表 -->
            <?php if (empty($filtered_cases)): ?>
            <div class="text-center py-5">
                <div class="icon-box-colorful bg-light text-muted mx-auto mb-4" style="width: 80px; height: 80px; font-size: 2.5rem;">
                    <i class="bi bi-folder-x"></i>
                </div>
                <h5>暂无该类别的公开案例</h5>
                <p class="text-muted">您可以联系我们的专家，获取更多未公开的定制化方案细节。</p>
                <a href="<?= $settings['tg_link'] ?? 'https://t.me/zhangshihao818' ?>" class="btn btn-primary mt-3 px-4">立即咨询专家</a>
            </div>
            <?php else: ?>
            <div class="row g-4">
                <?php foreach ($filtered_cases as $case): ?>
                <div class="col-md-6 col-lg-4 col-xl-3">
                    <div class="case-card h-100 shadow-sm border-0">
                        <div class="case-image-wrapper">
                            <img src="<?= htmlspecialchars($case['img']) ?>" alt="<?= htmlspecialchars($case['title']) ?>" class="case-image">
                        </div>
                        <div class="case-content p-4 d-flex flex-column">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <span class="badge bg-primary bg-opacity-10 text-primary"><?= htmlspecialchars($case['tag']) ?></span>
                                <span class="x-small text-muted"><?= htmlspecialchars($case['category']) ?></span>
                            </div>
                            <h6 class="fw-bold mb-3"><?= htmlspecialchars($case['title']) ?></h6>
                            <p class="x-small text-muted mb-4 flex-grow-1"><?= htmlspecialchars($case['description']) ?></p>
                            <a href="case-detail.php?id=<?= $case['slug'] ?>" class="text-primary small text-decoration-none fw-bold mt-auto">查看案例细节 <i class="bi bi-chevron-right"></i></a>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
        </div>
    </main>

    <!-- 底部 CTA -->
    <section class="container py-5 mb-5">
        <div class="tech-card bg-dark text-white p-5 text-center overflow-hidden shadow-lg border-0 rounded-4">
            <h3 class="fw-bold mb-3">没有看到您所需的特定案例？</h3>
            <p class="lead mb-4 opacity-75">我们的技术储备远超网页展示。请联系我们，为您匹配最接近的实战经验。</p>
            <a href="<?= $settings['tg_link'] ?? 'https://t.me/zhangshihao818' ?>" class="btn btn-primary btn-lg px-5 py-3 shadow">
                <i class="bi bi-telegram me-2"></i> 立即沟通定制需求
            </a>
        </div>
    </section>

    <footer class="py-5 bg-white border-top">
        <div class="container text-center small text-muted">
            <p class="mb-0">&copy; 2020 <?= htmlspecialchars($project_name) ?>. 专业数字化工程与行业方案专家.</p>
        </div>
    </footer>

    <!-- 手机端底部菜单 -->
    <div class="mobile-bottom-nav">
        <a href="index.php?v=home" class="mobile-nav-item">
            <i class="bi bi-house-door"></i>
            <span>首页</span>
        </a>
        <a href="cases.php" class="mobile-nav-item active">
            <i class="bi bi-briefcase"></i>
            <span>作品</span>
        </a>
        <a href="index.php?v=pricing#pricing" class="mobile-nav-item">
            <i class="bi bi-tags"></i>
            <span>报价</span>
        </a>
        <a href="<?= $settings['tg_link'] ?? 'https://t.me/zhangshihao818' ?>" class="mobile-nav-item highlight">
            <i class="bi bi-telegram"></i>
            <span>咨询</span>
        </a>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/assets/js/main.js?v=<?= time() ?>"></script>
</body>
</html>