@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> Produksi</a></li>
			<li class="active">Upah Tenun</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Bayar Upah Tenun</h5>
			<div class="heading-elements">
				<ul class="icons-list">
					<li><a href="{{ url('upahproduksitenun') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
				</ul>
			</div>
			<div class="col-lg-12">
		    	@if(Session::has('flash_message_error'))
				<div class="alert alert-error alert-block">
					<button type="button" class="close" data-dismiss="alert">x</button>
					<strong>{!! session('flash_message_error') !!}</strong>
				</div>
				@endif
				@if(Session::has('flash_message_success'))
				<div class="alert alert-success alert-block">
					<button type="button" class="close" data-dismiss="alert">x</button>
					<strong>{!! session('flash_message_success') !!}</strong>
				</div>
				@endif 
		    </div>
		</div>
		<div class="panel-body">
			<form class="form-horizontal" id="form_submit" method="post" action="{{ url('upahproduksitenun/bayar') }}" autocomplete="off">
				<input type="hidden" name="_token" value="{{ csrf_token() }}">
				<input type="hidden" name="id_upah_tenun" value="{{ $data['id_upah_tenun'] }}">
				<div class="col-sm-6">
					<div class="form-group">
						<label class="control-label col-sm-4">Petenun</label>
						<div class="col-sm-8">
							<input type="text" class="form-control" name="petenun" readonly value="{{ $data['upah_tenun']->rekanan }}">
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-4">Makelar</label>
						<div class="col-sm-8">
							<input type="text" class="form-control" name="petenun" readonly value="{{ $data['upah_tenun']->makelar }}">
						</div>
					</div>

					<div class="form-group">
						<label class="control-label col-sm-4">Tanggal Upah</label>
						<div class="col-sm-8">
							<input class="form-control datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ date_db($data['upah_tenun']->tanggal_upah, true, 2) }}" readonly />
						</div>
					</div>

					<div class="form-group">
						<label class="control-label col-sm-4">Nomor</label>
						<div class="col-sm-8">
							<input class="form-control" type="text" name="nomor" placeholder="" value="{{ $data['upah_tenun']->nomor }}" id="nomor" readonly />
						</div>
					</div>
				</div>

				<div class="col-sm-12 form-group">
					<h5>Hasil</h5>
					<table class="table table-striped" id="table_hasil">
						<thead>
							<tr>
								<th>No</th>
								<th>Keterangan</th>
								<th>Kualitas</th>
								<th>Jumlah</th>
								<th>Upah Petenun</th>
								<th>Upah Makelar</th>						
							</tr>
						</thead>
						<tbody>
							@php
								$no = 1;
								$total_upah_petenun = 0;
								$total_upah_makelar = 0;
							@endphp
							@if(sizeof($data['upah_tenun_dt']) > 0)
							@foreach($data['upah_tenun_dt'] as $key => $val_ut)
								<tr>
									<td>{{ $no }}</td>
									<td>{{ $val_ut->keterangan }}</td>
									<td>{{ $val_ut->nama_kualitas }}</td>
									<td class="text-center">{{ $val_ut->jumlah }}</td>
									<td class="text-right">{{ number_format($val_ut->upah_petenun, 0, '.', '.') }}</td>
									<td class="text-right">{{ number_format($val_ut->upah_makelar, 0, '.', '.') }}</td>
								</tr>
								@php
									$no++;
									$total_upah_petenun += $val_ut->upah_petenun;
									$total_upah_makelar += $val_ut->upah_makelar;
								@endphp
							@endforeach
							@endif
						</tbody>
						<tfoot>
							<tr>
								<th></th>
								<th></th>
								<th></th>
								<th>Sub Total</th>
								<th class="text-right"><p id="sub_total_upah_petenun"></p><input type="hidden" id="total_upah_petenun" value="{{$total_upah_petenun}}">{{ number_format($total_upah_petenun, 0, '.', '.') }}</th>
								<th class="text-right"><p id="sub_total_upah_makelar"></p><input type="hidden" id="total_upah_makelar" value="{{$total_upah_makelar}}">{{ number_format($total_upah_makelar, 0, '.', '.') }}</th>
							</tr>
						</tfoot>
					</table>
				</div>

				<div class="col-sm-12 form-group">
					<h5>Pick</h5>
					<table class="table table-striped" id="table_pick">
						<thead>
							<tr>
								<th>No</th>
								<th>keterangan</th>
								<th>Pick</th>
								<th>Jumlah</th>
								<th>Upah</th>						
							</tr>
						</thead>
						<tbody>
							@php
								$no_p = 1;
								$total_upah_pick = 0;
							@endphp
							@if(sizeof($data['upah_tenun_pick']) > 0)
							@foreach($data['upah_tenun_pick'] as $key => $val_pick)
								<tr>
									<td>{{ $no_p }}</td>
									<td>{{ $val_pick->keterangan }}</td>
									<td>{{ $val_pick->pick }}</td>
									<td class="text-center">{{ $val_pick->jumlah }}</td>
									<td class="text-right">@php if($val_pick->tipe_akun == "K") { echo '-'.number_format($val_pick->upah_pick, 0, '.', '.');}else{echo number_format($val_pick->upah_pick, 0, '.', '.');} @endphp</td>
								</tr>
								@php
									$no_p++;
									if ($val_pick->tipe_akun == "K") {
										$total_upah_pick -= $val_pick->upah_pick;
									} else {
										$total_upah_pick += $val_pick->upah_pick;
									}
								@endphp
							@endforeach
							@endif
						</tbody>
						<tfoot>
							<tr>
								<th></th>
								<th></th>
								<th></th>
								<th>Sub Total</th>
								<th class="text-right"><p id="sub_total_upah_pick"></p><input type="hidden" id="total_upah_pick" value="{{$total_upah_pick}}">{{number_format($total_upah_pick, 0, '.', '.')}}</th>
							</tr>
						</tfoot>
					</table>
				</div>

				<div class="col-sm-12 form-group">
					<h5>Lain-lain</h5>
					<div class="pull-right">
						<button type="button" id="btn_add_lain" class="btn btn-success"><i class="fa fa-plus"></i></button>
					</div>
					<table class="table table-striped" id="table_lain">
						<thead>
							<tr>
								<th>No</th>
								<th style="width: 30%">Akun</th>
								<th>keterangan</th>
								<th>Tipe</th>
								<th>Keterangan</th>
								<th>Jumlah</th>	
								<th></th>		
							</tr>
						</thead>
						<tbody>
							@php
								$no_l = 1;
								$total_upah_lain = 0;
							@endphp
							@if(sizeof($data['upah_tenun_lain']) > 0)
							@foreach($data['upah_tenun_lain'] as $key => $val_lain)
								<tr>
									<td>{{ $no_l }}</td>
									<td>{{ $val_lain->nama_akun }}</td>
									<td>{{ $val_lain->keterangan }}</td>
									<td>@if($val_lain->tipe == "D") Penjumlahan @else Pengurangan @endif <input type="hidden" class="tipelain" name="tipe_lain_[]" value="{{ $val_lain->tipe }}"></td>
									<td>{{ ($val_lain->jenis == 0) ? "Petenun":"Makelar" }}</td>
									<td class="text-right">@php if($val_lain->tipe == "K") { echo '-'.number_format($val_lain->jumlah, 0, '.', '.');}else{echo number_format($val_lain->jumlah, 0, '.', '.');} @endphp <input type="hidden" class="jumlah_lain" name="jumlah_lain_old_[]" onkeypress="return isNumberKey(event)" onkeyup="hitungJumlahLain()" value="{{ $val_lain->jumlah }}"></td>
									<td></td>
								</tr>
								@php
									$no_l++;
									if ($val_lain->tipe == "K") {
										$total_upah_lain -= $val_lain->jumlah;
									} else {
										$total_upah_lain += $val_lain->jumlah;
									}
								@endphp
							@endforeach
							@endif
						</tbody>
						<tfoot>
							<tr>
								<th></th>
								<th></th>
								<th></th>
								<th></th>
								<th>Sub Total</th>
								<th class="text-right"><p id="sub_total_upah_lain">{{number_format($total_upah_lain, 0, '.', '.')}}</p><input type="hidden" id="total_upah_lain" value="0"></th>
								<th></th>
							</tr>
						</tfoot>
					</table>
				</div>

				@php
					$total_upah = $total_upah_petenun + $total_upah_makelar + $total_upah_pick + $total_upah_lain;
				@endphp

				<div class="col-lg-6 pull-right">
					<div class="form-group">
						<label class="control-label col-sm-4">Total Upah</label>
						<div class="col-sm-8">
							<input class="form-control text-right" type="text" name="total_upah" value="{{number_format($total_upah, 0, '.', '.')}}" id="total_upah" readonly />
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-4">Akun Bayar</label>
						<div class="col-sm-8">
							<select class="form-control select-search" name="akun_bayar" id="akun_bayar" required>
								<option value="">--- Pilih Akun Bayar ---</option>
								@foreach($data['akun_bayar'] as $key => $ab)
									<option value="{{$key}}">{{$data['akun_bayar'][$key]}}</option>
								@endforeach
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="control-label col-sm-4">Tanggal bayar</label>
						<div class="col-sm-8">
							<input class="form-control datepicker" type="text" name="tanggal_bayar" id="tanggal_bayar" value="{{ date('d-m-Y') }}" />
						</div>
					</div>
				</div>

				<div class="col-sm-12">
					<a href="{{url('upahproduksitenun')}}" class="btn bg-warning-400">Kembali</a>
					<div class="form-group pull-right">
						<button class="btn btn-primary" type="submit" id="btn_simpan">Bayar</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>

