<?php
// post.php - Single Blog Post Page
// File location: /bidwheels.net/blog/post.php

require_once '../includes/functions.php';
require_once '../includes/header.php';

$post_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($post_id <= 0) {
    redirect('/blog/index.php');
}

// Get post
$post = fetchOne("SELECT * FROM blog_posts WHERE id = $post_id");

if (!$post) {
    redirect('/blog/index.php');
}

// Update view count
query("UPDATE blog_posts SET views = views + 1 WHERE id = $post_id");

// Get related posts (same category or tags)
$related_posts = fetchAll("
    SELECT * FROM blog_posts 
    WHERE id != $post_id 
    AND (category = '{$post['category']}' OR tags LIKE '%{$post['tags']}%')
    ORDER BY created_at DESC 
    LIMIT 3
");

// Get popular tags
$all_posts = fetchAll("SELECT tags FROM blog_posts WHERE tags IS NOT NULL AND tags != ''");
$tag_counts = [];
foreach ($all_posts as $p) {
    $tags = explode(',', $p['tags']);
    foreach ($tags as $tag) {
        $tag = trim($tag);
        if (!empty($tag)) {
            $tag_counts[$tag] = ($tag_counts[$tag] ?? 0) + 1;
        }
    }
}
arsort($tag_counts);
$popular_tags = array_slice($tag_counts, 0, 10, true);

// Get categories with post counts
$categories = fetchAll("
    SELECT category, COUNT(*) as count 
    FROM blog_posts 
    WHERE category IS NOT NULL AND category != ''
    GROUP BY category 
    ORDER BY count DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="<?php echo htmlspecialchars(substr(strip_tags($post['content']), 0, 160)); ?>">
    <title><?php echo htmlspecialchars($post['title']); ?> - <?php echo SITE_NAME; ?> Blog</title>
    
    <style>
        .post-page {
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            min-height: 100vh;
            padding: 60px 0;
        }
        
        .post-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 24px;
        }
        
        /* Breadcrumb */
        .breadcrumb {
            display: flex;
            align-items: center;
            gap: 8px;
            flex-wrap: wrap;
            margin-bottom: 30px;
            padding: 10px 16px;
            background: white;
            border-radius: 50px;
            width: fit-content;
            font-size: 13px;
        }
        
        .breadcrumb a {
            color: #666;
            text-decoration: none;
            transition: color 0.3s;
        }
        
        .breadcrumb a:hover {
            color: #FF8C3E;
        }
        
        .breadcrumb span {
            color: #FF8C3E;
        }
        
        .breadcrumb i {
            font-size: 10px;
            color: #ccc;
        }
        
        /* Blog Layout */
        .blog-layout {
            display: grid;
            grid-template-columns: 1fr 320px;
            gap: 40px;
        }
        
        /* Main Content */
        .post-content {
            background: white;
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,0.05);
        }
        
        .post-header {
            padding: 40px 40px 0;
        }
        
        .post-category {
            display: inline-block;
            background: #FF8C3E;
            color: white;
            padding: 5px 16px;
            border-radius: 30px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 20px;
        }
        
        .post-title {
            font-size: 36px;
            font-weight: 800;
            color: #1a1a1a;
            line-height: 1.2;
            margin-bottom: 20px;
        }
        
        .post-meta {
            display: flex;
            gap: 25px;
            flex-wrap: wrap;
            padding-bottom: 20px;
            border-bottom: 1px solid #f0f0f0;
            margin-bottom: 30px;
        }
        
        .post-meta-item {
            display: flex;
            align-items: center;
            gap: 8px;
            color: #666;
            font-size: 13px;
        }
        
        .post-meta-item i {
            color: #FF8C3E;
        }
        
        .post-featured-image {
            width: 100%;
            max-height: 500px;
            object-fit: cover;
        }
        
        .post-body {
            padding: 30px 40px 40px;
        }
        
        .post-body-content {
            font-size: 16px;
            line-height: 1.8;
            color: #333;
        }
        
        .post-body-content p {
            margin-bottom: 1.2rem;
        }
        
        .post-body-content h2 {
            font-size: 28px;
            font-weight: 700;
            margin: 1.5rem 0 1rem;
            color: #1a1a1a;
        }
        
        .post-body-content h3 {
            font-size: 24px;
            font-weight: 600;
            margin: 1.2rem 0 0.8rem;
            color: #1a1a1a;
        }
        
        .post-body-content ul, 
        .post-body-content ol {
            margin: 1rem 0;
            padding-left: 1.5rem;
        }
        
        .post-body-content li {
            margin-bottom: 0.5rem;
        }
        
        .post-body-content blockquote {
            border-left: 4px solid #FF8C3E;
            padding-left: 1.2rem;
            margin: 1.2rem 0;
            color: #555;
            font-style: italic;
        }
        
        .post-body-content img {
            max-width: 100%;
            border-radius: 16px;
            margin: 1rem 0;
        }
        
        .post-tags {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
        }
        
        .post-tag {
            background: #f0f0f0;
            color: #555;
            padding: 5px 14px;
            border-radius: 30px;
            font-size: 12px;
            text-decoration: none;
            transition: all 0.3s;
        }
        
        .post-tag:hover {
            background: #FF8C3E;
            color: white;
        }
        
        /* Share Section */
        .share-section {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #f0f0f0;
            display: flex;
            align-items: center;
            gap: 20px;
            flex-wrap: wrap;
        }
        
        .share-section span {
            color: #666;
            font-size: 14px;
        }
        
        .share-buttons {
            display: flex;
            gap: 12px;
        }
        
        .share-btn {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            color: white;
            text-decoration: none;
            transition: all 0.3s;
        }
        
        .share-btn:hover {
            transform: translateY(-3px);
        }
        
        .share-btn.facebook { background: #3b5998; }
        .share-btn.twitter { background: #1da1f2; }
        .share-btn.linkedin { background: #0077b5; }
        .share-btn.whatsapp { background: #25d366; }
        .share-btn.email { background: #666; }
        
        /* Related Posts */
        .related-section {
            margin-top: 40px;
            padding: 0 40px 40px;
        }
        
        .related-title {
            font-size: 22px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 25px;
            padding-bottom: 10px;
            border-bottom: 2px solid #FF8C3E;
            display: inline-block;
        }
        
        .related-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 25px;
        }
        
        .related-card {
            background: #f8f9fa;
            border-radius: 20px;
            overflow: hidden;
            transition: all 0.3s;
        }
        
        .related-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        
        .related-card img {
            width: 100%;
            height: 150px;
            object-fit: cover;
        }
        
        .related-card-content {
            padding: 15px;
        }
        
        .related-card-title {
            font-size: 14px;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 8px;
            line-height: 1.4;
        }
        
        .related-card-title a {
            color: #1a1a1a;
            text-decoration: none;
        }
        
        .related-card-title a:hover {
            color: #FF8C3E;
        }
        
        .related-card-date {
            font-size: 11px;
            color: #888;
        }
        
        /* Sidebar */
        .blog-sidebar {
            position: sticky;
            top: 100px;
            height: fit-content;
        }
        
        .sidebar-widget {
            background: white;
            border-radius: 24px;
            padding: 28px;
            margin-bottom: 30px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
        }
        
        .sidebar-widget h3 {
            font-size: 18px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 20px;
            padding-bottom: 12px;
            border-bottom: 2px solid #FF8C3E;
            display: inline-block;
        }
        
        .categories-list {
            list-style: none;
            padding: 0;
        }
        
        .categories-list li {
            margin-bottom: 12px;
        }
        
        .categories-list a {
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: #555;
            text-decoration: none;
            padding: 8px 0;
            transition: all 0.3s;
        }
        
        .categories-list a:hover {
            color: #FF8C3E;
            transform: translateX(5px);
        }
        
        .categories-list span {
            background: #f0f0f0;
            padding: 2px 10px;
            border-radius: 20px;
            font-size: 11px;
        }
        
        .tags-cloud {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
        }
        
        .tag {
            background: #f0f0f0;
            color: #555;
            padding: 6px 14px;
            border-radius: 30px;
            font-size: 12px;
            text-decoration: none;
            transition: all 0.3s;
        }
        
        .tag:hover {
            background: #FF8C3E;
            color: white;
        }
        
        .newsletter-widget {
            background: linear-gradient(135deg, #1a1a1a 0%, #2a2a2a 100%);
            color: white;
        }
        
        .newsletter-widget h3 {
            color: white;
            border-bottom-color: #FF8C3E;
        }
        
        .newsletter-widget p {
            color: #aaa;
            font-size: 13px;
            margin-bottom: 20px;
        }
        
        .newsletter-form {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }
        
        .newsletter-form input {
            padding: 12px 16px;
            border: none;
            border-radius: 12px;
            font-size: 13px;
            background: #333;
            color: white;
        }
        
        .newsletter-form button {
            padding: 12px;
            background: #FF8C3E;
            border: none;
            border-radius: 12px;
            color: #0a0a0a;
            font-weight: 600;
            cursor: pointer;
        }
        
        /* Responsive */
        @media (max-width: 992px) {
            .blog-layout {
                grid-template-columns: 1fr;
            }
            
            .blog-sidebar {
                position: static;
                margin-top: 40px;
            }
            
            .related-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        
        @media (max-width: 768px) {
            .post-page {
                padding: 40px 0;
            }
            
            .post-container {
                padding: 0 16px;
            }
            
            .post-header {
                padding: 25px 25px 0;
            }
            
            .post-title {
                font-size: 26px;
            }
            
            .post-body {
                padding: 20px 25px 30px;
            }
            
            .related-section {
                padding: 0 25px 30px;
            }
            
            .related-grid {
                grid-template-columns: 1fr;
            }
            
            .post-meta {
                gap: 15px;
            }
        }
    </style>
</head>
<body>

<div class="post-page">
    <div class="post-container">
        <!-- Breadcrumb -->
        <div class="breadcrumb">
            <a href="<?php echo SITE_URL; ?>/index.php"><i class="fas fa-home"></i> Home</a>
            <i class="fas fa-chevron-right"></i>
            <a href="index.php">Blog</a>
            <i class="fas fa-chevron-right"></i>
            <span><?php echo htmlspecialchars($post['title']); ?></span>
        </div>
        
        <div class="blog-layout">
            <!-- Main Content -->
            <div class="post-content">
                <div class="post-header">
                    <?php if ($post['category']): ?>
                        <div class="post-category"><?php echo htmlspecialchars($post['category']); ?></div>
                    <?php endif; ?>
                    <h1 class="post-title"><?php echo htmlspecialchars($post['title']); ?></h1>
                    <div class="post-meta">
                        <div class="post-meta-item">
                            <i class="fas fa-calendar-alt"></i>
                            <span><?php echo date('F d, Y', strtotime($post['created_at'])); ?></span>
                        </div>
                        <div class="post-meta-item">
                            <i class="fas fa-eye"></i>
                            <span><?php echo number_format($post['views']); ?> views</span>
                        </div>
                        <div class="post-meta-item">
                            <i class="fas fa-clock"></i>
                            <span><?php echo ceil(strlen(strip_tags($post['content'])) / 1000); ?> min read</span>
                        </div>
                    </div>
                </div>
                
                <?php if ($post['featured_image']): ?>
                    <img class="post-featured-image" src="<?php echo SITE_URL; ?>/uploads/blog/<?php echo $post['featured_image']; ?>" alt="<?php echo htmlspecialchars($post['title']); ?>">
                <?php endif; ?>
                
                <div class="post-body">
                    <div class="post-body-content">
                        <?php echo htmlspecialchars_decode($post['content']); ?>
                    </div>
                    
                    <?php if ($post['tags']): ?>
                        <div class="post-tags">
                            <?php 
                            $tags = explode(',', $post['tags']);
                            foreach ($tags as $tag): ?>
                                <a href="search.php?tag=<?php echo urlencode(trim($tag)); ?>" class="post-tag">
                                    #<?php echo htmlspecialchars(trim($tag)); ?>
                                </a>
                            <?php endforeach; ?>
                        </div>
                    <?php endif; ?>
                    
                    <!-- Share Section -->
                    <div class="share-section">
                        <span><i class="fas fa-share-alt"></i> Share this article:</span>
                        <div class="share-buttons">
                            <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(SITE_URL . '/blog/post.php?id=' . $post_id); ?>" target="_blank" class="share-btn facebook">
                                <i class="fab fa-facebook-f"></i>
                            </a>
                            <a href="https://twitter.com/intent/tweet?url=<?php echo urlencode(SITE_URL . '/blog/post.php?id=' . $post_id); ?>&text=<?php echo urlencode($post['title']); ?>" target="_blank" class="share-btn twitter">
                                <i class="fab fa-twitter"></i>
                            </a>
                            <a href="https://www.linkedin.com/shareArticle?mini=true&url=<?php echo urlencode(SITE_URL . '/blog/post.php?id=' . $post_id); ?>" target="_blank" class="share-btn linkedin">
                                <i class="fab fa-linkedin-in"></i>
                            </a>
                            <a href="https://wa.me/?text=<?php echo urlencode($post['title'] . ' - ' . SITE_URL . '/blog/post.php?id=' . $post_id); ?>" target="_blank" class="share-btn whatsapp">
                                <i class="fab fa-whatsapp"></i>
                            </a>
                            <a href="mailto:?subject=<?php echo urlencode($post['title']); ?>&body=<?php echo urlencode(SITE_URL . '/blog/post.php?id=' . $post_id); ?>" class="share-btn email">
                                <i class="fas fa-envelope"></i>
                            </a>
                        </div>
                    </div>
                </div>
                
                <!-- Related Posts -->
                <?php if (count($related_posts) > 0): ?>
                    <div class="related-section">
                        <h3 class="related-title">You May Also Like</h3>
                        <div class="related-grid">
                            <?php foreach ($related_posts as $related): ?>
                                <div class="related-card">
                                    <?php if ($related['featured_image']): ?>
                                        <img src="<?php echo SITE_URL; ?>/uploads/blog/<?php echo $related['featured_image']; ?>" alt="<?php echo htmlspecialchars($related['title']); ?>">
                                    <?php else: ?>
                                        <img src="<?php echo SITE_URL; ?>/assets/images/blog-placeholder.jpg" alt="Blog image">
                                    <?php endif; ?>
                                    <div class="related-card-content">
                                        <h4 class="related-card-title">
                                            <a href="post.php?id=<?php echo $related['id']; ?>"><?php echo htmlspecialchars($related['title']); ?></a>
                                        </h4>
                                        <div class="related-card-date">
                                            <i class="fas fa-calendar-alt"></i> <?php echo date('M d, Y', strtotime($related['created_at'])); ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            
            <!-- Sidebar -->
            <aside class="blog-sidebar">
                <!-- Categories Widget -->
                <div class="sidebar-widget">
                    <h3><i class="fas fa-folder"></i> Categories</h3>
                    <ul class="categories-list">
                        <?php foreach ($categories as $cat): ?>
                            <li>
                                <a href="category.php?name=<?php echo urlencode($cat['category']); ?>">
                                    <?php echo htmlspecialchars($cat['category']); ?>
                                    <span><?php echo $cat['count']; ?></span>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                
                <!-- Popular Tags Widget -->
                <div class="sidebar-widget">
                    <h3><i class="fas fa-tags"></i> Popular Tags</h3>
                    <div class="tags-cloud">
                        <?php foreach ($popular_tags as $tag => $count): ?>
                            <a href="search.php?tag=<?php echo urlencode($tag); ?>" class="tag">
                                #<?php echo htmlspecialchars($tag); ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
                
                <!-- Newsletter Widget -->
                <div class="sidebar-widget newsletter-widget">
                    <h3><i class="fas fa-envelope"></i> Newsletter</h3>
                    <p>Subscribe to get the latest articles delivered to your inbox.</p>
                    <form class="newsletter-form" id="newsletterForm" method="POST">
                        <input type="email" name="newsletter_email" placeholder="Your email address" required>
                        <button type="submit"><i class="fas fa-paper-plane"></i> Subscribe</button>
                    </form>
                </div>
            </aside>
        </div>
    </div>
</div>

<script>
document.getElementById('newsletterForm').addEventListener('submit', function(e) {
    e.preventDefault();
    var email = this.querySelector('input[name="newsletter_email"]').value;
    if (email) {
        alert('Thank you for subscribing to our newsletter!');
        this.reset();
    }
});
</script>

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