<head>
    <style>
        .tblhdr { height:35px; background-color:#6495ED; color: white; font-size: 14px;}
        
        #tblpiutang td,tr { font-size:13px; border:0px gray solid; }
        
    </style>
</head>

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

<?php $__env->stopSection(); ?>

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


<?php $__env->startSection('content'); ?>   
<div class="row">
	<div class="container-fluid">
		<div class="card">
			<div class="card-block">  

<form method="post" id="form-pelunasan">
<?php echo e(csrf_field()); ?>


<a onclick="showhide()" id="tampil" style="cursor:pointer">Pelunasan Faktur Penjualan</a>
<input type="hidden" id="idtampil" value="1">

<section id="header" class="show">
    
<div class="row">
    <div class="col-sm-3 col-md-3">
        <div class="form-group">
            <label>Salesman</label>
            <select type="text" id="sls" name="sls" autofoucs class="form-control">
                <?php $__currentLoopData = $sales; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $list): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <option value="<?php echo e($list->id); ?>"><?php echo e($list->sales_kode."-".$list->sales_name); ?></option>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </select>
        </div>    
    </div>
    
    <div class="col-sm-3 col-md-3">
		<label>Tgl.Awal</label>
		<input id="awal" type="date" class="form-control" name="awal" value="<?php echo e(date('Y-m-d', strtotime("-35 day", strtotime(date('Y-m-d'))))); ?>" autofocus required>
    </div>
	<div class="col-sm-3 col-md-3">	
	    <label>Tgl.Akhir</label>
		<input id="akhir" type="date" class="form-control" name="akhir" value="<?php echo e(date('Y-m-d')); ?>" autofocus required>
    </div>
	<div class="col-md-2"> 
        <label></label>
		<button type="button" class="btn btn-primary btn-sm pull-right" onclick="showData()" style=margin-top:30px;><i class="fa fa-table"></i> View </button>
    </div>
</div>
</section>

<input type="hidden" id="lunasbelum" name="lunasbelum" value="1">

<div class="pull-right" style="margin-top:-100px;">   
    <input type="radio" id="blmlunas" name="blmlunas" value="1" checked>
    <label> Yang belum lunas</label>
    <input type="radio" id="all" name="all" value="2">
    <label> ALL</label>
</div>   

<!--table class="table table-striped table-padding table-bordered" id="tblpiutang" width="100%"-->
<table class="table table-striped table-padding table-bordered table-responsive" id="tblpiutang" width="100%">    
<thead>
   <tr class="tblhdr">
   
 	    <th width="7%">Tg.Faktur</th>
 	    <th width="12%">No.Faktur</th>
 	    <th width="25%">Customer</th>
 	    <th width="8%">J.Tempo</th>
		<th width="12%" class="text-right">Total</th>
		<th width="10%" class="text-right">Pot</th>
		<th width="10%" class="text-right">Cicil</th>
		<th width="12%" class="text-right">Bayar</th>
		<th width="25%">Keterangan</th>
		<th width="3px"></th>
        <th width="3px"></th>  	  
 
	</tr>
</thead>
<tbody></tbody>

</table>
</form>

        </div>

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

