<div class="mb-2">
    <button class="btn btn-primary" onclick="export2csv()">Download CSV</button>
</div>

<div class="col-md-12">
    <div class="badge badge-info float-right m-1">
        Total Row: {{ $list->count() }}
    </div>
    <div class="badge badge-info float-right m-1">
        Total Amount: {{ number_format($list->sum('amount'), 2) }} TK
    </div>

    @can('show_commission_reseller_balance_log')
        <div class="badge badge-info float-right m-1">
            Total Manager Miscellaneous Expense: {{ number_format($list->sum('commission'), 2) }} TK
        </div>
    @endcan
</div>

<div class="table-responsive">
    <table class="table table-bordered table-striped table-condensed" id="tableId">
        <thead class="bg-blue-50">
            <tr>
                <th>ID</th>
                <th>Date</th>
                <th>Reseller Name</th>

                @can('reseller_balance_log_with_pop')
                    <th>Pop Name</th>
                @endcan

                <th>User Name</th>
                <th>Action</th>
                <th class="text-right">Amount</th>

                @can('show_commission_reseller_balance_log')
                    <th class="text-right">Manager Expense</th>
                @endcan

                @can('show_commission_percentage_reseller_balance_log')
                    <th class="text-right">Manager Expense (%)</th>
                @endcan

                @if (checkSettings('ResellerClientOnlineRecharge') == 'enable')
                    <th class="text-right">Client Bill</th>
                @endif

                <th class="text-right">Remarks</th>
            </tr>
        </thead>

        <tbody>
            @php
                $total = 0;
                $manager_commission = 0;
                $total_client_bill = 0;
            @endphp

            @foreach ($list as $r)
                @php
                    $amount = $r->amount ?? 0;
                    $total += $amount;

                    $percentageCommission =
                        $amount > 0 && ($r->reseller_commission_percentage ?? 0) > 0
                            ? ($amount * $r->reseller_commission_percentage) / 100
                            : 0;

                    $manager_commission += $percentageCommission;

                    $clientbill = 0;
                    if (
                        checkSettings('setClientPaymentAmountToClient') == 'enable' &&
                        optional($r->client)->client_payment_amount > 0
                    ) {
                        $clientbill = $r->client->client_payment_amount;
                    } else {
                        $clientbill = optional(optional($r->client)->packages)->client_payment_amount ?? 0;
                    }
                    $total_client_bill += $clientbill;
                @endphp

                <tr>
                    <td>{{ $r->id }}</td>
                    <td>{{ $r->created_at->format('d-M-Y h:i:sa') }}</td>
                    <td>{{ $r->reseller->name ?? '' }}</td>

                    @can('reseller_balance_log_with_pop')
                        <td>{{ $r->pop->popname ?? '' }}</td>
                    @endcan

                    <td>{{ $r->client->userid ?? '' }}</td>
                    <td>{{ $r->action ?? '' }}</td>
                    <td class="text-right">{{ number_format($amount, 2) }}</td>

                    @can('show_commission_reseller_balance_log')
                        <td class="text-right">{{ number_format($r->commission ?? 0, 2) }}</td>
                    @endcan

                    @can('show_commission_percentage_reseller_balance_log')
                        <td class="text-right">{{ number_format($percentageCommission, 2) }}</td>
                    @endcan

                    @if (checkSettings('ResellerClientOnlineRecharge') == 'enable')
                        <td class="text-right">{{ number_format($clientbill, 2) }}</td>
                    @endif

                    <td class="text-right">{{ $r->remarks }}</td>
                </tr>
            @endforeach

            {{-- TOTAL ROW --}}
            <tr class="font-weight-bold bg-light">
                <td colspan="6" class="text-right">Total</td>
                <td class="text-right">{{ number_format($total, 2) }}</td>

                @can('show_commission_reseller_balance_log')
                    <td class="text-right">{{ number_format($list->sum('commission'), 2) }}</td>
                @endcan

                @can('show_commission_percentage_reseller_balance_log')
                    <td class="text-right">{{ number_format($manager_commission, 2) }}</td>
                @endcan

                @if (checkSettings('ResellerClientOnlineRecharge') == 'enable')
                    <td class="text-right">{{ number_format($total_client_bill, 2) }}</td>
                @endif

                <td></td>
            </tr>
        </tbody>
    </table>
</div>
