<?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>
 <?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" /> 

   
    
    

 <style>
.items-mobile {
margin: 30px 0 0;
border: 2px solid #adadad;
padding: 10px 0;
height: 150px;
background-color: #fff;
}.items-mobile .project-name p {
font-weight: bold;
font-size: 12px;
margin: 12px 0;
}.icon-wrapper i{
    font-size: 82px;
}
.items-mobile:hover {
box-shadow: 0 0.25rem 0.5rem #6e8ca5;
}
        </style>
        
        <style>
body {
  font-family: "Lato", sans-serif;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidenav a:hover {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
</style>
<style>
* {box-sizing: border-box}

/* Set height of body and the document to 100% */
body, html {
  height: 100%;
  margin: 0;
  font-family: Arial;
}

/* Style tab links */
.tablink {
  background-color: #555;
  color: white;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  font-size: 17px;
  width: 50%;
}

.tablink:hover {
  background-color: #777;
}

/* Style the tab content (and add height:100% for full page content) */
.tabcontent {
  color: white;
  display: none;
  padding: 100px 20px;
  height: 100%;
}

#Home {background-color: #D1D0C0;}
#News {background-color: #D1D0C0;}

</style>
    <style>
    /* sign in FORM */
    #logreg-forms {
        width: 412px;
        margin: 10vh auto;
        background-color: #f3f3f3;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
        transition: all 0.3s cubic-bezier(.25, .8, .25, 1);
    }

    #logreg-forms form {
        width: 100%;
        max-width: 410px;
        padding: 15px;
        margin: auto;
    }

    #logreg-forms .form-control {
        position: relative;
        box-sizing: border-box;
        height: auto;
        padding: 10px;
        font-size: 16px;
    }

    #logreg-forms .form-control:focus {
        z-index: 2;
    }

    #logreg-forms .form-signin input[type="email"] {
        margin-bottom: -1px;
        border-bottom-right-radius: 0;
        border-bottom-left-radius: 0;
    }

    #logreg-forms .form-signin input[type="password"] {
        border-top-left-radius: 0;
        border-top-right-radius: 0;
    }

    #logreg-forms .social-login {
        width: 390px;
        margin: 0 auto;
        margin-bottom: 14px;
    }

    #logreg-forms .social-btn {
        font-weight: 100;
        color: white;
        width: 190px;
        font-size: 0.9rem;
    }

    #logreg-forms a {
        display: block;
        padding-top: 10px;
        color: lightseagreen;
    }

    #logreg-form .lines {
        width: 200px;
        border: 1px solid red;
    }


    #logreg-forms button[type="submit"] {
        margin-top: 10px;
    }

    #logreg-forms .facebook-btn {
        background-color: #3C589C;
    }

    #logreg-forms .google-btn {
        background-color: #DF4B3B;
    }

    #logreg-forms .form-reset,
    #logreg-forms .form-signup {
        display: none;
    }

    #logreg-forms .form-signup .social-btn {
        width: 210px;
    }

    #logreg-forms .form-signup input {
        margin-bottom: 2px;
    }

    .form-signup .social-login {
        width: 210px !important;
        margin: 0 auto;
    }

    /* Mobile */

    @media screen and (max-width:500px) {
        #logreg-forms {
            width: 300px;
        }

        #logreg-forms .social-login {
            width: 200px;
            margin: 0 auto;
            margin-bottom: 10px;
        }

        #logreg-forms .social-btn {
            font-size: 1.3rem;
            font-weight: 100;
            color: white;
            width: 200px;
            height: 56px;

        }

        #logreg-forms .social-btn:nth-child(1) {
            margin-bottom: 5px;
        }

        #logreg-forms .social-btn span {
            display: none;
        }

        #logreg-forms .facebook-btn:after {
            content: 'Facebook';
        }

        #logreg-forms .google-btn:after {
            content: 'Google+';
        }
    }
    </style>
    
    
        <style>
        .content {
            position: absolute;
            top: 300px;
            left: 50%;
            transform: translate(-50%, -50%);
            background: rgba(0, 0, 0, 0.5);
            color: #f1f1f1;
            padding: 20px;
            border-radius: 5px;
            width: 300px;
            height:400px;
        }
        @media(max-width:500px){
            .content {
                left: 50%;
                width: 100%;
            }
        }
        .content3{
            background: #000;
            color:#fff;
            padding:20px;
        }
        .testing{
            text-align: right;
        }
        @media(max-width:768px){
            .testing{
                text-align: left;
            }
        }
    </style>
    
