@extends('layout')


@section('content')

<style type="text/css">
.modal-cetak .form-group{
	margin-bottom: 5px !important;
}
</style>

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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Sewa</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<a href="{{url('sewa/tambah')}}" class="btn btn-success" style="margin-bottom: 1%;">Tambah</a>

	    	@if(Session::has('flash_message_error'))
	    	<div class="alert alert-danger no-border">
				<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
				{!! session('flash_message_error') !!}
				<!-- <span class="text-semibold">Well done!</span> You successfully read <a href="#" class="alert-link">this important</a> alert message. -->
		    </div>
		    @elseif(Session::has('flash_message_success'))
		    <div class="alert alert-success no-border">
				<button type="button" class="close" data-dismiss="alert"><span>×</span><span class="sr-only">Close</span></button>
				{!! session('flash_message_success') !!}
				<!-- <span class="text-semibold">Well done!</span> You successfully read <a href="#" class="alert-link">this important</a> alert message. -->
		    </div>
		    @endif

		    
	    </div>
	    <div class="table-responsive">

    		    <table class="table table_sewa table-stripped">
    		    	<thead>
    					<tr>
    						<th>No</th>
    						<th class="text-center">Aksi</th>
    						<th>Lokasi</th>
    						<th>Tanggal Faktur</th>
    						<th>No. Faktur</th>
    						<th>Penyedia</th>
    						<th>Lama Sewa (Tahun)</th>
    	                    <th>Harga/Tahun</th>
    						<th>Tanggal Awal</th>
    						<th>Tanggal Akhir</th> 
    	                    <th>Total Tagihan</th> 
    	                    <th>Pembayaran</th> 
    					</tr>
    				</thead>
    				<tbody>  				
    				</tbody>
    		    </table>
		    </div>
	</div>
</div>

<!---Modal--->
<div class="modal fade modal-cetak" id="cetak-pembelian"  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-pembelian" target="_blank">
	      	<div class="modal-body">
	      			<div class="form-group">
	      				{{ Form::label("No. Faktur", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-no-pembelian"></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-pembelian"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Supplier", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-supplier-pembelian"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				{{ Form::label("Total Tagihan", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<p class="form-control-static text-tagihan-pembelian"></p>
	      				</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", ['1'=>'Kecil', '2'=>'Sedang'], 1, ['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 class="form-group">
	      				{{ Form::label("&nbsp;", "", ['class'=>'control-label col-sm-4 col-md-4']) }}
	      				<div class="col-sm-8 col-md-8">
	      					<div class="checkbox">
	      						<label>
	      							{{ Form::checkbox("tampil_harga", "true", true, ['class'=>'styled']) }} Tampilkan Harga
	      						</label>
	      					</div>
	      				</div>
	      			</div>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="reset" class="btn btn-default" data-dismiss="modal" id="btn-batal-cetak-pembelian">Batal</button>
	        	<button type="submit" class="btn btn-primary" >Cetak</button>
	      	</div>
	      		</form>
	    </div>
	</div>
</div>

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

	$(document).ready(function(){
		loadTabelSewa();
	})

	function loadTabelSewa() {
		tb = $(".table_sewa").DataTable({
			processing: true,
	        serverSide: true,
	        deferRender : true,
	        searchDelay : 2000,
	        ajax: '{{ url('sewa/get_data') }}',
	        columns: [
	            {data: 'id_sewa', name: 'id_sewa', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
			  	{data: 'aksi', name: 'aksi', orderable: false, searchable: false},
	            {data: 'nama_lokasi', name: 'nama_lokasi'},
	            {data: 'tanggal_faktur_sewa', name: 'tanggal_faktur_sewa'},
	            {data: 'no_faktur', name: 'no_faktur'},
	            {data: 'nama_penyedia', name: 'nama_penyedia'},
	            {data: 'tahun', name: 'tahun', class: 'text-center'},
	            {data: 'harga_per_tahun', name: 'harga_per_tahun'},
	            {data: 'tanggal_awal', name: 'tanggal_awal'},
	            {data: 'tanggal_akhir', name: 'tanggal_akhir'},
	            {data: 'tagihan', name: 'tagihan'},
	            {data: 'pembayaran', name: 'pembayaran'}
	        ],
	        createdRow: function(row, data, index) {
                $('td', row).eq(7).html(accounting.formatMoney(data.harga_per_tahun)).attr('align','right'); // 6 is index of column
                $('td', row).eq(10).html(accounting.formatMoney(data.tagihan)).attr('align','right'); // 6 is index of column
                $('td', row).eq(11).html(accounting.formatMoney(data.pembayaran)).attr('align','right'); // 6 is index of column
                                    
            },
		});
	}

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

		var row_data = tb.row(this_.parents("tr")).data();
		$(".text-no-pembelian").html(row_data.no_faktur);
		$(".text-tanggal-pembelian").html(row_data.tanggal);
		$(".text-supplier-pembelian").html(row_data.nama_penyedia);
		$(".text-tagihan-pembelian").html(accounting.formatMoney(row_data.tagihan));

		showModal($("#cetak-pembelian"));
	}
	
	function hapus(id){
		swal({
			title: 'Anda yakin akan menghapus data ini?',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Ya',
			cancelButtonText: 'Tidak',
		}).then((result) => {
		 	if(result.value){
		 		$.ajax({
					url: "{{url('sewa/hapus')}}",
					type: 'get',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						if (respon.status == "success") {
							swal({
								icon: "success",
								title: respon.message,
								type: 'success'
							})
							tb.ajax.reload();
						} else {
							swal({
								icon: "error",
								title: respon.message,
								type: 'error'
							})
						}
					}
				})
		 	}
		})
	}
</script>
@endsection