<?php $__env->startSection('title'); ?>
  Master User 
<?php $__env->stopSection(); ?>

<?php $__env->startSection('breadcrumb'); ?>
   ##parent-placeholder-6e5ce570b4af9c70279294e1a958333ab1037c86##
   <li>user</li>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>     
<div class="row">
  <div class="col-xs-12">
    <div class="card">

    <div class="card-block">        
    
      <div class="text-left">
        <a onclick="addForm()" class="btn btn-outline-success btn-sm"><i class="fa fa-plus-circle"></i> New User </a>&nbsp;
      </div>

<table class="table table-striped table-padding table-bordered table-responsive" id="tbl-user" width="100%">
<thead>
   <tr class="tblheader">
      <th width="5%">No</th>
      <th>Nama User</th>
      <th>Group</th>
      <th width="5%">Aksi</th>
   </tr>
</thead>
<tbody></tbody>
</table>

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

<?php echo $__env->make('user.form', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('script'); ?>
<script type="text/javascript">
var table, save_method;
$(function(){
   table = $('.table').DataTable({
     "processing" : true,
     "ajax" : {
       "url" : "<?php echo e(route('user.data')); ?>",
       "type" : "GET"
     }
   }); 
   
   $('#modal-form form').validator().on('submit', function(e){
      if(!e.isDefaultPrevented()){
         var id = $('#id').val();
         if(save_method == "add") url = "<?php echo e(route('user.store')); ?>";
         else url = "user/"+id;
         
          $.ajax({
           url : url,
           type : "POST",
           data : $('#modal-form form').serialize(),
           dataType: 'JSON',		   
           success : function(data){
             if(data.msg=="error"){
               	swal("WARNING", " Gagal menyimpan data !","error");
                $('#nama').focus().select();
             }else{
                $('#modal-form').modal('hide');
				swal("SUCCESS", " User berhasil disimpan ! ","success");
                table.ajax.reload();
             }   
 		   },
           error : function(){
             //alert("Tidak dapat menyimpan data!");
			 swal("WARNING"," Urer tidak dapat disimpan, harap hubungi IT !","error");			 
           }   
         });
		
//		$.ajax({
//           url : url,
//           type : "POST",
//           data : $('#modal-form form').serialize(),
//           success : function(data){
//             $('#modal-form').modal('hide');
//             table.ajax.reload();
//           },
//           error : function(){
//             alert("Tidak dapat menyimpan data!");
 //          }   
 //        });
         return false;
     }
   });
});

function addForm(){
   save_method = "add";
   $('input[name=_method]').val('POST');
   $('#modal-form').modal('show');
   $('#modal-form form')[0].reset();            
   $('.modal-title').text('Tambah User');
   $('#password, #password1').attr('required', true);
}

function editForm(id){
   save_method = "edit";
   $('input[name=_method]').val('PATCH');
   $('#modal-form form')[0].reset();
   $.ajax({
     url : "user/"+id+"/edit",
     type : "GET",
     dataType : "JSON",
     success : function(data){
        $('#modal-form').modal('show');
        $('.modal-title').text('Edit User');
       
        $('#id').val(data.id);
        $('#nama').val(data.username);
        $('#email').val(data.email);
        $('#password, #password1').removeAttr('required');
	      $('#group').val(data.group);
       
     },
     error : function(){
       alert("Tidak dapat menampilkan data!");
     }
   });
}

function deleteData(id){
   if(confirm("Apakah yakin data akan dihapus?")){
     $.ajax({
       url : "user/"+id,
       type : "POST",
       data : {'_method' : 'DELETE', '_token' : $('meta[name=csrf-token]').attr('content')},
       success : function(data){
         table.ajax.reload();
       },
       error : function(){
         alert("Tidak dapat menghapus data!");
       }
     });
   }
}
</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/myprojeq/public_html/sriayu/resources/views/user/index.blade.php ENDPATH**/ ?>