@if($policies != null)
    <script>
        $(document).ready(function (){
            $('#available-policies').dataTable();
            $('.activator').click(function (){
                let pl_cd = $(this).val();
                $.ajax({
                    method: 'GET',
                    url: '/search-debit-note/'+pl_cd+'/debit-note',
                    data: pl_cd,
                    success: function (res){
                        if(res != ""){
                            $('#policy-details').show();
                            $('#policy-details').html(res);
                        }else{
                            $('#policy-details').hide();
                        }
                    }
                });
                return false;
            });
        });
        var a = document.getElementsByClassName("pr");
        for (var i = 0; i < a.length; i++) {
            a[i].innerHTML = (i+1)+".";
        }
    </script>
    <div class="guestDetails_inputDivider"></div>
    <div class="inputDivider" style="background-color: lightcyan">
        <div class="fields">
            <div style="text-align: center"><p><b class="error">Available Policies: Please select a Particular Policy you wish to Endorse on the below List</b></p></div>
            <table id="available-policies" style="width: 100%; border-collapse: collapse">
                <thead>
                <tr style="padding: 5px">
                    <th>No</th>
                    <th>DN Number</th>
                    <th>Class of Insurance</th>
                    <th>Item(s)</th>
                    <th>Start Date</th>
                    <th>End Date</th>
                    <th>Selector</th>
                </tr>
                </thead>
                <tbody>
                @foreach($policies as $row)
                    <tr style="padding: 5px; border-top: 2px solid black ;border-bottom: 2px solid black">
                        <td class="pr" style="padding: 6px;"></td>
                        <td><b class="error">DN{{$row->pl_cd}}</b></td>
                        <td>
                            @if($row->product != null)
                                {{$row->product->lgnm}}
                                @if($row->prod_type != null)
                                    ({{$row->prod_type}})
                                @endif
                                @if($row->productCategory != null)
                                    - {{$row->productCategory->name}}
                                    @if($row->productSubCategory != null)
                                        {{$row->productSubCategory->name}}
                                        @if($row->productMiniSubCategory != null)
                                            - <b>{{$row->productMiniSubCategory->name}}</b>
                                        @endif
                                    @endif
                                @endif
                            @endif
                        </td>
                        <td> <b>{!! $row->policyItems($row->pl_cd) !!}</b></td>
                        <td>{{date('d/m/Y', strtotime($row->st))}}</td>
                        <td>{{date('d/m/Y', strtotime($row->ed))}}</td>
                        <td>
                            {!! Form::checkbox('pl_cd', $row->pl_cd, false,['class'=>'activator']) !!}
                        </td>
                    </tr>
                @endforeach
                </tbody>

            </table>
        </div>

    </div>


@else
    @if($plcy != null)
        <script>
            $(document).ready(function (){
                $('#yes').on('click', function (){
                    endorsePolicy({{$plcy->pl_cd}});
                });
            });
        </script>
        <div class="guestDetails_inputDivider"></div>
        <div class="inputDivider" style="background-color: lightcyan">
            <div class="fields">
                <div style="text-align: center"><p><b class="error">Policy Particulars:</b></p></div>
                <table style="width: 100%; border-collapse: collapse;">
                    <tr style="padding: 5px">
                        <th>Class of Insurance</th>
                        <th>Insurance Company</th>
                        <th>Client Name</th>
                        <th>Period From</th>
                        <th>Period To</th>
                        <th>Item(s)</th>
                    </tr>
                    <tr style="border-top: 2px solid black ;border-bottom: 2px solid black">
                        <td style="padding: 6px;">
                            @if($plcy->product != null)
                                {{$plcy->product->lgnm}}
                                @if($plcy->prod_type != null)
                                    ({{$plcy->prod_type}})
                                @endif
                                @if($plcy->productCategory != null)
                                    - {{$plcy->productCategory->name}}
                                    @if($plcy->productSubCategory != null)
                                        {{$plcy->productSubCategory->name}}
                                        @if($plcy->productMiniSubCategory != null)
                                            - <b>{{$plcy->productMiniSubCategory->name}}</b>
                                        @endif
                                    @endif
                                @endif
                            @endif
                        </td>
                        <td>
                            @if($plcy->insurer != null)
                                {!! strtoupper($plcy->insurer->nm) !!}
                            @endif
                        </td>
                        <td>
                            @if($plcy->client != null)
                                {!! strtoupper($plcy->client->nms) !!}
                            @endif
                        </td>
                        <td>{!! date('d/m/Y', strtotime($plcy->st)) !!}</td>
                        <td>{!! date('d/m/Y', strtotime($plcy->ed)) !!}</td>
                        <td> <b>{!! $plcy->policyItems($id) !!}</b></td>
                    </tr>
                </table>
            </div>
        </div>

        @if($plcy->first_confirmed != 0)
            @if($allowEndorsement != 'no')
                <div class="guestDetails_inputDivider"></div>
                <div style="text-align: center">
                    <p class="error"><b>Do you agree to continue and Endorse this Policy?</b></p>
                    Yes <input type="radio" name="agree" id="yes">
                    <span style="margin-left: 100px">No  <input type="radio" name="agree" id="no"></span>
                </div>
            @else
                <div class="guestDetails_inputDivider"></div>
                <div style="text-align: center"><p class="error"><b>Entered/Selected Debit Note cannot be Endorsed because Policy has been {!! $policyStatus !!}</b></p></div>
            @endif

        @else
            <div class="guestDetails_inputDivider"></div>
            <div style="text-align: center"><p class="error"><b>Entered/Selected Debit Note is not yet Authorized. You cannot Endorse it!</b></p></div>
        @endif

    @else
        <div class="guestDetails_inputDivider"></div>
        <div style="text-align: center"><p class="error"><b>No Record Found! Check Debit Note Number Entered exists in the System</b></p></div>
    @endif
@endif

