@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">
			<div class="row">
				<div class="col-md-6">
					<form method="GET" action="{{ url($pageModule) }}" class="form-inline">
						<div class="form-group">
							<label for="filter_tgl">Filter Tanggal: </label>
							<input type="date" name="filter_tgl" id="filter_tgl" class="form-control-sm mx-sm-2" value="{{ request('filter_tgl') }}">
						</div>
						<button type="submit" class="btn btn-primary btn-sm">Filter</button>
					</form>
				</div>
				<div class="col-md-6 text-right">
					<div class="btn-group">
						<button type="button" class="btn dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-bars"></i> Bulk Action </button>
						<ul class="dropdown-menu">
							<li class="nav-item"><a href="javascript:void(0);" class="nav-link" onclick="exportTableToExcel('data.xls')"> Export Excel </a></li>
							<li class="nav-item"><a href="javascript:void(0);" class="nav-link" onclick="exportTableToPDF()"> Export PDF </a></li>
						</ul>

						

						
					</div>	
				</div>    
			</div>
		<div >

	



		 <div class="table-responsive" id="previewtable" style="min-height:300px;">
					
		 <table class="table table-hover table-striped">
			<thead>
				<tr>
					<th colspan="10" style="text-align: center; background-color: #d1ecf13b;"><h4>Rekap Klentek Per Kebun<h4></th>
				</tr>
				<tr>
					<th colspan="10" style="text-align: center; background-color: #d1ecf13b;"><h5 class="text-center">Data per Tanggal: {{ request('filter_tgl') ? \Carbon\Carbon::parse(request('filter_tgl'))->translatedFormat('d F Y') : \Carbon\Carbon::now()->translatedFormat('d F Y') }}</h5></th>
				</tr>
				<tr>
					<th>Kebun</th>
					<th colspan="3" style="text-align: center; background-color: #f8d7da3b;">Klentek 1</th>
					<th colspan="3" style="text-align: center; background-color: #d4edda3b;">Klentek 2</th>
					<th colspan="3" style="text-align: center; background-color: #d1ecf13b;">Klentek 3</th>
				</tr>
				<tr>
					<th></th>
					<th style="background-color: #f8d7da3b;">Sasaran</th>
					<th style="background-color: #f8d7da3b;">Real</th>
					<th style="background-color: #f8d7da3b;">% Sasaran</th>
					<th style="background-color: #d4edda3b;">Sasaran</th>
					<th style="background-color: #d4edda3b;">Real</th>
					<th style="background-color: #d4edda3b;">% Sasaran</th>
					<th style="background-color: #d1ecf13b;">Sasaran</th>
					<th style="background-color: #d1ecf13b;">Real</th>
					<th style="background-color: #d1ecf13b;">% Sasaran</th>
				</tr>
			</thead>

			<tbody>
				@php
					$totalSasaran1 = $totalReal1 = $totalPersen1 = 0;
					$totalSasaran2 = $totalReal2 = $totalPersen2 = 0;
					$totalSasaran3 = $totalReal3 = $totalPersen3 = 0;
				@endphp

				@foreach ($results as $row)
					<tr>
						<td>{{ $row->kebun }}</td>
						<td style="text-align: right; background-color: #f8d7da3b;">{{ $row->sasaran_klentek_1 && $row->sasaran_klentek_1 != 0.00 ? $row->sasaran_klentek_1 : '-' }}</td>
						<td style="text-align: right; background-color: #f8d7da3b;">{{ $row->real_klentek_1 && $row->real_klentek_1 != 0.00 ? $row->real_klentek_1 : '-' }}</td>
						<td style="text-align: right; background-color: #f8d7da3b;">{{ $row->persen_klentek_1 && $row->persen_klentek_1 != 0.000 ? $row->persen_klentek_1 : '-' }}</td>
						<td style="text-align: right; background-color: #d4edda3b;">{{ $row->sasaran_klentek_2 && $row->sasaran_klentek_2 != 0.00 ? $row->sasaran_klentek_2 : '-' }}</td>
						<td style="text-align: right; background-color: #d4edda3b;">{{ $row->real_klentek_2 && $row->real_klentek_2 != 0.00 ? $row->real_klentek_2 : '-' }}</td>
						<td style="text-align: right; background-color: #d4edda3b;">{{ $row->persen_klentek_2 && $row->persen_klentek_2 != 0.000 ? $row->persen_klentek_2 : '-' }}</td>
						<td style="text-align: right; background-color: #d1ecf13b;">{{ $row->sasaran_klentek_3 && $row->sasaran_klentek_3 != 0.00 ? $row->sasaran_klentek_3 : '-' }}</td>
						<td style="text-align: right; background-color: #d1ecf13b;">{{ $row->real_klentek_3 && $row->real_klentek_3 != 0.00 ? $row->real_klentek_3 : '-' }}</td>
						<td style="text-align: right; background-color: #d1ecf13b;">{{ $row->persen_klentek_3 && $row->persen_klentek_3 != 0.000 ? $row->persen_klentek_3 : '-' }}</td>
					</tr>
					@php
						$totalSasaran1 += $row->sasaran_klentek_1 ?: 0;
						$totalReal1 += $row->real_klentek_1 ?: 0;
						$totalPersen1 += $row->persen_klentek_1 ?: 0;
						$totalSasaran2 += $row->sasaran_klentek_2 ?: 0;
						$totalReal2 += $row->real_klentek_2 ?: 0;
						$totalPersen2 += $row->persen_klentek_2 ?: 0;
						$totalSasaran3 += $row->sasaran_klentek_3 ?: 0;
						$totalReal3 += $row->real_klentek_3 ?: 0;
						$totalPersen3 += $row->persen_klentek_3 ?: 0;
					@endphp
				@endforeach
			</tbody>

			<tfoot>
				<tr>
					<th>Total</th>
					<th style="text-align: right; background-color: #f8d7da3b;">{{ $totalSasaran1 ?: '-' }}</th>
					<th style="text-align: right; background-color: #f8d7da3b;">{{ $totalReal1 ?: '-' }}</th>
					<th style="text-align: right; background-color: #f8d7da3b;">{{ number_format((($totalReal1/$totalSasaran1)*100),2)  ?: '-' }}</th>
					<th style="text-align: right; background-color: #d4edda3b;">{{ $totalSasaran2 ?: '-' }}</th>
					<th style="text-align: right; background-color: #d4edda3b;">{{ $totalReal2 ?: '-' }}</th>
					<th style="text-align: right; background-color: #d4edda3b;">{{ number_format((($totalReal2/$totalSasaran2)*100),2) ?: '-' }}</th>
					<th style="text-align: right; background-color: #d1ecf13b;">{{ $totalSasaran3 ?: '-' }}</th>
					<th style="text-align: right; background-color: #d1ecf13b;">{{ $totalReal3 ?: '-' }}</th>
					<th style="text-align: right; background-color: #d1ecf13b;">{{ number_format((($totalReal3/$totalSasaran3)*100),2) ?: '-' }}</th>
				</tr>
			</tfoot>
		</table>
		<input type="hidden" name="md" value="" />
		</div>

		
	</div>  
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.5.25/jspdf.plugin.autotable.min.js"></script>
<script>
							function exportTableToExcel(filename) {
								const rows = document.querySelectorAll("table tr");
								let excelContent = "<table>";

								rows.forEach(row => {
									const cols = row.querySelectorAll("th, td");
									excelContent += "<tr>";
									cols.forEach(col => {
										excelContent += `<td>${col.innerText}</td>`;
									});
									excelContent += "</tr>";
								});

								excelContent += "</table>";

								const blob = new Blob([excelContent], { type: "application/vnd.ms-excel" });
								const link = document.createElement("a");
								link.href = URL.createObjectURL(blob);
								link.download = filename;
								link.click();
							}

							function exportTableToPDF() {
								const { jsPDF } = window.jspdf;
								const doc = new jsPDF('landscape');

								doc.autoTable({
									html: 'table',
									// styles: { halign: 'center' }, // Center align all text in the table
									headStyles: { halign: 'center' } // Center align text in the header
								});
								doc.save('data.pdf');
							}
						</script>
@stop