<script>
    $(document).ready(function (){
        $('.cart-type').select2();
        $('#insurers2').DataTable({
            dom: 'lBfrtip',
            buttons: [
                { extend: 'excel', className: 'download-buttons' },
            ],
            footerCallback: function ( row, data, start, end, display ) {
            var api = this.api(), data;
            var numberRenderer = $.fn.dataTable.render.number(',', '.', 2).display;

            // Remove the formatting to get integer data for summation
            var intVal = function ( i ) {
                return typeof i === 'string' ? i.replace(/[\.]/g, '.').replace(/[\,]/g, '') * 1 : typeof i === 'number' ? i : 0;
            };

            // Total over all pages
            $totalAgentNetBal = api.column( 18 ).data().reduce( function (a, b) {return intVal(a) + intVal(b)}, 0 );

            // Total over this page
            currentAgentNetBal = api.column( 18, { page: 'current'} ).data().reduce( function (a, b) {return intVal(a) + intVal(b)}, 0 );

            // Update footer
            $( api.column( 18 ).footer() ).html(numberRenderer(currentAgentNetBal) +'<br>('+ numberRenderer($totalAgentNetBal) +')').css('color', 'blue');
        },
        });
    });
    var c = document.getElementsByClassName("cr");
    for (var i = 0; i < c.length; i++) {
        c[i].innerHTML = (i+1)+".";
    }
