<?php
    session_start();
   
    include "../../db.php";
    date_default_timezone_set('Asia/Kolkata');
    $currentTime = date('M j, Y g:i a');
   
?>
<!DOCTYPE html>
<html lang="en">
<head>
 <?php include "includes/meta.php"; ?>             
    <?php include "includes/css.php"; ?>
    <link href="css/all.min.css" rel="stylesheet" type="text/css">
    <link href="css/alltable.css" rel="stylesheet"> 
    <link rel="stylesheet" href="vendor/croppie/croppie.css" /> 
    
 
    
</head>

<body>

  <!--==========================
  Header
  ============================-->
  
  <?php include "includes/header2.php"; ?>
  <!-- #header -->

  <!--==========================
    Intro Section
  ============================-->
<div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                        <div class="card-body">

                        
               <h1 class="h3 mb-2 text-gray-800 text-center" style="margin-top: 90px">Academic Programmes Details</h1>
                            <hr>
             <?php
                if ( isset($_GET["edit_id"]) ) 
                {    
                    $edit_sql = "SELECT * FROM admission_procedure WHERE id = '$_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">Name</font>
               <input type="text" class="form-control mb-2" id="ap_name" value="<?php echo $edit_row['ap_name']; ?>" name="ap_name">
              <!-- <font color="black">Image
                             <?php
                                /*  if (!empty($edit_row['image']))
                                    {
                                    echo " <a href='$edit_row[image]' title='Click to open' target='_blank'> (<b>Already Uploaded - </b>$edit_row[image]</a> )
                                    ";
                                    }
                                */ ?> 
              </font>
                <input type='file' name='image' class="form-control"><br> -->
                <font color="black">Description</font>
                <textarea name="content" id="content" class="form-control ckeditor" required><?php echo $edit_row['ap_details']; ?></textarea><br>
                <input type='submit' value='Update' class="btn btn-primary" name='update'>    
              </form>



             <?php 
                }
                else
                { 

                 ?>
                  
              <form method='post' action='' enctype='multipart/form-data'>
                <font color="black">Name</font>
               <input type="text" class="form-control mb-2" id="ap_name" placeholder="" name="ap_name">
              <!-- <font color="black">Image</font>
                <input type='file' name='image' class="form-control"><br> -->
                <font color="black">Description</font>
                <textarea name="content" id="content" class="form-control ckeditor" required></textarea><br>
                <input type='submit' value='Upload' class="btn btn-primary" name='upload'>    
              </form>

              <?php 
                        }   ?> 

<br><hr>
          </div>
        </div>
      </div>
    </div>


<div class="container">
            <div class="row mx-0 just ify-content-center">
                <div class="col-12 col-sm-12 col-md-12">
                            <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> Index</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM admission_procedure WHERE ap_by = '$_SESSION[uid]' ORDER BY ap_pos ASC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                                                                            
                                               echo "
                                                        <tr id='$rows[id]'>
                                                            <td>
                                                            <span class='badge badge-info badge-pill'>$c</span>
                                                            </td>
                                                            <td>
                                                            <p class='text-dark mb-0'><b>Title: </b> $rows[ap_name] </p>
                                                           <br>
                                                            
                                                                <img src='$rows[image]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                            </td>
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                               <a href='admission_procedurers.php?edit_id=<?php echo $rows["id"]; ?>' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a>
                                                                <a href='admission_procedurers.php?del_id=<?php echo $rows["id"]; ?>&imgpath=<?php echo $rows["image"]; ?>' 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><br><br><br><br>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
            </div>
