@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">Kontrak</li>
		</ul>
	</div>
</div>

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

	       	<div class="heading-elements">
		    	<div class="btn-group">
		    		<a href="{{ url('sales_contract/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">
		    	<thead>
					<tr>
						<th>No</th>
						<th>Lokasi</th>
						<th>Tanggal</th>
						<th>No. Kontrak</th>
						<th>Rekanan</th>
						<th>Jumlah</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tbody>

				</tbody>
		    </table>

	    </div>

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

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

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

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

	function loadDataTransaksi() {
		tabelSalescontract = $("#tabel-sales-contract").DataTable({
			serverSide : true,
			processing : true,
			ajax : "{{ url('sales_contract/load_data_kontrak') }}",
			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_kontrak', name:'tanggal_kontrak'},
			  {data:'no_kontrak', name:'no_kontrak'},
			  {data:'nama_rekanan', name:'nama_rekanan'},
			  {data:'nominal_dpp', name:'nominal_dpp'},
			  {data:'action', name:'action', searchable:false, orderable : false},
			],
			createdRow : function( row, data, dataIndex ) {
				$(row).attr('data-idsaleskontrak', data['id']);
				var jumlah_total = parseFloat(data['nominal_dpp'])+parseFloat(data['nominal_pajak']);
				console.log(jumlah_total);
				$('td', row).eq(5).html(accounting.formatMoney(jumlah_total));
			},
		});
	}

	function btnHapusKontrak(this_) {
		var id_transaksi = this_.parents("tr").data("idsaleskontrak");
		
		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')}}/"+id_transaksi,
					type: 'post',
					data: {'_method' : 'DELETE'},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tabelSalescontract.ajax.reload();
					}
				})
		 	}
		})
	}
</script>

@endsection