<?php $user_id=$this->session->userdata('user_id');
if(!$user_id){
  redirect('user/login_view', 'refresh');
}
?>
<!DOCTYPE html>
<html lang="en">

    <?php
    $this->load->view('head.php');
    ?>

<body id="profile-page">

    <div id="wrapper" class="profile-page">

      <div class="admin-title">
      <div class="row">
        <?php $this->load->view('header.php'); ?>
      </div>
    </div>
        
     <div class="content-area">
      <div class="row">
       
          <div class="admin-menu">
            <?php $this->load->view('sidebar.php'); ?>
          </div>
           <div class="content-menu">
                    <div class="panel-heading headingnone">
                        <h2 class="transac">Profile</h2>
                                
                    </div>
                </div>
     
          <div class="main-content">
           <div class="mypro-con"> 
           
              <?php $user_id=$this->session->userdata('user_id'); ?>
  <div class="edit_profile">
 
    <?php $logo='';
    $type=$this->db->query("select logo  from user 
      where user_id=$user_id");
    foreach ($type->result() as $key => $logo)
      $logo=$logo->logo; 
    $defult="logo.jpg";
    if($logo){
      echo '<img src='.base_url().'uploads/'.$logo.' alt="Smiley face" class="profile"  >';
    }
    else{
      echo '<img src='.base_url().'uploads/'.$defult.' alt="Smiley face" class="profile"  >'; 
    }
    ?>

  </div>
            <!-- /.panel-heading -->
             <div id ="overlay" class="save-msg" style="display:none;">Changes to your profile are now saved.</div>
             
              <form method="post" id="form" >
                             <?php foreach ($profile as $key=> $item) ?>
                              <div class="form-group ">
                      <label for="pwd"></label>
                      <!-- <input type="file" name="file[]" id="file" > -->
<input type="button" id="loadimage" value="Upload Photo" onclick="document.getElementById('file').click();" />
<input type="file" style="display:none;" id="file" name="file[]"/>
                  </div>
                        <div class="form-group bank">
                      <label for="pwd">Name:</label>
                      
                      <input type="hidden" class="userid" value="<?php echo $item->user_id;?>">
                      <input type="text" class="form-control fname " placeholder="first_name" name= "fname" value="<?php echo $item->first_name; ?>">
                      <input type="text" class="form-control lname" placeholder="last_name"  name="laname" value="<?php echo $item->last_name; ?>">          
                    </div>

                             <div class="form-group ">
                      <label for="pwd">Email Address:</label>
                      <input type="email" class="form-control email" name= "email" value="<?php echo $item->user_email; ?>" >
                      <label for=""></label><button type="button" class="for-change changeemail">Change email Address</button>
                    </div>
                      <div class="showemail" style="display:none;">
                      <div class="form-group ">
                      <label for="#"></label>
                      <input type="email" class="form-control new-email" placeholder="new-email" name= "new_email" >
                      
                    </div>
                    <div class="form-group ">
                      <label for="#"></label>
                      <input type="email" class="form-control confirm-email " placeholder="confirm-email" name= "confirm-email" >
                       <label for=""></label><button type="button" class="for-change cancelemail">Cancel</button>
                    </div>
                            </div>
                        <div class="form-group ">
                      <label for="pwd">Password:</label>
                      <div class="user_password" >
                      <input type="password" class="form-control userold_password" name="user_password" value="<?php echo ($item->user_password); ?>" >
                            </div>
                      
                       <label for=""></label><button type="button" class="for-change changepassword">Change Password</button>
                    </div>
                    <div class="showpass" style="display:none;">
                    <div class="form-group ">
                      <label for="pwd"></label>
                      <input type="password" class="form-control old-password " placeholder="old-password" name="old_password" >
                      
                    </div>
                     <div class="form-group ">
                      <label for="pwd"></label>
                      <input type="password" class="form-control new-password " placeholder="new-password" name="user_password" >
                      
                    </div>
                    <div class="form-group ">
                      <label for="pwd"></label>
                      <input type="password" class="form-control confirm-password" placeholder="confirm-password" name="user_password"  >
                      <label for=""></label><button type="button" class="for-change passcancel">Cancel</button>
                      
                    </div>
                             </div>

                             <input type="button" class="btn btn-success save" onclick="profileupdate()" value="Save">
                              </form>
    

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

    </div>

     <?php $this->load->view('footer.php'); ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/md5.js"></script>
  
    
  <script type="text/javascript" src="<?= base_url() ?>assets/admin/js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap.min.js"></script>
<script type="text/javascript" src="<?= base_url() ?>assets/admin/js/my.js"></script>
<script type="text/javascript" src="<?= base_url() ?>assets/admin/js/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>
<script>
   $('.changepassword').click(function(){
        
        $('.showpass').toggle('slow'); 
       
       $('.old_password').fadeOut('slow'); 
    });
     $('.changeemail').click(function(){
        $('.showemail').toggle('slow'); 
    });
     $('.passcancel').click(function(){
       $('.showpass').fadeOut('slow'); 
         $('.old_password').toggle('slow');
         
    });
      $('.cancelemail').click(function(){
       $('.showemail').fadeOut('slow');  
    });
    </script>
    <script>
        function profileupdate(){
            
          /*var fname= $('.fname').val();
            var lname= $('.lname').val();
            var fname= $('.email').val();*/
            
             /*var file_data = $('#file').prop('files')[0];
                    var form_data = new FormData();
                    form_data.append('file', file_data);*/
            var fd = new FormData();
        var files = $('#file')[0].files[0];
        fd.append('file',files);
                    $.ajax({
                        url: "<?php echo base_url()?>user/profileupdate", // point to server-side controller method
                        dataType: 'json', // what to expect back from the server
                        cache: false,
                        contentType: false,
                        processData: false,
                        data: fd,
                        type: 'post',
                        success: function (response) {
                            var id= $('.userid').val();
                            var fname= $('.fname').val();
                            var lname= $('.lname').val();
            var email= $('.email').val();
                          
            var n_email= $('.new-email').val();
                            var old_pass=($('.old-password').val()) ; 
                        
            var c_email= $('.confirm-email').val();
            var file= $('#file').val().replace(/C:\\fakepath\\/i, '');
            var pass= $('.userold_password').val();
            var n_pass= $('.new-password').val();
            var c_pass= $('.confirm-password').val();
                         
                             $.ajax({
            url: "<?php echo base_url()?>user/changeprofile", 
            type: "POST",
            data    : {'fname':fname,'lname':lname,'email':email,'n_email':n_email,'c_email':c_email,'pass':pass,'n_pass':n_pass,'c_pass':c_pass,'id':id,'file':file,'old':old_pass},
                 dataType: 'json',
            success: function(data){
            
           
                if(n_email!==c_email){
                  swal(" Email not matched");
              }  
               if(n_pass!==c_pass && data.oldpass!==pass  ){
              swal(" Password not matched");
            }  
                      
              else if(n_email==c_email && fname && lname &&n_pass==c_pass) {
                  $('#overlay').fadeIn('fast').delay(1000).fadeOut('fast');
              }  
             
            }
        });
                        },
                        error: function (response) {
                            $('#msg').html(response); // display error response from the server
                        }
                    });
           
        }
    </script>
    <script>
         function readURL(input) {

     if (input.files && input.files[0]) {
         var reader = new FileReader();

       reader.onload = function(e) {
       $('.profile').attr('src', e.target.result);
       }

      reader.readAsDataURL(input.files[0]);
      }
          }

$("#file").change(function() {
  readURL(this);
});
    </script>
 <style>
     .type-center{
         text-align: center;
         width: 34%;
     }
     .action{
         width: 20%;
     }
     #overlay{
         background: blue;
     }
    </style>
</body>

</html>
