<head>
    <style>
        #tbltrans th, tr, td {
            border-bottom:1px black solid; border-top:1px black solid;
        }        
    </style>
</head>

@extends('layouts.app')

@section('title')

@endsection

@section('breadcrumb')
   @parent
   <li>Transaksi Penjualan</li>
@endsection


@section('content')   
<div class="row">
	<div class="container-fluid">
		<div class="card">
			<div class="card-block">  
			
    <a onclick="addForm()" class="btn btn-success btn-sm"><i class="fa fa-plus-circle"> </i> New Transaksi </a> 			
    
<form method="post" id="form-project">
{{ csrf_field() }}

<input type="hidden" id="mgr" name ="mgr" value="{{ Auth::user()->group }}"> 

<table class="table table-padding table-striped" id="tbltrans" width="100%">
<thead>
   <tr class="tblheader">
        <th width="110px">No.Transaksi</th>
        <th width="90px">Tgl.Trx</th>
        <th width="150px">Perumahan</th>
        <th width="80px">Block</th>
        <th width="150px">Pembeli</th>
        <th class="text-center" width="100px">Harga</th>
        <th class="text-center" width="100px">Angsuran</th>
        <th width="80px">Pembayaran</th>
        <th class="text-center" width="5px">Act</th>
	</tr>
</thead>
<tbody></tbody>

</table>
</form>

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

@include('transaksi.transaksi_create')
@include('transaksi.listrumah')
@include('transaksi.infotrx')
@include('transaksi.form_cicilan')
@include('transaksi.add_cicilan')
@include('transaksi.transaksi_batal')

@endsection

@section('script')
<script type="text/javascript">
var table, save_method;

$(function(){
    table = $('#tbltrans').DataTable({
        "processing" : true,
        "lengthMenu": [[8, 25, 50, -1], [8, 25, 50, "All"]],       //pilihan jumlah record show
        "order" : [0,'DESC'],
        "ajax" : {
            "url" : "{{ route('transaksi.datatransaksi') }}",
            "type" : "GET"
        }
    }); 
});   

//submit
$('#form-create').validator().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 = "{{ route('transaksi.store') }}"; }
        else { url = "{{ DIR_APP }}transaksi/"+id; }

        $.ajax({
            url : url,
            type : "POST",
            data : $('#form-create').serialize(),
            dataType: 'JSON',		   
            success : function(data){
                if(data.msg=='error2'){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Type Pembayaran DP harus dipilih ","error");
					$('#tpdp').focus().select();
					return false;
                }
                
			    if(data.msg=="error"){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Gagal menyimpan data !","error");
					$('#prj').focus().select();
				}else{
					$('#modal-form_create').modal('hide');
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');		
					$('#tbltrans').DataTable().ajax.reload(null, false);
				}   
 		    },
            error : function(){
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			    $('#submit-control').html('Save');
			    $('#submit-control').removeAttr('disabled');			 
            }   
        });
        return false;
	}
});
   

function addForm(){
    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#modal-form_create').modal('show');
    $('#modal-form_create form')[0].reset();     
    
    $('#prj').removeAttr('disabled');   
    $('#cus').removeAttr('disabled'); 
    $('#dp').removeAttr('disabled');
	$('#editket').removeClass('hide').addClass('show');
} 	 


tblrumah = $('#tbl-rumah').DataTable({
    "scrollY": '55vh',
    "scrollCollapse": true,
    "bSort": false,
    "destroy": true,
    "processing" : true,
    "paging" : false,
    "dom" : 'bti',
});


function showRumah(){
    var prj =$('#prj').val();
    
    if(prj=="") { alert('Harus pilih Perumahan dulu !'); return false; }
    
    $('#modal-rumah').modal('show');

    tblrumah.ajax.url("{{ DIR_APP }}rumah/"+prj+"/daftarrumah");
	tblrumah.ajax.reload();
}

function selectRumah(id){
	$('#id_rmh').val(id);
	$('#modal-rumah').modal('hide');
	
	$.ajax({
        url : "{{ DIR_APP }}rumah/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            $('#nomor').val(data.cluster.concat('/', data.block. concat('/', data.nomor)));
			$('#tipe').val(data.luasrumah.concat('/', data.luastanah));
			$('#harga').val(data.harga);
        },
        error : function(){
          alert("Tidak dapat menampilkan data!");
        }   
     });  
}


