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

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Penggunaan Produksi</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>No</th>
					<th>Tanggal</th>
					<th>Barang</th>
					<th>Proses</th>
					<th>Corak</th>
					<th>Motif</th>
					<th>Barang Dikeluarkan</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" id="form_popup" autocomplete="off">
	      			<input type="hidden" name="id">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Tanggal</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_tanggal" class="form-control" value="{{ date("d-m-Y") }}" id="popup_tanggal" readonly>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Lokasi Produksi</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_lokasi" id="popup_lokasi">
	      						@php
	      						foreach($data['lokasi'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_lokasi }}">{{ $b->nama_lokasi }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>    
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Nama Barang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_barang" id="popup_barang">
	      						<option value="">-- Pilih Barang --</option>
	      						@php
	      						foreach($data['barang'] as $b){
	      						@endphp
	      						<option value="{{ $b->id }}">{{ $b->nama_barang }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>
	      			<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">
	      						<option value="">-- Pilih Proses --</option>
	      						@php
	      						foreach($data['proses'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_proses }}">{{ $b->nama_proses }}</option>
	      						@php
	      						}
	      						@endphp
	      					</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">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach($data['corak'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_motif_barang }}">{{ $b->motif_keterangan }}</option>
	      						@php
	      						}
	      						@endphp
	      					</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">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach($data['motif'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_motif }}">{{ $b->nama_motif }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>    			

	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlah" class="form-control text-right" id="popup_jumlah">
	      					<p class="help-block pull-right" id="help_popup_jumlah"></p>
	      				</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" id="popup_satuan">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach($data['satuan'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_satuan }}">{{ $b->nama_satuan }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>   
	      			<hr>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Barang Dikeluarkan</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_barangkeluar" id="popup_barangkeluar">
	      						<option value="">-- Pilih Barang --</option>
	      						@php
	      						foreach($data['barang'] as $b){
	      						@endphp
	      						<option value="{{ $b->id }}">{{ $b->nama_barang }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Gudang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_gudang" id="popup_gudang">
	      						<option value="">-- Pilih Gudang --</option>
	      						@php
	      						foreach($data['gudang'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_gudang }}">{{ $b->nama_gudang }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlahkeluar" class="form-control text-right" id="popup_jumlahkeluar">
	      					<p class="help-block pull-right" id="help_popup_jumlahkeluar"></p>
	      				</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_satuankeluar" id="popup_satuankeluar">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach($data['satuan'] as $b){
	      						@endphp
	      						<option value="{{ $b->id_satuan }}">{{ $b->nama_satuan }}</option>
	      						@php
	      						}
	      						@endphp
	      					</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('penggunaan_produksi/get_data') }}',
	        searchDelay : 2000,
	        columns: [
	            {data: 'id', name: 'id', orderable: false, searchable: false, render : function(data, type, row, meta){
			  		return meta.row+1;
			  	}},
			  	{data: 'tgl_input', name: 'tgl_input'},
	            {data: 'nama_barang', name: 'nama_barang'},
	            {data: 'nama_proses', name: 'nama_proses'},
	            {data: 'nama_corak', name: 'nama_corak'},
	            {data: 'nama_motif', name: 'nama_motif'},
	            {data: 'nama_barangkeluar', name: 'nama_barangkeluar'},
	            {data: 'aksi', name: 'aksi', orderable: false, searchable: false}
	        ],
	        createdRow: function(row, data, index){
	        	
	        }
		});
	})
	$("#btn_tambah_satuan").click(function(){
		$("[name=id]").val('');
		$("[name=popup_tanggal]").val('{{ date('d-m-Y') }}').change();
		$("[name=popup_lokasi]").val(null).trigger('change');
		$("[name=popup_barang]").val(null).trigger('change');
		$("[name=popup_proses]").val('').trigger('change');
		$("[name=popup_jumlah]").val('0');
		$("[name=popup_satuan]").val(null).trigger('change');
		$("[name=popup_motif]").val(null).trigger('change');
		$("[name=popup_corak]").val(null).trigger('change');
		$("[name=popup_lokasi]").val('1').trigger('change');
		
		$("[name=popup_barangkeluar]").val(null).trigger('change');
		$("[name=popup_jumlahkeluar]").val('0');
		$("[name=popup_gudang]").val(null).trigger('change');
		$("[name=popup_satuankeluar]").val(null).trigger('change');
		
		rp();
		jum();

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

	function get_edit(id){
		if(id != ''){
			$.ajax({
				url : "{{ url('penggunaan_produksi/get_edit') }}",
				type : "POST",
				dataType : "json",
				data : { id : id },
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success : function(respon){
					if(respon.status == 1){
						edit(respon.data);
					}else{
						swal("", "Gagal mendapat data", "warning");
					}
				}
			})
		}
	}
	

	function edit(data){
		$("[name=id]").val(data.id);
		$("[name=popup_proses]").val(data.id_proses).trigger('change');
		$("[name=popup_lokasi]").val(data.id_lokasi).trigger('change');
		$("[name=popup_motif]").val(data.id_motif).trigger('change');
		$("[name=popup_corak]").val(data.id_corak).trigger('change');
		$("[name=popup_barang]").val(data.id_barang).trigger('change');
		$("[name=popup_satuan]").val(data.id_satuan).trigger('change');
		$("[name=popup_warna]").val(data.id_komposisi).trigger('change');
		$("[name=popup_jumlah]").val(data.jumlah);
		$("[name=popup_tanggal]").val(data.tgl);

		$("[name=popup_barangkeluar]").val(data.id_barangkeluar).trigger('change');
		$("[name=popup_gudang]").val(data.id_gudang).trigger('change');
		$("[name=popup_jumlahkeluar]").val(data.jumlahkeluar).trigger('change');
		$("[name=popup_satuankeluar]").val(data.id_satuankeluar).trigger('change');
		
		rp();
		jum();
		$("#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('penggunaan_produksi/hapus')}} ",
					type: 'post',
					data: {id : id},
					headers : {
						'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
					},
					success: function(respon){
						if(respon.status == 0){
							swal("", respon.keterangan, "success");
						}else{
							swal("", respon.keterangan, "warning");
						}

						table_satuan.ajax.reload();
					}
				})
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id = $("[name=id]").val();
		var proses = $("[name=popup_proses]").val();
		var satuan = $("[name=popup_satuan]").val();
		var motif = $("[name=popup_motif]").val();
		var corak = $("[name=popup_corak]").val();
		var barang = $("[name=popup_barang]").val();
		var jumlah = $("[name=popup_jumlah]").val();

		if(jumlah != ''){
			$.ajax({
				url: "{{ url('penggunaan_produksi/simpan')}} ",
				type: 'post',
				data: $("#form_popup").serialize(),
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){
					if(respon.status == 0){
						swal("", respon.keterangan, "warning");
					}else{
						table_satuan.ajax.reload();
						$("#exampleModal").modal('hide');
					}
				}
			})
		}
	})

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

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

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

	function jum(){
		var rp = $("[name=popup_jumlahkeluar]").val();
		if(rp == '' || rp == 0 || isNaN(rp)){
			rp = parseInt(0);
		}
		$("#help_popup_jumlahkeluar").text(format_angka(rp));
	}

	$("[name=popup_barang]").change(function(){
		var id_satuan = $("[name=popup_barang] :selected").attr("id_satuan");
		var id = $(this).val();
		var id_data = $("[name=id]").val();
		if(id != ''){
			$("[name=popup_satuan]").val(id_satuan).trigger('change');
		}
	})
</script>

@endsection