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

        exit();

    }

?>

<!DOCTYPE html>

<html lang="en">

<head>

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

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

<!--  css crop image -->   

    <link rel="stylesheet" href="vendor/croppie/croppie.css" />





</head>

<body id="page-top" class="mt-55">



    <div style="margin-top: 70px;"></div>



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

<div class="container">

            <div class="row">

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

                        <div cl ass="card-body">



                                <h1 class="h3 mb-2 text-gray-800 text-center">Profile</h1>

                                <hr>



                                <?php

                                    $edit_run = mysqli_query($con, "SELECT * FROM users WHERE uid = '$_SESSION[uid]'");

                                    $edit_row = mysqli_fetch_assoc($edit_run);

                                    if (!empty($edit_row["uphoto"])) {

                                        

                                         $img = "../upload/profile_img/".$edit_row["uphoto"];

                                    }else{

                                        $img = "../upload/profile_img/default.jpg";

                                    }

                                ?>



                                

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

                                    <h4 class="mb-3 badge badge-secondary">Personal Details</h4>

                                     <label class="form-control form-control-sm mb-2"><b>Name : </b> <?php echo $edit_row["uname"]; ?></label>

                                      <label class="form-control form-control-sm mb-2"><b>Contact No. : </b> <?php echo $edit_row["ucontact"]; ?></label>

                                      <label class="form-control form-control-sm mb-2"><b>Class : </b> <?php echo showClass($con,$edit_row["sclass"]); ?></label>

                                      <label class="form-control form-control-sm mb-2"><b>Userid : </b> <?php echo $edit_row["uphone"]; ?></label>

                                      <h4 class="mb-3 badge badge-secondary">Address</h4>

                                      <label class="form-control form-control-sm mb-2"> <?php echo $edit_row["saddress"]; ?></label>

                                      <label class="form-control form-control-sm mb-2"> <?php echo $edit_row["dist"]; ?></label>

                                      <label class="form-control form-control-sm mb-2"> <?php echo $edit_row["pin"]; ?></label>

                                </div>

                                <div class="col-md-6" sty le="margin-top: 27px;">

                                    <h4 class="mb-3 badge badge-secondary">Profie Photo</h4> 

                                <form class="user" method="post" enctype="multipart/form-data">

                                    

                                    <div class="col-sm-4" style="margin: 0 20px 20px 0;">

                                         <img src="<?php echo $img; ?>" width="150" height="150">

                                    </div>

                                    <div class="col-sm-8">

                                        <input type="file" class="custom-file-input" name="facilitylgimage" id="file" accept="image/*" required>

                                        <label class="custom-file-label" for="file" style="color: green;">Select profile Image *</label>

                                    </div>

                                </form>

                                <form class="user" method="post" enctype="multipart/form-data">

                                    <div class="col-sm-12" style="margin-top: 10px;">

                                    <h4 class="mb-3 badge badge-secondary">Login Password</h4>

                                        <input type="text" class="form-control form-control-sm mb-2" id="upass" placeholder="Password *" name="upass" value="<?php echo $edit_row["upass"]; ?>" required>

                                      

                                    <button type="submit" class="btn btn-success btn-user m-2" name="edit_cat">

                                        <i class="fa fa-image"></i> Update Password

                                    </button>

                                    <a href="index.php" class="btn btn-warning btn-user m-2"> <i class="fa fa-ban"></i> Cancel </a>

                                   </div>

                                </form>

                                </div>

                            

                            </div>

                        </div>

                    </div>



                </div>



<div style="margin-top: 60px;"></div>

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





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

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

 <!--  js crop image -->

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

<script type="text/javascript">

        $(document).ready(function(){

            $('#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="fa fa-spinner fa-pulse"></i>');

                

        $image_crop.croppie('result', {

          type: 'canvas',

          size: 'viewport'

        }).then(function (response) {

          $.ajax({

            url: "profile.php",

            type: "POST",

            data: {

              "image": response

            },

            success: function (data) {

              $('#uploadimageModal').modal('hide');

              $('#uploaded_image').html(data);

            }

          });

        })

      });

    });

</script>

</body>

</html>





<?php

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



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



        $up_sql = "UPDATE users SET upass = '$upass' WHERE uid = '$_SESSION[uid]'";



        $_SESSION["uname"] = $uname;



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

            ?>

            <script>window.location = 'profile.php';</script>

            <?php

        }

        else{

            ?>

            <script>

                alert('OOPS !!!!');

                setTimeout(() => {

                    window.location = 'profile.php';

                }, 500);

            </script>

            <?php

        }

    }

?>







<?php 

    if(isset($_POST["image"]))

    {

        $data = $_POST["image"];

    

        $faciref = rand()."_".time();



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



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



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



        $imageSource = '../upload/profile_img/'.rand() . '.png';



        file_put_contents($imageSource, $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;

        }

        $imageName = rand() . '.png';

        $destination_img = '../upload/profile_img/'.$imageName;

        $d = compress($imageSource, $destination_img, 40);

        unlink($imageSource);

        $sql_update = "UPDATE users set uphoto= '$imageName' WHERE uid = '$_SESSION[uid]'"; 

        // echo "<pre>";print_r($new_ins_sql);exit();

        

        mysqli_query($con, $sql_update);

    }



 ?>