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

    $edit_id = $_GET['edit_ref'];

    $admin_2_run = "SELECT admin_ref FROM users WHERE uref = '$_SESSION[uref]'";
    $admin_2_row2 = mysqli_query($con, $admin_2_run);
    $admin_2_row = mysqli_fetch_assoc($admin_2_row2);
    $admin_2 = $admin_2_row["admin_ref"];
?>

 <!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"; ?>
     <?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']);
                $n_class = $edit_row['n_class'];
            }
     ?>
     <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 = '$admin_2'");
                                            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?cid=<?php echo $n_class; ?>"
                                     class="btn btn-warning text-sm-center mt-4">Cancel</a>
                             </div>
                         </form>
                     </div>
                 </div>
             </div>
         </div>
     </div>

     <?php include("includes/notes-edit-img.php");?>
     <?php include("includes/notes-edit-doc.php");?>
     <?php include("includes/notes-edit-video.php");?>

     <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;
             var admin_2 = "<?php echo $admin_2; ?>";
             $("#classMsg").hide();
             $.ajax({
                 url: "notes-upload-CatSubcat-Ajax.php",
                 type: "POST",
                 data: {
                     admin_2: admin_2,
                     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']));

    $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
        }
}
?>