@extends('layouts.app')

@section('title')
    {{ $pageInfo['title'] }}
@endsection

@section('admin-content')
	<section class="content-header">
		<div class="container-fluid">
			<div class="row">
                <div class="col-sm-6">
                    <h1>{{ $pageInfo['title'] }}</h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-form"><a href="{{ route('admin.dashboard') }}">{{ __('Dashboard') }}</a> / </li>
                        <li class="breadcrumb-form active">&nbsp;{{ $pageInfo['title'] }}</li>
                    </ol>
                </div>
            </div>
			<div class="row mt-4">
				<div class="col-12">
                    <div class="card">
                        <div class="card-body">
							<ul>
								<li><b>Cabang:</b> {{ $cabang->name }}</li>
								<li><b>Periode:</b> {{ ucfirst($month) }} {{ $year }}</li>
								<li><b>Cost Centre</b> {{ $cabang->cost->code }}</li>
							</ul>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>

	@php
		use App\Helpers\FormHelper;
	@endphp

    <section class="content">
		<div class="container-fluid">
			<!-- All Forms Table -->
			<div class="row">
				<div class="col-12">
					<div class="card">
						<div class="card-body">
							<div class="data-tables">
								@include('backend.layouts.partials.messages')
								<div class="table-responsive">
									<table id="dataTable-table">
										<thead class="bg-light text-capitalize">
											<tr>
												<th>#</th>
												<th>Type</th>
												<th>Account Number</th>
												<th>Dr</th>
												<th>Cr</th>
											</tr>
										</thead>
										<tbody>
											<!-- Dynamic Rows -->
											@php
												$totalDr = 0;
												$totalCr = 0;
											@endphp
											@foreach ($kategori as $item)
												@php
													$drValue = $nominals[$item->id] ?? 0;
													$drFormatted = $drValue == 0 ? '' : number_format($drValue, 0, ',', '.');
													$totalDr += $drValue;
												@endphp
												<tr>
													<td>{{ $loop->iteration }}</td>
													<td>{{ $item->name }}</td>
													<td>{{ $item->account_number }}</td>
													<td>{{ $drFormatted }}</td>
													<td></td>
												</tr>
											@endforeach


											<tr>
												<td>{{ $kategori->count() + 1 }}</td>
												<td>Petty Cash</td>
												<td></td>
												<td></td>
												<td>
													@php
														$crValue = $budget && $budget->amount != 0 ? $budget->amount : 0;
														$totalCr += $crValue;
														echo $crValue == 0 ? '' : number_format($crValue, 0, ',', '.');
													@endphp
												</td>
											</tr>

											{{-- Static Rows --}}
											<tr>
												<td>{{ $kategori->count() + 2 }}</td>
												<td>Cash Advance (Opening Balance)</td>
												<td>{{ $kategori_bca->account_number }}</td>
												<td></td>
												<td>
													@php
														$totalCr += 500000;
													@endphp
													{{ number_format(500000, 0, ',', '.') }}
												</td>
											</tr>

											<tr>
												<td>{{ $kategori->count() + 3 }}</td>
												<td>Cash Advance (Ending Balance)</td>
												<td>{{ $kategori_bca->account_number }}</td>
												<td>
													@php
														$totalDr += 500000;
													@endphp
													{{ number_format(500000, 0, ',', '.') }}
												</td>
												<td></td>
											</tr>
										</tbody>
										<tfoot>
											<tr>
												<td colspan="3" class="text-right"><strong>Total</strong></td>
												<td>{{ number_format($totalDr, 0, ',', '.') }}</td>
												<td>{{ number_format($totalCr, 0, ',', '.') }}</td>
											</tr>
										</tfoot>
									</table>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</section>
@endsection

@section('scripts')
    <script>
        if ($('#dataTable-table').length) {
            $('#dataTable-table').DataTable({
                responsive: false,
                dom: 'Bfrtip',
                buttons: [
                    {
                        extend: 'excel',
                        text: 'Export Excel',
                        customize: function (xlsx) {
							var sheet = xlsx.xl.worksheets['sheet1.xml'];

							// Add a blank row for spacing
							var blankRow = `<row><c t="inlineStr"><is><t></t></is></c></row>`;

							// Bold text using Excel XML structure
							var additionalInfo = `
								<row>
									<c t="inlineStr" s="2"><is><t>Cabang: {{ $cabang->name }}</t></is></c>
								</row>
								<row>
									<c t="inlineStr" s="2"><is><t>Periode: {{ ucfirst($month) }} {{ $year }}</t></is></c>
								</row>
								<row>
									<c t="inlineStr" s="2"><is><t>Cost Centre: {{ $cabang->cost->code }}</t></is></c>
								</row>
							`;

							// Locate the sheet data and append the blank row and additional information
							var sheetData = $(sheet).find('sheetData');
							var existingRows = sheetData.children();
							existingRows.last().after(blankRow + additionalInfo);

							// Add bold style to the sheet
							var styles = xlsx.xl['styles.xml'];
							var cellXfs = $(styles).find('cellXfs');
							cellXfs.append(`
								<xf xfId="0" applyFont="1">
									<alignment horizontal="left"/>
									<font>
										<b/>
										<sz val="11"/>
										<name val="Calibri"/>
										<family val="2"/>
									</font>
								</xf>
							`);
						}
                    },
                    {
                        extend: 'csv',
                        text: 'Export CSV',
                        customize: function (csv) {
                            // Add header information to the CSV
                            var additionalInfo = `
                                Cabang: {{ $cabang->name }}
                                Periode: {{ ucfirst($month) }} {{ $year }}
                                Cost Centre: {{ $cabang->cost->code }}
                            `;
                            return additionalInfo + csv;
                        }
                    },
                    {
                        extend: 'pdf',
                        text: 'Export PDF',
                        customize: function (doc) {
                            // Add additional information to the PDF
                            doc.content.splice(0, 0, {
                                margin: [0, 0, 0, 12],
                                alignment: 'left',
                                table: {
                                    widths: ['*'],
                                    body: [
                                        [{ text: 'Cabang: {{ $cabang->name }}', style: 'header' }],
                                        [{ text: 'Periode: {{ ucfirst($month) }} {{ $year }}', style: 'header' }],
                                        [{ text: 'Cost Centre: {{ $cabang->cost->code }}', style: 'header' }]
                                    ]
                                },
                                layout: 'noBorders'
                            });
                        }
                    },
                    {
                        extend: 'print',
                        text: 'Print'
                    },
                    {
                        extend: 'colvis',
                        text: 'Column Visibility'
                    }
                ],
                paging: false,
                info: false,
                pageLength: -1
            });
        }
    </script>
@endsection