</head>

<body>

  <!--==========================
  Header
  ============================-->
  
  
  <!-- #header -->

  <!--==========================
    Intro Section
  ============================-->
 <?php include "includes/header2.php"; ?>

    <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">facility</h1>
                            <hr>

                            <?php
                                if ( isset($_GET["edit_id"]) ) {
                                    $edit_run = mysqli_query($con, "SELECT * FROM facility WHERE faciid = '$_GET[edit_id]'");
                                    $edit_row = mysqli_fetch_assoc($edit_run);
                                    ?>
                                    <form class="ufaci" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-2 badge badge-secondary">Edit facility</h4>
                                        <div>
                                            <div class="form-group row">
                                                <div class="col-sm-6 mb-1 mb-sm-0">
facility Name
                                                    <input type="text" class="form-control mb-2" id="faciname" placeholder="Enter Link for facility (Optional)" name="faciname" value="<?php echo $edit_row["faciname"]; ?>">
                                                </div>
 <div class="col-sm-6 mb-1 mb-sm-0">
facility Title
                                                    <input type="text" class="form-control mb-2" id="facititle" placeholder="Title" name="facititle" value="<?php echo $edit_row["facititle"]; ?>">
                                                </div>
                                            </div>
                       <div class="form-group row">
                                               
                                            </div>
                      
                       <div class="form-group row">
                                                <div class="col-sm-12 mb-1 mb-sm-0">
facility Details
                                                   
 <textarea name="facidis" id="facidis" class="form-control" rows="2" ><?php echo $edit_row["facidis"]; ?></textarea>                                              
  </div>
                                            </div>
                      
                                            <div class="form-group row">
                                                <div class="col-sm-12 mb-1 mb-sm-0">
                                                    <img src="<?php echo $edit_row["facilgimage"]; ?>" alt="facility" class="img-thumbnail mb-3" style="height:50px;">
                                                    
                                                </div>
                                            </div>
                                            <button type="submit" class="btn btn-success btn-ufaci m-2" name="update_facility">
                                                <i class="fas fa-image"></i> Update facility
                                            </button>
                                            <a href="facility.php" class="btn btn-warning btn-ufaci m-2"> <i class="fas fa-ban"></i> Cancel </a>
                                        </div>
                                    </form>
                                    <?php
                                } else {
                                    ?>
                                    <form class="ufaci" method="post" enctype="multipart/form-data">
                                        <h4 class="mb-2 badge badge-secondary" data-toggle="collapse" data-target="#target" style="cursor:pointer;">Add New facility</h4>
                                        <p class="small"> <i class="fas fa-hand-point-up"></i> Tap Here to add New facility </p>
                                        <div class="collapse" id="target">
                                            <div class="form-group row">
                                                <div class="col-sm-6 mb-1 mb-sm-0">
                                                    <input type="text" class="form-control mb-2" id="faciname" placeholder="facility Name" name="faciname">
                                                </div>
                         <div class="col-sm-6 mb-1 mb-sm-0">
                                                    <input type="text" class="form-control mb-2" id="facititle" placeholder="facility Title" name="facititle">
                                                </div>
                        
                                            </div>
                                            <div class="form-group row">
                                               <div class="col-sm-12 mb-1 mb-sm-0">
                                                 
  <textarea name="facidis" id="facidis" class="form-control" rows="2" placeholder="facility  Details" required></textarea>
                                                </div>
                                            </div>
                                            <div class="form-group row">
                                                <div class="col-sm-12 mb-1 mb-sm-0">
                                                    <div class="custom-file mb-2">
                                                        <input type="file" class="custom-file-input" name="facilitylgimage" id="file" accept="image/*" required>
                                                        <label class="custom-file-label" for="file">Select facility Image *</label>
                                                    </div>
                                                </div>
                                            </div>
                                            <!-- <button type="submit" class="btn btn-primary btn-ufaci m-2" name="new_facility">
                                                <i class="fas fa-ufaci-plus"></i> Add New facility
                                            </button> -->
                                            <a href="#target" data-toggle="collapse" class="btn btn-warning btn-ufaci m-2"> <i class="fas fa-ban"></i> Cancel </a>
                                        </div>
                                    </form>
                                    <?php
                                }
                            ?>
                        </div>
                    </div>
                </div>
            </div>

 <hr>

 <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">
                                <h4 class="mb-3 badge badge-secondary">facilitys</h4>

                                <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>Image</th>
                                                    <th>Name</th>
                                                    
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                    $sql = "SELECT * FROM facility WHERE faciby = '$_SESSION[uid]' ORDER BY facipos ASC";
                                                    $run = mysqli_query($con, $sql);

                                                    $c = 1;
                                                    while ( $rows = mysqli_fetch_assoc($run) ) {
                                                        $rows["faciname"] == "" ? $faciname = "<span class='badge badge-danger badge-pill'> <i class='fas fa-times-circle'></i> No Link </span>" : $faciname = "<a href='$rows[faciname]' target='_blank' class='badge badge-info badge-pill'> $rows[facititle] <i class='fas fa-share'></i> </a>";
$trimmefacititle = mb_strimwidth($rows['facidis'], 0, 20, "...");                                                       
                             echo "
                                                        <tr id='$rows[faciid]'>
                                                            <td>
                                                                <img src='$rows[facilgimage]' height='50' data-toggle='modal' data-target='#show-img' class='showModal'>
                                                            </td>
                                                            <td>
                                                            $rows[faciname]<br> $rows[facititle]<br> $trimmefacititle 
                                                            </td>
                                                            
                                                        ";
                                                        ?>
                                                            <td class='text-center align-middle'>
                                                                <a href='facility.php?edit_id=<?php echo $rows["faciid"]; ?>' class='m-2 btn btn-success btn-sm rounded-circle' title='Click to Edit'> <i class='fas fa-pencil-alt'></i> </a> 

                                                                <a href='facility.php?del_id=<?php echo $rows["faciid"]; ?>&imgpath=<?php echo $rows["facilgimage"]; ?>' 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><br><br>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            
      
               <?php include "includes/ft.php"; ?>

 <!--==========================
    Footer
  ============================-->

  
  <!-- #footer -->

    
    <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 src="js/mobile-nav/mobile-nav.js"></script>

  

   
   
    <div class="modal fade" id="show-img" tabindex="-1" role="dialog" aria-labellefaciby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-body">
                    <img src="" alt="facilityImage" id="image_modal" class="w-100">
                    <p class="small mt-3 mb-2" id="dimension"> <span class="font-weight-bold"> Name </span> : </p>
                    <p class="small mb-2" id="dimension"> <span class="font-weight-bold"> Dimension </span> : </p>
                    <p class="small mb-2" id="dimension"> <span class="font-weight-bold"> Size </span> : </p>
                </div>
                <div class="modal-footer">
                    <button type="close" class="btn btn-secondary" data-dismiss="modal"> Close </button>
                </div>
            </div>
        </div>
    </div>




    <div id="uploadimageModal" class="modal" role="dialog">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">

        <div class="modal-header">
          <h4 class="modal-title">Upload & Crop Image</h4>
        </div>

        <div class="modal-body">
          <div class="row mx-0">
            <div class="col-md-12 col-sm-12 col-12 text-center">
              <div id="image_demo" style="margin-top:10px;height:40%;"></div>
              <p class="small">scroll through mouse to zoom in or out</p>
            </div>
            <div class="col-md-12 col-sm-12 col-12 text-center">
              <button class="btn btn-success crop_image" id="cropppp">Crop & Upload Image</button>
            </div>
          </div>
        </div>

        <div class="modal-footer">
          <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
        </div>

      </div>
    </div>
  </div>




    <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);
            });$image_crop = $('#image_demo').croppie({
        enableExif: true,
        viewport: {
          width: 300,
          height: 300,
          type: 'square' //circle
        },
        boundary: {
          width: 310,
          height: 310,
        }
      });$('#file').on('change', function () {
        var reader = new FileReader();
        reader.onload = function (event) {
          $image_crop.croppie('bind', {
            url: event.target.result
          }).then(function () {
            console.log('jQuery bind complete');
          });
        }
        reader.readAsDataURL(this.files[0]);
        $('#uploadimageModal').modal('show');
      });$('.crop_image').click(function (event) {
          $('#cropppp').html('uploading <i class="fas fa-spinner fa-pulse"></i>');
                var faciname = $('#faciname').val();
        var facidis = $('#facidis').val();
                var facititle = $('#facititle').val();
        
        $image_crop.croppie('result', {
          type: 'canvas',
          size: 'viewport'
        }).then(function (response) {
          $.ajax({
            url: "facility.php",
            type: "POST",
            data: {
              "image": response,
                            faciname: faciname,
              facidis: facidis,
                            facititle: facititle
               
            },
            success: function (data) {
              $('#uploadimageModal').modal('hide');
              $('#uploaded_image').html(data);
            }
          });
        })
      });
    });


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

        $sortable.sortable({
            stop: function ( event, ui ) {
                var parameters = $sortable.sortable( "toArray" );
                $.post("facility_position.php",{value:parameters},function(result){
                    console.log("Position Complete");
                });
            }
        });
  </script>

