<?php
require('db.php');
date_default_timezone_set("Asia/Kolkata");
include "x.php";

$h_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$x'");
$h_row = mysqli_fetch_assoc($h_run);   

if (isset($_GET['id'])) {
    $receive_id = $_GET['id'];

    $news_sql = "SELECT * FROM blog AS b
                 INNER JOIN users AS u ON b.bby = u.uid
                 INNER JOIN category AS c ON b.bcat = c.cid
                 WHERE b.bid = '$receive_id'";
    $news_run = mysqli_query($con, $news_sql);
    $newsx_rows = mysqli_fetch_assoc($news_run);

    // Update view count
    $newCount = $newsx_rows['bcount'] + 1;
    mysqli_query($con, "UPDATE blog SET bcount = '$newCount' WHERE bid = '$receive_id'");

    $sliderPath = "login/" . substr($newsx_rows['bimage'], 3);
    $bPath = $sliderPath;

    $see_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$x'");
    $see_row = mysqli_fetch_assoc($see_run);

    $sm = $newsx_rows['bcat'];
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <title><?php echo $see_row["uname"]; ?></title>
    <link rel="shortcut icon" href="images/logos.jpg" />
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">

    <!-- SEO & Social Tags -->
    <meta name="keywords"
        content="bengali news, latest news, news, <?php echo $newsx_rows['btitle'] . ',' . $newsx_rows['btag']; ?>" />
    <meta property="og:url"
        content="<?php echo $see_row["uweburl"]; ?>/single_news?id=<?php echo $newsx_rows['bid'] . $newsx_rows['btag']; ?>/" />
    <meta property="og:type" content="article" />
    <meta property="og:title" content="<?php echo $newsx_rows['btitle']; ?>" />
    <meta property="og:description" content="<?php echo $newsx_rows['btitle']; ?>" />
    <meta property="og:image" content="<?php echo $bPath; ?>" />
    <meta name="twitter:card" content="summary_large_image" />
    <meta name="twitter:title" content="<?php echo $newsx_rows['btitle']; ?>" />
    <meta name="twitter:description" content="<?php echo $newsx_rows['btitle']; ?>" />
    <meta name="twitter:image:src" content="<?php echo $bPath; ?>" />
    <meta name="twitter:site" content="<?php echo $see_row["uname"]; ?>" />

    <?php include "include/css.php"; ?>
</head>
<style>
.hover-shadow {
    transition: all 0.3s ease;
}

.hover-shadow:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}
</style>

<body>
    <?php include "include/preloader.php"; ?>
    <?php include "include/header.php"; ?>

    <!-- Page Header Start -->
    <div class="container-fluid page-header py-5 position-relative overflow-hidden">
        <img src="<?php echo $sliderPath; ?>" class="w-100 h-100 object-fit-cover" style="position: absolute;top: 0;"
            alt="Header Image">
        <div class="video-overlay"></div>
        <div class="container py-5 position-relative z-1">
            <h1 class="display-3 text-white mb-3 animated slideInDown"><?php echo $newsx_rows['btitle']; ?></h1>
            <nav aria-label="breadcrumb animated slideInDown">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a class="text-white" href="index.php">Home</a></li>
                    <li class="breadcrumb-item text-white active" aria-current="page">
                        <?php echo $newsx_rows['btitle']; ?></li>
                </ol>
            </nav>
        </div>
    </div>
    <!-- Page Header End -->

    <div class="container-fluid overflow-hidden py-5 bg-light">
        <div class="container">
            <div class="row g-5">
                <!-- Blog Content -->
                <div class="col-lg-8 col-md-8">
                    <article class="blog-content border-0 shadow-sm">
                        <div class="p-3 p-lg-4">
                            <div class="mb-4">
                                <h1 class="display-5 d-flex align-items-baseline fw-bold mb-3">
                                    <?php echo $newsx_rows['btitle']; ?>
                                    <div class="text-muted mb-4" style="font-size: 14px;">
                                        <span class="badge bg-primary me-3"><?php echo $newsx_rows['cname']; ?></span>
                                    </div>
                                </h1>
                            </div>

                            <img src="<?php echo $sliderPath; ?>" class="img-fluid rounded-3 mb-5 shadow"
                                alt="Blog Image">

                            <div class="content-body fs-5 lh-base text-dark">
                                <?php echo nl2br($newsx_rows['blog']); ?>
                            </div>
                        </div>
                    </article>
                </div>

                <!-- Sidebar -->
                <div class="col-lg-4 col-md-4">
                    <aside class="sidebar">
                        <div class="card border-0 shadow-sm">
                            <div class="card-body p-4">
                                <h4 class="fw-bold mb-4 pb-2 border-bottom">Related Articles</h4>
                                <div class="tab-content">
                                    <div id="mostPopular" class="tab-pane fade show active" role="tabpanel">
                                        <?php
                                        $related_sql = "SELECT * FROM blog WHERE bcat='$sm' ORDER BY bid DESC";
                                        $related_run = mysqli_query($con, $related_sql);
                                        while ($news_rows = mysqli_fetch_assoc($related_run)) {
                                                $newsPath = "login/" . substr($news_rows['bimage'], 3);
                                                $trimmedTitle = mb_strimwidth($news_rows['btitle'], 0, 60, "...");
                                                echo '
                                                <a href="article.php?id='.$news_rows['bid'].'/" class="d-block text-decoration-none text-dark mb-4 hover-shadow">
                                                    <div class="d-flex align-items-center gap-3">
                                                        <div class="flex-shrink-0">
                                                            <img src="'.$newsPath.'" class="rounded-2 object-fit-cover" style="width: 100px; height: 70px;" alt="'.$news_rows['btitle'].'">
                                                        </div>
                                                        <div class="flex-grow-1">
                                                            <h6 class="fw-semibold mb-1 lh-tight">'.$trimmedTitle.'</h6>
                                                        </div>
                                                    </div>
                                                </a>';
                                            } 
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </aside>
                </div>
            </div>
        </div>
    </div>

    <?php include "include/footer.php"; ?>
    <?php include "include/js.php"; ?>
</body>

</html>