@extends('layouts.app')

@section('content')
{{--*/ usort($tableGrid, "SiteHelpers::_sort") /*--}}
<div class="page-titles">
  <h2> {{ $pageTitle }} <small> {{ $pageNote }} </small></h2>
</div>
<div  class="card">
	<div class="card-body">
		<div class="toolbar-nav">
		{!! Form::open(['url' => $pageModule . '/', 'method' => 'get', 'class' => 'form-horizontal']) !!}
				<div class="row ">				
					<div class="col-md-8 ">
						<div class="" style="display:flex;">
		
						@if($level < 3)
						<select class="select2 form-control-sm" name="plant" id="plant"></select>
						@endif
						</div>  
							
					</div>
					<div class="col-md-4 text-right"> 	
					<button class="btn btn-sm btn-primary" id="btn-tarikdata"><i class="fas fa-paper-plane"></i> Filter </button>	
					<button class="btn btn-sm btn-success" id="btn-download-excel">
						<i class="fas fa-file-excel"></i> Download Excel
					</button>

					</div>
						
				</div>		
				{!! Form::close() !!}			
			</div>

		


		 {!! Form::open(array('url'=>'rekaptakmarcluster/delete/', 'class'=>'form-horizontal' ,'id' =>'SximoTable' )) !!}
		 <div class="table-responsive" style="height:600px;">
	    <table class="table table-hover table-striped  ">
	        <thead style="position: sticky; top: 0; background-color: #fff; z-index: 1;">
				<tr>
					<th rowspan='2' class="number"> No </th>			
					@foreach ($tableGrid as $key => $t)
						@if($t['view'] =='1')
							@if($key <= 3)
								<th rowspan='2'>{{ $t['label'] }}</th>
							@endif

						@endif
					@endforeach
								<th style='text-align: center;' colspan='6'>Luas</th>
								<th style='text-align: center;' colspan='6'>Tebu</th>
								<th style='text-align: center;' colspan='6'>Protas</th>
				  </tr>
				  <tr>
					@foreach ($tableGrid as $key => $t)
							@if($t['view'] =='1')
								@if($key >= 4)
									<th>{{ $t['label'] }}</th>
								@endif
							@endif
						@endforeach
				  </tr>
	        </thead>

	        <tbody>
							
	            @foreach ($rowData as $row)
	                <tr>
						<td width="30"> {{ ++$i }} </td>	
					@foreach ($tableGrid as $key => $field)
						 @if($field['view'] =='1')
						 	<?php $limited = isset($field['limited']) ? $field['limited'] :''; ?>
						 	@if(SiteHelpers::filterColumn($limited ))
							 <td>					 
							 	{!! SiteHelpers::formatRows($row->{$field['field']},$field ,$row ) !!}						 
							 </td>
							@endif	
						 @endif					 
					 @endforeach				 
					
	                </tr>

					@foreach ($tableGrid as $key => $field)
							@if($field['view'] == '1' && $key >= 4)
								@php
										$totalSum[$field['field']] = isset($totalSum[$field['field']]) ? $totalSum[$field['field']] + $row->{$field['field']} : $row->{$field['field']};
								@endphp
							@endif
						@endforeach
						
					
	            @endforeach
	              
	        </tbody>
	      
			<tfoot style="position: sticky; bottom: 0; background-color: #fff; z-index: 1;">
					<tr>
						<td colspan="5"><strong>Total</strong></td>
						@foreach ($tableGrid as $key => $field)
							@if($field['view'] == '1' && isset($totalSum[$field['field']]))
								@if($key > 15 && $key < 21)
									@php
										$tebuField = "tebu_" . explode("_", $field['field'])[1];
										$luasField = "luas_" . explode("_", $field['field'])[1];
										$tebu = isset($totalSum[$tebuField]) ? $totalSum[$tebuField] : 0;
										$luas = isset($totalSum[$luasField]) ? $totalSum[$luasField] : 0;
										$ratio = ($luas != 0) ? $tebu / $luas : 0; // Cek division by zero
									@endphp
									<td><strong>{{ number_format($ratio, 2) }}</strong></td>
								@elseif($key == 21)
									@php
										$totalTebu = isset($totalSum["total_tebu"]) ? $totalSum["total_tebu"] : 0;
										$totalLuas = isset($totalSum["total_luas"]) ? $totalSum["total_luas"] : 0;
										$totalRatio = ($totalLuas != 0) ? $totalTebu / $totalLuas : 0; // Cek division by zero
									@endphp
									<td><strong>{{ number_format($totalRatio, 2) }}</strong></td>
								@else
									<td><strong>{{ number_format($totalSum[$field['field']], 2) }}</strong></td>
								@endif
							@endif
						@endforeach

					</tr>
		        </tfoot>
	    </table>
		<input type="hidden" name="md" value="" />
		</div>
		{!! Form::close() !!}
		
		
	</div>  
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.2/xlsx.full.min.js"></script>

<script type="text/javascript">
	$(document).ready(function() {
		@if($level < 3)
		$("#plant").jCombo("{!! url('dataprodisconsole/comboselect?filter=master_kebun:kode_kebun:kode_kebun|nama_kebun') !!}",
		{  initial_text : 'Pilih Kebun' });
		@endif
	});
	document.getElementById('btn-download-excel').addEventListener('click', function() {
        // Get the table data
        let table = document.querySelector('table');
        let wb = XLSX.utils.table_to_book(table, {sheet: "Sheet1"});
        
        // Trigger the download
        XLSX.writeFile(wb, 'tabledata.xlsx');
    });
</script>
@stop