@extends('admin.layouts.app')

@section('title', 'Detailed FIFO Report')

@section('content')
    <div class="page-header">
        <div class="page-title">
            <h4>Detailed FIFO Report (Batch-wise Consumption)</h4>
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('reports.current-balance-filter') }}">Current Balance Report</a></li>
                <li class="breadcrumb-item active">FIFO Report</li>
            </ul>
        </div>
        <!-- <div class="text-right d-print-none">
            <button class="btn btn-primary" onclick="printWithTimestamp()"><i class="fas fa-file-pdf me-2"></i>Generate PDF</button>
        </div> -->
    </div>

    <div class="card">
        <div class="card-body">
            @if(isset($selectedCategoryName) || isset($tillDate))
            <div class="mb-4">
                @if($selectedCategoryName)
                    <span class="badge bg-primary fs-6">Category: {{ $selectedCategoryName }}</span>
                @endif
                @if($tillDate)
                    <span class="badge bg-info fs-6">Till Date: {{ $tillDate }}</span>
                @endif
            </div>
            @endif

            <div class="table-responsive">
                <table class="table no-footer table-hover" id="fifoReportTable">
                    <thead class="table-light">
                        <tr>
                            <th>GRN Date</th>
                            <th>GRN Code</th>
                            <th>Item Name</th>
                            <th>Batch #</th>
                            <th>Mfg. Date</th>
                            <th>Exp. Date</th>
                            <th>Received Qty</th>
                            <th>Unit Price</th>
                            <th>Leftover</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($batches as $batch)
                            <!-- GRN Batch Row -->
                            <tr class="table-primary fw-bold">
                                <td>{{ \Carbon\Carbon::parse($batch->grn->created_at)->format('d-m-Y') }}</td>
                                <td>{{ $batch->grn->code }}</td>
                                <td>
                                    {{ $batch->grnItem->rawMaterialItem->name ?? $batch->grnItem->product->name ?? 'N/A' }}
                                </td>
                                <td>{{ $batch->batch }}</td>
                                <td>{{ $batch->mfg_date ? \Carbon\Carbon::parse($batch->mfg_date)->format('d-m-Y') : 'N/A' }}</td>
                                <td>{{ $batch->exp_date ? \Carbon\Carbon::parse($batch->exp_date)->format('d-m-Y') : 'N/A' }}</td>
                                <td>{{ number_format($batch->receiving_quantity, 2) }} {{ $batch->unit }}</td>
                                <td>{{ number_format($batch->unit_price, 2) }}</td>
                                <td class="text-danger">{{ number_format($batch->leftover, 2) }} {{ $batch->unit }}</td>
                            </tr>

                            <!-- Consumption (SIR) Header -->
                            @if($batch->issues->count() > 0)
                                <!-- <tr class="table-light">
                                    <td colspan="9" class="ps-5 py-1">
                                        <div class="d-flex align-items-center">
                                            <i class="fas fa-level-up-alt fa-rotate-90 me-2 text-muted"></i>
                                            <span class="small fw-bold text-muted">Consumption History for this Batch:</span>
                                        </div>
                                    </td>
                                </tr> -->
                                <!-- SIR Rows -->
                                <!-- @foreach($batch->issues as $issue)
                                    <tr class="small text-muted">
                                        <td colspan="2"></td>
                                        <td class="ps-5">
                                            <strong>SIR Date:</strong> {{ \Carbon\Carbon::parse($issue->store_issue_requisition->created_at)->format('d-m-Y') }}
                                        </td>
                                        <td colspan="3">
                                            <strong>SIR Code:</strong> {{ $issue->store_issue_requisition->code }}
                                        </td>
                                        <td class="text-info">
                                            <strong>Consumed:</strong> {{ number_format($issue->quantity, 2) }} {{ $issue->unit }}
                                        </td>
                                        <td colspan="2"></td>
                                    </tr>
                                @endforeach -->
                                <!-- Batch Summary Line -->
                                <!-- <tr class="table-light">
                                    <td colspan="6"></td>
                                    <td colspan="3" class="text-end small pe-4">
                                        <span class="text-muted">Total Consumed from this Batch: </span>
                                        <strong>{{ number_format($batch->total_consumed, 2) }} {{ $batch->unit }}</strong>
                                    </td>
                                </tr> -->
                            @else
                                <tr class="small text-muted italic">
                                    <td colspan="2"></td>
                                    <td colspan="7" class="ps-5 py-1">
                                        <i class="fas fa-info-circle me-1"></i> No consumption recorded for this batch yet.
                                    </td>
                                </tr>
                            @endif
                            <!-- Spacer Row -->
                            <tr style="height: 15px;"><td colspan="9"></td></tr>
                        @empty
                            <tr>
                                <td colspan="9" class="text-center">No GRN batches found for the selected items.</td>
                            </tr>
                        @endforelse
                        <tr class="fw-bold bg-light">
                            <td colspan="6" class="text-end pe-3 py-2">
                                Grand Total:
                            </td>
                            <td class="py-2 text-dark">
                                {{ number_format(collect($batches)->sum('leftover'), 2) }}
                            </td>
                            <td colspan="2"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <style>
        .table-primary { background-color: #e9ecef !important; }
        .ps-5 { padding-left: 3rem !important; }
        .italic { font-style: italic; }
    </style>

    <script>
        $(document).ready(function() {
            // No DataTable initialization here because of the complex row structure, 
            // but we can add export buttons if needed manually.
        });
    </script>
@endsection
