
<?php include("header.php"); ?>

<?php

// Check connection
if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
}

// Select a random news article from the database
$sql = "SELECT * FROM news ORDER BY RAND() LIMIT 1";
$result = $connect->query($sql);

// Check if any result is returned
if ($result->num_rows > 0) {
    // Fetch the main news article data
    $row = $result->fetch_assoc();
    $category = htmlspecialchars($row['category']);
    $name = htmlspecialchars($row['name']);
    $description = htmlspecialchars($row['description']);
    $image = htmlspecialchars($row['image']);
} else {
    echo "No news found.";
}

// Fetch three random suggested headlines
$suggested_sql = "SELECT name FROM news ORDER BY RAND() LIMIT 3";
$suggested_result = $connect->query($suggested_sql);
$suggested_headlines = [];
if ($suggested_result->num_rows > 0) {
    while ($suggested_row = $suggested_result->fetch_assoc()) {
        $suggested_headlines[] = htmlspecialchars($suggested_row['name']);
    }
}


?>

<style>
.card-img-top {
    width: 100%;            /* Ensures image takes up the full width */
    height: 200px;          /* Fixed height (optional, for uniformity) */
    object-fit: cover;      /* Ensures the image covers the container, cropping if necessary */
    border-radius: 0.25rem; /* Optional: Adds rounded corners */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Optional: Adds shadow for better depth */
}
.latest-art{
    background-color:red;
    height:10px;
}
.card-titlex{
    font-size:16px;
}
.card-textx{
     font-size:14px; 
}
}
</style>

    <div class="container mt-5">
        <div class="row">
    <?php if (isset($row)): ?>
        <!-- Main News Article (col-12 for small screens, col-6 for large screens) -->
        <div class="col-sm-12 col-lg-6 mb-4">
            <div class="card">
                <div class="row g-0">
                    <!-- Image Column -->
                    <div class="col-12 col-md-4">
                        <?php if (!empty($image)): ?>
                            <img src="administrators/<?php echo $image; ?>" class="card-img-top img-fluid rounded" alt="News Image">
                        <?php else: ?>
                            <img src="https://via.placeholder.com/150" class="card-img-top img-fluid rounded" alt="No Image Available">
                        <?php endif; ?>
                    </div>
                    <!-- Text Column -->
                    <div class="col-12 col-md-8">
                        <div class="card-body">
                            <h5 class="card-title"><?php echo $name; ?></h5>
                            <h6 class="card-subtitle mb-2 text-muted"><?php echo $category; ?></h6>
                            <!-- Truncated description -->
                            <p class="card-text" id="news-description">
                                <?php echo nl2br(substr($description, 0, 200)); ?>... 
                                <span id="more-text" style="display:none;"><?php echo nl2br(substr($description, 200)); ?></span>
                            </p>

                            <!-- Read more / Read less toggle button -->
                            <a href="javascript:void(0);" id="read-more" class="card-link">Read more...</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php else: ?>
        <p>No news available at the moment.</p>
    <?php endif; ?>

    <!-- Suggested Headlines Section -->
    <div class="col-sm-12 col-lg-6 mb-4">
        <h4>Suggested Headlines</h4>
        <ul class="list-group">
            <?php foreach ($suggested_headlines as $headline): ?>
                <li class="list-group-item"><?php echo $headline; ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
</div>

    </div>
    
    
       
<div class="container mt-5">
    <div>
        <h2>Latest Articles</h2>
        <div class="latest-art"></div>
    </div>

    <div class="row">
        <?php
        // Check connection
        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }

        // Select all news articles from the database
        $sql = "SELECT * FROM news ORDER BY created_at DESC";  // Assuming 'created_at' is a timestamp column
        $result = $connect->query($sql);

        // Check if any results are returned
        if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
                $category = htmlspecialchars($row['category']);
                $name = htmlspecialchars($row['name']);
                $description = htmlspecialchars($row['description']);
                $image = htmlspecialchars($row['image']);
        ?>
                <div class="col-sm-12 col-md-4 col-lg-4" style="margin-top:10px;">
                    <?php if (!empty($image)): ?>
                        <img src="administrators/<?php echo $image; ?>" class="card-img-top img-fluid rounded" alt="News Image">
                    <?php else: ?>
                        <img src="https://via.placeholder.com/150" class="card-img-top img-fluid rounded" alt="No Image Available">
                    <?php endif; ?>
                    <div class="card-body">
                        <h5 class="card-title"><?php echo nl2br(substr($name, 0, 50)); ?>...</h5>
                        <h6 class="card-subtitle mb-2 text-muted"><?php echo $category; ?></h6>
                        <!-- Truncated description -->
                        <p class="card-text" id="news-description">
                            <?php echo nl2br(substr($description, 0, 80)); ?>... 
                            <span id="more-text" style="display:none;"><?php echo nl2br(substr($description, 80)); ?></span>   <!-- Read more / Read less toggle button -->
                            <a href="javascript:void(0);" id="read-more" class="card-link">Read more...</a>
                        </p>
                    </div>
                </div>
        <?php
            }
        } else {
            echo "<p>No news found.</p>";
        }
        ?>
    </div>
</div>

