@extends('layout')


@section('content')

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="index.html"><i class="icon-home2 position-left"></i> Master</a></li>
			<li class="active">Upah</li>
			<li class="active">Upah Borongan</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Upah Borongan</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a data-action="collapse"></a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<button class="btn btn-primary" type="button" id="btn_tambah_gudang">Tambah</button>
	    </div>
	    <table class="table table-striped tabel_upah">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Tanggal Berlaku</th>
					<th>Lokasi</th>
					<th>Proses</th>
					<th>Benang</th>
					<th>Motif</th>
					<th>Corak</th>
					<th>Nominal</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
	</div>
</div>

<div class="modal fade" id="exampleModal" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off" id="form_upah">
	      			<input type="hidden" name="id_upah">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tanggal</label>
	      				<div class="col-sm-8">
	      					<input type="text" class="form-control datepicker" name="popup_tanggal" value="{{ date('d-m-Y') }}">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Proses</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_barang">
	      				<label class="control-label col-sm-4">Barang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_barang">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_motif">
	      				<label class="control-label col-sm-4">Motif</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_motif">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Corak</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_corak">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Satuan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_satuan">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Lokasi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_lokasi">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nominal</label>
	      				<div class="col-sm-8">
	      					<input type="text" class="form-control text-right number-only" name="popup_nominal">
	      					<p class="help-block pull-right" id="help_popup_harga"></p>
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

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

	$(document).ready(function(){
		tabel_upah = $(".tabel_upah").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('upah_borongan/get_upahborongan') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
			  	{data: 'tanggal', name: 'tanggal'},
			  	{data: 'nama_lokasi', name: 'nama_lokasi'},
			  	{data: 'nama_proses', name: 'nama_proses'},
	            {data: 'nama_barang', name: 'nama_barang'},
	            {data: 'motif', name: 'motif'},
	            {data: 'motif_keterangan', name: 'motif_keterangan'},
	            {data: 'nominal', name: 'nominal', className: 'text-right'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
	        createdRow: function(row, data, index){
	        	$('td', row).eq(7).html(accounting.formatMoney(data.nominal)).attr('align','right'); // 6 is index of column
	        }
		});

		
        get_barang();
        /*get_proses();
        get_motifket();
        get_satuan();
        get_lokasi();*/

	})

	$("#btn_tambah_gudang").click(function(){
		$("[name=id_upah]").val('');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_motif]").val(null).trigger('change');
		$("[name=popup_corak]").val(null).trigger('change');
		$("[name=popup_proses]").val(null).trigger('change');
		$("[name=popup_satuan]").val(null).trigger('change');
		$("[name=popup_lokasi]").val(null).trigger('change');
		$("[name=popup_nominal]").val('');
		$("#help_popup_harga").text('');
		$("#exampleModal").modal('show');
	})

	function get_barang(){
		$.ajax({
			url: "{{ url('upah_borongan/get_barang') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.barang.length > 0){
					$("[name=popup_barang]").html('');
					$("[name=popup_barang]").append('<option value="" kode="">--- Pilih barang ---</option>');
					for(i in result.barang){
						$("[name=popup_barang]").append('<option value="'+result.barang[i].id+'" nama="'+result.barang[i].nama_barang+'">'+result.barang[i].nama_kategori+' - '+result.barang[i].nama_sub+' - '+result.barang[i].kode_barang+' - '+result.barang[i].nama_barang+'</option>');
					}
				}

				if(result.proses.length > 0){
					$("[name=popup_proses]").html('');
					$("[name=popup_proses]").append('<option value="" kode="">--- Pilih proses ---</option>');
					for(i in result.proses){
						$("[name=popup_proses]").append('<option value="'+result.proses[i].id_proses+'" nama="'+result.proses[i].nama_proses+'">'+result.proses[i].nama_proses+'</option>');
					}
				}


				if(result.motif.length > 0){
				$("[name=popup_motif]").html('');
				$("[name=popup_motif]").append('<option value="" nama="">--- Pilih motif ---</option>');
					for(i in result.motif){
						$("[name=popup_motif]").append('<option value="'+result.motif[i].id_motif+'" nama="'+result.motif[i].nama_motif+'">'+result.motif[i].nama_motif+'</option>');
					}
				}

				if(result.corak.length > 0){
					$("[name=popup_corak]").html('');
					$("[name=popup_corak]").append('<option value="" nama="">--- Pilih Corak ---</option>');
					for(i in result.corak){
						$("[name=popup_corak]").append('<option value="'+result.corak[i].id_motif_barang+'" nama="'+result.corak[i].motif_keterangan+'">'+result.corak[i].motif_keterangan+'</option>');
					}
				}

				if(result.satuan.length > 0){
					$("[name=popup_satuan]").html('');
					$("[name=popup_satuan]").append('<option value="" kode="">--- Pilih Satuan ---</option>');
					for(i in result.satuan){
						$("[name=popup_satuan]").append('<option value="'+result.satuan[i].id_satuan+'" nama="'+result.satuan[i].nama_satuan+'">'+result.satuan[i].nama_satuan+'</option>');
					}
				}

				if(result.lokasi.length > 0){
					$("[name=popup_lokasi]").html('');
					$("[name=popup_lokasi]").append('<option value="" kode="">--- Pilih Lokasi ---</option>');
					for(i in result.lokasi){
						$("[name=popup_lokasi]").append('<option value="'+result.lokasi[i].id_lokasi+'" nama="'+result.lokasi[i].nama_lokasi+'">'+result.lokasi[i].nama_lokasi+'</option>');
					}
				}

			}
		})
	}

	/*function get_proses(){
		$.ajax({
			url: "{{ url('upah_borongan/get_proses') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_proses]").html('');
					$("[name=popup_proses]").append('<option value="" kode="">--- Pilih proses ---</option>');
					for(i in result.data){
						$("[name=popup_proses]").append('<option value="'+result.data[i].id_proses+'" nama="'+result.data[i].nama_proses+'">'+result.data[i].nama_proses+'</option>');
					}
				}
			}
		})
	}

	function get_motifket(){
		$.ajax({
			url: "{{ url('upah_borongan/get_motifket') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_motif]").html('');
					$("[name=popup_motif]").append('<option value="" kode="">--- Pilih Motif ---</option>');
					for(i in result.data){
						$("[name=popup_motif]").append('<option value="'+result.data[i].id_motif_barang+'" nama="'+result.data[i].motif_keterangan+'">'+result.data[i].motif_keterangan+'</option>');
					}
				}
			}
		})
	}

	function get_satuan(){
		$.ajax({
			url: "{{ url('upah_borongan/get_satuan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_satuan]").html('');
					$("[name=popup_satuan]").append('<option value="" kode="">--- Pilih Satuan ---</option>');
					for(i in result.data){
						$("[name=popup_satuan]").append('<option value="'+result.data[i].id_satuan+'" nama="'+result.data[i].nama_satuan+'">'+result.data[i].nama_satuan+'</option>');
					}
				}
			}
		})
	}

	function get_lokasi(){
		$.ajax({
			url: "{{ url('upah_borongan/get_lokasi') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_lokasi]").html('');
					$("[name=popup_lokasi]").append('<option value="" kode="">--- Pilih Lokasi ---</option>');
					for(i in result.data){
						$("[name=popup_lokasi]").append('<option value="'+result.data[i].id_lokasi+'" nama="'+result.data[i].nama_lokasi+'">'+result.data[i].nama_lokasi+'</option>');
					}
				}
			}
		})
	}

	$("[name=popup_barang]").change(function(){
		var nilai = $(this).val();
		

		if(nilai != ''){
			$("#div_motif").hide();
			$("[name=popup_motif]").val(null).trigger('change');
		}else{
			$("#div_motif").show();
		}
	})

	$("[name=popup_motif]").change(function(){
		var nilai = $(this).val();
		

		if(nilai != ''){
			$("#div_barang").hide();
			$("[name=popup_barang]").val(null).trigger('change');
		}else{
			$("#div_barang").show();
		}
	})*/

	$("[name=popup_nominal]").keyup(function(){
		var rp = $("[name=popup_nominal]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#help_popup_harga").text(curr(rp));
		
	})

	function rp(){
		var rp = $("[name=popup_nominal]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#help_popup_harga").text(curr(rp));
	}

	function edit(id){
		$("[name=id_upah]").val($("#table_idupahborongan"+id).val());
		$("[name=popup_tanggal]").val($("#table_tanggal"+id).val());
		$("[name=popup_barang]").val($("#table_barang"+id).val()).trigger('change');
		$("[name=popup_proses]").val($("#table_proses"+id).val()).trigger('change');
		$("[name=popup_motif]").val($("#table_motif"+id).val()).trigger('change');
		$("[name=popup_corak]").val($("#table_motifbarang"+id).val()).trigger('change');
		$("[name=popup_satuan]").val($("#table_satuan"+id).val()).trigger('change');
		$("[name=popup_nominal]").val($("#table_nominal"+id).val());
		$("[name=popup_lokasi]").val($("#table_lokasi"+id).val()).trigger('change');
		
		rp();
		
		$("#exampleModal").modal('show');
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id_upah]").val();
		var tanggal = $("[name=popup_tanggal]").val();
		var barang = $("[name=popup_barang]").val();
		var proses = $("[name=popup_proses]").val();
		var motif = $("[name=popup_motif]").val();
		var corak = $("[name=popup_corak]").val();
		var satuan = $("[name=popup_satuan]").val();
		var nominal = $("[name=popup_nominal]").val();
		var lokasi = $("[name=popup_lokasi]").val();

		if(tanggal != '' && proses != '' && nominal != '' && satuan != '' && lokasi != ''){
			$.ajax({
				url: "{{ url('upah_borongan/simpan') }}",
				type: 'post',
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				data: $("#form_upah").serialize(),
				success: function(respon){
					tabel_upah.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})

	function hapus(id){
		swal({
			title: 'Anda yakin akan menghapus item ini?',
			type: 'warning',
			showCancelButton: true,
			confirmButtonColor: '#3085d6',
			cancelButtonColor: '#d33',
			confirmButtonText: 'Ya',
			cancelButtonText: 'Tidak',
		}).then((result) => {
		 	if(result.value){
		 		$.ajax({
					url: "{{ url('upah_borongan/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						tabel_upah.ajax.reload();
					}
				})
		 	}
		})
	}
</script>

@endsection