<br><br>
               <?php include "includes/ft.php"; ?>
               <?php include "includes/js.php"; ?>

     <script>
        function checkFileDetails() {
            var fi = document.getElementById('file');
            if (fi.files.length > 0) {
                for (var i = 0; i <= fi.files.length - 1; i++) {
                    var fileName, fileExtension, fileSize, fileType, dateModified;
                    fileName = fi.files.item(i).name;
                    fileExtension = fileName.replace(/^.*\./, '');
                    if (fileExtension == 'png' || fileExtension == 'jpg' || fileExtension == 'jpeg') {
                        readImageFile(fi.files.item(i));
                    }
                }
                function readImageFile(file) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var img = new Image();
                        img.src = e.target.result;
                        img.onload = function () {
                            var imgWidth = this.width;
                            var imgHeight = this.height;

                            if ( imgWidth == 1200 && imgHeight == 512 || imgWidth == 1500 && imgHeight == 640 ) {
                                var imgCheck = "<i class='fas fa-check-circle text-success'></i>";
                            } else {
                                var imgCheck = "<i class='fas fa-times-circle text-danger'></i>";
                            }
                            document.getElementById('fileInfo').innerHTML =
                                document.getElementById('fileInfo').innerHTML + '<br> ' +
                                'Name: <b>' + file.name + '</b> <br>' +
                                'Size: <b>' + Math.round((file.size / 1024)) + '</b> KB <br>' +
                                'Dimension: <b>' + imgWidth + '</b> x <b>' + imgHeight + '</b> ' + imgCheck + ' <br>';
                        }
                    };
                    reader.readAsDataURL(file);
                }
            }
        }
    </script>

   <script>
        $(document).ready(function(){
            $('#thisismytable').DataTable();
            $('#show-img').on('show.bs.modal', function (e) {
                var src = $(e.relatedTarget).attr('src');
                $(this).find('.modal-body > img').attr('src', src);
            });
        });

        var $sortable = $("#thisismytable > tbody");

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("admission_procedures_position.php",{value:parameters},function(result){
                    $(document).ready(function() {
                        const Toast = Swal.mixin({
                            toast: true,
                            position: 'bottom',
                            showConfirmButton: false,
                            timer: 2000
                        });
                        Toast.fire({
                            type: 'success',
                            title: result
                        });
                    });
                });
            }
        });
    </script> 
</body>
</html>

<?php
    
   if(isset($_POST['upload'])){

        if (isset($_FILES['image']['name']) AND !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 = '../upload/admission_procedure/'.time().'.'.$phototest1;
                  
                   // Location
                   $location = "".$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 ........

                    }
                  else
                    {
                            echo "File format is not correct.";
                    }
                
                

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

             $ins_sql = "INSERT INTO admission_procedure (image, ap_name, ap_by, ap_date, ap_details)VALUES ('$new_profle_pic','$ap_name','$_SESSION[uid]','$currentTime', '$_POST[content]')";

          
        }else{

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

             $ins_sql = "INSERT INTO admission_procedure (ap_name, ap_by, ap_date, ap_details)VALUES ('$ap_name','$_SESSION[uid]','$currentTime', '$_POST[content]')";

        }



 if( mysqli_query($con, $ins_sql) ){
        ?>
        <script>
            alert("Details Sent Successfully");
            window.location = "admission_procedurers.php";
        </script>
        <?php
    }
    else{
        ?>
        <script>
            alert("Something's Wrong");
            window.location = "index.php";
        </script>
        <?php
    }
  
  
   }
?>
<?php
  if ( isset($_GET["del_id"]) ) {
           unlink($_GET["imgpath"]);
        $del_sql = "DELETE FROM admission_procedure WHERE id = '$_GET[del_id]'";

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

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

    }

?>

<?php 

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

        if (isset($_FILES['image']['name']) AND !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 = '../upload/admission_procedure/'.time().'.'.$phototest1;
                  
                   // Location
                   $location = "".$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 ........

                    }
                  else
                    {
                            echo "File format is not correct.";
                    }
                
                

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

             $edit_sql = "UPDATE admission_procedure SET ap_name = '$ap_name', ap_details ='$_POST[content]', image ='$new_profle_pic' WHERE id = '$_GET[edit_id]'";

          
        }else{

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

            $edit_sql = "UPDATE admission_procedure SET ap_name = '$ap_name', ap_details ='$_POST[content]' WHERE id = '$_GET[edit_id]'";

             

        }



 if( mysqli_query($con, $edit_sql) ){
        ?>
        <script>
            alert("Details Sent Successfully");
            window.location = "admission_procedurers.php";
        </script>
        <?php
    }
    else{
        ?>
        <script>
            alert("Something's Wrong");
            window.location = "index.php";
        </script>
        <?php
    }
  
  
   }

 ?>