@extends('layout')

@section('js_script')
@endsection

@section('content')
<style type="text/css">
	.details-control{
		cursor: pointer;
	}

	table{  
	    border-collapse: collapse;  
	}  

	td {  
	    padding: 5px;
	}  

	.leftMargin-lev-1 td {  
	    position: relative;  
	    left: 0px;  
	    z-index: 1;  
	}

	.leftMargin-lev-2 td {  
	    position: relative;  
	    left: 30px;  
	    z-index: 1;  
	}

	.leftMargin-lev-3 td {  
	    position: relative;  
	    left: 60px;  
	    z-index: 1;  
	}

	.level-1 > th {
		padding-left: 1em;
	}
</style>

@php
	function isNegative($number) {
		if ($number < 0) {
			return "(".currency(abs($number)).")";
		} else {
			return currency($number);
		}
	}

	function currency($number) {
		return number_format($number, 2, ',', '.');
	}

	$GLOBALS['total_arr'] = [];
	$GLOBALS['total_aktiva'] = 0;
	$GLOBALS['total_pasiva'] = 0;
	$GLOBALS['total_ekuitas'] = 0;
	$GLOBALS['level'] = [];
	function createTreeNeraca($array, $akun, $currentParent, $currLevel = 0, $prevLevel = -1) {
		$total = [];
		$sub_total = [];
		echo "<table class='table'>";
		foreach ($array as $idPerkiraan => $value) {

			if ($currentParent == $value['parent']) {                       
				if ($currLevel > $prevLevel) echo " <tr> "; 

				if ($currLevel == $prevLevel) echo " </tr> ";

				if($value['used']==0) {
					echo '<td>'.$value['nama_perkiraan'];
					if(!isset($GLOBALS['level'][$value['level']][$value['id']])) {
						$GLOBALS['level'][$value['level']][$value['id']] = $value['saldo'];
					} else {
						$GLOBALS['level'][$value['level']][$value['id']] += $value['saldo'];
					}

					if(!isset($GLOBALS['level'][$value['level']][$value['parent']])) {
						$GLOBALS['level'][$value['level']][$value['parent']] = $value['saldo'];
					} else {
						$GLOBALS['level'][$value['level']][$value['parent']] += $value['saldo'];
					}
				} else {
					if(!isset($GLOBALS['level'][$value['level']][$value['id']])) {
						$GLOBALS['level'][$value['level']][$value['id']] = $value['saldo'];
					} else {
						$GLOBALS['level'][$value['level']][$value['id']] += $value['saldo'];
					}

					if(!isset($GLOBALS['level'][$value['level']][$value['parent']])) {
						$GLOBALS['level'][$value['level']][$value['parent']] = $value['saldo'];
					} else {
						$GLOBALS['level'][$value['level']][$value['parent']] += $value['saldo'];
					}

					if($akun == 'aktiva'){
						$GLOBALS['total_aktiva'] += $value['saldo'];
					} else if($akun == 'pasiva') {
						$GLOBALS['total_pasiva'] += $value['saldo'];
					} else if($akun == 'ekuitas') {
						$GLOBALS['total_ekuitas'] += $value['saldo'];
					}

					$total[$value['parent']][] = $value['saldo'];
					$sub_total[$value['parent']] = array_sum($total[$value['parent']]);
					array_push($GLOBALS['total_arr'], $sub_total[$value['parent']]);
					echo '<td>'.$value['nama_perkiraan'].'</td><td class="akun'.$akun.$value['parent'].'">'.isNegative($value['saldo']);
				}

				if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }

				$currLevel++; 

				$child = createTreeNeraca ($array, $akun, $idPerkiraan, $currLevel, $prevLevel);

				if( $child ){
					
				}else{
					if($value['used']==0){
						if($value['parent']==0){
							echo "<tr><td><strong>Total ".$value['nama_perkiraan']."</strong></td><td class='totalakun".$akun.$value["parent"]."'><strong>".isNegative($GLOBALS['total_'.$akun])."</strong></td></tr>";
						} else {
							//echo "<tr><td><strong>Total ".$value['nama_perkiraan']."</strong></td><td class='totalakun".$akun.$value["parent"]."'><strong>".isNegative(end($GLOBALS['total_arr']))."</strong></td></tr>";

							if(isset($GLOBALS['level'][$value['level']+1][$value['id']])){
								//$GLOBALS['level'][$value['level']][$value['parent']] += $GLOBALS['level'][$value['level']+1][$value['id']];
								echo "<tr><td><strong>Total ".$value['nama_perkiraan']."</strong></td><td class='totalakun".$akun.$value["parent"]."'><strong>".isNegative($GLOBALS['level'][$value['level']+1][$value['id']])."</strong></td></tr>";
							} 
							else if(isset($GLOBALS['level'][$value['level']][$value['id']])) {
								//$GLOBALS['level'][$value['level']-1][$value['parent']] += $GLOBALS['level'][$value['level']][$value['id']];
								echo "<tr><td><strong>Total ".$value['nama_perkiraan']."</strong></td><td class='totalakun".$akun.$value["parent"]."'><strong>".isNegative($GLOBALS['level'][$value['level']][$value['id']])."</strong></td></tr>";
							}
						}
					}
				}

				$currLevel--;
			}

		}

		if ($currLevel == $prevLevel) echo "</td></tr> ";
		echo "</table> ";
	}
