<?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>
<style>
#ck-img-preview img {
    max-width: 100%;
    height: auto;
}
</style>

<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_id = $_GET["view_id"];
                    $view_run = mysqli_query($con, "SELECT * FROM notice WHERE notice_recycle = '1' AND notice_id = '$_GET[view_id]'");
                    
                    if($view_row = mysqli_fetch_assoc($view_run)){

                        $state_sql = mysqli_query($con, "SELECT * FROM state WHERE id = '$view_row[notice_state]' ");
                        if($state_row = mysqli_fetch_assoc($state_sql)){
                            $state_name = $state_row["name"];
                            $state_id = $view_row["notice_state"];
                        }else{
                            $state_name = "";
                            $state_id = "";
                        }

                        $dist_sql = mysqli_query($con, "SELECT * FROM dist WHERE id = '$view_row[notice_dist]' ");
                        if($dist_row = mysqli_fetch_assoc($dist_sql)){
                            $dist_name = $dist_row["distName"];
                            $dist_id = $view_row["notice_dist"];
                        }else{
                            $dist_name = "";
                            $dist_id = "";
                        }
                    ?>
                <div class="card mt-3">
                    <h5 class="card-header text-dark  text-center font-weight-bold">
                        <?php echo $view_row["notice_title"];?></h5>
                    <div class="card-body">
                        <div class="notice-image">
                            <img src="<?php echo $view_row["notice_image"];?>" class="img-fluid" alt="">
                        </div>
                        <div class="notice-preiew">
                            <div id="ck-img-preview"><strong>Notice Details :
                                </strong><?php echo $view_row["notice_script"];?>
                            </div>
                            <p><strong>Notice Date : </strong><?php echo $view_row["notice_date"];?></p>
                            <p><strong>Notice Applicable for : </strong><?php echo $view_row["notice_for"];?></p>
                            <?php if($view_row["notice_for"] == "Student"){?>
                            <?php if(!empty($view_row["notice_class"])){?>
                            <p><strong>Notice Class : </strong><?php echo $class_name;?></p>
                            <?php } elseif(!empty($view_row["notice_sub_class"])){?>
                            <p><strong>Notice SubClass : </strong><?php echo $sub_class_name;?></p>
                            <?php } elseif(!empty($view_row["notice_year"])){?>
                            <p><strong>Notice Semister : </strong><?php echo $sub_class_year_name;?></p>
                            <?php } }?>
                            <p><strong>Notice Cteate Date : </strong><?php echo $view_row["notice_createdate"];?></p>
                        </div>

                        <a href="notice.php" class="btn btn-warning"> <i class="fa fa-arrow-left"></i>
                            Back
                        </a>
                    </div>
                </div>
                <?php }
                }  else{?>
                <div class="card mt-3 mb-3">
                    <div class="card-header">
                        <div class="row align-items-center">
                            <h4 class="text-gray-800">Notice List</h4>
                        </div>
                    </div>
                    <div class="card-body">
                        <ul class="mt-3" style="list-style:none;padding:0;">
                            <?php
                                $userDetails_sql = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]' ");
                                if($userDetails_row = mysqli_fetch_assoc($userDetails_sql)){
                                    $userName = $userDetails_row["uname"];
                                    $userState_id = $userDetails_row["state"];
                                    $userDist_id = $userDetails_row["dist"];
                                    $admin_ref = $userDetails_row["admin_ref"];
                                }

                                $conditions = [];
                                $conditions[] = "(notice_for = '$_SESSION[utype]' OR notice_for = 'All')";

                                $todayDate = date("Y-m-d");

                                // $conditions[] = "(notice_date = '$todayDate' OR notice_date < '$todayDate')";
                                $conditions[] = "(notice_state = '' OR notice_state = '$userState_id')";
                                $conditions[] = "(notice_dist = '' OR notice_dist = '$userDist_id')";
                                $conditions[] = "notice_recycle = '1'";
                                $conditions[] = "admin_ref = 'fssdf4543frts'";

                                $whereClause = implode(' AND ', $conditions);

                                $notice_sql = "SELECT * FROM notice WHERE $whereClause ORDER BY notice_id DESC";
                                $notice_run = mysqli_query($con, $notice_sql);

                                if ($notice_run) {
                                    if (mysqli_num_rows($notice_run) > 0) {
                                        while ($notice_rows = mysqli_fetch_assoc($notice_run)) {
                            ?>
                            <li class="mb-2">
                                <a href="notice.php?view_id=<?php echo $notice_rows['notice_id']; ?>"
                                    style="text-decoration:none;">
                                    <div class="blog-preview">
                                        <h4 class="m-0">
                                            <i class="fa fa-bell"></i> <?php echo $notice_rows['notice_title']; ?>
                                        </h4>
                                    </div>
                                </a>
                            </li>
                            <?php
                                        }
                                    } else {
                                        echo "<li>No notices Yet.</li>";
                                    }
                                } else {
                                    echo "<li>Error fetching notices.</li>";
                                }
                            ?>
                        </ul>
                    </div>
                </div>
                <?php }?>
            </div>
        </div>

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

    <!-- JavaScript Libraries -->

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

</body>

</html>