<?php
// 下载处理必须放在任何输出之前
if (isset($_GET['action']) && $_GET['action'] === 'download') {
    $type = $_GET['type'] ?? '';
    
    $downloads = [
        'checklist' => [
            'filename' => 'wedding_checklist_2026.csv',
            'name' => '婚礼筹备清单',
            'type' => 'text/csv; charset=utf-8'
        ],
        'schedule' => [
            'filename' => 'wedding_day_schedule_2026.csv',
            'name' => '婚礼当日流程表',
            'type' => 'text/csv; charset=utf-8'
        ],
        'roles' => [
            'filename' => 'wedding_roles_responsibilities_2026.csv',
            'name' => '婚礼角色分工表',
            'type' => 'text/csv; charset=utf-8'
        ],
        'budget' => [
            'filename' => 'wedding_budget_template_2026.csv',
            'name' => '婚礼预算表模板',
            'type' => 'text/csv; charset=utf-8'
        ],
        'speech' => [
            'filename' => 'wedding_speeches_templates_2026.html',
            'name' => '婚礼致辞范文',
            'type' => 'text/html; charset=utf-8'
        ],
        'checklist_pdf' => [
            'filename' => 'wedding_checklist_2026.html',
            'name' => '婚礼筹备清单（可打印版）',
            'type' => 'text/html; charset=utf-8'
        ],
        'schedule_pdf' => [
            'filename' => 'wedding_day_schedule_2026.html',
            'name' => '婚礼当日流程表（可打印版）',
            'type' => 'text/html; charset=utf-8'
        ],
        'roles_pdf' => [
            'filename' => 'wedding_roles_responsibilities_2026.html',
            'name' => '婚礼角色分工表（可打印版）',
            'type' => 'text/html; charset=utf-8'
        ],
        'vendor_template' => [
            'filename' => 'vendor_contact_template_2026.csv',
            'name' => '商家联系方式汇总表',
            'type' => 'text/csv; charset=utf-8'
        ],
        'guest_template' => [
            'filename' => 'guest_management_template_2026.csv',
            'name' => '宾客管理登记表',
            'type' => 'text/csv; charset=utf-8'
        ],
        'timeline' => [
            'filename' => 'wedding_preparation_timeline_2026.html',
            'name' => '婚礼筹备倒计时时间轴',
            'type' => 'text/html; charset=utf-8'
        ],
        'tips' => [
            'filename' => 'wedding_planning_tips_2026.html',
            'name' => '婚礼筹备注意事项大全',
            'type' => 'text/html; charset=utf-8'
        ],
        'items' => [
            'filename' => 'wedding_essential_items_2026.csv',
            'name' => '当日必备物品清单',
            'type' => 'text/csv; charset=utf-8'
        ],
        'home_return' => [
            'filename' => 'yangquan_home_return_banquet.html',
            'name' => '阳泉市区·酒店回门宴操作指南',
            'type' => 'text/html; charset=utf-8'
        ]
    ];
    
    if (isset($downloads[$type])) {
        $item = $downloads[$type];
        $filepath = __DIR__ . '/downloads/' . $type . '.php';
        
        if (file_exists($filepath)) {
            // 清空输出缓冲
            while (ob_get_level()) {
                ob_end_clean();
            }
            
            // 设置下载头
            header('Content-Type: ' . $item['type']);
            header('Content-Disposition: attachment; filename="' . $item['filename'] . '"');
            header('Cache-Control: no-cache, must-revalidate');
            header('Pragma: public');
            
            // 输出文件内容
            include($filepath);
            exit;
        }
    }
    
    // 如果没找到文件，返回错误
    http_response_code(404);
    echo '文件不存在';
    exit;
}

// 以下是页面显示逻辑
$pageTitle = '资料下载';
require_once 'header.php';
require_once 'config.php';
?>

