<?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']) )
        header("Location: logout.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Admin Panel</title>

    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/sb-admin-2.min.css" rel="stylesheet">
    <link href="css/alltable.css" rel="stylesheet">
    <link rel="stylesheet" href="vendor/croppie/croppie.css" />
    <link href="css/style.css" rel="stylesheet">

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

    <style>
        #uploadimageModal .modal-dialog{
            max-width: 100% !important;
        }
    </style>
</head>
<body id="page-top" class="mt-55">

<div class="overlay"></div>

    <div id="wrapper">

        <div id="content-wrapper" class="d-flex flex-column">

            <div id="content">

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

                <div class="container-fluid px-0">

                    <div class="card shadow mb-4">
                        <div class="card-body">

                        
                            <h1 class="h3 mb-2 text-gray-800 text-center">Notice</h1>
                            <hr>
                        <?php
                        if ( isset($_GET["edit_id"]) ) 
                        {    
                            $edit_sql = "SELECT * FROM notice_table WHERE n_sl = '$_GET[edit_id]'";
                            $edit_run = mysqli_query($con, $edit_sql);
                            $edit_row = mysqli_fetch_assoc($edit_run);
                        ?>
                         
                            <form method='post' action='' enctype='multipart/form-data'>
                                <font color="black">Title</font>
                                <input type="text" class="form-control mb-2" name="title" value="<?php echo $edit_row['n_title']; ?>" required>
                                <font color="black">Date</font>
                                <input type="date" class="form-control mb-2" name="date" value="<?php echo $edit_row['n_date']; ?>" required>
                                <font color="black">Time</font>
                                <input type="time" class="form-control mb-2" name="time" value="<?php echo $edit_row['n_time']; ?>" required>
                                <font color="black">Upload Pdf
                                 <?php
                                  if (!empty($edit_row['notice']))
                                    {
                                    echo " <a href='../upload/noticePdf/$edit_row[notice]' title='Click to open' target='_blank'> (<b>Already Uploaded - </b>$edit_row[notice]</a> )
                                    ";
                                    }
                                 ?>
                                </font>
                                <input type='file' name='file' class="form-control"><br>
                                <font color="black">Description</font>
                                <textarea name="content" id="content" class="form-control ckeditor" >  <?php echo $edit_row['m_notice']; ?>  </textarea><br>

                                <input type='submit' value='Notice Update' class="btn btn-primary" name='n_update'> 
                                <a href="notice.php" class="btn btn-warning my-4"> Cancel </a>   

                            </form>
                        <?php 
                        }
                        else
                        {  ?>
                          
                            <form method='post' action='' enctype='multipart/form-data'>
                                <font color="black">Title</font>
                                <input type="text" class="form-control mb-2"  placeholder="Notice Title" name="title" required>
                                <font color="black">Date</font>
                                <input type="date" class="form-control mb-2" name="date" required>
                                <font color="black">Time</font>
                                <input type="time" class="form-control mb-2" name="time" required>
                                <font color="black">Upload Pdf</font>
                                <input type='file' name='file' class="form-control"><br>
                                <font color="black">Description</font>
                                <textarea name="content" id="content" class="form-control ckeditor" >    </textarea><br>

                                <input type='submit' value='Notice Submit' class="btn btn-primary" name='n_submit'>    

                            </form>


                        <?php 
                        }   ?> 


<BR><BR>


                            <div class="row m-0">
                             

                                <div class="col-md-12 p-0">
                                    <div class="table-responsive">
                                        <table class="table table-hover table-bordered table-sm" id="thisismytable">
                                            <thead>
                                                <tr>
                                                    <th>No.</th>
                                                    <th>Title</th>
                                                    <th>Notice</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM notice_table WHERE n_by = '$_SESSION[uid]' ORDER BY n_date DESC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
                                                       echo "
                                                        <tr>
                                                            <td>
                                                                <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            $rows[n_title] <br>
                                                        "; ?>
                                                            <?php echo showDate($rows['n_date']); ?>
                                                            <?php echo showTime($rows['n_time']); ?>  
                                                        <?php   
                                                        echo "     

                                                            </td>
                                                            <td>
                                                                <a href='noticeView.php?n=$rows[n_sl]' title='Click to open' class='m-2 btn btn-warning btn-sm'> View</a>
                                                            </td>
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                               <a href='notice.php?edit_id=<?php echo $rows["n_sl"]; ?>' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>
                                                                <a href='notice.php?del_id=<?php echo $rows["n_sl"]; ?>' class='delete m-2 btn btn-danger btn-sm rounded-circle' title='Click to Delete'> <i class='fas fa-trash'></i> </a>
                                                            </td>
                                                        </tr>
                                                        <?php
                                                        $c++;
                                                    }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

            </div>

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

        </div>

    </div>












    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/sb-admin-2.min.js"></script>
    <script src="js/sweetalert2.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/datatables.mark.min.js"></script>
    <script src="vendor/croppie/croppie.js"></script>
    <script src="js/jquery-ui-custom.min.js"></script>
    <script src="js/custom.js"></script>

    <script>
        $(document).ready(function(){
            $('#thisismytable').DataTable();          
        });
   </script>
  