function infotrx(id){

    $('#modal-info').modal('show');
    tblinfo.ajax.url("{{ DIR_APP }}transaksi/"+id+"/infotransaksi");
	tblinfo.ajax.reload();
}

tblinfo = $('#tbl-info').DataTable({
    "scrollY": '55vh',
    "scrollCollapse": true,
    "bSort": false,
    "destroy": true,
    "processing" : true,
    "paging" : false,
    "dom" : 'bt',
});


function pembayaran(id){
    $('#modal-cicilan').modal('show');
    $('#idtrx').val(id);
    tampilTransaksi(id);
    tampilDetail(id);
}

function tampilDetail(id){

    tblcicil.ajax.url("{{ DIR_APP }}transaksi/"+id+"/datacicilan");
	tblcicil.ajax.reload();
}    

tblcicil = $('#tbl-cicilan').DataTable({
    "scrollY": '55vh',
    "scrollCollapse": true,
    "bSort": false,
    "destroy": true,
    "processing" : true,
    "paging" : false,
    "dom" : 'bt',
});

function tampilTransaksi(id){
    $.ajax({
        url : "{{ DIR_APP }}transaksi/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){

            $('#project').text(data.project_name);
            $('#cust').text(data.cus_name +' - '+ data.address);
            $('#block').text("CLUSTER/BLOCK : "+data.cluster +'/'+data.block+'/'+data.nomor);
	        $('#harga2').text("HARGA  : IDR "+formatAngka(data.harga_rumah));
	        //$('#dp2').text("DP : IDR  "+formatAngka(data.total_dp));
	        $('#tp2').text(data.tipe);
	        $('#tlcicil').text("TOTAL ANGSURAN : IDR  "+formatAngka(data.total_cicilan));
	        $('#sisa').text("SISA : IDR "+formatAngka(data.sisa));
	        $('#ketr').val(data.deskripsi);
	 
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });       
}