<main class="site-main">
    <!-- 页面横幅 -->
    <section class="page-banner" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);">
        <div class="container">
            <h1>📥 资料下载中心</h1>
            <p>免费下载婚礼筹备所需的各种清单、模板和指南</p>
        </div>
    </section>

    <!-- 搜索筛选 -->
    <section class="download-filters">
        <div class="container">
            <div class="filter-bar">
                <div class="filter-tabs">
                    <button class="filter-tab active" data-category="all">全部资料</button>
                    <button class="filter-tab" data-category="checklist">清单类</button>
                    <button class="filter-tab" data-category="schedule">流程类</button>
                    <button class="filter-tab" data-category="template">模板类</button>
                    <button class="filter-tab" data-category="guide">指南类</button>
                </div>
                <div class="filter-formats">
                    <select id="formatFilter">
                        <option value="all">全部格式</option>
                        <option value="csv">CSV（Excel可用）</option>
                        <option value="html">HTML（可打印）</option>
                    </select>
                </div>
            </div>
        </div>
    </section>

    <!-- 下载卡片列表 -->
    <section class="downloads-grid-section">
        <div class="container">
            <div class="downloads-grid" id="downloadsGrid">
                
                <!-- 清单类 -->
                <div class="download-card" data-category="checklist" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-clipboard-list"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">热门</span>
                        <h3>婚礼筹备清单</h3>
                        <p>包含婚前12个月到婚礼当天的全部筹备任务，支持勾选完成状态</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约15分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=checklist" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                            <a href="downloads.php?action=download&type=checklist_pdf" class="btn-download-secondary">
                                <i class="fas fa-file-print"></i> 打印版
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="checklist" data-format="html">
                    <div class="card-icon">
                        <i class="fas fa-calendar-alt"></i>
                    </div>
                    <div class="card-content">
                        <h3>婚礼筹备倒计时</h3>
                        <p>按月份划分的筹备时间轴，明确每个阶段的关键任务节点</p>
                        <div class="card-meta">
                            <span><i class="fas fa-file-alt"></i> HTML格式</span>
                            <span><i class="far fa-clock"></i> 约10分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=timeline" class="btn-download">
                                <i class="fas fa-download"></i> 下载HTML
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="checklist" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-shopping-bag"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">必备</span>
                        <h3>当日必备物品清单</h3>
                        <p>婚礼当天需要携带的所有物品清单，包含新娘物品、新郎物品、应急物品等</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约5分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=items" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 流程类 -->
                <div class="download-card" data-category="schedule" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-tasks"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">推荐</span>
                        <h3>婚礼当日流程表</h3>
                        <p>婚礼当天完整时间安排，包含每个环节的时间、负责人和注意事项</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约20分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=schedule" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                            <a href="downloads.php?action=download&type=schedule_pdf" class="btn-download-secondary">
                                <i class="fas fa-file-print"></i> 打印版
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="schedule" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-users"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">必备</span>
                        <h3>婚礼角色分工表</h3>
                        <p>明确婚礼中各角色的职责：伴郎伴娘、双方父母、亲戚朋友的分工</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约8分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=roles" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                            <a href="downloads.php?action=download&type=roles_pdf" class="btn-download-secondary">
                                <i class="fas fa-file-print"></i> 打印版
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 模板类 -->
                <div class="download-card" data-category="template" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-calculator"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">实用</span>
                        <h3>婚礼预算表模板</h3>
                        <p>详细的婚礼预算分类模板，涵盖场地、摄影、摄像、化妆等各项费用</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约12分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=budget" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="template" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-address-book"></i>
                    </div>
                    <div class="card-content">
                        <h3>商家联系方式汇总表</h3>
                        <p>统一记录婚庆公司、酒店、摄影、摄像、主持等商家信息的表格</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约5分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=vendor_template" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="template" data-format="csv">
                    <div class="card-icon">
                        <i class="fas fa-user-friends"></i>
                    </div>
                    <div class="card-content">
                        <h3>宾客管理登记表</h3>
                        <p>宾客名单、联系方式、桌位安排、回礼信息等综合管理表格</p>
                        <div class="card-meta">
                            <span><i class="far fa-file-excel"></i> CSV格式</span>
                            <span><i class="far fa-clock"></i> 约8分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=guest_template" class="btn-download">
                                <i class="fas fa-download"></i> 下载CSV
                            </a>
                        </div>
                    </div>
                </div>

                <div class="download-card" data-category="template" data-format="html">
                    <div class="card-icon">
                        <i class="fas fa-microphone-alt"></i>
                    </div>
                    <div class="card-content">
                        <h3>婚礼致辞范文</h3>
                        <p>包含主婚人致辞、证婚人致辞、父母致辞、新人答谢词等多种范文</p>
                        <div class="card-meta">
                            <span><i class="fas fa-file-alt"></i> HTML格式</span>
                            <span><i class="far fa-clock"></i> 约15分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=speech" class="btn-download">
                                <i class="fas fa-download"></i> 下载HTML
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 指南类 -->
                <div class="download-card" data-category="guide" data-format="html">
                    <div class="card-icon">
                        <i class="fas fa-lightbulb"></i>
                    </div>
                    <div class="card-content">
                        <h3>婚礼筹备注意事项</h3>
                        <p>资深婚礼策划师总结的100条实用经验，涵盖各环节避坑指南</p>
                        <div class="card-meta">
                            <span><i class="fas fa-file-alt"></i> HTML格式</span>
                            <span><i class="far fa-clock"></i> 约25分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=tips" class="btn-download">
                                <i class="fas fa-download"></i> 下载HTML
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 本地特色类 -->
                <div class="download-card" data-category="guide" data-format="html">
                    <div class="card-icon">
                        <i class="fas fa-store-alt"></i>
                    </div>
                    <div class="card-content">
                        <span class="card-badge">本地特色</span>
                        <h3>阳泉市区·酒店回门宴操作指南</h3>
                        <p>阳泉本地回门宴完整操作流程：时间表、仪式流程、必备物品、本地规矩</p>
                        <div class="card-meta">
                            <span><i class="fas fa-file-alt"></i> HTML格式</span>
                            <span><i class="far fa-clock"></i> 约10分钟阅读</span>
                        </div>
                        <div class="card-actions">
                            <a href="downloads.php?action=download&type=home_return" class="btn-download">
                                <i class="fas fa-download"></i> 下载HTML
                            </a>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </section>

    <!-- 使用说明 -->
    <section class="usage-guide">
        <div class="container">
            <div class="guide-box">
                <h3><i class="fas fa-info-circle"></i> 使用说明</h3>
                <div class="guide-content">
                    <div class="guide-item">
                        <h4><i class="far fa-file-excel"></i> CSV格式文件</h4>
                        <p>可以直接用Microsoft Excel、WPS或Google Sheets打开编辑，已预设表头格式。</p>
                    </div>
                    <div class="guide-item">
                        <h4><i class="fas fa-file-alt"></i> HTML格式文件</h4>
                        <p>适合打印使用，已优化打印格式。建议使用浏览器打开后按Ctrl+P打印。</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- 常见问题 -->
    <section class="faq-section">
        <div class="container">
            <h2 class="section-heading">常见问题</h2>
            <div class="faq-grid">
                <div class="faq-item">
                    <h4><i class="fas fa-question-circle"></i> 下载的CSV文件怎么打开？</h4>
                    <p>双击文件即可用Excel或WPS打开，选择UTF-8编码以正确显示中文。</p>
                </div>
                <div class="faq-item">
                    <h4><i class="fas fa-question-circle"></i> 可以修改表格内容吗？</h4>
                    <p>所有模板都可以自由编辑修改，根据实际情况调整内容。</p>
                </div>
                <div class="faq-item">
                    <h4><i class="fas fa-question-circle"></i> 需要打印使用怎么办？</h4>
                    <p>下载HTML格式文件，用浏览器打开后按Ctrl+P打印，选择合适的纸张大小。</p>
                </div>
                <div class="faq-item">
                    <h4><i class="fas fa-question-circle"></i> 更多资料需求？</h4>
                    <p>如有其他需求，请通过网站底部联系方式告诉我们，我们会尽快准备。</p>
                </div>
            </div>
        </div>
    </section>