<script type="text/javascript">
	var table_hasil, table_pick, table_lain;
	$(document).ready(function(){

		table_hasil = $("#table_hasil").DataTable({
        	"paging": false,
        });
		table_hasil.on( 'order.dt search.dt', function () {
            table_hasil.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        table_hasil.on("draw.dt", function(){
        	$("[data-popup='popover']").popover();
        });

        table_pick = $("#table_pick").DataTable({
        	"paging": false,
        });
		table_pick.on( 'order.dt search.dt', function () {
            table_pick.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        table_pick.on("draw.dt", function(){
        	$("[data-popup='popover']").popover();
        });

        table_lain = $("#table_lain").DataTable({
        	"paging": false,
        });
		table_lain.on( 'order.dt search.dt', function () {
            table_lain.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        table_lain.on("draw.dt", function(){
        	$("[data-popup='popover']").popover();
        });

		$("#petenun").on("change", function(){
			$.ajax({
				url: "{{ url('upahproduksitenun/get-makelar')}} ",
				type: 'post',
				data: {id_rekanan : $(this).val()},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(response){
					$("#makelar").val(response);
				}
			})
		})

		$("#btn_get_upah").on("click", function(evt){
			evt.preventDefault();
			$.ajax({
				url: "{{ url('upahproduksitenun/get-hasil')}} ",
				type: 'post',
				data: {id_rekanan : $("#petenun").val(), tanggal_awal : $("#tanggal_awal").val(), tanggal_akhir : $("#tanggal_akhir").val()},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(response){
					// table hasil
					table_hasil.clear().draw();
					var no_h = 1;
					var total_upah_petenun = 0;
					var total_upah_makelar = 0;
					for(i in response.hasil){
						var upah_petenun = parseFloat(response.hasil[i].upah_petenun) * parseFloat(response.hasil[i].jumlah);
						var upah_makelar = parseFloat(response.hasil[i].upah_makelar) * parseFloat(response.hasil[i].jumlah);
						table_hasil.row.add([
							'<div>'+no_h+'</div>',
							'<div><input type="hidden" name="id_barangmasuk_[]" value="'+response.hasil[i].id_barangmasuk+'"><input type="hidden" name="id_barangkeluar_[]" value="'+response.hasil[i].id_barangkeluar+'"><input type="hidden" name="id_proses_[]" value="'+response.hasil[i].id_proses+'"><input type="hidden" name="hasil_jumlah_[]" value="'+response.hasil[i].jumlah+'"><input type="hidden" name="upah_petenun_[]" value="'+upah_petenun+'"><input type="hidden" name="upah_makelar_[]" value="'+upah_makelar+'"><input type="text" class="form-control" name="keterangan_hasil_[]" value="'+response.hasil[i].nama_barang+'"></div>',
							'<div>'+response.hasil[i].nama_kualitas+'</div>',
							'<div class="text-center">'+response.hasil[i].jumlah+'</div>',
							'<div class="text-right">'+new Intl.NumberFormat('de-DE').format(upah_petenun)+'</div>',
							'<div class="text-right">'+new Intl.NumberFormat('de-DE').format(upah_makelar)+'</div>'
							]).draw(false);
						no_h +=1;
						total_upah_petenun += upah_petenun;
						total_upah_makelar += upah_makelar;
					}

					$("#sub_total_upah_petenun").text(new Intl.NumberFormat('de-DE').format(total_upah_petenun));
					$("#sub_total_upah_makelar").text(new Intl.NumberFormat('de-DE').format(total_upah_makelar));
					$("#total_upah_petenun").val(total_upah_petenun);
					$("#total_upah_makelar").val(total_upah_makelar);

					// table pick
					table_pick.clear().draw();
					var no_p = 1;
					var total_upah_pick = 0;
					for(i in response.hasil){
						if (response.hasil[i].jenis_pick == "K") {
							var pick_upah = Math.abs(parseFloat(response.hasil[i].upah_pick)) * -1;
						} else {
							var pick_upah = parseFloat(response.hasil[i].upah_pick);
						}
						var upah_pick = pick_upah * parseFloat(response.hasil[i].jumlah);
						table_pick.row.add([
							'<div>'+no_p+'</div>',
							'<div><input type="hidden" name="tipe_akun_[]" value="'+response.hasil[i].jenis_pick+'"><input type="hidden" name="pick_[]" value="'+response.hasil[i].pick+'"><input type="hidden" name="pick_jumlah_[]" value="'+response.hasil[i].jumlah+'"><input type="hidden" name="pick_upah_[]" value="'+Math.abs(upah_pick)+'"><input type="text" class="form-control" name="keterangan_pick_[]" value="'+response.hasil[i].nama_barang+'"></div>',
							'<div class="text-center">'+response.hasil[i].pick+'</div>',
							'<div class="text-center">'+response.hasil[i].jumlah+'</div>',
							'<div class="text-right">'+new Intl.NumberFormat('de-DE').format(upah_pick)+'</div>'
							]).draw(false);
						no_p +=1;
						total_upah_pick += upah_pick;
					}

					$("#sub_total_upah_pick").text(new Intl.NumberFormat('de-DE').format(total_upah_pick));
					$("#total_upah_pick").val(total_upah_pick);
					hitungUpah();
				}
			})
		})

		$("#btn_add_lain").on("click", function(evt){
			evt.preventDefault();
			addLain();
		})

		function addLain()
		{
			table_lain.row.add([
				'<div></div>',
				'<div><select class="form-control select-search akunlain" name="akun_lain_[]" required><option value="">--- Pilih Akun ---</option>@foreach($data['akun'] as $key => $ab)<option value="{{$key}}">{{$data['akun'][$key]}}</option>@endforeach</select></div>',
				'<div><input type="text" class="form-control keteranganlain" name="keterangan_lain_[]" required></div>',
				'<div><select class="form-control select-search tipelain" name="tipe_lain_[]" onchange="hitungJumlahLain()"><option value="D">Penjumlahan</option><option value="K">Pengurangan</option></select></div>',
				'<div><select class="form-control select-search" name="jenis_lain_[]"><option value="0">Petenun</option><option value="1">Makelar</option></select></div>',
				'<div><input type="text" class="form-control text-right jumlah_lain" name="jumlah_lain_[]" onkeypress="return isNumberKey(event)" onkeyup="hitungJumlahLain()" required></div>',
				'<div class="text-center"><button type="button" onclick="removeLain($(this))" class="btn btn-danger btn-xs"><i class="fa fa-times"></i></button></div>'
				]).draw(false);
		}

	})
	
	function removeLain(this_)
	{
		table_lain.row(this_.parents('tr')).remove().draw(false);
		hitungJumlahLain();
	}

	function hitungJumlahLain()
	{
		var sub_total_lain = 0;
		var jumlah_input = document.getElementsByClassName('jumlah_lain'),
                            jumlah_lain  = [].map.call(jumlah_input, function( input ) {
                            	if (input.value == "") {
                            		return 0;
                            	} else {
                            		return input.value;
                            	}
                            });

        var tipe_lain = document.getElementsByClassName('tipelain'),
                            tipe  = [].map.call(tipe_lain, function( input ) {
                            	if (input.value == "") {
                            		return "D";
                            	} else {
                            		return input.value;
                            	}
                            });

        for (var i = 0; i < jumlah_lain.length; i++){
        	if (tipe[i] == "D") {
        		sub_total_lain += parseFloat(jumlah_lain[i]);
        	} else {
        		sub_total_lain -= parseFloat(jumlah_lain[i]);
        	}
        }

        $("#sub_total_upah_lain").text(new Intl.NumberFormat('de-DE').format(sub_total_lain));
        $("#total_upah_lain").val(sub_total_lain);
        hitungUpah();
	}

	function hitungUpah()
	{
		var petenun = parseFloat($("#total_upah_petenun").val()), makelar = parseFloat($("#total_upah_makelar").val()), pick = parseFloat($("#total_upah_pick").val()), lain = parseFloat($("#total_upah_lain").val());
		var total = petenun + makelar + pick + lain;
		$("#total_upah").val(new Intl.NumberFormat('de-DE').format(total))
	}

	function isNumberKey(evt) {
	    var charCode = (evt.which) ? evt.which : evt.keyCode;
	    if (charCode > 31 && (charCode < 48 || charCode > 57))
	        return false;
	    return true;
	}
</script>
@endsection