<?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");

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <?php include "includes/meta.php"; ?>        

    <?php include "includes/css.php"; ?>  

        <link hr ef="css/all.min.css" rel="stylesheet" type="text/css">  



</head>

<body id="page-top" class="mt-55">

            <div style="margin-top: 130px;"></div>

                <?php include "includes/header2.php"; ?>

                <div class="container">

                    <div class="row justify-content-center">

                    <div class="col-md-8">

                        <div class="card shadow mt-3">

                        <h5 class="card-header text-dark  text-center font-weight-bold"> Student Notes</h5>

                            <div class="card-body">

                                <form method="POST" enctype="multipart/form-data">

                                    <div class="row">

                                        <div class="col-md-6 mt-2">

                                        <a href="add-class.php" class="text-decoration-none badge badge-dark mb-2"><i class="fa fa-user-plus"></i> Add Class</a>

                                        <select class="form-control" name="sclass" id="category-dropdown">

                                            <option value="" selected disabled>Select Class...</option>

                                            <?php

                                            $result = mysqli_query($con,"SELECT * FROM class WHERE uploadedby = '$_SESSION[uref]'");

                                            while($row = mysqli_fetch_array($result)) {

                                            ?>

                                            <option value="<?php echo $row['sid'];?>"><?php echo $row["sclass"];?></option>

                                            <?php

                                            }

                                            ?>

                                        </select>



   

                                        </div>

                                        <div class="col-md-6 mt-2">

                                            <label class="badge badge-warning">Subject</label>

                                            <select class="form-control" name="subject" id="sub-category-dropdown">

                                                <option value="" id="classMsg" selected disabled >Select Class First</option>

                                            </select>

                                        </div>

                                        

                                    </div>

                                    <div class="row">

                                        <div class="col-md-12 mt-2">

                                            <label class="badge badge-warning">Note Title</label>

                                            <input type="text" class="form-control"name="title" placeholder="Enter Note Title" required>

                                        </div>

                                    </div>

                                    <div class="row">

                                        <div class="col-md-12 mt-2">

                                            <label class="badge badge-warning">Note Script</label>

                                            <textarea name="content" id="content" class="form-control ckeditor" required></textarea>

                                        </div>

                                    </div>

                                   



                                    <div class="row justify-content-center text-center">

                                        <div class="col-md-6">

                                            <input type="submit" value=" Submit Then Upload Files" name="uploadStudentNotes" id="upload" class="btn btn-success text-sm-center mt-4"/>

                                        </div>

                                        <div class="col-md-6">

                                            <a href="view-notes.php" class="btn btn-primary text-sm-center mt-4"><i class="fa fa-eye"></i> Review Notes</a>

                                        </div>

                                         



                                         &nbsp;&nbsp;&nbsp; 

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>



     <div style="margin-top: 170px;"></div>

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

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

            <!-------Including ckeditor Js File------>

            <script src="vendor/ckeditor.js"></script>



<script>

$(document).ready(function() {



    $('#category-dropdown').on('change', function() {

        var category_id = this.value;

        $("#classMsg").hide();

        $.ajax({

            url: "notes-upload-CatSubcat-Ajax.php",

            type: "POST",

            data: {

            category_id: category_id

            },

            cache: false,

            success: function(result){

            $("#sub-category-dropdown").html(result);

            }

        });

    });



});

</script>



    <script>

     CKEDITOR.replace( 'content', {

        height: 300,

        filebrowserUploadUrl: "upload.php"

    });

</script>



</body>

</html>



<?php

if (isset($_POST['uploadStudentNotes'])) {





    $subject = mysqli_real_escape_string($con, strip_tags($_POST['subject']));

    $sclass = mysqli_real_escape_string($con, strip_tags($_POST['sclass']));

    $title = mysqli_real_escape_string($con, strip_tags($_POST['title']));

    $content = mysqli_real_escape_string($con, strip_tags($_POST['content']));

    $admin_ref = $_SESSION['uref'];

    $n_ref = rand().'_'.time();



    $ins_sql = "INSERT INTO notes (n_class, n_subject, n_title, note_script, n_ref, uploadedby, n_date, n_admin_2) 

    VALUES ('$sclass', '$subject', '$title', '$content', '$n_ref', '$admin_ref', '$currentTime', '$_SESSION[uref]')";



        if(mysqli_query($con, $ins_sql)){

            ?>

            <script>

                //alert('Submit Successfully');

                window.location = 'notes-upload-img.php?ref=<?php echo $n_ref; ?>';

            </script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'notes-upload.php';

                }, 1200);

            </script>

            <?php

        }

}



function compress_image($tempPath, $originalPath, $imageQuality){

  

    // Get image info 

    $imgInfo = getimagesize($tempPath); 

    $mime = $imgInfo['mime']; 

     

    // Create a new image from file 

    switch($mime){ 

        case 'image/jpeg': 

            $image = imagecreatefromjpeg($tempPath); 

            break; 

        case 'image/png': 

            $image = imagecreatefrompng($tempPath); 

            break; 

        case 'image/gif': 

            $image = imagecreatefromgif($tempPath); 

            break; 

        default: 

            $image = imagecreatefromjpeg($tempPath); 

    } 

     

    // Save image 

    imagejpeg($image, $originalPath, $imageQuality);    

    // Return compressed image 

    return $originalPath; 



}





?>

