<script>
    $(document).ready(function (){
        $('#insurers2').DataTable({
            dom: 'lBfrtip',
            buttons: [
                { extend: 'excel', className: 'download-buttons' },
            ]
        } );
        /**If Allocated Amount is Entered, start to Sum Up values so that we can display the total Amount to be Allocated **/
        $(document).on('input paste change',".refundablePremium",function (){
            var sum = 0;
            $('.refundablePremium').each(function() {
                sum += Number($(this).val());
            });
            $('#total-alloc-amount-entered').val(sum).addClass('error').css('font-weight','bold').attr('readonly', false);
            if(sum > 0){
                $('#total-alloc-amount-entered').attr('readonly', true);
            }
        });

        /**Anytime We Click on the Item on the left, activate the field to the right to allow enter amount to Remit**/
        $('.activatorAlloc').click(function (){
            var selectedCert = $(this).val();
            let declaredAmount = 0;
            if($(this).is(':checked')){
                $('#'+selectedCert).attr('disabled', false);
            }else{
                $('#'+selectedCert).attr('disabled', true).val('').trigger('change');
            }
        });
    });
    var c = document.getElementsByClassName("cr");
    for (var i = 0; i < c.length; i++) {
        c[i].innerHTML = (i+1)+".";
    }
</script>
@if($plcy != null)
    <table id="insurers2"  class="table-overflow" style="width: 100%; border-collapse: collapse;">
        <thead>
        <tr style="background-color: #a1b5d3">
            <th>No</th>
            <th>Insured</th>
            <th>Class of Insurance</th>
            <th>Date From</th>
            <th>Date To</th>
            <th>Total Premium</th>
            <th>Paid to Us</th>
            <th>Paid to Insurer</th>
            <th>Credit Notes</th>
            <th>Policy Balance</th>
            <th>Policy Status</th>
            <th>Activator</th>
            <th>Refund Premium</th>
        </tr>
        </thead>
        <tbody>
        <tr class="debit-row">
            <td class="cr"></td>
            <td>
                @if($plcy->client != null)
                    {!! $plcy->client->nms !!}
                @else
                    <i class="error">--Details Not Found--</i>
                @endif
            </td>
            <td>
                @if($plcy->policyIndividualItems($plcy->pl_cd) != "")
                    {!! $plcy->policyIndividualItems($plcy->pl_cd)  !!}
                @endif
            </td>
            <td >{{date('d/m/Y', strtotime($plcy->st))}}</td>
            <td >{{date('d/m/Y', strtotime($plcy->ed))}}</td>
            <td ><b>{{number_format($plcy->total_premium, 2)}}</b></td>
            <td ><b class="error">
                    @if($plcy->allocatedReceipt != null)
                        {{number_format($plcy->allocatedReceipt->sum('amount'), 2)}}
                    @else
                        0.00
                    @endif
                </b>
            </td>
            <td >
                <b class="error">
                    @if($plcy->allocatedDirectPayments != null)
                        {{number_format($plcy->allocatedDirectPayments->sum('amount'), 2)}}
                    @else
                        0.00
                    @endif
                </b>
            </td>
            <td><b class="error">{!! number_format($plcy->policyApprovedCreditNotes($plcy->pl_cd),2) !!}</b></td>
            <td ><b>{{number_format($plcy->policyBalance($plcy->pl_cd), 2)}}</b></td>
            <td>
                @if($plcy->currentPolicyStatus($plcy->pl_cd) != 'Active')
                    <b class="error">{!! $plcy->currentPolicyStatus($plcy->pl_cd) !!}</b>
                @else
                    <b class="success">Active</b>
                @endif
            </td>
            @if($plcy->requisitionDetails != null)
                <td><i class="error">Already Requisitioned</i></td>
            @else
                <td>{!! Form::checkbox('activator['.$plcy->pl_cd.']', $plcy->pl_cd, false,['class'=>'activatorAlloc']) !!}</td>
            @endif
            <td >
                {!! Form::hidden('pl_cd['.$dnNo.']', $dnNo) !!}
                @if($plcy->policyBalance($plcy->pl_cd) < 0)
                    {!! Form::number('refundable-premium['.$dnNo.']', '', ['class'=>'input-text-height refundablePremium', 'id'=>$dnNo,'style'=>'border:1px solid green; width:80px','disabled'=>true]) !!}
                @else
                    <i class="error">--Policy has Positive Balance--</i>
                @endif
            </td>
        </tr>
        </tbody>
        <tfoot>
        <tr style="background-color: #a1b5d3">
            <th>No</th>
            <th>Insured</th>
            <th>Class of Insurance</th>
            <th>Date From</th>
            <th>Date To</th>
            <th>Total Premium</th>
            <th>Paid to Us</th>
            <th>Paid to Insurer</th>
            <th>Credit Notes</th>
            <th>Policy Balance</th>
            <th>Policy Status</th>
            <th>Activator</th>
            <th>Refund Premium</th>
        </tr>
        </tfoot>
    </table>
@else
    <p class="error" style="text-align: center">No Data Available. Please Check your Inputs!</p>
@endif

