<?php
if($this->session->userdata('level') == "1"){
?>
    <div id="modalEditProfil" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Ubah Profil</h6>
                      <div class="dropdown no-arrow">
	                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                      </div>
                </div>
                <form id="formEditProfil" class="form-horizontal" method="post">
                    <input type="hidden" name="id_user" id="id_userProfil" class="form-control" required />
                    <div class="modal-body">
                    <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-3">NIK</div>
                        <div class="col-md-8">
	                      <input type="text" class="form-control form-control-user" name="nik" id="nikProfil" placeholder="NIK" required>
                          <br/>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">No HP</div>
                        <div class="col-md-8">
	                      <input type="text" class="form-control form-control-user" name="username" id="usernameProfil" placeholder="No HP" required>
                          <br/>
	                    </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Password</div>
                        <div class="col-md-8">
	                      <input type="password" id="passwordProfil" class="form-control form-control-user" name="password" placeholder="Password">
                          <span style="color:red; font-size:10px;">Kosongkan jika tidak dirubah</span>
                          <br/><br/>
	                    </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Nama User</div>
                        <div class="col-md-8">
	                      <input type="text" class="form-control form-control-user" name="nama_user" id="nama_userProfil" placeholder="Nama" required>
                          <br/>
    	                </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3">Alamat</div>
                        <div class="col-md-8">
	                      <input type="text" class="form-control form-control-user" name="alamat_user" id="alamat_userProfil" placeholder="Alamat" required>
                          <br/>
	                    </div>
                    </div>

                    </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitEditProfil" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>                    
    </div>
<?php
}
?>    
    
    
    
    
   <div id="modalDisplayProfile" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Profile</h6>
                      <div class="dropdown no-arrow">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      </div>
                </div>
                <div class="modal-body">
                <div id="foto_profile_display"></div>
                <br/>
                <div id="isi_profile_display"></div>
                </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>
            </div>
        </div>                    
    </div>
   <div id="modalEditProfile" class="modal fade" role="dialog">
        <div class="modal-dialog modal-lg">
                <!-- Modal content-->
            <div class="modal-content">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                    <h6 class="m-0 font-weight-bold text-primary">Edit Profile</h6>
                      <div class="dropdown no-arrow">
                      <button type="button" class="close" data-dismiss="modal">&times;</button>
                      </div>
                </div>
                <form id="formEditProfile" class="form-horizontal" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="id_profile" value="1">
                    <div class="modal-body">
                    <div class="container-fluid">
                      <div class="row">
                          <div class="col-md-3">Gambar</div>
                          <div class="col-md-8">
                            <input type="file" name="foto_profile" id="foto_profile" class="form-control" onchange="return validasiFileEditProfile()" />
                          </div>
                      </div>
                      <div class="row">
                        <div class="col-md-3"></div>
                          <div class="col-md-8">
                            <div id="pratinjauGambarEditProfile"></div>
                            <br/>
                          </div>
                        <br/>
                      </div>
                      <div class="row">
                          <div class="col-md-12 control-label">Isi Profile</div>
                          <div class="col-md-12">
                            <textarea class="form-control" name="isi_profile" id="isi_profile"></textarea>
                          </div>
                      </div> 
                    </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Batal</button>
                        <button id="btnSubmitEditProfile" type="submit" class="btn btn-success">Submit</button>
                    </div>
                </form>
            </div>
        </div>                    
    </div>


  <!-- Bootstrap core JavaScript-->
  <script src="<?php echo base_url('assets/template/backend/')?>vendor/jquery/jquery.min.js"></script>
  <script src="<?php echo base_url('assets/template/backend/')?>vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  <!-- Core plugin JavaScript-->
  <script src="<?php echo base_url('assets/template/backend/')?>vendor/jquery-easing/jquery.easing.min.js"></script>

  <!-- Custom scripts for all pages-->
  <script src="<?php echo base_url('assets/template/backend/')?>js/sb-admin-2.min.js"></script>

  <!-- Page level plugins -->
  <script src="<?php echo base_url('assets/template/backend/')?>vendor/chart.js/Chart.min.js"></script>

  <!-- Page level custom scripts -->
  <script src="<?php echo base_url('assets/template/backend/')?>sweetalert/sweetalert.min.js"></script>
  
  <link href="https://cdn.datatables.net/1.10.20/css/dataTables.bootstrap4.min.css" rel="stylesheet" type="text/css" />
  <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js" type="text/javascript"></script>
  <script src="https://cdn.datatables.net/1.10.20/js/dataTables.bootstrap4.min.js" type="text/javascript"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/themes/jquery.nivo.slider.js"></script>
<script src="<?php echo base_url('assets/plugins/')?>ckeditor/ckeditor.js"></script>
  <link href="<?php echo base_url('assets/plugins/')?>datepicker/css/bootstrap-datepicker.css" rel="stylesheet">
  <script src="<?php echo base_url('assets/plugins/')?>datepicker/js/bootstrap-datepicker.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $('.datatable').DataTable();
        $('#slider').nivoSlider();
    $('.editor').each(function(e){
        CKEDITOR.replace(this.id);
    });
        $('.datepicker').datepicker({autoclose: true,todayHighlight: true,format: 'dd-mm-yyyy'});
            $(".datebulan").datepicker( {
                format: "mm-yyyy",
                viewMode: "months", 
                minViewMode: "months"
            }).on('changeDate', function(ev){                 
                $('.datebulan').datepicker('hide');
                
            });


  });

function edit_editor(){
     CKEDITOR.replace('isi_profile');
}

