@extends('layouts.app')

@section('content')
{{--*/ usort($tableGrid, "SiteHelpers::_sort") /*--}}
<div class="page-titles">
  <h2> {{ $pageTitle }} <small> {{ $pageTitle }} </small></h2>
</div>

<div class="card">
	<div class="card-body">
		<div class="toolbar-nav">
			<div class="row">
				<div class="col-md-6">
					<div class="btn-group">
				    </div>	
				</div>    
			</div>

			<!-- Add Date Filter Form -->
			<div class="row">
				<div class="col-md-6">
					<form action="{{ url($pageModule) }}" method="GET" id="dateFilterForm">
						<div class="input-group">
							<input type="date" name="start_date" class="form-sm-control" value="{{ request('start_date') }}" placeholder="Start Date">
							<input type="date" name="end_date" class="form-sm-control" value="{{ request('end_date') }}" placeholder="End Date">
							<span class="input-group-btn">
								<button class="btn btn-xs btn-primary" type="submit">Filter</button>
                                <button id="downloadExcel" class="btn btn-xs btn-success">Download Excel</button>

							</span>
						</div>
					</form>
				</div>
			</div>
		</div>

		{!! Form::open(array('url'=>'realbiayalkp/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>kode_rayon</th>
        <th>nama_rayon</th>
        <th>nama_kebun</th>
        <th>luas_polig</th>
        <th class="text-right">total_rp</th>
        <th class="text-right">total_rp_perha</th>
        <th class="text-right">nkm_bph_sewa</th>
        <th class="text-right">biaya_garap</th>
        <th class="text-right">saprodi_bibit</th>
        <th class="text-right">saprodi_pupuk</th>
        <th class="text-right">saprodi_herbisida</th>
        <th class="text-right">biaya_luar_kebun</th>
        <th class="text-right">tma_tebangmanual</th>
        <th class="text-right">tma_tebangmekanis</th>
        <th class="text-right">tma_muatmanual</th>
        <th class="text-right">tma_muatmekanis</th>
        <th class="text-right">tma_angkut</th>
        <th class="text-right">tma_operasional</th>
        <th class="text-right">mekanisasi_olahtanah</th>
        <th class="text-right">mekanisasi_kultivasi</th>
        <th class="text-right">pengemasan</th>
        <th>entry_by</th>
        <th>tgl_input_terbaru</th>
    </tr>
</thead>

				<tbody>
					@php
						// Initialize total variables
						$total_luas_polig = 0;
						$total_rp = 0;
						$total_rp_perha = 0;
						$nkm_bph_sewa = 0;
						$biaya_garap = 0;
						$saprodi_bibit = 0;
						$saprodi_pupuk = 0;
						$saprodi_herbisida = 0;
						$biaya_luar_kebun = 0;
						$tma_tebangmanual = 0;
						$tma_tebangmekanis = 0;
						$tma_muatmanual = 0;
						$tma_muatmekanis = 0;
						$tma_angkut = 0;
						$tma_operasional = 0;
						$mekanisasi_olahtanah = 0;
						$mekanisasi_kultivasi = 0;
						$pengemasan = 0;
					@endphp

					@foreach ($results as $row)
	<tr>
            <td>{{ $row->kode_rayon }}</td>
            <td>{{ $row->nama_rayon }}</td>
            <td>{{ $row->nama_kebun }}</td>
            <td>{{ $row->luas_polig }}</td>
            <td class="text-right">{{ number_format($row->total_rp, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->total_rp_perha, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->nkm_bph_sewa, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->biaya_garap, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->saprodi_bibit, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->saprodi_pupuk, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->saprodi_herbisida, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->biaya_luar_kebun, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_tebangmanual, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_tebangmekanis, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_muatmanual, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_muatmekanis, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_angkut, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->tma_operasional, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->mekanisasi_olahtanah, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->mekanisasi_kultivasi, 0, ',', '.') }}</td>
            <td class="text-right">{{ number_format($row->pengemasan, 0, ',', '.') }}</td>
            <td>{{ $row->entry_by }}</td>
            <td>{{ $row->tgl_input_terbaru }}</td>
    
    @php
        // Update total variables
        $total_luas_polig += $row->luas_polig;
        $total_rp += $row->total_rp;
        $total_rp_perha += $row->total_rp_perha;
        $nkm_bph_sewa += $row->nkm_bph_sewa;
        $biaya_garap += $row->biaya_garap;
        $saprodi_bibit += $row->saprodi_bibit;
        $saprodi_pupuk += $row->saprodi_pupuk;
        $saprodi_herbisida += $row->saprodi_herbisida;
        $biaya_luar_kebun += $row->biaya_luar_kebun;
        $tma_tebangmanual += $row->tma_tebangmanual;
        $tma_tebangmekanis += $row->tma_tebangmekanis;
        $tma_muatmanual += $row->tma_muatmanual;
        $tma_muatmekanis += $row->tma_muatmekanis;
        $tma_angkut += $row->tma_angkut;
        $tma_operasional += $row->tma_operasional;
        $mekanisasi_olahtanah += $row->mekanisasi_olahtanah;
        $mekanisasi_kultivasi += $row->mekanisasi_kultivasi;
        $pengemasan += $row->pengemasan;
    @endphp
						</tr>
					@endforeach
				</tbody>

        <!-- Total Row -->
        <tfoot>
    <tr>
        <td colspan="3"><strong>Total</strong></td>
        <td class="text-right"><strong>{{$total_luas_polig }}</strong></td>
        <td class="text-right"><strong>{{ number_format($total_rp, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ $total_luas_polig > 0 ? number_format($total_rp/$total_luas_polig, 0, ',', '.') : 'N/A' }}</strong></td>
        <td class="text-right"><strong>{{ number_format($nkm_bph_sewa, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($biaya_garap, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($saprodi_bibit, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($saprodi_pupuk, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($saprodi_herbisida, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($biaya_luar_kebun, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_tebangmanual, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_tebangmekanis, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_muatmanual, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_muatmekanis, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_angkut, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($tma_operasional, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($mekanisasi_olahtanah, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($mekanisasi_kultivasi, 0, ',', '.') }}</strong></td>
        <td class="text-right"><strong>{{ number_format($pengemasan, 0, ',', '.') }}</strong></td>
        <td colspan="2"></td> <!-- Empty columns for alignment -->
    </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.5/xlsx.full.min.js"></script>

<script>
    document.getElementById('downloadExcel').addEventListener('click', function () {
        let table = document.querySelector("table"); // Ambil tabel
        let wb = XLSX.utils.book_new(); // Buat workbook baru
        let ws = XLSX.utils.table_to_sheet(table); // Konversi tabel ke sheet

        XLSX.utils.book_append_sheet(wb, ws, "Data"); // Tambahkan sheet ke workbook
        XLSX.writeFile(wb, "real_biaya.xlsx"); // Simpan file Excel
    });
</script>


@stop
