<div class="print-content" id="result">
    <table class="table table-bordered table-condensed table-striped" id="tableId">
        <thead class="bg-dark">
            <tr >
                <th>
                    ID
                </th>
                <th>
                    Name
                </th>
                <th>
                    Pop
                </th>

                <th>
                    Due Amount
                </th>

                <th>
                    Paid Amount (With Discount)
                </th>

                <th>
                    Bill amount
                </th>


            </tr>
        </thead>
        @php
            $totalDue = 0;
            $totalPaid = 0;
            $totalBill = 0;
            $client_count = 0;
        @endphp
        <tbody class="bg-white divide-y divide-gray-200">

            @foreach ($clients as $client)

                @if (($client->generatedBill->sum('bill_amount') - ($client->paymentBill->sum('paid_amount') + $client->paymentBill->sum('discount_amount'))) > 0)
                    <tr>
                        <td>

                            <a class=" btn-info btn-block btn-xs text-nowrap clear"
                            href="{{ route('getPaymentHistory', $client->id) }}">CID: {{ $client->id }}</a>
                        </td>
                        <td>
                           Name:  {{ $client->clientsinfo->clients_name ?? '' }} <br>
                           User Name:  {{ $client->userid ?? '' }}
                        </td>
                        <td>
                            {{ $client->pop->popname ?? '' }}
                        </td>
                        <td>{{$client->generatedBill->sum('bill_amount') - ($client->paymentBill->sum('paid_amount') + $client->paymentBill->sum('discount_amount') )}} </td>
                        <td>
                            {{ $client->paymentBill->sum('paid_amount') + $client->paymentBill->sum('discount_amount') }}
                        </td>

                        <td>
                            {{ $client->generatedBill->sum('bill_amount') ?? "0" }}
                        </td>
                    </tr>
                    @php
                        $totalDue += $client->generatedBill->sum('bill_amount') - ($client->paymentBill->sum('paid_amount') + $client->paymentBill->sum('discount_amount'));
                        $totalPaid += $client->paymentBill->sum('paid_amount') + $client->paymentBill->sum('discount_amount');
                        $totalBill += $client->generatedBill->sum('bill_amount');
                        $client_count++;
                    @endphp
                @endif


            @endforeach

        </tbody>
        <tr class="text-bold">
            <td colspan="2">
                Total
            </td>
            <td>
                {{ $client_count }}
            </td>
            <td>
                {{ $totalDue }}
            </td>
            <td>
                {{ $totalPaid }}
            </td>
            <td>
                {{ $totalBill }}
            </td>
        </tr>
    </table>
</div>
