<?php
require_once 'config.php';

$invitation = null;
$error = '';

if (!isset($_GET['code']) || empty($_GET['code'])) {
    $error = '请柬链接无效，请检查链接是否正确。';
} else {
    $code = $_GET['code'];
    $stmt = $pdo->prepare("SELECT * FROM invitations WHERE share_code = ?");
    $stmt->execute([$code]);
    $invitation = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($invitation) {
        // 增加浏览量
        $pdo->prepare("UPDATE invitations SET view_count = view_count + 1 WHERE id = ?")->execute([$invitation['id']]);
        $invitation['view_count']++;
    } else {
        $error = '未找到该请柬，可能已被删除。';
    }
}

$themes = [
    'pink'    => ['name' => '浪漫粉', 'primary' => '#e891a0', 'secondary' => '#fce4ec', 'bg' => '#fff5f7', 'deco' => '🌸'],
    'red'     => ['name' => '中国红', 'primary' => '#c41e3a', 'secondary' => '#fde8ec', 'bg' => '#fff8f8', 'deco' => '🏮'],
    'gold'    => ['name' => '典雅金', 'primary' => '#b8860b', 'secondary' => '#fef9e7', 'bg' => '#fffdf5', 'deco' => '✨'],
    'purple'  => ['name' => '梦幻紫', 'primary' => '#7b2d8e', 'secondary' => '#f3e5f5', 'bg' => '#fdfbff', 'deco' => '💜'],
    'blue'    => ['name' => '星空蓝', 'primary' => '#1a5276', 'secondary' => '#eaf2f8', 'bg' => '#f7fbfd', 'deco' => '🌊'],
    'green'   => ['name' => '清新绿', 'primary' => '#1e8449', 'secondary' => '#eafaf1', 'bg' => '#f8fdf9', 'deco' => '🌿'],
];