function editinfo(id){
    save_method = "edit";

    $('input[name=_method]').val('PATCH');
    $('#modal-form_create form')[0].reset();
    $('#modal-form_create').modal('show');   
    $('#id').val(id);
    
    $.ajax({
        url : "{{ DIR_APP }}transaksi/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){

            $('#tgltr').val(data.tgl_book);
            $('#prj').val(data.id_prj).attr('disabled', 'disabled');
            $('#cus').val(data.id_cust).attr('disabled', 'disabled');
            $('#tipe').val(data.luasrumah. concat('/', data.luastanah)).attr('disabled', 'disabled');
	        $('#nomor').val(data.cluster. concat('/', data.block. concat('/', data.nomor))).attr('disabled', 'disabled');
	        $('#harga').val(formatAngka(data.harga_rumah));
	        $('#byr').val(data.status_trx);
	        //$('#dp').val(data.total_dp).attr('disabled', 'disabled');
	        $('#ref').val(data.ref_id);
	        $('#ket').val(data.deskripsi);
	        $('#kdbank').val(data.bank_id);
	        
	        $('#editket').removeClass('show').addClass('hide');
	        
	        if(data.status_trx=='1'){
                $('#bank').removeClass('hide').addClass('show');
            }else{
                $('#bank').removeClass('show').addClass('hide');
            }
	        
	        
	        
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function formatAngka(x) {
    x = x.toString();
    var pattern = /(-?\d+)(\d{3})/;
    while (pattern.test(x))
        x = x.replace(pattern, "$1.$2");
    return x;
}

/* cicilan */
function cicilan(){
    var id=$('#idtrx').val();
    
    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#modal-addcicilan').modal('show');
    $('#modal-addcicilan form')[0].reset();     
    
    tampilinfo(id);
	tampilcicil(id);
} 

function tampilcicil(id){

    tblcicil.ajax.url("{{ DIR_APP }}transaksi/"+id+"/datacicilan");
	tblcicil.ajax.reload();
}

function tampilinfo(id){

    $.ajax({
        url : "{{ DIR_APP }}transaksi/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){

            $('#id2').val(data.id);

            $('#namaproject').val(data.project_name);
            $('#namacus').val(data.cus_name);
            $('#tprumah').val(data.luasrumah. concat('/', data.luastanah));
	        $('#norumah').val(data.cluster. concat('/', data.block. concat('/', data.nomor)));
	        $('#hgrumah').val(formatAngka(data.harga_rumah));
	        $('#ttlcicil').val(formatAngka(data.total_cicilan));
	        $('#sisa2').val(formatAngka(data.sisa));
	        $('#hgrumah2').val(data.harga_rumah);

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


//submit
$('#form-addcicilan').on("submit", function(e) {

    if(!e.isDefaultPrevented()){
        
        var id = $('#id2').val();		 
        if(save_method == "add") { url = "{{ route('transaksi.simpancicilan') }}"; }

        $.ajax({
            url : url,
            type : "POST",
            data : $('#form-addcicilan').serialize(),
            dataType: 'JSON',		   
            success : function(data){
			    if(data.msg=="error"){
					swal("WARNING", " Gagal menyimpan data !","error");

				}else{
					$('#modal-addcicilan').modal('hide');
					$('#tbl-cicilan').DataTable().ajax.reload(null, false);
					tampilTransaksi(id);
				}   
 		    },
            error : function(){
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
		 
            }   
        });
        return false;
	}
});

function printkwt(id){
    window.location="{{ DIR_APP }}transaksi/"+id+"/printkwt";
} 

function pengajuankpr(id){
    window.location="{{ DIR_APP }}transaksi/"+id+"/pengajuankpr";
} 

function pem___batalan(id){
    
    if($('#mgr').val() != "MANAGER"){
        swal("WARNING", " Anda tidak berhak membatalkan !","error");
        return false;
    }

    swal({
        title: "Anda Yakin membatalkan ini ?",
        text: "Coba pastikan lagi !",
        icon: "info",
        buttons: [
            'Tidak jadi',
            'Proses Cancel'
            ],
        dangerMode: true,
        }).then(function(isConfirm) {
        if (isConfirm) {
            swal({
                title: 'Confirm!',
                text: 'Klik OK untuk melanjutkan  !',
                icon: 'info'
            }).then(function() {
                batalTrx(id);
            });
        }else {
            swal("Tidak Jadi", "Transaksi masih lanjut ", "error");
        }
    })
}    


function pembatalan(id){
    
    if($('#mgr').val() != "MANAGER"){
        if($('#mgr').val() != 'SALES'){
            swal("WARNING", " Anda tidak berhak membatalkan !","error");
            return false;
        }
    }
    
    save_method = "edit";

    $('input[name=_method]').val('PATCH');
    $('#modal-form_batal form')[0].reset();
    $('#modal-form_batal').modal('show');   
    $('#idx').val(id);
    
    $.ajax({
        url : "{{ DIR_APP }}transaksi/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            
            $('#tgltrx').val(data.tgl_book);
            $('#prjx').val(data.id_prj).attr('disabled', 'disabled');
            $('#cusx').val(data.id_cust).attr('disabled', 'disabled');
            $('#tipex').val(data.luasrumah. concat('/', data.luastanah)).attr('disabled', 'disabled');
	        $('#nomorx').val(data.cluster. concat('/', data.block. concat('/', data.nomor))).attr('disabled', 'disabled');
	        $('#hargax').val(formatAngka(data.harga_rumah)).attr('disabled', 'disabled');
	        $('#byrx').val(data.status_trx).attr('disabled', 'disabled');
	        $('#dpx').val(data.total_dp);
	        $('#refx').val(data.referensi);
	        $('#ketx').val('');
	        
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}


function batalTrx(){
    
    var id=$('#idx').val();
    //alert('proses batal, belum siap '+id);
   
    $.ajax({
        url : "{{ DIR_APP }}transaksi/"+id+"/bataltransaksi",
        type : "GET",
        data : $('#form-batal').serialize(),
        dataType: 'JSON',		   
        success : function(data){
		    if(data.msg=="error"){
				swal("WARNING", " Proses Pembataln tidak berhasil !","error");
				
			}else{
                swal("Success", "Transaksi berhasil dibatalkan !", "success");
                $('#tbltrans').DataTable().ajax.reload(null, false);
			}   
	    },
        error : function(){
		    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
        }   
    });
    return false;

}


$('#dp').on('change', function(){
    var hg=$('#dp').val();
    $('#booking').val(formatAngka(hg));
})

$('#nom').on('change', function(){
    var hg=$('#nom').val();
    var tl=parseInt($('#hgrumah2').val());
    
    $('#cicilan').val(formatAngka(hg));
    
    var pct = hg/tl*100;
    $('#persen').val(pct);
    
})

$('#byr').on('change', function(){
    var byr=$('#byr').val();
    if(byr=='1'){
        $('#bank').removeClass('hide').addClass('show');
        $('#kdbank').focus();
    }else{
        $('#bank').removeClass('show').addClass('hide');
    }
})


</script>


@endsection
