<script>
    $(document).ready(function (){
        $('#req-payment-details').DataTable( {
            dom: 'lBfrtip',
            buttons: [
                { extend: 'excel', className: 'download-buttons'},
                { extend: 'pdf', className: 'download-buttons'},
            ]
        } );
    });
    var c = document.getElementsByClassName("cr");
    for (var i = 0; i < c.length; i++) {
        c[i].innerHTML = (i+1)+".";
    }

    function emptyCart(id){
        if(confirm('Are you sure to Empty this Cart?')){
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                method: 'PUT',
                url: '/empty-declared-cart/'+ id,
                data: id,
                success: function (res) {
                    if (res == 'error') {
                        alert('System is unable to perform the Requested action. Please Try Again!');
                    }else if(res == 'success'){
                        alert('Cart Successfully Emptied');
                        location.reload();
                    }
                }
            });
        }
        return false;
    }
</script>
<style>
    #req-payment-details th, td {
        text-align: left;
        padding: 5px;
    }
    #req-payment-details tr:hover {background-color: #999ebe;}
</style>
<div class="error" style="text-align: center;">On this Window, you can view a List of all Declared Certificates in this Cart</div>
<div class="guestDetails_inputDivider"></div>
<div style="text-align: center">
    <button onclick="emptyCart('{{$id}}')" type="button" class="navButtons" style="background-color: #ef112a; margin-bottom: 0; margin-left: 0"> <i class="fa fa-trash"></i> EMPTY CART</button>
</div>

