@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="#"><i class="icon-home2 position-left"></i> Keuangan</a></li>
			<li>Sales Contract</li>
			<li class="active">Tagihan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Daftar Tagihan</h5>

	       	<div class="heading-elements">
		    	<div class="btn-group">
		    		<a href="{{ url('sales_contract_tagihan/create') }}" class="btn btn-sm btn-success"><i class="fa fa-plus"></i> Tambah</a>
		    	</div>
		    </div>
	    </div>
	    <div class="panel-body">

		    <table class="table table-striped" id="tabel-sales-contract-tagihan">
		    	<thead>
					<tr>
						<th>No</th>
						<th>Lokasi</th>
						<th>Tanggal</th>
						<th>No. Tagihan</th>
						<th>Rekanan</th>
						<th>Jumlah</th>
						<th>Status</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tbody>

				</tbody>
		    </table>

	    </div>

	    <div class="panel-footer">
	    </div>
	    
	</div>
</div>

<!---Modal--->
<div class="modal fade" id="cetak-buktitransaksi"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-sm" role="document">
	    <div class="modal-content">
	    	<div class="modal-header with-border">
	    		<h5 class="modal-title" id="exampleModalLabel">Cetak</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      		<form class="form-horizontal" autocomplete="off" method="get" action="#" id="form-cetak-buktitransaksi" target="_blank">
	      	<div class="modal-body">
	      			<div class="form-group">
	      				{{ Form::label("No. Tagihan", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-no-transaksi"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Tanggal", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-tanggal-transaksi"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Rekanan", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-supplier-transaksi"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::hidden("id", 0) }}
		    			{{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<div class="checkbox">
		    					<label>
			    					{{ Form::checkbox("label_cv", 'true', true, ['class'=>'styled']) }}
		    					</label>
		    				</div>
		    			</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Ukuran Kertas", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					{{ Form::select("uk_kertas", ['2'=>'Sedang','1'=>'Kecil'], 2, ['class'=>'form-control']) }}
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Set Tanggal Cetak", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					{{ Form::text("tanggal_cetak", "", ['class'=>'form-control datepicker']) }}
	      				</div>
	      			</div>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="reset" class="btn btn-default" data-dismiss="modal" id="btn-batal-cetak-buktitransaksi">Batal</button>
	        	<button type="submit" class="btn btn-primary" >Cetak</button>
	      	</div>
	      		</form>
	    </div>
	</div>
</div>

<script type="text/javascript">
	var tabelTagihanSalescontract;

	$(document).ready(function () {
		loadDataTransaksi();
		$("#tabel-sales-contract-tagihan").DataTable();

		tabelTagihanSalescontract.on( 'order.dt search.dt', function () {
            tabelTagihanSalescontract.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();
        tabelTagihanSalescontract.clear().draw();
	});

	function loadDataTransaksi() {
		tabelTagihanSalescontract = $("#tabel-sales-contract-tagihan").DataTable({
			serverSide : true,
			processing : true,
			ajax : "{{ url('sales_contract_tagihan/get_data_tagihan') }}",
			columns : [
			  {data:'id', name:'id', orderable:false, searchable:false, render : function(data, type, full, meta){
			  	return meta.row+1;
			  }},
			  {data:'nama_lokasi', name:'nama_lokasi'},
			  {data:'tanggal_tagihan', name:'tanggal_tagihan', render:function (tanggal_tagihan) {
			  	return moment(tanggal_tagihan, "YYYY-MM-DD").format("DD-MM-YYYY");
			  }},
			  {data:'nomor_tagihan', name:'nomor_tagihan'},
			  {data:'nama_kelompok', name:'nama_kelompok'},
			  {data:'nominal', name:'nominal'},
			  {data:'status', name:'status'},
			  {data:'aksi', name:'aksi', searchable:false, orderable : false},
			],
			createdRow : function( row, data, dataIndex ) {
				$(row).attr('data-idtagihan', data['id']);
				var jumlah_total = parseFloat(data['nominal']);
				$('td', row).eq(5).html(accounting.formatMoney(jumlah_total)).addClass("text-right");
			},
		});
	}

	function btnHapusTagihan(this_) {
		var id_transaksi = this_.parents("tr").data("idtagihan");
		
		swal({
			title: 'Anda yakin akan menghapus data ini?',
			text : 'Menghapus data akan mempengaruhi transaksi Anda.',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#d33',
			cancelButtonColor: '#3085d6',
			confirmButtonText: 'Hapus',
			cancelButtonText: 'Batal',
		}).then((result) => {
			
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('sales_contract_tagihan')}}/"+id_transaksi,
					type: 'post',
					data: {'_method' : 'DELETE'},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tabelTagihanSalescontract.ajax.reload();
					}
				})
		 	}
		})
	}

	function btnPrintOrderBuktiTransaksi(this_) {
		var id = this_.parents('tr').data('idtagihan');
		var href = this_.data("href");
		$("#form-cetak-buktitransaksi").attr("action", href);
		$("#form-cetak-buktitransaksi").find("input[name=tanggal_cetak]").val('{{date("d-m-Y")}}');

		var row_data = tabelTagihanSalescontract.row(this_.parents("tr")).data();
		var set_tanggal_tagihan = moment(row_data.tanggal_tagihan, "YYYY-MM-DD").format("DD-MM-YYYY");
		$("#form-cetak-buktitransaksi input[name=id]").val(id);
		$("#form-cetak-buktitransaksi input[name=tanggal_cetak]").val(set_tanggal_tagihan).datepicker("setDate", set_tanggal_tagihan);
		$(".text-no-transaksi").html(row_data.nomor_tagihan);
		$(".text-tanggal-transaksi").html(set_tanggal_tagihan);
		$(".text-supplier-transaksi").html(row_data.nama_kelompok);

		showModal($("#cetak-buktitransaksi"));
	}
</script>

@endsection