<?php
    // session_start();
    include "../db.php";
    require '../auth.php';
    restrictAccess(['Student']);
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');

    if ( !isset($_SESSION['uid']) && !isset($_SESSION['uref']) && !isset($_SESSION['utype']) ) {
        header("Location: logout.php");
        exit();
    }
?>

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

<head>
    <?php include "include/meta.php"; ?>
    <?php include "include/css.php"; ?>
    <?php include "include/php-functions.php"; ?>
</head>

<body>
    <!--  Header -->
    <?php 
    include "include/header.php"; 
    ?>
    <!-- #header -->

    <section id="inner-body">
        <div class="body-content">
            <div class="container-fluid">
                <?php if(isset($_GET["view_id"])){
                    $view_sql = "SELECT * FROM data WHERE data_sl_no='$_GET[view_id]'";
                    $view_data = mysqli_query($con,$view_sql);
                    if($view_rows = mysqli_fetch_array($view_data)){
                        $update_count = $view_rows["data_count"] + 1;
                        
                        $updare_sql = mysqli_query($con, "UPDATE data SET data_count = '$update_count' WHERE data_sl_no='$_GET[view_id]'");
                    ?>
                <div class="card">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-8 mb-2">
                                <div class="mb-2">
                                    <?php
                                        if (!empty($view_rows['data_img']))
                                        {
                                    ?>
                                    <img src="<?php echo $view_rows["data_img"];?>" class="img-fluid"
                                        alt="<?php echo $view_rows["data_title"];?>">
                                    <?php } ?>
                                </div>
                                <hr>
                                <div class="mb-2">
                                    <h4><?php echo $view_rows['data_title']; ?></h4>
                                    <p>
                                        <em>
                                            <?php echo $view_rows['data_short_des']; ?>
                                        </em>
                                    </p>
                                    <p><?php echo $view_rows['data_long_des']; ?></p>
                                    <p><?php echo $view_rows['data_seo_tag']; ?></p>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-2" style="padding: 20px 10px;background: #ddd;border-radius: 5px;">
                                    <h5>Latest News</h5>
                                    <?php
                                         $blog_sql = "SELECT * FROM data WHERE data_catagory = 'news' AND data_recycle = '1' ORDER BY data_sl_no DESC";
                                         $blog_run = mysqli_query($con, $blog_sql);
     
                                         $c = 1;
                                         while ( $blog_rows = mysqli_fetch_assoc($blog_run) ) {
                                            if($blog_rows["data_sl_no"] != $_GET["view_id"]){
                                                ?>
                                    <div class="card mb-2">
                                        <a href="news.php?view_id=<?php echo $blog_rows["data_sl_no"];?>"
                                            style="text-decoration:none;color:#000;">
                                            <div class="row">
                                                <div class="col-md-4 col-4">
                                                    <img src="<?php echo $blog_rows["data_img"];?>" class="img-fluid"
                                                        alt="<?php echo $blog_rows["data_title"];?>">
                                                </div>
                                                <div class="col-md-8 col-8">
                                                    <p class="m-0">
                                                        <?php echo substr($blog_rows['data_short_des'], 0, 70); ?>...
                                                    </p>
                                                </div>
                                            </div>
                                        </a>
                                    </div>
                                    <?php 
                                            }}
                                        ?>
                                </div>
                            </div>
                        </div>
                        <br>
                        <a href="news.php?show_list" class="btn btn-warning btn-user m-2"> <i
                                class="fa fa-arrow-left"></i>
                            Back </a>
                    </div>
                </div>
                <?php } } ?>
                <?php if(isset($_GET["show_list"])){?>
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between mb-3">
                            <div>
                                <h3>Latest News</h3>
                            </div>
                        </div>
                        <div class="">
                            <ul class="mt-3" style="list-style:none;padding:0;">
                                <?php
                                    $sql = "SELECT * FROM data WHERE data_catagory = 'news' AND data_recycle = '1' ORDER BY data_sl_no DESC";
                                    $run = mysqli_query($con, $sql);

                                    $c = 1;
                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                    ?>
                                <li class="mb-2">
                                    <a href="news.php?view_id=<?php echo $rows["data_sl_no"];?>"
                                        style="text-decoration:none;">
                                        <div class="blog-preview">
                                            <div class="row">
                                                <div class="col-lg-3 col-md-3 col-3">
                                                    <img src="<?php echo $rows["data_img"];?>" class="img-fluid"
                                                        alt="<?php echo $rows['data_title']; ?>">
                                                </div>
                                                <div class="col-lg-9 col-md-9 col-9">
                                                    <h6 class="m-0"><?php echo $rows['data_title']; ?></h6>
                                                    <p class="m-0">
                                                        <?php echo substr($rows['data_short_des'], 0, 1000); ?>...
                                                    </p>
                                                </div>
                                            </div>
                                        </div>
                                    </a>
                                </li>
                                <?php $c++; }?>
                            </ul>
                        </div>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

        <?php include "include/footer.php"; ?>
    </section>

    <!-- JavaScript Libraries -->

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

</body>

</html>