<?php
/**
 * Frontend Sidebar Widgets - Royal Blue Theme (Lakshya Commerce Style)
 * Displays: Search, Latest Posts, Services/Categories, Project Consultation CTA, Social Links
 */
if (!isset($conn)) {
    include_once __DIR__ . '/db.php';
}

// Fetch latest 5 published blogs for widget
$latestPosts = [];
try {
    $stmt_lp = $conn->prepare("SELECT id, title, image_url, created_at FROM blogs WHERE (status = 'published' OR status IS NULL) AND deleted_at IS NULL ORDER BY created_at DESC LIMIT 5");
    $stmt_lp->execute();
    $res_lp = $stmt_lp->get_result();
    while ($row = $res_lp->fetch_assoc()) {
        $latestPosts[] = $row;
    }
    $stmt_lp->close();
} catch (Exception $e) { /* silent */ }

// Service categories for quick navigation
$serviceCategories = [
    'Petroleum Retail Outlets' => '/bm/blog.php?category=Petroleum',
    'Civil Construction & Engineering' => '/bm/blog.php?category=Civil',
    'Fabrication & Steel Works' => '/bm/blog.php?category=Steel',
    'Aluminium & Glass Architecture' => '/bm/blog.php?category=Aluminium',
    'Modern Interior Designing' => '/bm/blog.php?category=Interior',
    'Smart Infrastructure & Road Works' => '/bm/blog.php?category=Road',
];
?>

<aside class="bm-sidebar">

    <!-- SEARCH WIDGET -->
    <div class="sidebar-widget sidebar-search-widget">
        <h3 class="widget-title"><i class="fas fa-search"></i> Search Blogs</h3>
        <form action="/bm/blog.php" method="get" class="sidebar-search-form">
            <div class="search-input-wrap">
                <input type="text" name="search" placeholder="Search by blog title…" class="sidebar-search-input"
                       value="<?php echo isset($_GET['search']) ? htmlspecialchars($_GET['search']) : ''; ?>">
                <button type="submit" class="sidebar-search-btn" title="Search"><i class="fas fa-arrow-right"></i></button>
            </div>
        </form>
    </div>

    <!-- LATEST POSTS WIDGET -->
    <div class="sidebar-widget">
        <h3 class="widget-title"><i class="fas fa-newspaper"></i> Recent Articles</h3>
        <?php if (!empty($latestPosts)): ?>
        <ul class="latest-posts-list">
            <?php foreach ($latestPosts as $lp):
                $slug = urlencode($lp['title']);
                $postUrl = '/bm/blog_post/?title=' . $slug;
                $imgSrc = !empty($lp['image_url']) ? $lp['image_url'] : '/bm/our servies image/Our Services/Construction_of_Building.jpeg';
            ?>
            <li class="latest-post-item">
                <a href="<?php echo $postUrl; ?>" class="lp-thumb">
                    <img src="<?php echo htmlspecialchars($imgSrc); ?>"
                         alt="<?php echo htmlspecialchars($lp['title']); ?>"
                         loading="lazy"
                         onerror="this.src='/bm/our servies image/Our Services/Construction_of_Building.jpeg'">
                </a>
                <div class="lp-info">
                    <a href="<?php echo $postUrl; ?>" class="lp-title"><?php echo htmlspecialchars(mb_substr($lp['title'], 0, 52)) . (mb_strlen($lp['title']) > 52 ? '…' : ''); ?></a>
                    <span class="lp-date"><i class="far fa-calendar-alt"></i> <?php echo date('d M Y', strtotime($lp['created_at'])); ?></span>
                </div>
            </li>
            <?php endforeach; ?>
        </ul>
        <?php else: ?>
        <p class="widget-empty">No recent posts found.</p>
        <?php endif; ?>
    </div>

    <!-- CATEGORIES / TOPICS WIDGET -->
    <div class="sidebar-widget">
        <h3 class="widget-title"><i class="fas fa-th-list"></i> Explore Topics</h3>
        <ul class="categories-list">
            <?php foreach ($serviceCategories as $name => $link): ?>
            <li>
                <a href="<?php echo $link; ?>">
                    <span><i class="fas fa-chevron-right me-2"></i> <?php echo htmlspecialchars($name); ?></span>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>

    <!-- PROJECT CONSULTATION CTA WIDGET -->
    <div class="sidebar-widget sidebar-cta-widget">
        <h3 class="widget-title"><i class="fas fa-headset"></i> Plan Your Project</h3>
        <p class="cta-text">Looking for reliable construction, fabrication, or architectural solutions? Connect with our senior engineers today.</p>
        <a href="/bm/contact" class="sidebar-cta-btn">
            <i class="fas fa-paper-plane me-1"></i> Get Free Consultation
        </a>
    </div>

    <!-- SOCIAL MEDIA WIDGET -->
    <div class="sidebar-widget">
        <h3 class="widget-title"><i class="fas fa-share-alt"></i> Follow Balimeen</h3>
        <div class="sidebar-social-links">
            <a href="https://www.linkedin.com/company/balimeen/" target="_blank" class="soc-icon soc-linkedin" title="LinkedIn"><i class="fab fa-linkedin-in"></i></a>
            <a href="https://whatsapp.com/channel/0029VbCM4kY4NVicRis6DF2T" target="_blank" class="soc-icon soc-whatsapp" title="WhatsApp"><i class="fab fa-whatsapp"></i></a>
            <a href="https://facebook.com" target="_blank" class="soc-icon soc-facebook" title="Facebook"><i class="fab fa-facebook-f"></i></a>
            <a href="https://youtube.com" target="_blank" class="soc-icon soc-youtube" title="YouTube"><i class="fab fa-youtube"></i></a>
            <a href="https://instagram.com" target="_blank" class="soc-icon soc-instagram" title="Instagram"><i class="fab fa-instagram"></i></a>
        </div>
    </div>

</aside>