<div class="guestDetails_inputDivider"></div>
<table id="req-payment-details" class="table-overflow" style="width: 100%; border-collapse: collapse; ">
    <thead>
    <tr style="background-color: #a1b5d3">
        <th>No</th>
        <th>Type</th>
        <th>Insurer</th>
        <th>Cert No.</th>
        <th>DN No.</th>
        <th>Insured Name</th>
        <th>KRA PIN</th>
        <th>Certificate Type</th>
        <th>Reg Number</th>
        <th>Chassis</th>
        <th>Value</th>
        <th>PLL</th>
        <th>Tonnage</th>
        <th>Engine No</th>
        <th>Make</th>
        <th>Model</th>
        <th>Body</th>
        <th>Policy Number</th>
        <th>Date Issued</th>
        <th>Period From</th>
        <th>Period To</th>
        <th>Allocated Amount</th>
        <th>Date Paid</th>
        <th>Mode of Payment</th>
        <th>Declaration Ref No.</th>
        <th>Certificate Status</th>
        <th>Notes</th>
    </tr>
    </thead>
    <tbody>
    @if($cart->payments != null)
        @foreach($cart->payments as $pv)
            @foreach($pv->allocationDetails($pv->id) as $row)
                {{--The $row variable is returning a List of all Certificates: Some of which are already partially Declared--}}
                @if($row->debit != null)
                    @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--}}
                            <tr style="padding: 4px; border-bottom: 2px solid black; border-top: 2px solid black">
                                <td class="cr"></td>
                                <td>
                                    <b>{!! strtoupper($row->debit->type) !!}</b>
                                </td>
                                <td>
                                    @if($row->debit->policy != null)
                                        @if($row->debit->policy->insurer != null)
                                            {{$row->debit->policy->insurer->nm}}
                                        @endif
                                    @endif
                                </td>
                                <td>
                                    <b class="error">{!! $row->cert_no !!}</b>
                                </td>
                                <td>
                                    <a href="{{url('policy/'.$row->debit->pl_cd.'?type=summary')}}" class="inner" ><b class="error">DN{{$row->debit->pl_cd}}</b></a>
                                </td>
                                <td >
                                    @if($row->policyMotor != null)
                                        @if($row->policyMotor->policy != null)
                                            @if($row->policyMotor->policy->client != null)
                                                {!! strtoupper($row->policyMotor->policy->client->nms) !!}
                                            @endif
                                        @endif
                                    @endif

                                </td>
                                <td>
                                    @if($row->policyMotor != null)
                                        @if($row->policyMotor->policy != null)
                                            @if($row->policyMotor->policy->client != null)
                                                {!! strtoupper($row->policyMotor->policy->client->pin) !!}
                                            @endif
                                        @endif
                                    @endif
                                </td>
                                <td >
                                    @if($row->policyMotor != null)
                                        @if($row->policyMotor->policy != null)
                                            @if($row->policyMotor->policy->productCategory != null)
                                                {!! $row->policyMotor->policy->productCategory->name !!} @if($row->policyMotor->policy->productSubCategory != null) {!! $row->policyMotor->policy->productSubCategory->name !!} @endif @if($row->policyMotor->policy->productMiniSubCategory != null)- {!! $row->policyMotor->policy->productMiniSubCategory->name !!} @endif
                                            @endif
                                        @endif
                                    @endif

                                </td>
                                <td ><b class="error">{!! $row->reg_no !!}</b></td>
                                <td>{!! $row->chassis !!}</td>
                                <td style="border-bottom: 2px solid black">{!! number_format($row->value) !!}</td>
                                <td style="border-bottom: 2px solid black">{{$row->pll ?? '-'}}</td>
                                <td style="border-bottom: 2px solid black">{{$row->tonnage ?? '-'}}</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 >{{date('d M Y H:s:i a', strtotime($row->created_at))}}</td>
                                <td >{{date('d M Y', strtotime($row->st))}}</td>
                                <td >{{date('d M Y', strtotime($row->ed))}}</td>
                                <td><b>{{number_format($partial->payment->amount,2)}}</b></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>
                                <td>
                                    @if($row->cert_status == 'cancelled')
                                        <b class="error">CANCELLED</b>
                                    @else
                                        <b class="success">Active</b>
                                    @endif
                                </td>
                                <td><b class="success">Partial Payment</b></td>
                            </tr>
                            @endif
                        @endforeach
                    @endif

                    {{--Here, we Display the Normal Full Allocations--}}
                    <tr style="padding: 4px; border-bottom: 2px solid black; border-top: 2px solid black">
                        <td class="cr"></td>
                        <td>
                            <b>{!! strtoupper($row->debit->type) !!}</b>
                        </td>
                        <td>
                            @if($row->debit->policy != null)
                                @if($row->debit->policy->insurer != null)
                                    {{$row->debit->policy->insurer->nm}}
                                @endif
                            @endif
                        </td>
                        <td>
                            <b class="error">{!! $row->cert_no !!}</b>
                        </td>
                        <td>
                            <a href="{{url('policy/'.$row->debit->pl_cd.'?type=summary')}}" class="inner" ><b class="error">DN{{$row->debit->pl_cd}}</b></a>
                        </td>
                        <td >
                            @if($row->policyMotor != null)
                                @if($row->policyMotor->policy != null)
                                    @if($row->policyMotor->policy->client != null)
                                        {!! strtoupper($row->policyMotor->policy->client->nms) !!}
                                    @endif
                                @endif
                            @endif

                        </td>
                        <td>
                            @if($row->policyMotor != null)
                                @if($row->policyMotor->policy != null)
                                    @if($row->policyMotor->policy->client != null)
                                        {!! strtoupper($row->policyMotor->policy->client->pin) !!}
                                    @endif
                                @endif
                            @endif
                        </td>
                        <td >
                            @if($row->policyMotor != null)
                                @if($row->policyMotor->policy != null)
                                    @if($row->policyMotor->policy->productCategory != null)
                                        {!! $row->policyMotor->policy->productCategory->name !!} @if($row->policyMotor->policy->productSubCategory != null) {!! $row->policyMotor->policy->productSubCategory->name !!} @endif @if($row->policyMotor->policy->productMiniSubCategory != null)- {!! $row->policyMotor->policy->productMiniSubCategory->name !!} @endif
                                    @endif
                                @endif
                            @endif

                        </td>
                        <td ><b class="error">{!! $row->reg_no !!}</b></td>
                        <td>{!! $row->chassis !!}</td>
                        <td style="border-bottom: 2px solid black">{!! number_format($row->value) !!}</td>
                        <td style="border-bottom: 2px solid black">{{$row->pll ?? '-'}}</td>
                        <td style="border-bottom: 2px solid black">{{$row->tonnage ?? '-'}}</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 >{{date('d M Y H:s:i a', strtotime($row->created_at))}}</td>
                        <td >{{date('d M Y', strtotime($row->st))}}</td>
                        <td >{{date('d M Y', strtotime($row->ed))}}</td>
                        <td><b>{{number_format($row->declared_amount,2)}}</b></td>
                        <td>{!! date('d/m/Y', strtotime($pv->pv_date)) !!}</td>
                        <td>
                            @if($pv->modeOfPayment != null)
                                <b class="error" > {{$pv->modeOfPayment->name}}  @if($pv->subModeOfPayment != null) - {{ $pv->subModeOfPayment->name}} @endif </b>
                            @endif
                        </td>
                        <td>{!! $pv->ref !!}</td>
                        <td>
                            @if($row->cert_status == 'cancelled')
                                <b class="error">CANCELLED</b>
                            @else
                                <b class="success">Active</b>
                            @endif
                        </td>
                        <td>
                            {!! $row->cert_notes." " !!} @if($row->debit->policy != null) {!! $row->debit->policy->end_cover_summary !!}@endif
                        </td>
                    </tr>
                @endif
            @endforeach
        @endforeach
    @endif
    </tbody>
    <tfoot>
    <tr style="background-color: #a1b5d3">
        <th>No</th>
        <th>Type</th>
        <th>Insurer</th>
        <th>Cert No.</th>
        <th>DN No.</th>
        <th>Insured Name</th>
        <th>KRA PIN</th>
        <th>Certificate Type</th>
        <th>Reg Number</th>
        <th>Chassis</th>
        <th>Value</th>
        <th>PLL</th>
        <th>Tonnage</th>
        <th>Engine No</th>
        <th>Make</th>
        <th>Model</th>
        <th>Body</th>
        <th>Policy Number</th>
        <th>Date Issued</th>
        <th>Period From</th>
        <th>Period To</th>
        <th>Allocated Amount</th>
        <th>Date Paid</th>
        <th>Mode of Payment</th>
        <th>Declaration Ref No.</th>
        <th>Certificate Status</th>
        <th>Notes</th>
    </tr>
    </tfoot>
</table>
