<?php include("conn.php");
$page='Profile Settings';?>
<!DOCTYPE html>
<html lang="en">
<?php include("common/head.php")?>
<style>
    .user{
        margin: 50px;
    }
    .img-p {
    width: 300px;
    
}
.user_text{
    font-size:20px;
}
</style>
<body>
<?php include("common/menu.php")?>
<!---side nav bar end-->
<?php include("common/tittle.php")?>
            <!--Main body Start-->
            <div id="accordions" class="col-sm-12 mt-2">

            <!--Main body Start-->
            <?php 
     include("conn.php");
$sql = "SELECT * FROM users WHERE id='$id_user' ";
$result=mysqli_query($conn,$sql);
    $row=mysqli_fetch_array($result);
?>


            <button type="button" name="moode" class="btn m-2 mood btn-primary">Mood</button>
              
                   <hr class="bg-success">

                   <div class="user">
                   <h5 class="text-center">User Profile</h5>
                    <div class="row">
                        <div class="col-sm-6">
                            <img class="img-p" src="user_image/<?php echo $row['image'] ?>"alt=""> <br>
                           <button type="button" name="change-image" class="btn m-2 change btn-primary">Change Profile</button>
                        </div>
                        <div class="col-sm-6">
                            <div class="user_text">
                                <p>User Name: <?php echo $row['fname']." ".$row['sname'] ?></p>
                                <p>Email: <?php echo $row['email'] ?></p>
                                <p>Number: <?php echo $row['number'] ?></p>
                                <p>Passward:********* <button data-toggle="modal" data-target=".bd-example-modal-lg"  class=" btn btn-primary btn-sn">Change password</button></p>
                                <p>Role: <?php echo $row['role'] ?></p>
                            </div>
                            <button class="btn btn-lg text-white bg-primary edit_user">Edit</button>
                        </div>
                    </div>
                   </div>
   
              

                <!--Orders start-->

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

          
     <!-- Modal -->
     <?php 
     include("conn.php");
$sql = "SELECT * FROM users WHERE id='$id_user' ";
$result=mysqli_query($conn,$sql);
    $row=mysqli_fetch_array($result);
?>

<!-- User details modals -->
<div id="dataModal"  class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_detail" class="modal-content text-dark">
    <div class="edit-header">
          <h3 class="edit-tittle text-center " id="exampleModalLabel">Edit User</h3>
        </div>
        <div class="edit-body text-center">
            <form  method="post" class="m-4 ">
                <div class="form-group">
                  <label for="exampleFormControlInput1">User FirstName</label>
                  <input type="text" class="form-control" name="fname" id="exampleFormControlInput1" value="<?php echo $row['fname']?>">
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">User LastName</label>
                  <input type="text" class="form-control" name="sname" id="exampleFormControlInput1" value="<?php echo $row['sname']?>">
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">Phone Number</label>
                  <input type="text" class="form-control" name="number" id="exampleFormControlInput1" placeholder=" User Phone number" value="<?php echo $row['number']?>">
                </div>
                <div class="form-group">
                  <label for="exampleFormControlInput1">Email Address</label>
                  <input type="email" class="form-control" name="email" id="exampleFormControlInput1" placeholder=" Email" value="<?php echo $row['email']?>">
                </div>


               
                <input type="hidden" name="password" value="1234">
                <input type="hidden" name="date" value="<?php echo date("F j, Y") ?>">
                
                  <button class="btn btn-primary "  name="submits">Submit</button>
              </form>
        </div>
    
    
    </div>
  </div>
</div>



<!--Chnage password modals--->

<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="form ">
        <h4 class="text-center">Change password</h4>
        <form method="post">
          <div class="form-group col-md-12">
            <label for="inputEmail4">Old password</label>
            <input type="password" class="form-control" id="inputEmail4" name="old" placeholder="Enter old password" required>
          </div>

          <div class="form-group col-md-12">
            <label for="inputEmail4">New password</label>
            <input type="password" class="form-control" id="inputEmail4" name="new" placeholder="Enter New password"required>
          </div>
         
          <div class="form-group col-md-12">
          <button class="btn btn-primary "  name="submita">Submit</button>
          </div>
         
          
        </form>

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

<?php
if(isset($_POST['submita'])){
 
  $old=$_POST['old'];
  $new=$_POST['new'];




     $sql7 = "SELECT  `password` FROM `users` WHERE `id`='".$id_user."'";
     $result7 = $conn->query($sql7);
     $row7=mysqli_fetch_array($result7);
     $old_pass=$row7["password"];


     if($old==$old_pass){
      $sql = "UPDATE `users` SET `password`='$new'WHERE id='$id_user'";
     }
     else{
      echo "<script>alert('Wrong old password $old_pass'); window.location='user_settings'</script>" ;
    }
      if(mysqli_query($conn,$sql)){
        echo"<script>alert('User password Changed'); window.location='user_settings'</script>";
      }
      else{
        echo 'Error: '.mysqli_error($conn);
      }
		

     }

?>