$t = $themes['pink']; // 默认主题
if ($invitation && isset($invitation['theme']) && isset($themes[$invitation['theme']])) {
    $t = $themes[$invitation['theme']];
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $invitation ? htmlspecialchars($invitation['groom_name']) . ' & ' . htmlspecialchars($invitation['bride_name']) . ' 的婚礼请柬' : '电子请柬'; ?></title>
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body.invitation-view-page {
            background: <?php echo isset($t) ? $t['bg'] : '#fff5f7'; ?>;
            min-height: 100vh;
            min-height: 100dvh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .invitation-view-page .top-bar,
        .invitation-view-page .main-nav,
        .invitation-view-page .site-footer { display: none; }

        .invitation-container {
            max-width: 520px;
            width: 100%;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.15);
            overflow: hidden;
            animation: inviteFadeIn 0.8s ease;
        }
        @keyframes inviteFadeIn {
            from { opacity: 0; transform: translateY(30px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .invitation-header {
            background: linear-gradient(135deg, <?php echo isset($t) ? $t['primary'] : '#e891a0'; ?>, <?php echo isset($t) ? $t['secondary'] : '#fce4ec'; ?>);
            padding: 60px 30px 40px;
            text-align: center;
            position: relative;
            overflow: hidden;
        }
        .invitation-header::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: rotate 20s linear infinite;
        }
        @keyframes rotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }
        .invitation-header * { position: relative; z-index: 1; }
        .invitation-deco {
            font-size: 3rem;
            margin-bottom: 10px;
        }
        .invitation-subtitle {
            font-size: 1rem;
            color: rgba(255,255,255,0.9);
            letter-spacing: 3px;
            margin-bottom: 5px;
        }
        .invitation-couple {
            font-size: 2.5rem;
            color: white;
            font-weight: bold;
            margin: 15px 0;
            text-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .invitation-and {
            display: inline-block;
            margin: 0 10px;
            font-size: 1.5rem;
            opacity: 0.8;
        }
        .invitation-title {
            font-size: 1.2rem;
            color: rgba(255,255,255,0.95);
            letter-spacing: 5px;
        }

        .invitation-body {
            padding: 40px 35px;
            text-align: center;
        }
        .invitation-body p {
            margin-bottom: 15px;
        }
        .invitation-info {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 20px;
            margin: 30px 0;
        }
        .invitation-info-item {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 1.05rem;
            color: #555;
        }
        .invitation-info-item .info-icon {
            font-size: 1.5rem;
        }
        .invitation-date-time {
            display: flex;
            gap: 30px;
            justify-content: center;
            margin: 25px 0;
            flex-wrap: wrap;
        }
        .invitation-venue-block {
            background: #fafafa;
            border-radius: 15px;
            padding: 25px;
            margin: 25px 0;
            text-align: left;
        }
        .invitation-venue-block h4 {
            color: <?php echo isset($t) ? $t['primary'] : '#e891a0'; ?>;
            margin-bottom: 8px;
        }
        .invitation-message {
            font-style: italic;
            color: #888;
            font-size: 1.1rem;
            line-height: 1.8;
            margin: 25px 0;
            padding: 20px;
            border-top: 1px solid #eee;
            border-bottom: 1px solid #eee;
        }
        .invitation-footer {
            padding: 30px;
            text-align: center;
            background: #fafafa;
            border-top: 1px solid #f0f0f0;
        }
        .invitation-footer .btn {
            margin: 5px;
        }
        .invitation-share-btns {
            display: flex;
            gap: 10px;
            justify-content: center;
            flex-wrap: wrap;
            margin-top: 15px;
        }
        .invitation-error {
            background: white;
            border-radius: 20px;
            padding: 60px 40px;
            text-align: center;
            box-shadow: 0 20px 60px rgba(0,0,0,0.15);
        }
        .invitation-error .error-icon {
            font-size: 4rem;
            margin-bottom: 20px;
        }

        @media (max-width: 600px) {
            .invitation-couple { font-size: 1.8rem; }
            .invitation-header { padding: 40px 20px 30px; }
            .invitation-body { padding: 30px 20px; }
        }
    </style>
</head>
<body class="invitation-view-page">

<?php if ($error): ?>
    <div class="invitation-error">
        <div class="error-icon">😢</div>
        <h2>抱歉</h2>
        <p style="color:#888; margin-top:10px;"><?php echo htmlspecialchars($error); ?></p>
        <a href="index.php" class="btn btn-primary" style="margin-top:20px;">返回首页</a>
    </div>
<?php else: ?>
    <div class="invitation-container">
        <!-- 头部 -->
        <div class="invitation-header">
            <div class="invitation-deco"><?php echo $t['deco']; ?></div>
            <p class="invitation-subtitle">诚 挚 邀 请 您 参 加</p>
            <h1 class="invitation-couple">
                <?php echo htmlspecialchars($invitation['groom_name']); ?>
                <span class="invitation-and">&</span>
                <?php echo htmlspecialchars($invitation['bride_name']); ?>
            </h1>
            <p class="invitation-title">的 婚 礼</p>
        </div>

        <!-- 主体 -->
        <div class="invitation-body">
            <?php if (!empty($invitation['cover_image'])): ?>
                <div style="margin-bottom:20px;">
                    <img src="<?php echo (strpos($invitation['cover_image'], 'http') === 0 ? '' : './') . htmlspecialchars($invitation['cover_image']); ?>" alt="封面" style="max-width:100%; border-radius:10px;">
                </div>
            <?php endif; ?>

            <?php if (!empty($invitation['wedding_date']) || !empty($invitation['wedding_time'])): ?>
            <div class="invitation-date-time">
                <?php if (!empty($invitation['wedding_date'])): ?>
                <div class="invitation-info-item">
                    <span class="info-icon">📅</span>
                    <span><?php echo htmlspecialchars($invitation['wedding_date']); ?></span>
                </div>
                <?php endif; ?>
                <?php if (!empty($invitation['wedding_time'])): ?>
                <div class="invitation-info-item">
                    <span class="info-icon">🕐</span>
                    <span><?php echo htmlspecialchars($invitation['wedding_time']); ?></span>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>

            <?php if (!empty($invitation['venue']) || !empty($invitation['venue_address'])): ?>
            <div class="invitation-venue-block">
                <h4>📍 婚礼地点</h4>
                <?php if (!empty($invitation['venue'])): ?>
                <p><strong><?php echo htmlspecialchars($invitation['venue']); ?></strong></p>
                <?php endif; ?>
                <?php if (!empty($invitation['venue_address'])): ?>
                <p style="color:#888;"><?php echo htmlspecialchars($invitation['venue_address']); ?></p>
                <?php endif; ?>
            </div>
            <?php endif; ?>

            <?php if (!empty($invitation['message'])): ?>
            <div class="invitation-message">
                "<?php echo nl2br(htmlspecialchars($invitation['message'])); ?>"
            </div>
            <?php endif; ?>

            <p style="color:#aaa; font-size:14px; margin-top:20px;">
                👀 已被浏览 <?php echo (int)$invitation['view_count']; ?> 次
            </p>
        </div>

        <!-- 底部 -->
        <div class="invitation-footer">
            <p style="margin-bottom:15px; color:#888;">期待您的到来！</p>
            <div class="invitation-share-btns">
                <button class="btn btn-primary" onclick="copyCurrentUrl()">
                    <i class="fas fa-copy"></i> 复制链接
                </button>
                <button class="btn btn-primary" onclick="shareWechat()" style="background:#07c160; border-color:#07c160;">
                    <i class="fab fa-weixin"></i> 微信分享
                </button>
            </div>
            <div style="margin-top:20px;">
                <a href="invitation_create.php" class="btn-link">我也要制作电子请柬 →</a>
            </div>
        </div>
    </div>
<?php endif; ?>

<script>
function copyCurrentUrl() {
    var dummy = document.createElement('textarea');
    dummy.value = window.location.href;
    document.body.appendChild(dummy);
    dummy.select();
    document.execCommand('copy');
    document.body.removeChild(dummy);
    alert('请柬链接已复制到剪贴板！');
}
function shareWechat() {
    alert('请复制链接后，在微信中粘贴发送给好友即可。');
    copyCurrentUrl();
}
</script>

</body>
</html>