</main>

<style>
.download-filters {
    background: #fff;
    padding: 20px 0;
    border-bottom: 1px solid #eee;
    position: sticky;
    top: 70px;
    z-index: 90;
}
.filter-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}
.filter-tabs {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}
.filter-tab {
    padding: 8px 20px;
    border: 1px solid #ddd;
    border-radius: 20px;
    background: #fff;
    cursor: pointer;
    transition: all 0.3s;
    font-size: 14px;
}
.filter-tab:hover, .filter-tab.active {
    background: #b64a2a;
    color: #fff;
    border-color: #b64a2a;
}
.filter-formats select {
    padding: 8px 15px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    cursor: pointer;
}

.downloads-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 25px;
    padding: 30px 0;
}
.download-card {
    background: #fff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.06);
    transition: all 0.3s;
    display: flex;
    flex-direction: column;
}
.download-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 30px rgba(0,0,0,0.1);
}
.download-card.hidden {
    display: none;
}
.card-icon {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 25px;
    text-align: center;
}
.card-icon i {
    font-size: 2.5rem;
    color: #fff;
}
.card-content {
    padding: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
}
.card-badge {
    display: inline-block;
    background: #ff6b6b;
    color: #fff;
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 10px;
    margin-bottom: 8px;
    width: fit-content;
}
.card-content h3 {
    color: #333;
    font-size: 1.1rem;
    margin-bottom: 10px;
}
.card-content p {
    color: #666;
    font-size: 13px;
    line-height: 1.6;
    flex: 1;
}
.card-meta {
    display: flex;
    gap: 15px;
    margin: 12px 0;
    font-size: 12px;
    color: #999;
}
.card-meta span {
    display: flex;
    align-items: center;
    gap: 5px;
}
.card-actions {
    display: flex;
    gap: 10px;
    margin-top: 15px;
}
.btn-download {
    flex: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: linear-gradient(135deg, #b64a2a, #d4756b);
    color: #fff;
    padding: 10px 15px;
    border-radius: 8px;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.3s;
}
.btn-download:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(182,74,42,0.3);
}
.btn-download-secondary {
    flex: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: #f0f0f0;
    color: #666;
    padding: 10px 15px;
    border-radius: 8px;
    text-decoration: none;
    font-size: 13px;
    font-weight: 500;
    transition: all 0.3s;
}
.btn-download-secondary:hover {
    background: #e0e0e0;
}

