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

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

    // if (empty($_GET['sid'])) {
    //     header("Location: view-notes-subject.php");
    // }

    $u_sql = "SELECT sclass,admin_ref FROM users WHERE uref = '$_SESSION[uref]'";
    $u_result = mysqli_query($con, $u_sql);
    $u_row = mysqli_fetch_assoc($u_result);
    $userClass = $u_row["sclass"];
    $admin_2 = $u_row["admin_ref"];

    $class_sql = mysqli_query($con, "SELECT * FROM class WHERE sclass = '$userClass'");
    if($class_row = mysqli_fetch_assoc($class_sql)){
        $class_sl = $class_row["sid"];
    }

    if(isset($_GET["n_id"])){
        $n_id = $_GET["n_id"];
        if(empty($n_id)){
            echo '<script type="text/javascript">window.location.href = "view-notes.php?sid=' . $class_sl . '";</script>';
            exit();
            }
        
        $note_sql = mysqli_query($con, "SELECT * FROM notes WHERE n_id = '$n_id'");
        $note_row = mysqli_fetch_assoc($note_sql);
        $note_title = $note_row["n_title"];
        $note_subject= $note_row["n_subject"];
    }
?>

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

<head>
    <?php include "includes/meta.php"; ?>
    <?php include "includes/css.php"; ?>
    <style>
    .btn-primary .active {
        color: #fff;
        background: #599d2a !important;
        border-color: #599d2a !important;
    }
    </style>
</head>

<body id="page-top" class="mt-55">
    <div style="margin-top: 70px;"></div>
    <?php include "includes/header2.php"; ?>
    <div class="container">
        <div class="row">
            <div class="col-12 col-sm-12 col-md-12">
                <div cla ss="card-body">
                    <h4 class="h3 mb-2 text-gray-800 text-center"><?php echo showSubject($con,$note_subject); ?> Subject
                    </h4>
                    <h1 class="h3 mb-2 text-gray-800 text-center"><?php echo $note_title;?> Note Attachments
                    </h1>
                    <div>
                        <a href="view-notes-attachment.php?n_id=<?php echo $_GET["n_id"];?>"
                            class="btn btn-primary <?php if(isset($_GET["img"]) || isset($_GET["doc"]) || isset($_GET["video"])) { echo 'deactive'; } else { echo 'active'; } ?>">ALL</a>
                        <a href="view-notes-attachment.php?n_id=<?php echo $_GET["n_id"];?>&img"
                            class="btn btn-primary <?php if(isset($_GET['img'])) { echo 'active'; } ?>">Images</a>
                        <a href="view-notes-attachment.php?n_id=<?php echo $_GET["n_id"];?>&doc"
                            class="btn btn-primary <?php if(isset($_GET["doc"])){echo "active" ;}?>">Documents</a>
                        <a href="view-notes-attachment.php?n_id=<?php echo $_GET["n_id"];?>&video"
                            class="btn btn-primary <?php if(isset($_GET["video"])){echo "active" ;}?>">Videos</a>
                    </div>
                    <hr>

                    <div class="images mb-5"
                        style="<?php if(isset($_GET["video"]) || isset($_GET["doc"])){echo "display:none;";}else{echo "display:block;";}?>">
                        <div class="row">
                            <div class="col-md-12">
                                <h4>Images</h4>
                            </div>
                            <?php
                            $img_sql = mysqli_query($con, "SELECT * FROM notes_img WHERE ni_type = 'image' AND ni_ref = '$n_id' ");
                            while($img_row = mysqli_fetch_assoc($img_sql)){
                            ?>
                            <div class="col-md-3 col-6 mb-3 text-center">
                                <img src="../upload/notes/<?php echo $img_row["ni_name"];?>" class="img-fluid"
                                    style="height: 250px;" alt="<?php echo $note_title;?>">
                            </div>
                            <?php }?>
                        </div>
                    </div>
                    <div class="document mb-5"
                        style="<?php if(isset($_GET["video"]) || isset($_GET["img"])){echo "display:none;";}else{echo "display:block;";}?>">
                        <div class="row">
                            <div class="col-md-12">
                                <h4>Documents</h4>
                            </div>
                            <?php
                            $img_sql = mysqli_query($con, "SELECT * FROM notes_img WHERE ni_type = 'document' AND ni_ref = '$n_id' ");
                            while($img_row = mysqli_fetch_assoc($img_sql)){
                            ?>
                            <div class="col-md-3 col-6 mb-3 text-center">
                                <?php
                                    // Assuming $img_row['ni_name'] contains the file name with extension
                                    $filename = $img_row["ni_name"];
                                    $file_ext = pathinfo($filename, PATHINFO_EXTENSION);
                                    $note_title = htmlspecialchars($note_title); // Ensure title is safely escaped

                                    // Base URL or path to the folder containing the documents
                                    $base_url = '../upload/notes/';

                                    // Determine the correct icon or preview image for the document type
                                    switch (strtolower($file_ext)) {
                                        case 'pdf':
                                            $embed_code = '<iframe src="' . $base_url . $filename . '" class="embed-responsive-item" style="width: 100%; height: 250px;" frameborder="0"></iframe>';
                                            break;
                                        case 'txt':
                                            $embed_code = '<iframe src="' . $base_url . $filename . '" class="embed-responsive-item" style="width: 100%; height: 250px;" frameborder="0"></iframe>';
                                            break;
                                        case 'xls':
                                        case 'xlsx':
                                            $embed_code = '<a href="' . $base_url . $filename . '" target="_blank">Click here to view the document</a>';
                                            break;
                                        default:
                                            $embed_code = '<p>Unsupported file type.</p>';
                                            break;
                                    }

                                    echo '<div class="document-display">';
                                    echo $embed_code;
                                    echo '</div>';
                                ?>
                            </div>
                            <?php }?>
                        </div>
                    </div>
                    <div class="videos mb-5"
                        style="<?php if(isset($_GET["img"]) || isset($_GET["doc"])){echo "display:none;";}else{echo "display:block;";}?>">
                        <div class="row">
                            <div class="col-md-12">
                                <h4>Videos</h4>
                            </div>
                            <?php
                            $img_sql = mysqli_query($con, "SELECT * FROM notes_img WHERE ni_type = 'video' AND ni_ref = '$n_id' ");
                            while($img_row = mysqli_fetch_assoc($img_sql)){
                            ?>
                            <div class="col-md-3 col-6 mb-3 text-center">
                                <iframe width="100%" height="200"
                                    src="https://www.youtube.com/embed/<?php echo $img_row["ni_name"];?>"
                                    frameborder="0" loading="lazy" allowfullscreen=""></iframe>
                            </div>
                            <?php }?>
                        </div>
                    </div>
                    <div>
                        <a href="view-notes.php?sid=<?php echo $note_subject;?>" class="btn btn-warning"><i
                                class="fa fa-arrow-left"></i> Back</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

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

</body>

</html>