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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Perubahan Satuan</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" id="btn_tambah_satuan"><i class=""></i>Tambah</button>
	    </div>

	    <table class="table table_satuan table-stripped">
	    	<thead>
				<tr>
					<th rowspan="2">No</th>
					<th colspan="2" class="text-center">Satuan Awal</th>
					<th colspan="2" class="text-center">Satuan Rencana</th>
					<th rowspan="2">Proses Produksi</th>
					<th rowspan="2">Motif</th>
					<th rowspan="2">Corak</th>
					<th rowspan="2">Lokasi</th>
					<th rowspan="2" class="text-center">Aksi</th>
				</tr>
				<tr>
					<th>Jumlah</th>
					<th>Satuan</th>
					<th>Jumlah</th>
					<th>Satuan</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">
	      			<input type="hidden" name="id_perubahansatuan">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Proses Produksi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_proses" id="popup_proses">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Satuan Awal</label>
	      				<div class="col-sm-4">
	      					<input type="text" name="popup_jumlahawal" class="form-control number-only">
	      				</div>
	      				<div class="col-sm-4">
	      					<select class="form-control select-search" name="popup_satuanawal" id="popup_satuanawal">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Satuan Perubahan</label>	      				
	      				<div class="col-sm-4">
	      					<input type="text" name="popup_jumlahperubahan" class="form-control number-only">
	      				</div>
	      				<div class="col-sm-4">
	      					<select class="form-control select-search" name="popup_satuanperubahan" id="popup_satuanperubahan">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Motif</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_motif" id="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" id="popup_corak">
	      						
	      					</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" id="popup_lokasi">
	      						
	      					</select>
	      				</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 table_satuan;

	$(document).ready(function(){
		table_satuan = $(".table_satuan").DataTable({
			processing: true,
	        serverSide: true,
	        ajax: '{{ url('perubahansatuan/get_data') }}',
	        columns: [
	            {data: 'rownum', name: 'rownum', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
			  	{data: 'jumlah_satuanawal', name: 'jumlah_satuanawal'},
	            {data: 'nama_satuanawal', name: 'nama_satuanawal'},
	            {data: 'jumlah_satuanperubahan', name: 'jumlah_satuanperubahan'},
	            {data: 'nama_satuanperubahan', name: 'nama_satuanperubahan'},
	            {data: 'nama_proses', name: 'nama_proses'},
	            {data: 'nama_motif', name: 'nama_motif'},
	            {data: 'nama_corak', name: 'nama_corak'},
	            {data: 'nama_lokasi', name: 'nama_lokasi'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
		});
		
		get_proses();
		get_satuan();
		get_motif();
		get_corak();
		get_lokasi();
	})
	$("#btn_tambah_satuan").click(function(){
		$("[name=id_perubahansatuan]").val('');
		$("[name=popup_proses]").val(null).trigger('change');
		$("[name=popup_jumlahawal]").val('');
		$("[name=popup_satuanawal]").val(null).trigger('change');
		$("[name=popup_jumlahawal]").val('');
		$("[name=popup_satuanperubahan]").val(null).trigger('change');
		$("[name=popup_jumlahperubahan]").val('');
		$("[name=popup_motif]").val(null).trigger('change');
		$("[name=popup_corak]").val(null).trigger('change');
		$("[name=popup_lokasi]").val(null).trigger('change');
		

		$("#exampleModal").modal("show");
	})
	

	function edit(id){
		var id = $("#table_idperubahansatuan"+id).val();
		var proses = $("#table_proses"+id).val();
		var jumlahawal = $("#table_jumlahawal"+id).val();
		var jumlahperubahan = $("#table_jumlahperubahan"+id).val();
		var satuanawal = $("#table_satuanawal"+id).val();
		var satuanperubahan = $("#table_satuanperubahan"+id).val();
		var motif = $("#table_motif"+id).val();
		var corak = $("#table_motifbarang"+id).val();
		var lokasi = $("#table_lokasi"+id).val();

		$("[name=id_perubahansatuan]").val(id);
		$("[name=popup_proses]").val(proses).trigger('change');
		$("[name=popup_jumlahawal]").val(jumlahawal);
		$("[name=popup_jumlahperubahan]").val(jumlahperubahan);
		$("[name=popup_satuanawal]").val(satuanawal).trigger('change');
		$("[name=popup_satuanperubahan]").val(satuanperubahan).trigger('change');
		$("[name=popup_motif]").val(motif).trigger('change');
		$("[name=popup_corak]").val(corak).trigger('change');
		$("[name=popup_lokasi]").val(lokasi).trigger('change');

		$("#exampleModal").modal('show');

	}

	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('perubahansatuan/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						table_satuan.ajax.reload();
					}
				})
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id_perubahansatuan]").val();
		var proses = $("[name=popup_proses]").val();
		var jumlahawal = $("[name=popup_jumlahawal]").val();
		var jumlahperubahan = $("[name=popup_jumlahperubahan]").val();
		var satuanawal = $("[name=popup_satuanawal]").val();
		var satuanperubahan = $("[name=popup_satuanperubahan]").val();
		var motif = $("[name=popup_motif]").val();
		var corak = $("[name=popup_corak]").val();
		var lokasi = $("[name=popup_lokasi]").val();
		var jumlahhasil = (parseFloat(jumlahperubahan)/parseFloat(jumlahawal)).toFixed(3);

		if(proses != '' && satuanawal != '' && satuanperubahan != '' && lokasi != ''){
			$.ajax({
				url: "{{ url('perubahansatuan/simpan')}} ",
				type: 'post',
				data: {id_perubahansatuan : id, id_proses : proses, jumlahawal : jumlahawal, jumlahperubahan : jumlahperubahan, id_satuanawal : satuanawal, id_satuanperubahan : satuanperubahan, id_motif : motif, id_motif_barang : corak, id_lokasi : lokasi, jumlahhasil : jumlahhasil},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					table_satuan.ajax.reload();
					$("#exampleModal").modal('hide');
				}
			})
		}
	})

	function get_proses(){
		$.ajax({
			url: "{{ url('perubahansatuan/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_motif(){
		$.ajax({
			url: "{{ url('perubahansatuan/get_motif') }}",
			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+'" nama="'+result.data[i].nama_motif+'">'+result.data[i].nama_motif+'</option>');
					}
				}
			}
		})
	}

	function get_corak(){
		$.ajax({
			url: "{{ url('perubahansatuan/get_corak') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_corak]").html('');
					$("[name=popup_corak]").append('<option value="" kode="">--- Pilih Corak ---</option>');
					for(i in result.data){
						$("[name=popup_corak]").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('perubahansatuan/get_satuan') }}",
			type: 'get',
			dataType: 'json',
			success: function(result){
				if(result.status == 1){
					$("[name=popup_satuanawal]").html('');
					$("[name=popup_satuanawal]").append('<option value="" kode="">--- Pilih Satuan ---</option>');
					$("[name=popup_satuanperubahan]").html('');
					$("[name=popup_satuanperubahan]").append('<option value="" kode="">--- Pilih Satuan ---</option>');
					for(i in result.data){
						$("[name=popup_satuanawal]").append('<option value="'+result.data[i].id_satuan+'" nama="'+result.data[i].nama_satuan+'">'+result.data[i].nama_satuan+'</option>');
					}
					for(i in result.data){
						$("[name=popup_satuanperubahan]").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('perubahansatuan/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>');
					}
				}
			}
		})
	}
</script>

@endsection