<?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"> 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="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 mt-2">

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

                                            <input type="date" name="date" class="form-control" required>

                                            

                                        </div>

                                        

                                    </div>

                                    

                                    <div class="row mt-2" 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-2">

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

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

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

                                        </div>

                                    </div>

                                    <div class="row mt-2">

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

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

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

                                        </div>

                                    </div>

                                    <div class="row mt-2">

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

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

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

                                        </div>

                                    </div>

                                   

                                    <br><br>

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

                                         <input type="submit" value="Submit" name="uploadNotice" id="upload" 

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



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

                                                <i class="fa fa-eye"></i> Review Notice

                                            </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['uploadNotice'])) {



    $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'];

    $admin_ref = $_SESSION['uref'];

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





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





           $ins_sql = "INSERT INTO notice_table (n_title, notice, n_by, n_updateDT, m_notice,n_date,n_ref, type, n_classid) VALUES ('$title', '$new_profle_pic', '$admin_ref', '$currentTime', '$content', '$date', '$n_ref', '$type','$class') ";

               

                

        }

        else

        {   

            ?>

                <script>

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

                </script>

            <?php

        }

  

    }else{



         $ins_sql = "INSERT INTO notice_table (n_title, n_by, n_updateDT, m_notice,n_date,n_ref, type, n_classid) VALUES ('$title', '$admin_ref', '$currentTime', '$content', '$date', '$n_ref', '$type','$class') ";

    }



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

        ?>

        <script>

            alert('Submit Successfully');

            window.location = 'add-notice.php';

        </script>

        <?php

    }

    else{

        ?>

        <script>

            alert('OOPS !!!!');

            setTimeout(() => {

                window.location = 'add-notice.php';

            }, 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);



}



?>

