<?php
// index.php - Professional Blog Listing Page
// File location: /bidwheels.net/blog/index.php

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

// Pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * BLOG_POSTS_PER_PAGE;

// Get blog posts
$posts = fetchAll("
    SELECT * FROM blog_posts 
    ORDER BY created_at DESC 
    LIMIT $offset, " . BLOG_POSTS_PER_PAGE
);

// Get popular tags
$all_posts = fetchAll("SELECT tags FROM blog_posts WHERE tags IS NOT NULL AND tags != ''");
$tag_counts = [];
foreach ($all_posts as $post) {
    $tags = explode(',', $post['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
");

$total = getCount('blog_posts');
$total_pages = ceil($total / BLOG_POSTS_PER_PAGE);
?>

<!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="Read the latest articles, news, and tips about car auctions, bidding strategies, and vehicle reviews from <?php echo SITE_NAME; ?> blog.">
    <title>Blog - <?php echo SITE_NAME; ?></title>
    
    <style>
        .blog-page {
            background: linear-gradient(135deg, #EFEBE0 0%, #e5e0d4 100%);
            min-height: 100vh;
            padding: 60px 0;
        }
        
        .blog-container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 0 24px;
        }
        
        /* Header Section */
        .blog-header {
            text-align: center;
            margin-bottom: 50px;
        }
        
        .blog-header h1 {
            font-size: 48px;
            font-weight: 800;
            background: linear-gradient(135deg, #1a1a1a 0%, #FF8C3E 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 15px;
        }
        
        .blog-header p {
            color: #555;
            font-size: 18px;
            max-width: 600px;
            margin: 0 auto;
        }
        
        /* Search Bar */
        .blog-search {
            max-width: 500px;
            margin: 0 auto 40px;
        }
        
        .search-form {
            display: flex;
            gap: 12px;
            background: white;
            border-radius: 60px;
            padding: 5px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
        }
        
        .search-form input {
            flex: 1;
            padding: 14px 24px;
            border: none;
            border-radius: 60px;
            font-size: 14px;
            background: transparent;
        }
        
        .search-form input:focus {
            outline: none;
        }
        
        .search-form button {
            padding: 12px 28px;
            background: linear-gradient(135deg, #FF8C3E, #e67a22);
            border: none;
            border-radius: 50px;
            color: white;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .search-form button:hover {
            transform: translateY(-2px);
        }
        
        /* Blog Layout */
        .blog-layout {
            display: grid;
            grid-template-columns: 1fr 320px;
            gap: 40px;
        }
        
        /* Blog Grid */
        .blog-grid {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 30px;
        }
        
        .blog-card {
            background: white;
            border-radius: 24px;
            overflow: hidden;
            transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
        }
        
        .blog-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(255,140,62,0.15);
        }
        
        .blog-image {
            position: relative;
            aspect-ratio: 16/9;
            overflow: hidden;
        }
        
        .blog-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s;
        }
        
        .blog-card:hover .blog-image img {
            transform: scale(1.05);
        }
        
        .blog-category {
            position: absolute;
            top: 15px;
            left: 15px;
            background: #FF8C3E;
            color: white;
            padding: 5px 14px;
            border-radius: 30px;
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .blog-content {
            padding: 24px;
        }
        
        .blog-meta {
            display: flex;
            gap: 20px;
            margin-bottom: 12px;
            font-size: 12px;
            color: #888;
        }
        
        .blog-meta i {
            color: #FF8C3E;
            margin-right: 5px;
        }
        
        .blog-title {
            font-size: 20px;
            font-weight: 700;
            color: #1a1a1a;
            margin-bottom: 12px;
            line-height: 1.4;
        }
        
        .blog-title a {
            color: #1a1a1a;
            text-decoration: none;
            transition: color 0.3s;
        }
        
        .blog-title a:hover {
            color: #FF8C3E;
        }
        
        .blog-excerpt {
            color: #666;
            font-size: 14px;
            line-height: 1.6;
            margin-bottom: 20px;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }
        
        .read-more {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            color: #FF8C3E;
            text-decoration: none;
            font-weight: 600;
            font-size: 13px;
            transition: gap 0.3s;
        }
        
        .read-more:hover {
            gap: 12px;
        }
        
        /* 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 */
        .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;
            color: #666;
        }
        
        /* Tags Cloud */
        .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;
            transform: translateY(-2px);
        }
        
        /* Newsletter Widget */
        .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;
            line-height: 1.5;
        }
        
        .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 input::placeholder {
            color: #888;
        }
        
        .newsletter-form button {
            padding: 12px;
            background: #FF8C3E;
            border: none;
            border-radius: 12px;
            color: #0a0a0a;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .newsletter-form button:hover {
            background: #e67a22;
        }
        
        /* Pagination */
        .pagination {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-top: 50px;
            flex-wrap: wrap;
        }
        
        .pagination a {
            padding: 10px 16px;
            background: white;
            color: #333;
            text-decoration: none;
            border-radius: 12px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .pagination a:hover {
            background: #FF8C3E;
            color: white;
            transform: translateY(-2px);
        }
        
        .pagination a.active {
            background: #FF8C3E;
            color: white;
        }
        
        /* No Results */
        .no-posts {
            grid-column: 1/-1;
            text-align: center;
            padding: 60px;
            background: white;
            border-radius: 24px;
        }
        
        .no-posts i {
            font-size: 60px;
            color: #ddd;
            margin-bottom: 20px;
        }
        
        .no-posts h3 {
            font-size: 24px;
            color: #333;
            margin-bottom: 10px;
        }
        
        .no-posts p {
            color: #888;
        }
        
        /* Responsive */
        @media (max-width: 1200px) {
            .blog-grid {
                grid-template-columns: 1fr;
            }
        }
        
        @media (max-width: 992px) {
            .blog-layout {
                grid-template-columns: 1fr;
            }
            
            .blog-sidebar {
                position: static;
                margin-top: 40px;
            }
        }
        
        @media (max-width: 768px) {
            .blog-page {
                padding: 40px 0;
            }
            
            .blog-container {
                padding: 0 16px;
            }
            
            .blog-header h1 {
                font-size: 32px;
            }
            
            .blog-header p {
                font-size: 16px;
            }
            
            .blog-title {
                font-size: 18px;
            }
            
            .search-form {
                flex-direction: column;
                background: transparent;
                box-shadow: none;
                gap: 10px;
            }
            
            .search-form input {
                background: white;
                padding: 12px 20px;
            }
        }
        
        @media (max-width: 480px) {
            .blog-meta {
                gap: 12px;
                font-size: 11px;
            }
            
            .blog-content {
                padding: 18px;
            }
            
            .sidebar-widget {
                padding: 20px;
            }
        }
    </style>
</head>
<body>

<div class="blog-page">
    <div class="blog-container">
        <!-- Header -->
        <div class="blog-header">
            <h1>Our Blog</h1>
            <p>Insights, tips, and news from the world of vehicle auctions</p>
        </div>
        
        <!-- Search Bar -->
        <div class="blog-search">
            <form method="GET" action="search.php" class="search-form">
                <input type="text" name="keyword" placeholder="Search articles..." value="<?php echo isset($_GET['keyword']) ? htmlspecialchars($_GET['keyword']) : ''; ?>">
                <button type="submit"><i class="fas fa-search"></i> Search</button>
            </form>
        </div>
        
        <div class="blog-layout">
            <!-- Main Content -->
            <div class="blog-main">
                <div class="blog-grid">
                    <?php if (count($posts) > 0): ?>
                        <?php foreach ($posts as $post): ?>
                            <div class="blog-card">
                                <div class="blog-image">
                                    <?php if ($post['featured_image']): ?>
                                        <img src="<?php echo SITE_URL; ?>/uploads/blog/<?php echo $post['featured_image']; ?>" alt="<?php echo htmlspecialchars($post['title']); ?>">
                                    <?php else: ?>
                                        <img src="<?php echo SITE_URL; ?>/assets/images/blog-placeholder.jpg" alt="Blog image">
                                    <?php endif; ?>
                                    <?php if ($post['category']): ?>
                                        <span class="blog-category"><?php echo htmlspecialchars($post['category']); ?></span>
                                    <?php endif; ?>
                                </div>
                                <div class="blog-content">
                                    <div class="blog-meta">
                                        <span><i class="fas fa-calendar-alt"></i> <?php echo date('M d, Y', strtotime($post['created_at'])); ?></span>
                                        <span><i class="fas fa-eye"></i> <?php echo number_format($post['views']); ?> views</span>
                                    </div>
                                    <h2 class="blog-title">
                                        <a href="post.php?id=<?php echo $post['id']; ?>"><?php echo htmlspecialchars($post['title']); ?></a>
                                    </h2>
                                    <div class="blog-excerpt">
                                        <?php echo substr(strip_tags($post['content']), 0, 120); ?>...
                                    </div>
                                    <a href="post.php?id=<?php echo $post['id']; ?>" class="read-more">
                                        Read More <i class="fas fa-arrow-right"></i>
                                    </a>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <div class="no-posts">
                            <i class="fas fa-blog"></i>
                            <h3>No posts found</h3>
                            <p>Check back later for new articles.</p>
                        </div>
                    <?php endif; ?>
                </div>
                
                <!-- Pagination -->
                <?php if ($total_pages > 1): ?>
                    <div class="pagination">
                        <?php if ($page > 1): ?>
                            <a href="?page=<?php echo $page - 1; ?>"><i class="fas fa-chevron-left"></i> Prev</a>
                        <?php endif; ?>
                        
                        <?php
                        $start_page = max(1, $page - 2);
                        $end_page = min($total_pages, $page + 2);
                        
                        if ($start_page > 1) {
                            echo '<a href="?page=1">1</a>';
                            if ($start_page > 2) echo '<span style="padding: 10px 8px;">...</span>';
                        }
                        
                        for ($i = $start_page; $i <= $end_page; $i++): ?>
                            <a href="?page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>"><?php echo $i; ?></a>
                        <?php endfor;
                        
                        if ($end_page < $total_pages) {
                            if ($end_page < $total_pages - 1) echo '<span style="padding: 10px 8px;">...</span>';
                            echo '<a href="?page=' . $total_pages . '">' . $total_pages . '</a>';
                        }
                        ?>
                        
                        <?php if ($page < $total_pages): ?>
                            <a href="?page=<?php echo $page + 1; ?>">Next <i class="fas fa-chevron-right"></i></a>
                        <?php endif; ?>
                    </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; ?>
                        <?php if (count($categories) == 0): ?>
                            <li>No categories yet</li>
                        <?php endif; ?>
                    </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; ?>
                        <?php if (count($popular_tags) == 0): ?>
                            <p>No tags yet</p>
                        <?php endif; ?>
                    </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 and auction tips 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>
// Newsletter subscription
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'; ?>