<!-- Change image modal--->
<div id="changeimage"  class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_detail" class="modal-content text-dark">
    <div class="edit-header">
          <h3 class="edit-tittle text-center " id="exampleModalLabel">Upload Profile picture</h3>
        </div>
        <div class="edit-body text-center">
            <form  method="post" enctype="multipart/form-data"  class="m-4 ">
                <div class="form-group">
                  <label for="exampleFormControlInput1">Select Image</label>
                  <input type="file" class="form-control" name="image" id="exampleFormControlInput1">
                </div>
                  <button class="btn btn-primary "  name="submit">Submit</button>
              </form>
        </div>
    
    
    </div>
  </div>
</div>
<?php

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

	$filename = $_FILES['image']['name'];

//returns 'does-this-thing-work-or-not'


	// Select file type
	$imageFileType = strtolower(pathinfo($filename,PATHINFO_EXTENSION));
	
	// valid file extensions
	$extensions_arr = array("jpg","jpeg","png");
 
	// Check extension
	if( in_array($imageFileType,$extensions_arr) ){
 
	// Upload files and store in database
	if(move_uploaded_file($_FILES["image"]["tmp_name"],'user_image/'.$filename)){
		// Image db insert sql
		$sql = "UPDATE `users` SET `image`='$filename'WHERE id='$id_user'";
		if(mysqli_query($conn,$sql)){
      echo"<script>alert('You Have Successfully Uploaded Your Image'); window.location='user_settings'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
	}else{
		echo 'Error in uploading file - '.$_FILES['image']['name'].'<br/>';
	}
	}
} 


?>

<!---close modals--->

</div>

<!-- Change image modal--->
<div id="mood"  class="modal fade bd-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div id="employee_detail" class="modal-content text-dark">
    <div class="edit-header">
          <h3 class="edit-tittle text-center " id="exampleModalLabel">Change color</h3>
        </div>
        <div class="edit-body text-center">
            <form  method="post" enctype="multipart/form-data"  class="m-4 ">
                <div class="form-group">
                  <label for="exampleFormControlInput1">Select mood</label>
                 <select name="mood" id="mood">
                    <option value="1">Dark</option>
                    <option value="2">Light</option>
                 </select>
                </div>
                  <button class="btn btn-primary "  name="submit">Submit</button>
              </form>
        </div>
    
    
    </div>
  </div>
</div>
<?php
if(isset($_POST['submit'])){
 

     $userid=$id_user;
     $mood=$_POST['mood'];



     $sql1 = "SELECT `mode` FROM `usersettings` WHERE `userid`='".$userid."'";
     $result = $conn->query($sql1);
     
     if($result->num_rows >= 1) {
		$sql = "UPDATE `usersettings` SET `mode`='$mood'WHERE userid='$id_user'";
		if(mysqli_query($conn,$sql)){

      echo"<script>alert('Color successfully updated'); window.location.href='user_settings'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}
    }
    else{
        $sql2="INSERT INTO `usersettings`(`userid`, `mode`) VALUES ('$userid','$mood')";
        if(mysqli_query($conn,$sql2)){
   
           echo"<script>alert('Color successfully updated'); window.location.href='user_settings'</script>";
             }
    }
     }

?>


<!--mood close--->

        <?php 





if(isset($_POST['submits'])){
 
    $fname=addslashes($_POST['fname']);
     $sname=addslashes($_POST['sname']);
     $email=$_POST['email'];
     $number=$_POST['number'];
    $password=$_POST['password'];
     $date=$_POST['date'];



     $sql = "SELECT `number`, `fname` FROM `users` WHERE `number`='".$number."' AND  `fname`='".$fname."'";
     $result = $conn->query($sql);
     
     if($result->num_rows >= 1) {
      // Image db insert sql
      echo"<script>alert('User email allready Exist'); window.location.href='user_settings'</script>";
		
     } else {
		
      $sql2 = "UPDATE `users` SET `fname`='$fname',`sname`='$sname',`number`='$number',`email`='$email',`date`='$date' WHERE id='$id_user'";
		if(mysqli_query($conn,$sql2)){

      echo"<script>alert('User edited succefully '); window.location.href='user_settings'</script>";
		}
		else{
		  echo 'Error: '.mysqli_error($conn);
		}

      
	
 
     }

}

?>



   
    
    <!--  isotope plugin cdn  -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.isotope/3.0.6/isotope.pkgd.min.js" integrity="sha256-CBrpuqrMhXwcLLUd5tvQ4euBHCdh7wGlDfNz8vbu/iI=" crossorigin="anonymous"></script>
    <script src="js/script.js"></script>
     <!-- jQuery js-->
 
     <!-- Bootstrap js -->
     <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
     <!-- jQuery Datatable js -->
     <script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
     <!-- Bootstrap Datatable js -->    
     <script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap4.min.js"></script>
     <script>
       $(document).ready(function(){  
      $('.edit_user').click(function(){  

        $('#dataModal').modal("show");  

      });  
 });  

 $(document).ready(function(){  
      $('.change').click(function(){  

        $('#changeimage').modal("show");  

      });  
 });  

 $(document).ready(function(){  
      $('.mood').click(function(){  

        $('#mood').modal("show");  

      });  
 });  
  
     </script>

</body>

</html>