.usage-guide {
    background: #f8f9fa;
    padding: 40px 0;
}
.guide-box {
    background: #fff;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.05);
}
.guide-box h3 {
    color: #333;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}
.guide-box h3 i {
    color: #667eea;
}
.guide-content {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
}
.guide-item {
    padding: 15px;
    background: #f8f9fa;
    border-radius: 10px;
}
.guide-item h4 {
    color: #667eea;
    font-size: 14px;
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.guide-item p {
    color: #666;
    font-size: 13px;
    line-height: 1.6;
}

.faq-section {
    padding: 50px 0;
}
.section-heading {
    text-align: center;
    color: #333;
    font-size: 1.6rem;
    margin-bottom: 30px;
}
.faq-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
}
.faq-item {
    background: #fff;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 3px 15px rgba(0,0,0,0.05);
}
.faq-item h4 {
    color: #b64a2a;
    font-size: 14px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}
.faq-item p {
    color: #666;
    font-size: 13px;
    line-height: 1.6;
}

@media (max-width: 768px) {
    .downloads-grid {
        grid-template-columns: 1fr;
    }
    .filter-bar {
        flex-direction: column;
        align-items: flex-start;
    }
    .card-actions {
        flex-direction: column;
    }
    .btn-download, .btn-download-secondary {
        width: 100%;
    }
}
</style>

<script>
// 分类筛选
document.querySelectorAll('.filter-tab').forEach(function(tab) {
    tab.addEventListener('click', function() {
        document.querySelectorAll('.filter-tab').forEach(function(t) { t.classList.remove('active'); });
        this.classList.add('active');
        filterCards();
    });
});

// 格式筛选
document.getElementById('formatFilter').addEventListener('change', filterCards);

function filterCards() {
    var category = document.querySelector('.filter-tab.active').dataset.category;
    var format = document.getElementById('formatFilter').value;
    
    document.querySelectorAll('.download-card').forEach(function(card) {
        var cardCat = card.dataset.category;
        var cardFormat = card.dataset.format;
        
        var showByCat = (category === 'all' || cardCat === category);
        var showByFormat = (format === 'all' || cardFormat === format);
        
        if (showByCat && showByFormat) {
            card.classList.remove('hidden');
        } else {
            card.classList.add('hidden');
        }
    });
}

// 统计下载次数（本地模拟）
document.querySelectorAll('.btn-download, .btn-download-secondary').forEach(function(btn) {
    btn.addEventListener('click', function(e) {
        var type = this.getAttribute('href').match(/type=([^&]+)/);
        if (type) {
            var downloads = JSON.parse(localStorage.getItem('wedding_downloads') || '{}');
            downloads[type[1]] = (downloads[type[1]] || 0) + 1;
            localStorage.setItem('wedding_downloads', JSON.stringify(downloads));
        }
    });
});
</script>

<?php require_once 'footer.php'; ?>
</body>
</html>
