@extends('layout')


@section('content')
<style type="text/css">
.select2-container {
	position: relative;
	z-index: 2;
	float: left;
	width: 100%;
	margin-bottom: 0;
	display: table;
	table-layout: fixed;
}

#tabel-service-detail > tbody > tr > td {
	vertical-align: top;
}

.summary-style{
	font-size: 18px;
	margin-bottom: 0px;
}
</style>
<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> Service</a></li>
			<li class="active">Order Service</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Order Service</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('order_service') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    <form method="post" class="form-horizontal" action="{{ url('order_service/'.$dataOrderService->id) }}" id="form-order-service">
	    {{ csrf_field() }}
	    {{ method_field("PUT") }}
	    <div class="panel-body">
	    	
	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-6">
		    		<div class="form-group">
		    			{{ Form::label("Lokasi", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<select name="lokasi" class="form-control set_select2">
		    					@foreach($lokasi AS $key => $value)
		    					<option value="{{ $value->id_lokasi }}" {{ ($dataOrderService->lokasi==$value->id_lokasi) ? "selected=''" : "" }}>{{ $value->nama_lokasi }}</option>
		    					@endforeach
		    				</select>
		    			</div>
		    		</div>
		    	</div>
	    	</div>
	    	
	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-6">
		    		<div class="form-group">
		    			{{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<div class="checkbox">
		    					<label>
			    					{{ Form::checkbox("cv", '1', $dataOrderService->cv==1 ? true : false, ['class'=>'styled']) }}
		    					</label>
		    				</div>
		    			</div>
		    		</div>
		    	</div>
	    	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Penyedia</label>
        			<div class="col-sm-8">
        				{{ Form::select("id_penyedia", $dataPenyedia, $dataOrderService->id_penyedia, ['class'=>'form-control set_select2', 'readonly'=>'readonly', 'disabled'=>'disabled']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Input</label>
        			<div class="col-sm-8">
        				{{ Form::text("tanggal", date_db($dataOrderService->tanggal, true), ['class'=>'form-control set_datepicker', 'readonly'=>'readonly', 'required'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Order Service</label>
        			<div class="col-sm-8">
        				{{ Form::text("tanggal_nota", date_db($dataOrderService->tanggal_nota, true), ['class'=>'form-control set_datepicker', 'onchange'=>'get_nomor($(this))', 'readonly'=>'readonly', 'required'=>'']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No Order Service</label>
        			<div class="col-sm-2 col-md-2">
        				{{ Form::text('no_nota', $dataOrderService->no_nota, ['class'=>'form-control text-right']) }}
        			</div>
        			<div class="col-sm-6 col-md-6">
        				{{ Form::text("sufix_no_nota", $dataOrderService->suffix_no_nota, ['class'=>'form-control text-uppercase']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Keterangan</label>
        			<div class="col-sm-8">
        				{{ Form::textarea("keterangan", $dataOrderService->keterangan, ['class'=>'form-control', 'rows'=>5]) }}
        			</div>
        		</div>
        	</div>

        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Syarat Pembayaran</label>
        			<div class="col-sm-8">
        				{{ Form::textarea("syarat_bayar", $dataOrderService->syarat_bayar, ['class'=>'form-control', 'rows'=>3]) }}
        			</div>
        		</div>

    			<div class="form-group">
    				{{ Form::label("Penerima", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::text("penerima", $dataOrderService->penerima, ['class'=>'form-control']) }}
    				</div>
    			</div>
        	</div>

	    </div>

	    <div class="table-responsive pre-scrollable">
	    <table class="table" id="tabel-service-detail">
	    	<thead>
	    		<tr>
	    			<th width="28%">Akun</th>
	    			<th>Deskripsi</th>
	    			<th width="10%">Jumlah</th>
	    			<th width="15%">Harga</th>
	    			<th width="15%">Total</th>
	    			<th width="8%"></th>
	    		</tr>
	    	</thead>
	    	<tbody>
	    		@foreach($dataOrderService->detailOrderservice AS $key => $detail)
	    		<tr>
					<td>
						{{ Form::hidden("transaksi[id_detail_orderservice][]", $detail->id) }}
						<select name='transaksi[id_akun][]' class='form-control set_select2'>
							<?php foreach ($akunBiaya as $key => $value){
								$attr = "";
								if($detail->id_akun == $key) $attr = "selected='selected'";
								echo "<option value='$key' $attr>$value</option>"; 
							} ?>
						</select>
					</td>
					<td><input type='text' name='transaksi[deskripsi][]' class='form-control' value="{{ $detail->deskripsi }}"></td>
					<td><input type='number' min="0" step="0.01" name='transaksi[jumlah][]' class='form-control text-right' value="{{ $detail->jumlah }}" onkeyup='sum_row_sub_total($(this))'></td>
					<td><input type='number' min="0" step="0.01" name='transaksi[harga][]' class='form-control text-right' onkeyup='setFormatMoney($(this), "td"), sum_row_sub_total($(this))' value="{{ $detail->harga_satuan }}"><span class='help-block text-right' style='font-weight: bold;'>0,00</span></td>
					<td class="text-right"><input type='hidden' name='transaksi[row_sub_total][]' value='0'><p class='form-control-static row_sub_total'>0</p></td>
					<td><button type='button' class='btn btn-xs btn-link text-danger' onclick='hapusRowAkun($(this))'><i class='icon-trash'></i></button></td>
		        </tr>
		        @endforeach
	    	</tbody>
			<tfoot>
				<tr>
					<td colspan="7">
						<button type="button" class="btn btn-xs btn-primary" onclick="tambah_row_detail()"><i class="icon-plus2"></i> Tambah</button>
					</td>
				</tr>
			</tfoot>
	    </table>
		</div>

	    <div class="panel-body text-right">
	    	<div class="row">
	    	<div class="col-sm-12 col-md-12">
		    	<div class="col-sm-4 col-md-4">&nbsp;</div>
		    	<div class="col-sm-4 col-md-4">&nbsp;</div>
		    	<div class="col-sm-4 col-md-4">
		    		<div class="form-group summary-style">
		    			{{ Form::label("Sub Total", "", ['class'=>'control-label col-sm-12 col-md-4']) }}
		    			<div class="col-sm-12 col-md-8">
		    				<p class="form-control-static summary-sub-total">0</p>
		    			</div>
		    		</div>
		    		<div class="form-group summary-style">
		    			<div class="control-label col-sm-12 col-md-4" >
			    			{{ Form::checkbox("gunakan_ppn", "1", false, ['onclick'=>'total_pajak_ppn($(this))']) }} PPN
							{{ Form::hidden("pajak[id_pajak_orderservice][]", sizeof($ppnOrderService)>0?$ppnOrderService->id:'') }}
			    			{{ Form::hidden("pajak[pemotongan][]", 0) }}
			    			{{ Form::hidden("pajak[id_akun][]", $akunPpn->id) }}
			    			{{ Form::hidden("pajak[nominal][]", sizeof($ppnOrderService)>0?$ppnOrderService->nominal:0, ['class'=>'input-nominal-ppn']) }}
			    		</div>
		    			<div class="col-sm-12 col-md-8">
		    				<p class="form-control-static summary-ppn">0</p>
		    			</div>
		    		</div>
		    		<div class="form-group summary-style text-left">
		    			<button type="button" class="btn btn-xs btn-link" onclick="tambah_row_pajak()"><i class="fa fa-plus"></i> Tambah Pajak</button>
		    		</div>
		    		<div class="form-group" id="div-pajak">
		    			@foreach($dataOrderService->pajakOrderService AS $id => $pajak)
		    			<div class='row-pajak'>
							<div class='col-sm-12 col-md-12 text-left'>
								<div class='checkbox'>
			    					<label>
			    						<input type='hidden' name='pajak[pemotongan][]' value='{{ $pajak->pemotongan }}'>
			    						<input type='checkbox' name='pajak[checkbox][]' class='' value='1' onclick='total_pajak()' {{ $pajak->pemotongan==1 ? "checked='checked'" : ""}}> Pemotongan
			    					</label>
			    				</div>
			    			</div>
							<div class='col-sm-12 col-md-4'>
								{{ Form::hidden("pajak[id_pajak_orderservice][]", $pajak->id) }}
			    				<select name='pajak[id_akun][]' class='form-control set_select2'>
			    					<?php
			    					foreach ($akunPajak as $key => $value) {
			    						$attr = "";
			    						if($pajak->id_akun == $key){
			    							$attr = "selected='selected'";
			    						}
			    						echo "<option value='$key' $attr>$value</option>";
			    					}
			    					?>
			    				</select>
			    			</div>
			    			<div class='col-sm-10 col-md-7'>
			    				<input type='number' step="0.01" name='pajak[nominal][]' class='form-control text-right' onkeyup='total_pajak()' value="{{ $pajak->nominal }}">
			    			</div>
			    			<div class='col-sm-2 col-md-1'>
			    				<button type='button' class='btn btn-sm btn-link text-danger' onclick='hapusRowPajak($(this))'><i class='fa fa-close'></i></button>
			    			</div>
		    			</div>
		    			@endforeach
		    		</div>
		    		<div class="form-group summary-style" style="margin-bottom: 3%;">
		    			{{ Form::label("Pajak", "", ['class'=>'control-label col-sm-12 col-md-4']) }}
		    			<div class="col-sm-12 col-md-8">
		    				<p class="form-control-static summary-pajak">0</p>
		    			</div>
		    		</div>
		    		<div class="form-group summary-style" style="font-weight: bold;">
		    			{{ Form::label("Tagihan", "", ['class'=>'control-label col-sm-12 col-md-4', 'style'=>'font-weight: bold;']) }}
		    			<div class="col-sm-12 col-md-8">
		    				<p class="form-control-static summary-tagihan">0</p>
		    			</div>
		    		</div>
		    	</div>
	    	</div>
	    	</div>
	    </div>

	    <div class="panel-footer">
	    	<div class="col-sm-12 col-md-12 text-right">
	    		<a href="{{ url('order_service') }}" class="btn btn-md btn-default">Batal</a>
	    		<button type="submit" class="btn btn-md btn-success">Simpan</button>
	    	</div>
	    </div>	
	    </form>

	</div>
</div>

<!-- hidden summaries -->
{{ Form::hidden("sub_total", 0, ['class'=>'input-sub-total']) }}
{{ Form::hidden("nominal_pajak", 0, ['class'=>'input-nominal-pajak']) }}
{{ Form::hidden("nominal_tagihan", 0, ['class'=>'input-nominal-tagihan']) }}
<!-- end : hidden summaries -->

<!----Modal---->
<script type="text/javascript">
var formOrderService = $("#form-order-service");
var hiddenSummarySubTotal = $(".input-sub-total");
var hiddenSummaryPajak = $(".input-nominal-pajak");
var hiddenSummaryPpn = $(".input-nominal-ppn");
var hiddenSummaryTagihan = $(".input-nominal-tagihan");

$('form').on('focus', 'input[type=number]', function (e) {
  $(this).on('wheel.disableScroll', function (e) {
    e.preventDefault()
  })
})
$('form').on('blur', 'input[type=number]', function (e) {
  $(this).off('wheel.disableScroll')
})

$(document).ready(function () {
	$(".set_select2").select2();

	formOrderService.find("input[name=tanggal_nota]").change();
	$(".summary-sub-total, .summary-pajak, .summary-tagihan, .summary-ppn").html(accounting.formatMoney(0));

	$("input[name='transaksi[jumlah][]'], input[name='transaksi[harga][]'], input[name='pajak[nominal][]']").trigger("keyup");

	if({{sizeof($ppnOrderService)}}>0){
		$("input[name=gunakan_ppn]").attr("checked", true);
		total_pajak_ppn($("input[name=gunakan_ppn]"));
	}
});

function get_nomor(this_){
	var id = "{{ $dataOrderService->id }}";
	var date = this_.val();
	var s_date = date.split("/");
	var sufix_no_nota = "/"+s_date[1]+"/"+s_date[2];
	$.ajax({
		url : "{{ url('order_service/get_nomor') }}",
		type : "get",
		data : {id:id, tanggal:date},
		dataType : "json",
		success : function(result){
			if(id!=""){
				sufix_no_nota = result.sufix_no_nota;
			}
			
			$("input[name=no_nota]").val(result.no_nota);
			$("input[name=sufix_no_nota]").val(sufix_no_nota);
		}
	});
}

function tambah_row_detail() {
	var rows = "<tr>"+
			"<td><input type='hidden' name='transaksi[id_detail_orderservice][]' value=''><select name='transaksi[id_akun][]' class='form-control set_select2'><?php foreach ($akunBiaya as $key => $value){ echo "<option value='$key'>$value</option>"; } ?></select></td>"+
			"<td><input type='text' name='transaksi[deskripsi][]' class='form-control'></td>"+
			"<td><input type='number' min='0' step='0.01' name='transaksi[jumlah][]' class='form-control text-right' value='1' onkeyup='sum_row_sub_total($(this))'></td>"+
			"<td><input type='number' min='0' step='0.01' name='transaksi[harga][]' class='form-control text-right' onkeyup='setFormatMoney($(this), \"td\"), sum_row_sub_total($(this))' value='0'><span class='help-block text-right' style='font-weight: bold;'>0,00</span></td>"+
			"<td class='text-right'><input type='hidden' name='transaksi[row_sub_total][]' value='0'><p class='form-control-static row_sub_total'>0</p></td>"+
			"<td style='vertical-align:top;'><button type='button' class='btn btn-xs btn-link text-danger' onclick='hapusRowAkun($(this))'><i class='icon-trash'></i></button></td>"+
        "</tr>";
  	$("table#tabel-service-detail > tbody").append(rows);
	$(".set_select2").select2();
}

function hapusRowAkun(this_) {
	var row = this_.parents("tr");
	row.remove();
	summary_sub_total();
}

function sum_row_sub_total(this_) {
	var row = this_.parents("tr");
	var jumlah = parseFloat(row.find("input[name='transaksi[jumlah][]']").val()) || 0;
	var harga = parseFloat(row.find("input[name='transaksi[harga][]']").val()) || 0;
	var sub_total = jumlah*harga;

	row.find("input[name='transaksi[row_sub_total][]']").val(sub_total);
	row.find("p.row_sub_total").html(accounting.formatMoney(sub_total));
	summary_sub_total();
}

function summary_sub_total() {
	var subtotal = $("input[name^='transaksi[row_sub_total]']");
	var nominal_subtotal = 0;
	$.each(subtotal, function(id, data){
		nominal_subtotal += parseFloat(data.value) || 0;
	});
	hiddenSummarySubTotal.val(nominal_subtotal);
	set_summaries();
}

function tambah_row_pajak() {
	var rows = "<div class='row-pajak'>"+
					"<div class='col-sm-12 col-md-12 text-left'>"+
						"<div class='checkbox'>"+
	    					"<label>"+
	    						"<input type='hidden' name='pajak[pemotongan][]' value='0'>"+
	    						"<input type='checkbox' name='pajak[checkbox][]' class='' value='1' onclick='total_pajak()'> Pemotongan"+
	    					"</label>"+
	    				"</div>"+
	    			"</div>"+
					"<div class='col-sm-12 col-md-4'>"+
						"<input type='hidden' name='pajak[id_pajak_orderservice][]' value=''>"+
	    				"<select name='pajak[id_akun][]' class='form-control set_select2'><?php foreach ($akunPajak as $key => $value) { echo '<option value=\''.$key.'\'>'.$value.'</option>';}?></select>"+
	    			"</div>"+
	    			"<div class='col-sm-10 col-md-7'>"+
	    				"<input type='number' step='0.01' name='pajak[nominal][]' class='form-control text-right' onkeyup='total_pajak()'>"+
	    			"</div>"+
	    			"<div class='col-sm-2 col-md-1'>"+
	    				"<button type='button' class='btn btn-sm btn-link text-danger' onclick='hapusRowPajak($(this))'><i class='fa fa-close'></i></button>"+
	    			"</div>"+
    			"</div>";
    $("#div-pajak").append(rows);
    $(".set_select2").select2();
}

function hapusRowPajak(this_) {
	var rowPajak = this_.parents(".row-pajak");
	var thisNominal = parseFloat(rowPajak.find("input[name='pajak[nominal][]']").val()) || 0;
	var pemotongan = rowPajak.find("input[name='pajak[checkbox][]']");
	if(pemotongan.is(":checked")){
		thisNominal *= -1;
	}
	var pajak = hiddenSummaryPajak.val() || 0;

	var nominal_pajak = pajak-thisNominal;
	hiddenSummaryPajak.val(nominal_pajak);

	rowPajak.remove();
	set_summaries();
}

function total_pajak_ppn(this_) {
	var id_pajak_orderservice = "{{ sizeof($ppnOrderService)>0?$ppnOrderService->id:'' }}";
	if(this_.is(":checked")){
		var ppn = 10*parseFloat(hiddenSummarySubTotal.val())/100;
		hiddenSummaryPpn.val(parseFloat(ppn));
		this_.parents(".form-group").find("input[name='pajak[id_pajak_orderservice][]']").val(id_pajak_orderservice);
	}else{
		this_.parents(".form-group").find("input[name='pajak[id_pajak_orderservice][]']").val(0);
		hiddenSummaryPpn.val(0);
	}
	$(".summary-ppn").html(accounting.formatMoney(hiddenSummaryPpn.val()));
	total_pajak();
}

function total_pajak() {
	var pajak = $("input[name^='pajak[nominal]']");
	var nominal_pajak = 0;
	$.each(pajak, function(id, data){
		var pemotongan = $(this).parents(".row-pajak").find("input[name='pajak[pemotongan][]']");
		var checkbox = $(this).parents(".row-pajak").find("input[name='pajak[checkbox][]']");
		if(checkbox.is(":checked")){
			nominal_pajak -= parseFloat(data.value) || 0;
			pemotongan.val(1);

		}else{
			nominal_pajak += parseFloat(data.value) || 0;
			pemotongan.val(0);

		}
	});
	hiddenSummaryPajak.val(nominal_pajak);
	set_summaries();
}

function set_summaries() {
	var subTotal = parseFloat(hiddenSummarySubTotal.val()) || 0;
	var pajak = parseFloat(hiddenSummaryPajak.val()) || 0;

	var tagihan = subTotal+pajak;
	hiddenSummaryTagihan.val(tagihan);

	$(".summary-sub-total").html( accounting.formatMoney(hiddenSummarySubTotal.val()) );
	$(".summary-pajak").html( accounting.formatMoney(hiddenSummaryPajak.val()) );
	$(".summary-tagihan").html( accounting.formatMoney(hiddenSummaryTagihan.val()) );
}

function setFormatMoney(this_, p) {
	var div = this_.parents(p);
	var data = this_.val();
	if(data==""){
		data = 0;
	}
	div.find(".help-block").html(accounting.formatMoney(data));
}
</script>

@endsection