@endphp

<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> Laporan</a></li>
			<li class="active">Neraca</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Neraca</h5>
		</div>
		<div class="panel-body">
			<div class="row">
				<div class="col-sm-8 col-md-8">
					<form class="form" method="get" action="" id="submit_neraca">
						<div class="form-group">
							{{ Form::label("Tanggal", "", ['class'=>'control-label col-sm-2 col-md-2']) }}
							<div class="col-sm-3 col-md-3" >
								{{ Form::text("tanggal_awal", date("d-m-Y"), ['class'=>'form-control set_datepicker','id'=>'tanggal_awal']) }}
							</div>
							<div class="col-sm-3 col-md-3" >
								{{ Form::text("tanggal_akhir", date("d-m-Y"), ['class'=>'form-control set_datepicker','id'=>'tanggal_akhir']) }}
							</div>
							<div class="col-sm-3 col-md-3 btn-group">
								<button type="submit" class="btn btn-sm btn-info">
									<i class="fa fa-filter"></i> Filter
								</button>
								<button type="button" id="btn_excel" class="btn btn-sm btn-success">
									<i class="fa fa-file-excel-o"></i> Excel
								</button>
							</div>
						</div>
					</form>	
				</div>

			</div>

			<!-- <div class="row panel overflow-auto" style="margin-top: 10px;" id="neraca">
				<div class="panel-body">
					<div class="col-sm-6 col-md-6" id="activa"></div>
					<div class="col-sm-6 col-md-6" id="pasiva"></div>
				</div>
			</div> -->

			{{--
			@if(isset($akun_neraca_aktiva) && isset($akun_neraca_pasiva) && isset($akun_neraca_ekuitas))
			<div class="row panel overflow-auto" style="margin-top: 10px;">
				<div class="panel-body">
					<div class="col-lg-12">
						<center>
							<h2 style="margin: 0px;"><strong>CV. KETJUBUNG</strong></h2>
							<h3 style="margin: 0px;"><strong>NERACA</strong></h3>
							<p>{{$periode}}</p>
						</center>
					</div>
					<div class="col-sm-6 col-md-6">

						@php
							if(isset($akun_neraca_aktiva)){
								createTreeNeraca($akun_neraca_aktiva, 'aktiva', 0);
							}
						@endphp

					</div>
					<div class="col-sm-6 col-md-6">

						@php
							if(isset($akun_neraca_pasiva)){
								createTreeNeraca($akun_neraca_pasiva, 'pasiva', 0);
							}
						@endphp

					</div>
					<div class="col-sm-6 col-md-6">

						@php
							if(isset($akun_neraca_ekuitas)){
								createTreeNeraca($akun_neraca_ekuitas, 'ekuitas', 0);
							}
						@endphp

					</div>
				</div>
			</div>
			@endif
			--}}

			@if(isset($periode))
			@php
				$total_kas = 0;
				$total_bank = 0;
				$total_piutang = 0;
				$total_piutang_pi = 0;
				$total_persediaan = 0;
				$total_uang_muka = 0;

				$total_aktiva_lancar = 0; 

				$total_aktiva_tetap = 0;
				$total_aktiva_tetap_lain = 0;
				$total_aktiva_tidak_berwujud = 0;

				$total_aktiva = 0; 

				$total_hutang_biaya = 0;
				$total_uang_muka_penjualan = 0;
				$total_hutang_usaha = 0;
				$total_hutang_pajak = 0;
				$total_hutang_ppn = 0;
				$total_hutang_lain = 0;
				$total_hutang_pi = 0;
				$total_hutang_bank = 0;

				$total_kewajiban_lancar = 0; 

				$total_kewajiban_jp = 0;

				$total_kewajiban = 0; 

				$total_ekuitas = 0;
			@endphp

			<div class="row panel overflow-auto" style="margin-top: 10px;">
				<div class="panel-body">
					<div class="col-lg-12">
						<center>
							<h2 style="margin: 0px;"><strong>CV. KETJUBUNG</strong></h2>
							<h3 style="margin: 0px;"><strong>NERACA</strong></h3>
							<p>{{$periode}}</p>
						</center>
					</div>
					<div class="col-sm-6 col-md-6 overflow-auto" style="border: 1px solid">

						<table class="table">
							<tr>
								<td>
									Aktiva
									<table class="table">
										<tr>
											<td>
												Aktiva Lancar
												<table class="table">
													<tr>
														<td>
															Kas
															<table class="table">
																@foreach($kas as $key_kas => $val_kas)
																<tr>
																	<td>{{ $val_kas['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_kas['saldo']) }}</td>
																</tr>
																@php($total_kas += $val_kas['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Kas</strong></td>
														<td><strong>{{ isNegative($total_kas) }}</strong></td>
													</tr>
													<tr>
														<td>
															Bank
															<table class="table">
																@foreach($bank as $key_bank => $val_bank)
																<tr>
																	<td>{{ $val_bank['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_bank['saldo']) }}</td>
																</tr>
																@php($total_bank += $val_bank['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Bank</strong></td>
														<td><strong>{{ isNegative($total_bank) }}</strong></td>
													</tr>
													<tr>
														<td>
															Piutang
															<table class="table">
																@foreach($piutang as $key_piutang => $val_piutang)
																<tr>
																	<td>{{ $val_piutang['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_piutang['saldo']) }}</td>
																</tr>
																@php($total_piutang += $val_piutang['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Piutang</strong></td>
														<td><strong>{{ isNegative($total_piutang) }}</strong></td>
													</tr>
													<tr>
														<td>
															Piutang Pihak Istimewa
															<table class="table">
																@foreach($piutang_pi as $key_piutang_pi => $val_piutang_pi)
																<tr>
																	<td>{{ $val_piutang_pi['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_piutang_pi['saldo']) }}</td>
																</tr>
																@php($total_piutang_pi += $val_piutang_pi['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Piutang Pihak Istimewa</strong></td>
														<td><strong>{{ isNegative($total_piutang_pi) }}</strong></td>
													</tr>
													<tr>
														<td>
															Persediaan
															<table class="table">
																@foreach($persediaan as $key_persediaan => $val_persediaan)
																<tr>
																	<td>{{ $val_persediaan['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_persediaan['saldo']) }}</td>
																</tr>
																@php($total_persediaan += $val_persediaan['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Persediaan</strong></td>
														<td><strong>{{ isNegative($total_persediaan) }}</strong></td>
													</tr>
													<tr>
														<td>
															Uang Muka
															<table class="table">
																@foreach($uang_muka as $key_uang_muka => $val_uang_muka)
																<tr>
																	<td>{{ $val_uang_muka['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_uang_muka['saldo']) }}</td>
																</tr>
																@php($total_uang_muka += $val_uang_muka['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Uang Muka</strong></td>
														<td><strong>{{ isNegative($total_uang_muka) }}</strong></td>
													</tr>
												</table>
											</td>
										</tr>
										@php
											$total_aktiva_lancar += ($total_kas+$total_bank+$total_piutang+$total_piutang_pi+$total_persediaan+$total_uang_muka);
										@endphp
										<tr>
											<td><strong>Total Aktiva Lancar</strong></td>
											<td><strong>{{ isNegative($total_aktiva_lancar) }}</strong></td>
										</tr>
										<tr>
											<td>
												Aktiva Tetap
												<table class="table">
													@foreach($aktiva_tetap as $key_aktiva_tetap => $val_aktiva_tetap)
													<tr>
														<td>{{ $val_aktiva_tetap['nama_perkiraan'] }}</td>
														<td>{{ isNegative($val_aktiva_tetap['saldo']) }}</td>
													</tr>
													@php($total_aktiva_tetap += $val_aktiva_tetap['saldo'])
													@endforeach
												</table>
											</td>
										</tr>
										<tr>
											<td><strong>Total Aktiva Tetap</strong></td>
											<td><strong>{{ isNegative($total_aktiva_tetap) }}</strong></td>
										</tr>
										<tr>
											<td>
												Aktiva Tetap Lainnya
												<table class="table">
													@foreach($aktiva_tetap_lain as $key_aktiva_tetap_lain => $val_aktiva_tetap_lain)
													<tr>
														<td>{{ $val_aktiva_tetap_lain['nama_perkiraan'] }}</td>
														<td>{{ isNegative($val_aktiva_tetap_lain['saldo']) }}</td>
													</tr>
													@php($total_aktiva_tetap_lain += $val_aktiva_tetap_lain['saldo'])
													@endforeach
												</table>
											</td>
										</tr>
										<tr>
											<td><strong>Total Aktiva Tetap Lainnya</strong></td>
											<td><strong>{{ isNegative($total_aktiva_tetap_lain) }}</strong></td>
										</tr>
										<tr>
											<td>
												Aktiva Tidak Berwujud
												<table class="table">
													@foreach($aktiva_tidak_berwujud as $key_aktiva_tidak_berwujud => $val_aktiva_tidak_berwujud)
													<tr>
														<td>{{ $val_aktiva_tidak_berwujud['nama_perkiraan'] }}</td>
														<td>{{ isNegative($val_aktiva_tidak_berwujud['saldo']) }}</td>
													</tr>
													@php($total_aktiva_tidak_berwujud += $val_aktiva_tidak_berwujud['saldo'])
													@endforeach
												</table>
											</td>
										</tr>
										<tr>
											<td><strong>Total Aktiva Tidak Berwujud</strong></td>
											<td><strong>{{ isNegative($total_aktiva_tidak_berwujud) }}</strong></td>
										</tr>
									</table>
								</td>
							</tr>
							@php
								$total_aktiva += ($total_kas+$total_bank+$total_piutang+$total_piutang_pi+$total_persediaan+$total_uang_muka+$total_aktiva_tetap+$total_aktiva_tetap_lain+$total_aktiva_tidak_berwujud)
							@endphp
							<tr>
								<td><strong>Total Aktiva</strong></td>
								<td><strong>{{ isNegative($total_aktiva) }}</strong></td>
							</tr>
						</table>

					</div>
					<div class="col-sm-6 col-md-6 overflow-auto" style="border: 1px solid">

						<table class="table">
							<tr>
								<td>
									Kewajiban
									<table class="table">
										<tr>
											<td>
												Kewajiban Lancar
												<table class="table">
													<tr>
														<td>
															Hutang Biaya
															<table class="table">
																@foreach($hutang_biaya as $key_hutang_biaya => $val_hutang_biaya)
																<tr>
																	<td>{{ $val_hutang_biaya['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_biaya['saldo']) }}</td>
																</tr>
																@php($total_hutang_biaya += $val_hutang_biaya['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Biaya</strong></td>
														<td><strong>{{ isNegative($total_hutang_biaya) }}</strong></td>
													</tr>
													<tr>
														<td>
															Uang Muka Penjualan
															<table class="table">
																@foreach($uang_muka_penjualan as $key_uang_muka_penjualan => $val_uang_muka_penjualan)
																<tr>
																	<td>{{ $val_uang_muka_penjualan['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_uang_muka_penjualan['saldo']) }}</td>
																</tr>
																@php($total_uang_muka_penjualan += $val_uang_muka_penjualan['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Uang Muka Penjualan</strong></td>
														<td><strong>{{ isNegative($total_uang_muka_penjualan) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang Usaha
															<table class="table">
																@foreach($hutang_usaha as $key_hutang_usaha => $val_hutang_usaha)
																<tr>
																	<td>{{ $val_hutang_usaha['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_usaha['saldo']) }}</td>
																</tr>
																@php($total_hutang_usaha += $val_hutang_usaha['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Usaha</strong></td>
														<td><strong>{{ isNegative($total_hutang_usaha) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang Pajak
															<table class="table">
																@foreach($hutang_pajak as $key_hutang_pajak => $val_hutang_pajak)
																<tr>
																	<td>{{ $val_hutang_pajak['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_pajak['saldo']) }}</td>
																</tr>
																@php($total_hutang_pajak += $val_hutang_pajak['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Pajak</strong></td>
														<td><strong>{{ isNegative($total_hutang_pajak) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang PPN
															<table class="table">
																@foreach($hutang_ppn as $key_hutang_ppn => $val_hutang_ppn)
																<tr>
																	<td>{{ $val_hutang_ppn['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_ppn['saldo']) }}</td>
																</tr>
																@php($total_hutang_ppn += $val_hutang_ppn['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang PPN</strong></td>
														<td><strong>{{ isNegative($total_hutang_ppn) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang Lain-Lain
															<table class="table">
																@foreach($hutang_lain as $key_hutang_lain => $val_hutang_lain)
																<tr>
																	<td>{{ $val_hutang_lain['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_lain['saldo']) }}</td>
																</tr>
																@php($total_hutang_lain += $val_hutang_lain['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Lain-Lain</strong></td>
														<td><strong>{{ isNegative($total_hutang_lain) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang Pihak Istimewa
															<table class="table">
																@foreach($hutang_pi as $key_hutang_pi => $val_hutang_pi)
																<tr>
																	<td>{{ $val_hutang_pi['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_pi['saldo']) }}</td>
																</tr>
																@php($total_hutang_pi += $val_hutang_pi['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Pihak Istimewa</strong></td>
														<td><strong>{{ isNegative($total_hutang_pi) }}</strong></td>
													</tr>
													<tr>
														<td>
															Hutang Bank
															<table class="table">
																@foreach($hutang_bank as $key_hutang_bank => $val_hutang_bank)
																<tr>
																	<td>{{ $val_hutang_bank['nama_perkiraan'] }}</td>
																	<td>{{ isNegative($val_hutang_bank['saldo']) }}</td>
																</tr>
																@php($total_hutang_bank += $val_hutang_bank['saldo'])
																@endforeach
															</table>
														</td>
													</tr>
													<tr>
														<td><strong>Total Hutang Bank</strong></td>
														<td><strong>{{ isNegative($total_hutang_bank) }}</strong></td>
													</tr>
												</table>
											</td>
										</tr>
										@php
											$total_kewajiban_lancar += ($total_hutang_biaya+$total_uang_muka_penjualan+$total_hutang_usaha+$total_hutang_pajak+$total_hutang_ppn+$total_hutang_lain+$total_hutang_pi+$total_hutang_bank);
										@endphp
										<tr>
											<td><strong>Total Kewajiban Lancar</strong></td>
											<td><strong>{{ isNegative($total_kewajiban_lancar) }}</strong></td>
										</tr>
										<tr>
											<td>
												Kewajiban Jangka Panjang
												<table class="table">
													@foreach($kewajiban_jp as $key_kewajiban_jp => $val_kewajiban_jp)
													<tr>
														<td>{{ $val_kewajiban_jp['nama_perkiraan'] }}</td>
														<td>{{ isNegative($val_kewajiban_jp['saldo']) }}</td>
													</tr>
													@php($total_kewajiban_jp += $val_kewajiban_jp['saldo'])
													@endforeach
												</table>
											</td>
										</tr>
										<tr>
											<td><strong>Total Kewajiban Jangka Panjang</strong></td>
											<td><strong>{{ isNegative($total_kewajiban_jp) }}</strong></td>
										</tr>
									</table>
								</td>
							</tr>
							@php
								$total_kewajiban += ($total_kewajiban_lancar+$total_kewajiban_jp);
							@endphp
							<tr>
								<td><strong>Total Kewajiban</strong></td>
								<td><strong>{{ isNegative($total_kewajiban) }}</strong></td>
							</tr>
							<tr>
								<td>
									Ekuitas
									<table class="table">
										@foreach($ekuitas as $key_ekuitas => $val_ekuitas)
										<tr>
											<td>{{ $val_ekuitas['nama_perkiraan'] }}</td>
											<td>{{ isNegative($val_ekuitas['saldo']) }}</td>
										</tr>
										@php($total_ekuitas += $val_ekuitas['saldo'])
										@endforeach
									</table>
								</td>
							</tr>
							<tr>
								<td><strong>Total Ekuitas</strong></td>
								<td><strong>{{ isNegative($total_ekuitas) }}</strong></td>
							</tr>
						</table>

					</div>
				</div>
			</div>
			@endif
		</div>
	</div>
</div>

<script type="text/javascript">
	var BASE_URL = "{{url('/')}}";

	$(document).ready(function(){
		$(".set_datepicker").datepicker({
			format : "dd-mm-yyyy",
			autoclose : true,
			zIndexOffset : 9999,
		});

		// $("#submit_neraca").submit(function(evt){
		// 	evt.preventDefault();
		// 	$.get(BASE_URL+"/laporan/neraca/get-data?tanggal_awal="+$("#tanggal_awal").val()+"&tanggal_akhir="+$("#tanggal_akhir").val(), function(data, status){
		//     	if (status == "success") {
		//     		createTreeNeraca(data, 0);
		//     	} else {
		//     		alert("Status: " + status);
		//     	}
		//   	});
		// })
		
		function createTreeNeraca(array, currentParent, currLevel = 0, prevLevel = -1) {
			var table = "<table class='table'>";
			for (var key in array.neraca_1) {
				if (array.neraca_1.hasOwnProperty(key)) {
					// console.log(array.neraca_1[key].nama_perkiraan);
					if (currentParent == array.neraca_1[key].parent) {
						if (currLevel > prevLevel) {
							console.log(currLevel+' > '+prevLevel);
							table += "<tr>";
						} else if (currLevel == prevLevel) {
							console.log(currLevel+' = '+prevLevel);
							table += "</tr>";
						}

						if (array.neraca_1[key].used == 0) {
							console.log('used = '+array.neraca_1[key].used);
							table += "<td>"+array.neraca_1[key].nama_perkiraan;
						} else {
							console.log('used = '+array.neraca_1[key].used);
							table += "<td>"+array.neraca_1[key].nama_perkiraan+"</td><td class='akun"+array.neraca_1[key].parent+"'>"+array.neraca_1[key].saldo;
						}

						if (currLevel > prevLevel) {
							prevLevel = currLevel;
							console.log('prevLevel = '+prevLevel);
						}

						currLevel++; 

						var child = createTreeNeraca(array, key, currLevel, prevLevel);
						if (child) {console.log('child')} else {
							if(array.neraca_1[key].used == 0) {
								table += "<tr><td><strong>Total "+array.neraca_1[key].nama_perkiraan+"</strong></td><td class='totalakun"+array.neraca_1[key].parent+"'></td></tr>";
							}
						}

						currLevel--;
					}
				}
			}

			if (currLevel == prevLevel) {
				table += "</td></tr>";
			}

			table += "</table>";
			// 	// $("#neraca").show();
			$("#activa").html(table);

			// $.each(array.neraca_1, function( idPerkiraan, value ) {
			//   alert( idPerkiraan + ": " + value.nama_perkiraan );
			// 	if (currentParent == value.parent) {

			// 		if (currLevel > prevLevel) {table += "<tr>";}

			// 		if (currLevel == prevLevel) {table += "</tr>";}

			// 		if (value.used == 0) {
			// 			table += "<td>"+value.nama_perkiraan;
			// 		} else {
			// 			table += "<td>"+value.nama_perkiraan+"</td><td class='akun"+value.parent+"'>"+value.saldo;
			// 		}

			// 		if (currLevel > prevLevel) {prevLevel = currLevel}

			// 		currLevel++;


			// 		var child = createTreeNeraca(array, idPerkiraan, currLevel, prevLevel);

			// 		if (child) {console.log('child')} else {
			// 			if(value.used == 0) {
			// 				table += "<tr><td><strong>Total "+value.nama_perkiraan+"</strong></td><td class='totalakun"+value.parent+"'></td></tr>";
			// 			}
			// 		}

			// 		currLevel--;
			// 	}

			// 	if (currLevel == prevLevel) {
			// 		table += "</td></tr>";
			// 	}
			// 	table += "</table>";
			// 	// $("#neraca").show();
			// 	$("#activa").html(table);
			// });
			
		}
	});

	$("#btn_excel").click(function(evt){
		window.open(BASE_URL+'/laporan/neraca/excel?tanggal_awal='+$("#tanggal_awal").val()+'&tanggal_akhir='+$("#tanggal_akhir").val());
	})

</script>

@endsection