</script>
@if(count($list) > 0)
    <h3 class="error">
        Motor Certificates Declaration Report for :{!! $regNo !!}
    </h3>
    <table id="insurers2"  class="table-overflow" style="width: 100%; border-collapse: collapse;">
        <thead>
        <tr style="background-color: #a1b5d3">
            <th>No</th>
            <th>Cert Number</th>
            <th>Cert Status</th>
            <th>Type</th>
            <th>Insured Name</th>
            <th>KRA PIN</th>
            <th>ID No.</th>
            <th>Reg Number</th>
            <th>PLL</th>
            <th>Tonnage</th>
            <th>Value</th>
            <th>Engine No</th>
            <th>Make</th>
            <th>Model</th>
            <th>Body</th>
            <th>Policy Number</th>
            <td>Date From</td>
            <td>Date To</td>
            <th>Declaration Amount</th>
            <th>Declaration Date</th>
            <th>Mode of Payment</th>
            <th>Declaration Code</th>
        </tr>
        </thead>
        <tbody>
        @foreach($list as $row)
            {{--If a Particular Certificate has Partial Payments, Display them First--}}
            @if($row->partialPayments != null)
                @foreach($row->partialPayments as $partial)
                    @if($partial->payment != null) {{--Here, we are Just sure that there exists a payment on this Partial Payment--}}
                    @if($row->debit != null)
                        <tr class="debit-row" style="padding: 4px; border-bottom: 2px solid black; border-top: 2px solid black">
                            <td class="cr"></td>
                            <td class="error"><b>{!! $row->cert_no ?? '-' !!}</b></td>
                            <td><b class="success">Partially Declared</b></td>
                            <td><b class="error">{!! strtoupper( $row->cert_type ?? '-') !!}</b></td>
                            <td><b>{!! $row->c_name ?? '-' !!}</b></td>
                            <td><b>{!! $row->kra ?? '-' !!}</b></td>
                            <td><b>{!! $row->id_no ?? '-' !!}</b></td>
                            <td><b class="error">{!! $row->reg_no ?? '-' !!}</b></td>
                            <td><b class="">{!! $row->pll ?? '-' !!}</b></td>
                            <td><b class="">{!! $row->tonnage  ?? '-' !!}</b></td>
                            <td><b class="">{!! number_format($row->value ?? 0) !!}</b></td>
                            <td style="border-bottom: 2px solid black">{{$row->engine ?? '-'}}</td>
                            <td style="border-bottom: 2px solid black">{{$row->make ?? '-'}}</td>
                            <td style="border-bottom: 2px solid black">{{$row->model ?? '-'}}</td>
                            <td style="border-bottom: 2px solid black">{{$row->body ?? '-'}}</td>
                            <td>{{$row->plcy_no}}</td>
                            <td> <b class="">{!! date('d/m/Y', strtotime($row->st)) !!}</b></td>
                            <td><b class="">{!! date('d/m/Y', strtotime($row->ed)) !!}</b></td>

                            <td style="font-weight: bold" class="error">{{ number_format($partial->payment->amount,2) }}</td>
                            <td>{!! $partial->payment->pv_date ?  date('d/m/Y', strtotime($partial->payment->pv_date)) : '-' !!}</td>
                            <td>
                                @if($partial->payment->modeOfPayment != null)
                                    <b class="error" > {{$partial->payment->modeOfPayment->name}}  @if($partial->payment->subModeOfPayment != null) - {{ $partial->payment->subModeOfPayment->name}} @endif </b>
                                @endif
                            </td>
                            <td>
                                @if($partial->pv_id != null)
                                    {!! $partial->payment->ref  !!}
                                @else
                                    <i class="error">Pending Payment</i>
                                @endif
                            </td>
                        </tr>
                    @endif
                    @endif
                @endforeach
            @endif

            {{--Original Certificate if Declared, then show it--}}
            @if($row->cert_declared == 1)
                <tr class="debit-row" style="padding: 4px; border-bottom: 2px solid black; border-top: 2px solid black">
                    <td style="border-bottom: 2px solid black" class="cr"></td>
                    <td style="border-bottom: 2px solid black" class="error"><b>{!! $row->cert_no ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b class="success">Declared</b></td>
                    <td style="border-bottom: 2px solid black"><b class="error">{!! strtoupper( $row->cert_type ?? '-') !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b >{!! $row->c_name ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b>{!! $row->kra ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b>{!! $row->id_no ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b class="error">{!! $row->reg_no ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"> <b class="">{!! $row->pll ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b class="">{!! $row->tonnage  ?? '-' !!}</b></td>
                    <td style="border-bottom: 2px solid black"> <b class="">{!! number_format($row->value ?? 0) !!}</b></td>
                    <td style="border-bottom: 2px solid black">{{$row->engine ?? '-'}}</td>
                    <td style="border-bottom: 2px solid black">{{$row->make ?? '-'}}</td>
                    <td style="border-bottom: 2px solid black">{{$row->model ?? '-'}}</td>
                    <td style="border-bottom: 2px solid black">{{$row->body ?? '-'}}</td>
                    <td style="border-bottom: 2px solid black">{{$row->plcy_no}}</td>
                    <td style="border-bottom: 2px solid black"> <b class="">{!! date('d/m/Y', strtotime($row->st)) !!}</b></td>
                    <td style="border-bottom: 2px solid black"><b class="">{!! date('d/m/Y', strtotime($row->ed)) !!}</b></td>
                    <td style="border-bottom: 2px solid black;font-weight: bold"  class="error">{{ number_format($row->declared_amount - $row->partialPayments->sum('amount'),2) }}</td>
                    <td style="border-bottom: 2px solid black">{!! $row->declaration_date ?  date('d/m/Y', strtotime($row->declaration_date)) : '-' !!}</td>
                    <td style="border-bottom: 2px solid black">
                        @if($row->requisitionDetails != null && $row->requisitionDetails->payment != null)
                            @if($row->requisitionDetails->payment->modeOfPayment != null)
                                <b class="error" > {{$row->requisitionDetails->payment->modeOfPayment->name}}  @if($row->requisitionDetails->payment->subModeOfPayment != null) - {{ $row->requisitionDetails->payment->subModeOfPayment->name}} @endif </b>
                            @endif
                        @endif
                    </td>
                    <td style="border-bottom: 2px solid black">
                        @if($row->requisitionDetails != null)
                            @if($row->requisitionDetails->payment != null)
                                {!! $row->requisitionDetails->payment->ref !!}
                            @else
                                <i class="error">Pending Payment</i>
                            @endif
                        @else
                            No req
                        @endif
                    </td>
                </tr>
            @endif
        @endforeach
        </tbody>
        <tfoot>
        <tr style="background-color: #a1b5d3">
            <th>No</th>
            <th>Cert Number</th>
            <th>Cert Status</th>
            <th>Type</th>
            <th>Insured Name</th>
            <th>KRA PIN</th>
            <th>ID No.</th>
            <th>Reg Number</th>
            <th>PLL</th>
            <th>Tonnage</th>
            <th>Value</th>
            <th>Engine No</th>
            <th>Make</th>
            <th>Model</th>
            <th>Body</th>
            <th>Policy Number</th>
            <td>Date From</td>
            <td>Date To</td>
            <th>Declaration Amount</th>
            <th>Declaration Date</th>
            <th>Mode of Payment</th>
            <th>Declaration Code</th>
        </tr>
        </tfoot>
    </table>
@else
    <p class="error" style="text-align: center">No Data Available. Please Check your Inputs!</p>
@endif