<?php echo $__env->make('pelunasan.form_create', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('pelunasan.form_cicil', \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, mgr;

function showData(){

    var sales= $('#sls').val();
    var tgl1 = $('#awal').val();
    var tgl2 = $('#akhir').val();
    var tipe = $('#lunasbelum').val();

    $(document).ready(function(){
        table = $('#tblpiutang').DataTable({
            "processing" : true,
            "bSort": false,
            "destroy": true,
            "dom" : "bftip",
            "lengthMenu": [[6, 10, 20, -1], [10, 10, 20, "All"]],
            "order": [0, 'desc'],
            "ajax" : {
                "url" : "<?php echo e(DIR_APP); ?>pelunasan/"+sales+"/"+tgl1+"/"+tgl2+"/"+tipe+"/tampildata",
                "type" : "GET"
            }
        })
   }); 
}

//submit
	$('#modal-form_create').on("submit", function(e) {
	    
      if(!e.isDefaultPrevented()){
		 
		 $('#submit-control').html(' Simpan Create ... ');
		 $('#submit-control').attr('disabled','disabled');
		  
         var id = $('#id').val();
         if(save_method == "add") url = "<?php echo e(route('pelunasan.store')); ?>";
         else url = "<?php echo e(DIR_APP); ?>pelunasan/"+id;

         $.ajax({
           url : url,
           type : "POST",
           data : $('#modal-form_create form').serialize(),
           dataType: 'JSON',		   
           success : function(data){
				if(data.msg=="error-1"){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Nominal Pembayaran invalid !","error");
					return false;
				}
				if(data.msg=="error-2"){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Nominal Pembayaran lebih kecil dari Faktur !","error");
					return false;
				    
				}else{
				
				    $('#modal-form_create').modal('hide');
					swal("SUCCESS", " Pelunasan berhasil dicreate ! ","success");
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');					
					table.ajax.reload();
				 }   
 		   },
           error : function(){
			 swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			 $('#submit-control').html('Save');
			 $('#submit-control').removeAttr('disabled');			 
           }   
         });
         return false;
	 }
   })
   
/** cicilan */
//submit
	$('#modal-form_cicil').on("submit", function(e) {
	    
      if(!e.isDefaultPrevented()){
		 
		 $('#submit-cicil').html(' Simpan Create ... ');
		 $('#submit-cicil').attr('disabled','disabled');
		  
         var id = $('#id').val();
         if(save_method == "add") url = "<?php echo e(DIR_APP); ?>pelunasan/simpancicilan";

         $.ajax({
           url : url,
           type : "POST",
           data : $('#modal-form_cicil form').serialize(),
           dataType: 'JSON',		   
           success : function(data){
				if(data.msg=="error-1"){
					$('#submit-cicil').html('Save');
					$('#submit-cicil').removeAttr('disabled');
					swal("WARNING", " Nominal Pembayaran invalid !","error");
					return false;

				}else{
				
				    $('#modal-form_cicil').modal('hide');
					swal("SUCCESS", " Cicilan berhasil disimpan ! ","success");
					$('#submit-cicil').html('Simpan Cicilan');
					$('#submit-cicil').removeAttr('disabled');					
					table.ajax.reload();
				 }   
 		   },
           error : function(){
			 swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			 $('#submit-cicil').html('Save');
			 $('#submit-cicil').removeAttr('disabled');			 
           }   
         });
         return false;
	 }
   })   
   
function pelunasan(id){

    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#modal-form_create').modal('show');
    $('#modal-form_create form')[0].reset();        
    
    tampilHeader(id);
    tampilCicilan(id);
} 	 

function cicilan(id){
    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#modal-form_cicil').modal('show');
    $('#modal-form_cicil form')[0].reset();        
    
    tampilHeader(id);
}

function tampilHeader(id){
    $('#id').val(id);
    $('#id2').val(id);
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>pelunasan/"+id+"/tampillunas",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            
            $('#nofaktur').text("NO.Faktur: "+data.sale_no+" -- "+data.cus_name+" - "+data.cus_address+" ");
            $('#nofaktur2').text("NO.Faktur: "+data.sale_no+" -- "+data.cus_name+" - "+data.cus_address+" ");
            $('#tlfaktur').val(data.sale_netto);
            $('#tcicilan').val(data.cicilan);
            $('#totalfaktur').val(data.sale_netto);

            $('#sts').text("belum lunas");
            $('#submit-control').removeAttr('disabled');

	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function showhide(){
    if($('#idtampil').val()=="1"){
        $('#idtampil').val(0);
        $('#header').removeClass('show').addClass('hide');
    }else{
        $('#idtampil').val(1);
        $('#header').removeClass('hide').addClass('show');        
    }
}

function tampilCicilan(id){
    tblcicilan.ajax.url("<?php echo e(DIR_APP); ?>pelunasan/"+id+"/cicilan");
	tblcicilan.ajax.reload(); 
}

tblcicilan = $('#tbl-cicilan').DataTable({
    "scrollY": '25vh',
    "scrollCollapse": true,
    "bSort": false,
    "destroy": true,
    "processing" : true,
	"paging" : false,
	"dom" : 'bti',    
})

$('#blmlunas').on('change', function(){
    if($('#blmlunas').is(':checked')) { $('#lunasbelum').val(1); 
        $('#all').prop('checked', false);
    }
    
})

$('#all').on('change', function(){
    if($('#all').is(':checked')) { $('#lunasbelum').val(2); 
        $('#blmlunas').prop('checked', false);
    }
})


</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/pelunasan/index.blade.php ENDPATH**/ ?>