 <?php

    session_start();

    include_once  "../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 (!isset($_GET['edit_ref']) OR empty($_GET['edit_ref'])) {

       header("Location: view-notes.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"> 

        <!-------Including Image Upload CSS File------>

        <link rel="stylesheet" type="text/css" href="multiPhotoUploadApp/style.css"> 



</head>

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

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

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

<?php 

$edit_sql = "SELECT * FROM notes  WHERE n_ref = '$_GET[edit_ref]'";

    $edit_res = mysqli_query($con,$edit_sql);

    if ($edit_total_rows=mysqli_num_rows($edit_res)>0) {

        $edit_row=mysqli_fetch_assoc($edit_res);

        $showCS=showClassSubjectFromSid($con,$edit_row['n_subject']);



    }

?>



                <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"> Edit Notes</h5>

                            <div class="card-body">

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

                                    <div class="row">

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

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



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

                                            <option value="<?php echo $edit_row['n_class']; ?>" selected><?php echo $showCS['showClass']; ?></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">

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

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

                                                <option value="<?php echo $edit_row['n_subject']; ?>" id="classMsg" selected ><?php echo $showCS['showSubject']; ?></option>

                                            </select>

                                        </div>

                                        

                                    </div>

                                    <div class="row mt-4">

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

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

                                            <input type="text" class="form-control"name="title" placeholder="Enter Note Title" value="<?php echo $edit_row['n_title']; ?>" required>

                                        </div>

                                    </div>

                                    <div class="row mt-4">

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

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

                                            <textarea name="content" id="content" class="form-control ckeditor" required><?php echo $edit_row['note_script']; ?></textarea>

                                        </div>

                                    </div>

                                   



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

                                         <input type="submit" value=" Update Notes " name="uploadStudentNotes" id="upload" 

                                         class="btn btn-success text-sm-center mt-4"/>



                                         &nbsp;&nbsp;&nbsp; <a href="view-notes.php" class="btn btn-warning text-sm-center mt-4">

                                                <i class="fa fa-backward"></i> Back

                                            </a>

                                    </div>

                                </form>

                            </div>

                        </div>

                    </div>

                </div>

            </div>







<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">Edit Image</h5>

                <div class="card-body">

                    

                        

                        <?php 

                            $img_sql = "SELECT * FROM notes_img  WHERE ni_ref = '$_GET[edit_ref]' ORDER BY ni_pos ASC";

                            $img_res = mysqli_query($con,$img_sql);

                            if ($img_total_rows=mysqli_num_rows($img_res)>0) {

                                $img_pos = 0;

                                while ($img_row=mysqli_fetch_assoc($img_res)) {

                                    $img_path = "../upload/notes/";

                                    $img_sl = $img_row['ni_sl'];

                                    $img_name = $img_row['ni_name'];

                                    $img_pos=$img_row['ni_pos'];

                                ?>  

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

                                <div class="row">

                                    <div class="col-md-1 col-xs-1" style="margin-bottom: 10px;">

                                        <h3 style="margin-top: 20px;"><?php echo $img_row['ni_pos']; ?></h3>

                                    </div>

                                    <div class="col-md-5 col-xs-5" style="margin-bottom: 10px;">

                                        

                                        <img src="<?php echo $img_path.$img_row['ni_name']; ?>" width="90" height="120" style="display: block;margin-left: auto;margin-right: auto;">

                                    </div>

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

                                           

                                         <input type="file" name="image" class="form-control" />



                                         <input type="hidden" name="ni_sl" value="<?php echo $img_sl; ?>" class="form-control"/>



                                         <input type="hidden" name="img_name" value="<?php echo $img_name; ?>" class="form-control"/>



                                         <input type="submit" value="Update Img <?php echo $img_row['ni_pos']; ?>" name="updateImage" id="upload" class="btn btn-success text-sm-center mt-4"/>



                                         

                                    </div>

                                 </div>

                                 </form>

                                <?php

                                }

                            }

                        ?>

                        <hr>

                        <hr>

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

                                    

                            <div class="row mt-4">

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

                                    <label class="badge badge-warning">Add New Images</label> 

                                    <br><br>

                                    <div id="filediv"><input name="file[]" type="file" id="file"/></div><br/>

   

                                    <input type="button" id="add_more" class="upload" value="Add More Files"/>



                                    <input type="hidden" name="note_ref" value="<?php echo $_GET['edit_ref']; ?>" class="form-control"/>

                                    <input type="hidden" name="img_pos" value="<?php echo $img_pos; ?>" class="form-control"/>

                                </div>

                            </div>



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

                                 <input type="submit" value="Upload New Images" name="img_submit" id="upload" 

                                 class="btn btn-success text-sm-center mt-4"/>

                                 &nbsp;&nbsp;&nbsp; 

                                 <a href="view-notes.php" class="btn btn-warning text-sm-center mt-4"><i class="fa fa-backward"></i>  Back</a>



                            </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>

            <!-------Including Image Upload Js File------>

            <script src="multiPhotoUploadApp/multiPhotoScript.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 include "multiPhotoUploadApp/edit_upload.php"; ?>



<?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']));

    



    $up_sql = "UPDATE notes SET n_class='$sclass',n_subject='$subject',n_title='$title',note_script='$content' WHERE n_ref='$_GET[edit_ref]'";





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

            ?>

            <script>

                //alert('Submit Successfully');

                window.location = "edit-notes.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

            </script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = "edit-notes.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

                }, 1200);

            </script>

            <?php

        }

}



?>

<?php

    

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





    $ni_sl = $_POST['ni_sl'];

    $old_img_name = $_POST['img_name'];



       // Getting file name

       $filename = $_FILES['image']['name'];

         

       // Valid extension

       $valid_ext = array('png','jpeg','jpg');



      

     $photoExt1 = @end(explode('.', $filename)); // explode the image name to get the extension

     $phototest1 = strtolower($photoExt1);

      

     $new_profle_pic = ''.md5(uniqid()).'.'.$phototest1;

      

       // Location

       $location = "../upload/notes/".$new_profle_pic;



       // file extension

       $file_extension = pathinfo($location, PATHINFO_EXTENSION);

       $file_extension = strtolower($file_extension);



       // Check extension

       if(in_array($file_extension,$valid_ext)){  



            // Compress Image

            compressedImage($_FILES['image']['tmp_name'],$location,20);

        

            //Here i am enter the insert code in the step ........

         

        echo $img_up_sql = "UPDATE notes_img SET ni_name='$new_profle_pic' WHERE ni_sl='$ni_sl'";

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



                $del_img_path = "../upload/notes/".$old_img_name;

                unlink($del_img_path);

                ?>

                <script>

                    alert("Image Upload Successfully");

                    window.location = "edit-notes.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

                </script>

                <?php

            }

            else{

                ?>

                <script>

                    alert("Something's Wrong");

                    //window.location = "edit-notes.php?edit_ref=<?php echo $_GET['edit_ref']; ?>";

                </script>

                <?php

            }



        }

        else

        {   

            ?>

                <script>

                    alert("File format is not correct.");

                </script>

            <?php

        }

  

   }







    // Compress image

    function compressedImage($source, $path, $quality) {



            $info = getimagesize($source);



            if ($info['mime'] == 'image/jpeg') 

                $image = imagecreatefromjpeg($source);



            elseif ($info['mime'] == 'image/gif') 

                $image = imagecreatefromgif($source);



            elseif ($info['mime'] == 'image/png') 

                $image = imagecreatefrompng($source);



            imagejpeg($image, $path, $quality);



    }

?>