@php
    use Carbon\Carbon;
@endphp

<div class="content-wrapper">
    <div class="container-xxl flex-grow-1 container-p-y">

        <div class="card shadow">
            <div class="card-header text-uppercase custom-header bg-primary text-bg-info fw-bold">
                <i class="menu-icon ti ti-device-desktop-cog"></i> Manage Collection VS Deposit Reports
            </div>

            <div class="card-body">

                {{-- PDF button (optional) --}}
                <a href="{{ route('view_collection_vs_deposit_report') }}" class="mt-3" target="_blank">
                    <button class="btn btn-secondary">
                        PDF
                    </button>
                </a>

                {{-- Date Range Filter --}}
                <form method="GET" action="{{ route('collectionvsdeposit') }}" class="row g-3 mb-3 mt-3">
                    <div class="col-md-3">
                        <label class="form-label">Start Date</label>
                        <input type="date" name="start_date" class="form-control" value="{{ $startDate ?? '' }}">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">End Date</label>
                        <input type="date" name="end_date" class="form-control" value="{{ $endDate ?? '' }}">
                    </div>
                    <div class="col-md-3 align-self-end">
                        <button type="submit" class="btn btn-primary">Filter</button>
                        <a href="{{ route('collectionvsdeposit') }}" class="btn btn-secondary">Reset</a>
                    </div>
                </form>

                {{-- Collection vs Deposit Table --}}
                <table class="table table-sm table-striped table-bordered" id="collectionvsdeposittable">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>Date</th>
                            <th>User</th>
                            <th>Collection Amount</th>
                            <th>Deposit Amount</th>
                            <th>Difference</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php 
                            $count = 1;
                            $grandTotalCollection = 0;
                            $grandTotalDeposit = 0;
                            $grandTotalDifference = 0;
                        @endphp

                        @foreach ($reportData as $row)
                            @php
                                $userName = $getUser->firstWhere('id', $row->depositBy)?->name ?? 'N/A';
                                $difference = $row->totalCollectionAmount - $row->totalDepositAmount;

                                $grandTotalCollection += $row->totalCollectionAmount;
                                $grandTotalDeposit += $row->totalDepositAmount;
                                $grandTotalDifference += $difference;
                            @endphp
                            <tr>
                                <td>{{ $count++ }}</td>
                                <td>{{ $row->dateOnly }}</td>
                                <td>{{ $userName }}</td>
                                <td>{{ number_format($row->totalCollectionAmount, 2) }}</td>
                                <td>{{ number_format($row->totalDepositAmount, 2) }}</td>
                                <td>{{ number_format($difference, 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                    <tfoot>
                        <tr class="fw-bold bg-light">
                            <td colspan="3" class="text-end">Grand Total</td>
                            <td>{{ number_format($grandTotalCollection, 2) }}</td>
                            <td>{{ number_format($grandTotalDeposit, 2) }}</td>
                            <td>{{ number_format($grandTotalDifference, 2) }}</td>
                        </tr>
                    </tfoot>
                </table>

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