<div class="d-flex justify-content-between mb-3 px-4">
    <div class="">
        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Batch No:</span> <span>{{ $batch?->batch_no ?? '' }}</span>
        </div>

        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Production Completion Date:</span>
            <span>
                {{ isset($batch->end_date) ? \Carbon\Carbon::parse($batch->end_date)->toDateTimeString() : '-' }}
            </span>
        </div>
    </div>

    <div class="">
        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Product Code:</span>
            <span>-</span>
        </div>
        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Product:</span>
            <span>{{ $batch->product?->brand_name ?? ($batch->product?->generic_name ?? '') }}</span>
        </div>

        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Unit:</span> <span>-</span>
        </div>

        <div class="d-flex align-items-center gap-2">
            <span class="fw-bold">Theoretical Qty:</span>
            <span>{{ $batch->masterFormulation?->batch_size ?? '' }}</span>
        </div>
    </div>
</div>

<div class="table-responsive">
    <table class="table table-hover text-black">
        <thead>
            <tr>
                <th style="border: 1px solid black;">#</th>
                <th style="border: 1px solid black;">Doc #</th>
                <th style="border: 1px solid black;">Type</th>
                <th style="border: 1px solid black;">Date</th>
                <th style="border: 1px solid black;">Code</th>
                <th style="border: 1px solid black;">Item Description</th>
                <th style="border: 1px solid black;">Unit</th>
                <th style="border: 1px solid black;">Qty</th>
                <th style="border: 1px solid black;">Rate</th>
                <th style="border: 1px solid black;">Debit</th>
                <th style="border: 1px solid black;">Credit</th>
            </tr>
        </thead>
        <tbody>
            @php
                $counter = 1;
                $totalDebit = $totalCredit = $totalQty = 0;
            @endphp
            @forelse ($allDocuments as $doc)
                {{-- @dd($doc) --}}
                @php
                    $docType = isset($doc['code']) ? strtok($doc['code'], '0123456789') : '';
                    $docTypeUrl = match ($docType) {
                        'SIR' => 'store-issue-requisitions.show',
                        'RRN' => 'rejection-return-notes.show',
                        'LON' => 'left-over-notes.show',
                        default => 'javascript:void(0);',
                    };
                @endphp
                @foreach ($doc['items'] as $item)
                    <tr>
                        <td style="color: black; border: 1px solid black;">
                            {{ $counter++ }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $doc['code'] ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $docType ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ isset($doc['created_at']) ? \Carbon\Carbon::parse($doc['created_at'])->format('d/m/Y') : '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $item['raw_material_item']['code'] ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $item['raw_material_item']['name'] ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $item['unit'] ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $item['quantity'] ?? '' }}
                            @php
                                $totalQty += $item['quantity'] ?? 0;
                            @endphp
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            {{ $item['unit_price'] ?? '' }}
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            @if (in_array($docType, ['SIR', 'sir', 'RRN', 'rrn']))
                                {{ ($item['quantity'] ?? 0) * ($item['unit_price'] ?? 0) }}
                                @php
                                    $totalDebit += ($item['quantity'] ?? 0) * ($item['unit_price'] ?? 0);
                                @endphp
                            @endif
                        </td>
                        <td style="color: black; border: 1px solid black;">
                            @if (in_array($docType, ['LON', 'lon']))
                                {{ ($item['quantity'] ?? 0) * ($item['unit_price'] ?? 0) }}
                                @php
                                    $totalCredit += ($item['quantity'] ?? 0) * ($item['unit_price'] ?? 0);
                                @endphp
                            @endif
                        </td>
                    </tr>
                @endforeach
            @empty
                <tr>
                    <td colspan="9" class="text-center">No data found</td>
                </tr>
            @endforelse
        </tbody>
        <tfoot>
            <tr>
                <td colspan="7" class="text-end fw-bold">Total</td>
                <td>{{ $totalQty }}</td>
                <td></td>
                <td class="fw-bold">
                    {{ $totalDebit ?? 0 }}
                </td>
                <td class="fw-bold">
                    {{ $totalCredit ?? 0 }}
                </td>
            </tr>
        </tfoot>
    </table>
</div>