</body>
</html>



<?php 

      if(isset($_POST['n_submit']))
       {
          
            if (!empty($_FILES["file"]["name"]))
            {
                         #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/noticePdf';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);




               
                     $sql = "INSERT INTO notice_table (n_title, notice, n_by, n_updateDT, m_notice,n_date,n_time) VALUES ('$_POST[title]', '$fname', '$_SESSION[uid]', ' $currentTime', '$_POST[content]', '$_POST[date]', '$_POST[time]') ";

            }
            else
            {

                     $sql = "INSERT INTO notice_table (n_title, n_by, n_updateDT, m_notice,n_date,n_time) VALUES ('$_POST[title]','$_SESSION[uid]', ' $currentTime', '$_POST[content]', '$_POST[date]', '$_POST[time]') ";
            }
               




               
    

                if(mysqli_query($con, $sql))
                {
                    ?>
                    <script>
                        alert("Record updated successfully");
                        window.location = 'notice.php';
                    </script>
                    <?php
                }
                else
                {
                    ?>
                    <script>
                        alert('OOPS !!!!');
                        /*setTimeout(() => {
                            window.location = 'notice.php';
                        }, 500); */
                    </script>
                    <?php
                }
    }
 ?>



<?php
  if ( isset($_GET["del_id"]) ) {
           
        $del_sql = "DELETE FROM notice_table WHERE n_sl = '$_GET[del_id]'" ;

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "notice.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'notice.php';
                }, 1200);
            </script>
            <?php
        }
        
    }

   
?>
<?php 

      if(isset($_POST['n_update']))
       {
          
            if (!empty($_FILES["file"]["name"]))
            {
                         #file name with a random number so that similar dont get replaced
                             $fname = rand(1000,10000)."-".$_FILES["file"]["name"];
                 
                        #temporary file name to store file
                             $tname = $_FILES["file"]["tmp_name"];
                           
                        #upload directory path
                            $uploads_dir = '../upload/noticePdf';
                        #TO move the uploaded file to specific location
                            move_uploaded_file($tname, $uploads_dir.'/'.$fname);


                     $edit_sql = "UPDATE notice_table SET n_title = '$_POST[title]', notice ='$fname',m_notice ='$_POST[content]', n_date ='$_POST[date]', n_time ='$_POST[time]' WHERE n_sl = '$_GET[edit_id]'";

            }
            else
            {

                     $edit_sql = "UPDATE notice_table SET n_title = '$_POST[title]', m_notice ='$_POST[content]', n_date ='$_POST[date]', n_time ='$_POST[time]' WHERE n_sl = '$_GET[edit_id]'";
            }
               
 
    

                if(mysqli_query($con, $edit_sql))
                {
                    ?>
                    <script>
                        alert("Record updated successfully");
                        window.location = 'notice.php';
                    </script>
                    <?php
                }
                else
                {
                    ?>
                    <script>
                        alert('OOPS !!!!');
                        /*setTimeout(() => {
                            window.location = 'notice.php';
                        }, 500); */
                    </script>
                    <?php
                }
    }
 ?>
<?php 

     function showDate($dt) 
     {
          $date=date_create();
          date_timestamp_set($date,
          strtotime("$dt"));
          return date_format($date,"M j, Y");
    }

    function showTime($t) 
     {
          $time=date_create();
          date_timestamp_set($time,
          strtotime("$t"));
          return date_format($time,"g:i a");
    }

 ?>