<!-- Add JavaScript for the "Read more" functionality -->
<script>
    document.querySelectorAll('#read-more').forEach(function(link) {
        link.addEventListener('click', function() {
            var description = this.closest('.card-body').querySelector('#news-description');
            var moreText = description.querySelector('#more-text');
            
            if (moreText.style.display === "none") {
                moreText.style.display = "inline";
                this.textContent = "Read less...";
            } else {
                moreText.style.display = "none";
                this.textContent = "Read more...";
            }
        });
    });
</script>


   
        <!-- Category News Start-->
        <div class="cat-news">
            <div class="container">
                <div class="row">
                    
             
      <div class="col-md-6">
                        <h2>Policitics</h2>
                        <div class="row cn-slider">
    <?php
    $sql = "SELECT * FROM news WHERE category = 'Politics' LIMIT 6"; // Limit to 6 items or change as needed
    $result = $connect->query($sql);

    // Check if there are results
    if ($result->num_rows > 0) {
        // Loop through each news item and display it
        while($row = $result->fetch_assoc()) {
            $name = $row['name'];
            $image = $row['image']; // Assuming the image path is stored
            $id = $row['id']; // Assuming the news article has a unique ID
            echo '<div class="col-md-6">';
            echo '<div class="cn-img">';
            echo '<img src="administrators/' . $image . '" alt="' . htmlspecialchars($name) . '" />';
            echo '<div class="cn-title">';
            echo '<a href="news_detail.php?id=' . $id . '">' . htmlspecialchars($name) . '</a>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
    } else {
        echo '<p>No  news found.</p>';
    }

   
    ?>
        </div>
                </div>
    


      <div class="col-md-6">
                        <h2>Technology</h2>
                        <div class="row cn-slider">
    <?php
    $sql = "SELECT * FROM news WHERE category = 'Technology' LIMIT 6"; // Limit to 6 items or change as needed
    $result = $connect->query($sql);

    // Check if there are results
    if ($result->num_rows > 0) {
        // Loop through each news item and display it
        while($row = $result->fetch_assoc()) {
            $name = $row['name'];
            $image = $row['image']; // Assuming the image path is stored
            $id = $row['id']; // Assuming the news article has a unique ID
            echo '<div class="col-md-6">';
            echo '<div class="cn-img">';
            echo '<img src="administrators/' . $image . '" alt="' . htmlspecialchars($name) . '" />';
            echo '<div class="cn-title">';
            echo '<a href="news_detail.php?id=' . $id . '">' . htmlspecialchars($name) . '</a>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
    } else {
        echo '<p>No  news found.</p>';
    }

   
    ?>
        </div>
                </div>
    


    
                  
                </div>
            </div>
        </div>
        <!-- Category News End-->

        <!-- Category News Start-->
        <div class="cat-news">
            <div class="container">
                <div class="row">
       <div class="col-md-6">
                        <h2>Business</h2>
                        <div class="row cn-slider">
    <?php
    $sql = "SELECT * FROM news WHERE category = 'Business' LIMIT 6"; // Limit to 6 items or change as needed
    $result = $connect->query($sql);

    // Check if there are results
    if ($result->num_rows > 0) {
        // Loop through each news item and display it
        while($row = $result->fetch_assoc()) {
            $name = $row['name'];
            $image = $row['image']; // Assuming the image path is stored
            $id = $row['id']; // Assuming the news article has a unique ID
            echo '<div class="col-md-6">';
            echo '<div class="cn-img">';
            echo '<img src="administrators/' . $image . '" alt="' . htmlspecialchars($name) . '" />';
            echo '<div class="cn-title">';
            echo '<a href="news_detail.php?id=' . $id . '">' . htmlspecialchars($name) . '</a>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
    } else {
        echo '<p>No  news found.</p>';
    }

   
    ?>
        </div>
                </div>
    
  <div class="col-md-6">
                        <h2>Entertainment</h2>
                        <div class="row cn-slider">
    <?php
    $sql = "SELECT * FROM news WHERE category = 'Entertainment' LIMIT 6"; // Limit to 6 items or change as needed
    $result = $connect->query($sql);

    // Check if there are results
    if ($result->num_rows > 0) {
        // Loop through each news item and display it
        while($row = $result->fetch_assoc()) {
            $name = $row['name'];
            $image = $row['image']; // Assuming the image path is stored
            $id = $row['id']; // Assuming the news article has a unique ID
            echo '<div class="col-md-6">';
            echo '<div class="cn-img">';
            echo '<img src="administrators/' . $image . '" alt="' . htmlspecialchars($name) . '" />';
            echo '<div class="cn-title">';
            echo '<a href="news_detail.php?id=' . $id . '">' . htmlspecialchars($name) . '</a>';
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }
    } else {
        echo '<p>No  news found.</p>';
    }

   
    ?>
        </div>
                </div>
         
                </div>
            </div>
        </div>

        <!-- Category News End-->
     
<script>
    // JavaScript to toggle the full text on "Read more" click
    document.getElementById('read-more').addEventListener('click', function() {
        var moreText = document.getElementById('more-text');
        var readMoreLink = document.getElementById('read-more');
        
        if (moreText.style.display === 'none') {
            moreText.style.display = 'inline';
            readMoreLink.textContent = 'Read less...';
        } else {
            moreText.style.display = 'none';
            readMoreLink.textContent = 'Read more...';
        }
    });
</script>
        <?php include("footer.php"); ?>