</body>
</html>


<?php
    if ( isset($_GET["del_id"]) ) {

        unlink($_GET["imgpath"]);

        $del_sql = "DELETE FROM facility WHERE faciid = '$_GET[del_id]'";

        if ( mysqli_query($con, $del_sql) ) {
            ?>
            <script>window.location = "facility.php";</script>
            <?php
        } else {
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'facility.php';
                }, 500);
            </script>
            <?php
        }
        
    }
    if(isset($_POST['update_facility'])){

        $faciname = mysqli_real_escape_string($con, strip_tags($_POST['faciname']));
        $facititle = mysqli_real_escape_string($con, strip_tags($_POST['facititle']));
        $facidis = mysqli_real_escape_string($con, strip_tags($_POST['facidis']));

        $up_sql = "UPDATE facility SET faciname = '$faciname', facititle = '$facititle', facidis = '$facidis' WHERE faciid = '$_GET[edit_id]'";

        if(mysqli_query($con, $up_sql)){
            ?>
            <script>window.location = 'facility.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'facility.php';
                }, 1200);
            </script>
            <?php
        }
    }
    if(isset($_POST['edit_cat'])){
        $cname = mysqli_real_escape_string($con, strip_tags($_POST['cname']));
        $up_sql = "UPDATE category SET cname = '$cname' WHERE cid = '$_GET[edit_id]'";

        if(mysqli_query($con, $up_sql)){
            ?>
            <script>window.location = 'category.php';</script>
            <?php
        }
        else{
            ?>
            <script>
                alert('OOPS !!!!');
                setTimeout(() => {
                    window.location = 'category.php?edit_id=<?php echo $_GET["edit_id"]; ?>';
                }, 500);
            </script>
            <?php
        }
    }
    if(isset($_POST["image"]))
    {
        $data = $_POST["image"];
        $faciname = $_POST["faciname"];
    
        $facititle = $_POST["facititle"];
    $facidis = $_POST["facidis"];
    
        $faciref = rand()."_".time();

        $image_array_1 = explode(";", $data);

        $image_array_2 = explode(",", $image_array_1[1]);

        $data = base64_decode($image_array_2[1]);

        $imageName = '../upload/facility/'.rand() . '.png';

        file_put_contents($imageName, $data);
        function compress($source, $destination, $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, $destination, $quality);
            return $destination;
        }
        $destination_img = '../upload/facility/'.rand() . '.png';
        $d = compress($imageName, $destination_img, 40);
        unlink($imageName);
        $new_ins_sql = "INSERT INTO facility (faciname, facititle, facidis, facilgimage, faciby, facicreated, faciref) VALUES ('$faciname', '$facititle','$facidis', '$destination_img', '$_SESSION[uid]', '$currentTime', '$faciref')";

        // echo "<pre>";print_r($new_ins_sql);exit();
        
        mysqli_query($con, $new_ins_sql);
    }
?>