function convert_date_indo (input) {
  var datePart = input.match(/\d+/g),
  year = datePart[0], // get only two digits
  month = datePart[1], day = datePart[2];

  return day+'-'+month+'-'+year;
}

  
</script>
<script>
		if ($(window).width() < 768) {
            $('#accordionSidebar').addClass('toggled');
        }
</script>


<script type="text/javascript">
    function showProfile(){
      var id='1';
    $.ajax({
      type: 'post',
      url: '<?php echo base_url(); ?>profile/get_data',
      data: 'id='+id
    }).done(function(res){
      var data = $.parseJSON(res);
      document.getElementById('foto_profile_display').innerHTML = '<img src="<?php echo base_url(); ?>assets/images/profile/'+data.foto_profile+'" width="100%"/>';
      document.getElementById('isi_profile_display').innerHTML = data.isi_profile;

      $('#modalDisplayProfile').modal('show');

    }).fail(function(res){
      console.log(res);
    });
  }



    function ubahProfile(){
      var id='1';
    $('#formEditProfile')[0].reset();
    $.ajax({
      type: 'post',
      url: '<?php echo base_url(); ?>profile/get_data',
      data: 'id='+id
    }).done(function(res){
      var data = $.parseJSON(res);
      $('#isi_profile').val(data.isi_profile);
      document.getElementById('pratinjauGambarEditProfile').innerHTML = '<img src="<?php echo base_url(); ?>assets/images/profile/'+data.foto_profile+'" width="100%"/>';

      $('#modalEditProfile').modal('show');
      edit_editor();

    }).fail(function(res){
      console.log(res);
    });
  }
  
  
  $('#formEditProfile').submit(function(e){
    e.preventDefault();
    for (instance in CKEDITOR.instances) {
       CKEDITOR.instances[instance].updateElement();
    }
    //var data = $(this).serialize();
    $('#btnSubmitEditProfile').html('<i class="spinner fa fa-spinner"></i> Loading...');
    $('#btnSubmitEditProfile').attr('disabled', true);
    var formData = new FormData($(this)[0]);
    $.ajax({
      type : 'post',
      url : '<?php echo base_url(); ?>profile/update',
      data : formData,
      async: false,
      cache: false,
      contentType: false,
      processData: false
    }).done(function(res){
      $('#btnSubmitEditProfile').html('Submit');
      $('#btnSubmitEditProfile').removeAttr('disabled');
      if(res=='1'){
        $("#modalEditProfile").modal('toggle');
        swal({
        title: 'Data Behasil Diubah',
        text: 'Data berhasil disimpan ke database.',
        type: 'success',
        closeOnConfirm: false
        }, function(isConfirm){
        if(isConfirm){
          location.reload();
        }
        })
      }else{
      swal("Kesalahan!", "Masukkan data dengan lengkap", 'error');
      }
    }).fail(function(res){
      console.log(res);
    });
  });


  function validasiFileEditProfile(){
    var inputFile = document.getElementById('foto_profile');
    var pathFile = inputFile.value;
    var ekstensiOk = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
    if(!ekstensiOk.exec(pathFile)){
        alert('Silakan upload file yang memiliki ekstensi .jpeg/.jpg/.png/.gif');
        inputFile.value = '';
        return false;
    }else{
        //Pratinjau gambar
        if (inputFile.files && inputFile.files[0]) {
            var reader = new FileReader();
            reader.onload = function(e) {
                document.getElementById('pratinjauGambarEditProfile').innerHTML = '<img src="'+e.target.result+'" width="100%"/>';
            };
            reader.readAsDataURL(inputFile.files[0]);
        }
    }
}






<?php
if($this->session->userdata('level') == "1"){
?>
	function ubahProfil(id){
		$('#formEditProfil')[0].reset();
		$.ajax({
			type: 'post',
			url: '<?php echo base_url(); ?>user/get_data',
			data: 'id=<?php echo encrypt($this->session->userdata('id_user')); ?>'
		}).done(function(res){
			var data = $.parseJSON(res);
			$('#nikProfil').val(data.nik);
			$('#usernameProfil').val(data.username);
			$('#alamat_userProfil').val(data.alamat_user);
			$('#nama_userProfil').val(data.nama_user);
			$('#id_userProfil').val('<?php echo encrypt($this->session->userdata('id_user')); ?>');

			$('#modalEditProfil').modal('show');

		}).fail(function(res){
			console.log(res);
		});
	}
	
	
	$('#formEditProfil').submit(function(e){
		e.preventDefault();
		var data = $(this).serialize();
		$('#btnSubmitEditProfil').html('<i class="spinner fa fa-spinner"></i> Loading...');
		$('#btnSubmitEditProfil').attr('disabled', true);
		$.ajax({
			type : 'post',
			url : '<?php echo base_url(); ?>user/update',
			data : 'submit=true&'+data
		}).done(function(res){
			$('#btnSubmitEditProfil').html('Submit');
			$('#btnSubmitEditProfil').removeAttr('disabled');
			if(res=='1'){
				$("#modalEditProfil").modal('toggle');
			  swal({
				title: 'Data Behasil Diubah',
				text: 'Data berhasil disimpan ke database.',
				type: 'success',
				closeOnConfirm: false
			  }, function(isConfirm){
				if(isConfirm){
				  location.reload();
				}
			  })
			}else{
			swal("Kesalahan!", "Masukkan data dengan lengkap", 'error');
			}
		}).fail(function(res){
			console.log(res);
		});
	});
<?php
}
?>


</script>


</body>

</html>