 <?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-notice.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"; ?>

<?php 

$edit_sql = "SELECT * FROM notice_table  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);





    }

?>



                <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 Notice</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">Categories</label>

                                        <select class="form-control" name="type"  id="showType">

                                            <option value="<?php echo $edit_row['type']; ?>" > <?php echo $edit_row['type']; ?> </option>

                                            <option value="All" > All </option>

                                            <option value="Teacher" > Teacher </option>

                                            <option value="Student" > Student </option>

                                            <option value="Class" > Class </option>

                                        </select>



   

                                        </div>

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

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

                                            <input type="date" name="date" class="form-control" value="<?php echo $edit_row['n_date']; ?>" required>

                                            

                                        </div>

                                        

                                    </div>

                                    <div class="row mt-4" id="classTable">

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

                                            <select name="class" id="showClass" class="form-control">

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

                                    <div class="row mt-4">

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

                                            <label class="badge badge-warning">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">Notice Script</label>

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

                                        </div>

                                    </div>

                                    <div class="row mt-4">

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

                                            <label class="badge badge-warning">Notice Image</label>

                                            

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

                                            <input type="hidden" name="image_path" value="<?php echo '../upload/notice/'.$edit_row['notice']; ?>" />

                                        </div>

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

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

                                            

                                        </div>

                                    </div>

                                   



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

                                         <input type="submit" value=" Update Notice " name="updateNotice" id="upload" 

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



                                         &nbsp;&nbsp;&nbsp; <a href="view-notice.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>

<script>



    $(document).ready(function() {



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

        $('#showType').on('change', function() {

            var type = this.value;

            if (type == 'Class') {

                $("#classTable").show();

                $('#title').on('click', function() {

                    var classValue = $('#showClass').val();

                    if($.isEmptyObject(classValue)){

                       alert("Select A Valid Class First");

                    }   

                });

                

            }

            

        });



    });

</script>





</body>

</html>

<?php

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



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

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

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

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

    $content = $_POST['content'];





    if(isset($_FILES['image']['name']) && !empty($_FILES['image']['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/notice/".$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 ........

             

           $up_sql = "UPDATE notice_table SET n_title='$title',m_notice='$content',notice='$new_profle_pic', type='$type',n_classid='$class', n_date='$date' WHERE n_ref='$_GET[edit_ref]'";



            if (!empty($_POST['image_path'])) {



                unlink($_POST['image_path']);

            }              



        }

        else

        {   

            ?>

                <script>

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

                </script>

            <?php

        }

  

    }else{



         $up_sql = "UPDATE notice_table SET n_title='$title',m_notice='$content',type='$type',n_classid='$class', n_date='$date' WHERE n_ref='$_GET[edit_ref]'";



    }



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

       

        ?>

        <script>

            alert('Submit Successfully');

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

        </script>

        <?php

    }

    else{

        ?>

        <script>

            alert('OOPS !!!!');

            setTimeout(() => {

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

            }, 1200);

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



}



?>

