<?php
include "./header.php";

// Validate and extract ID
$id = $_GET['id'] ?? '';

// $file = $fileMap[$category] ?? '';
$file = 'data/weekly_roundup.json';

if (!$id || !$file || !file_exists($file)) {
    echo "Post not found.";
    exit;
}

$posts = json_decode(file_get_contents($file), true);
$post = null;
foreach ($posts as $p) {
    if ($p['id'] == $id) {
        $post = $p;
        break;
    }
}

if (!$post) {
    echo "Post not found.";
    exit;
}

?>
<?php
// Load comments
$commentFile = __DIR__ . '/admin/comment.json';
$comments = file_exists($commentFile) ? json_decode(file_get_contents($commentFile), true) : [];
$postComments = isset($comments[$post['id']]) ? $comments[$post['id']] : [];

// Handle form submit
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['comment_submit'])) {
    $name = trim($_POST['name'] ?? 'Anonymous');
    $commentText = trim($_POST['comment'] ?? '');
    if (!empty($commentText)) {
        $newComment = [
            'name' => htmlspecialchars($name),
            'comment' => htmlspecialchars($commentText),
            'timestamp' => date('Y-m-d H:i')
        ];
        $comments[$post['id']][] = $newComment;
        file_put_contents($commentFile, json_encode($comments, JSON_PRETTY_PRINT));
        // Refresh to prevent resubmit
        header("Location: postDetail.php?id=" . urlencode($post['id']));
        exit;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta name="google-adsense-account" content="ca-pub-3429299386991375">
  <title><?= htmlspecialchars($foundPost['title']) ?></title>
  <link rel="stylesheet" href="./assets/postDetails.css" />
  <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
  <style>
     .share-buttons {
    display: flex;
    gap: 10px;
    margin-top: 10px;
  }
  .share-buttons .btn {
      padding: 8px 12px;
      text-decoration: none;
      border-radius: 5px;
      color: white;
      font-size: 14px;
  }
  .whatsapp { background: #25D366; }
  .facebook { background: #1877F2; }
  .twitter { background: #1DA1F2; }
  .linkedin { background: #0077B5; }
  .telegram { background: #0088cc; }
@media screen AND (max-width:500px){
    .share-buttons {
        display: flex;
        margin: 5px;
    }
    .share-buttons .btn {
        height: 20px;
        padding: 4px;
        text-decoration: none;
        border-radius: 5px;
        color: white;
        font-size: 10px;
    }
}
  </style>
</head>
<body>
    <div class="post-wrap">
        <div class="post">
            <h1 class="postTitle"><?= htmlspecialchars($post['title']) ?></h1>
            <p class="postInfo"><strong>Posted by: <?= htmlspecialchars($post['author']) ?></strong> — <?= htmlspecialchars($post['published_at']) ?></p>
            <div class="tags">
                    <?php if (!empty($post['tags'])): ?>
                    <?php foreach ($post['tags'] as $tag): ?>
                        <span>#<?= htmlspecialchars($tag) ?></span>
                    <?php endforeach; ?>
                    <?php endif; ?>
            </div>  <div class="post-meta"><?= $post['summary'] ?></div>

            <?php
                $post_url = urlencode("https://cryptobitsense.com/weekly_more?id={$post['id']}"); 
                $post_title = urlencode($post['title']); 
                ?>

                <!-- Social Media Share Buttons -->
                <div class="share-buttons">
                    <!-- WhatsApp -->
                    <a href="https://api.whatsapp.com/send?text=<?php echo $post_title; ?>%20<?php echo $post_url; ?>" target="_blank" class="btn whatsapp">
                        WhatsApp
                    </a>
                    <!-- Facebook -->
                    <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $post_url; ?>" target="_blank" class="btn facebook">
                      Facebook
                    </a>
                    <!-- Twitter / X -->
                    <a href="https://twitter.com/intent/tweet?text=<?php echo $post_title; ?>&url=<?php echo $post_url; ?>" target="_blank" class="btn twitter">
                        Twitter
                    </a>
                    <!-- LinkedIn -->
                    <a href="https://www.linkedin.com/sharing/share-offsite/?url=<?php echo $post_url; ?>" target="_blank" class="btn linkedin">
                        LinkedIn
                    </a>
                    <!-- Telegram -->
                    <a href="https://t.me/share/url?url=<?php echo $post_url; ?>&text=<?php echo $post_title; ?>" target="_blank" class="btn telegram">
                       Telegram
                    </a>
                </div> <br>
            


            <?php if (!empty($post['imageurl'])): ?>
            <img class="postImage" src="<?= htmlspecialchars($post['imageurl']) ?>" alt="Image">
            <?php endif; ?>
            <div class="postContent"><?= $post['content']?></div>
        </div>
    </div>

<!-- COMMENT FORM -->

<h3 class="recent-comm">Leave a Comment</h3>
<form method="POST">
  <input type="text" name="name" placeholder="Your name" style="width: 100%; padding: 8px;" />
  <textarea name="comment" placeholder="Your comment..." required style="width: 100%; padding: 8px; margin-top: 10px;"></textarea>
  <button type="submit" name="comment_submit" style="margin-top:10px; padding: 10px 20px;">Submit</button>
</form>

<!-- DISPLAY COMMENTS -->
<?php if (!empty($postComments)): ?> 
    <div class="commentBox">
        <h4 class="recent-comm" style="margin-top: 30px;">Recent Comments:</h4>
        <ul class="coms">
            <?php foreach (array_reverse($postComments) as $c): ?>
            <li style="margin-bottom: 15px;">
                <strong><?= htmlspecialchars($c['name']) ?></strong>
                <em>(<?= $c['timestamp'] ?>)</em><br>
                <?= nl2br(htmlspecialchars($c['comment'])) ?>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>


<?php
echo "<div class='commentBox'>
<hr><h3 class='recent-comm'>Related Posts</h3><ul style='list-style:none; width:90%; margin: 5px auto; padding:5px;'>";
// Pick 3 other posts randomly
$related = [];
foreach ($posts as $p) {
    if ($p['id'] !== $post['id']) {
        $related[] = $p;
    }
}
shuffle($related);
$related = array_slice($related, 0, 3);
foreach ($related as $r) {
    echo "<li style='margin-bottom:10px;'><a href='postDetail.php?id=" . urlencode($r['id']) . "'><strong>" . htmlspecialchars($r['title']) . "</strong></a></li>";
}
echo "</ul></div>";
?>

</body>
</html>
























