<style type="text/css">
.new-bill {
    width: 95px !important;
    height: auto !important;
    line-height: 1.5 !important;
    padding: 0.275rem 0.5rem !important;
}
</style>
<div class="table-responsive">
    <table id="example" class="table table-bordered align-middle" style="width:100%">
        <thead class="table-light">
            <tr>
                <th class="thbutton">Sl No</th>
                <th class="thbutton">IPD/CASE NO</th>
                <!-- <th class="thbutton">Admission Dt.</th> -->
                <th>Patient Name</th>
                <th>Consultant</th>
                <th class="text-center"><strong>Bed No.</strong></th>
                {{-- <th>TPA</th> --}}
                <th style="text-align:right">Bill Amount</th>
                <th class="thbutton">Balance</th>
                
                @if(auth()->user()->can('pathology-lab-billing-list'))
                 <th class="thbutton">Intent List</th>
                @endif
                {{-- @if(in_array('pathology-lab-billing-create', $authRolePermissions))
                    <th class="thbutton">Service</th>
                @endif --}}
                <th class="thbutton">Cons. Bill</th>
                {{-- <th class="thbutton">Info</th>--}}
                {{-- <th class="thbutton">Discharge</th> --}}
                @if(in_array('ipd-admission-edit', $authRolePermissions))
                    <th class="thbutton">Edit</th>
                @endif
                {{-- @if(in_array('ipd-admission-delete', $authRolePermissions))
                    <th class="thbutton">Delete</th>
                @endif --}}
            </tr>
        </thead>
        <tbody>
            @php $i=1;
            $Receive=[];
            @endphp
            @if(count($data)>0)
                @foreach($data as $key => $ipd)
                @if(isset($ipd->sale_invoices_name) && !empty($ipd->sale_invoice_id) && $ipd->sale_invoice_id!='')
                @php $Receive = TransactionCalculationReceiveAmount($ipd->sale_invoice_id,'IPD_INVOICE'); @endphp
                @endif
                <tr>
                    <td class="text-center">{{$data->firstItem() + $key}} </td>
                    <td class="text-center"><a href="{{ route('ipd-admissions.show', $ipd->id) }}"><strong>{{ $ipd->ipd_admission_no ?? '' }}</strong><!-- </a> <br> <small>{{ $ipd->ipd_file_no }}</small> --><br>
                        {!! @formatedDate($ipd->admission_date_time) !!}<br>
                        {!! @formatedTime($ipd->admission_date_time) !!}<br>
                        {!! ($ipd->casualty == 1) ? '<span class="badge bg-danger">Emergency</span>' : '' !!}
                    </td>
                    <td style="white-space: break-spaces;"><b>{!! @$ipd->contacts->name . ' ' . ($ipd->tpa_account_id != '' ? '<span class="badge bg-danger">TPA</span>' : '') !!}</b><br>{{ $ipd->contacts->account_contact->gender->name ?? '' }}, {{ (@$ipd->contacts->account_contact != null) ? age($ipd->contacts->account_contact->date_of_birth) . ' Years' : '' }}<br>{{ (@$ipd->contacts->account_contact != null) ? $ipd->contacts->account_contact->phone_no : '' }}</td> 
                    <td>
                        {!! $ipd->consultantHistory->consultants->account_title->name ?? '' !!}
                        {!! $ipd->consultantHistory->consultants->name ?? '' !!}
                        <!-- ({!! $ipd->consultantHistory->consultants->code ?? '' !!}) -->
                        @if(isset($ipd->prepared_by))
                        <div style="color:#aaa">By: {!! $ipd->prepared_by??'' !!}</div>
                        @endif
                    </td>

                    <td title="" class="text-center">
                        @if($ipd->bed=='')
                            <a type="button" class="btn-sm btn-danger text-white" id="createBed" data-bs-toggle="modal" data-admission-no="{!! $ipd->ipd_admission_no  !!}" data-patient-details="{!! $ipd->contacts->name  !!}" data-id="{!! $ipd->id  !!}" data-bs-target="#commonModal"><i class="bx bx-plus-circle"></i> Assign Bed</a>
                        @else
                            <span style="font-size: 15px;font-weight: bold;">{{ $ipd->bed ?? 'Not Assigned' }}</span>
                            <br>
                            <span style="border-top: 1px solid" class="text-secondary"><b>{!! $ipd->floor_name??'' !!}</b></span>
                        @endif

                    </td>
                    {{-- <td>{{($ipd->getTpa->name  ?? 'N/A')}} </td> --}}
                    <td style="text-align:right">
                        @if(isset($Receive['totalBilling']) && isset($ipd->sale_invoice_id) && !empty($ipd->sale_invoice_id) )
                        Total: {{ convertDecimelPoint($Receive['totalBilling']) ?? ''}}
                         @if(isset($Receive['dueAmount']) && $Receive['dueAmount']>0)
                            <br>
                            <span class="badge btn-secondary text-white mb-1"> Due: {{ convertDecimelPoint($Receive['dueAmount']) ?? ''}}</span>
                        @endif
                        @else
                        <b>Bill Not Generated</b>
                        @endif
                        <br>
                        @if(isset($Receive['receivedAmount']) && $Receive['receivedAmount']>0 && !empty($ipd->sale_invoice_id))
                        <a href="{!! route('billing-collection-list',['invoice_id'=>$ipd->sale_invoice_id,'module_code'=>'IPD_INVOICE']) !!}?redirect=ipd-admissions" class="mt-2"><b>Received: {{ convertDecimelPoint($Receive['receivedAmount']) ?? ''}}</b></a>
                        @endif
                    </td>
                   
                    <td class="text-center">
                        @if(isset($Receive['dueAmount']) && $Receive['dueAmount'] > 0)
                        <a href="{!! route('billing-collection',['invoice_id'=>$ipd->id,'voucher_type'=>'ipd-invoice']) !!}?redirect=ipd-admissions" class="btn-sm btn-success text-white"><i class="bi bi-coin"></i> Collect</a>
                        @elseif(isset($Receive['dueAmount']) && $Receive['dueAmount']==0)
                         <i class="bi bi-check-circle text-success" style="font-size: 30px;" title="Amount Collected."></i>
                        @endif
                    </td>
                    {{-- @if(in_array('pathology-lab-billing-create', $authRolePermissions))
                        @if(!is_null($ipd->sale_invoices_name))
                        <td class="text-center">
                            <a href="{{ route('ipd-billings.add-more', $ipd->id) }}" style="color: white" class="btn btn-info new-bill">
                                <i class="bx bx-plus text-white"></i> xMore
                            </a>
                        </td>
                        @else
                        <td class="text-center">
                            <a href="{{ route('ipd-billings.create', $ipd->id) }}"  class="btn btn-info new-bill">
                                <i class="bx bx-plus text-white"></i> Intent
                            </a>
                        </td>
                        @endif
                    @endif --}}

                     @if(auth()->user()->can('pathology-lab-billing-list'))
                        <td class="text-center">
                            @if(empty($ipd->sale_invoice_id))
                                <a href="{{ route('ipd-billings.index', $ipd->id) }}"  style="width: 90px; color: white; height: 29px;" class="btn btn-primary" title="All Bills"><i class="bi bi-plus-circle text-white"></i> New Intent</a>
                            @else
                                <a href="{{ route('ipd-billings.index', $ipd->id) }}"  style="width: 90px; color: white; height: 29px;" class="btn btn-primary" title="All Bills"><i class="bi bi-check-circle text-white"></i> Intents</a>
                            @endif

                        </td>
                    @endif

                    <td class="text-center">
                        <a href="{!! route('ipd-invoice', $ipd->id) !!}" class="btn btn-info">
                            <i class="bi bi-printer text-white"></i>
                        </a>
                    </td>

                    {{--<td class="text-center">
                        <a href="{!! route('ipd-certificate', $ipd->id) !!}" class="btn btn-secondary text-white">
                            <i class="bi bi-file-medical"></i> 
                        </a>
                    </td>--}}
                    
                    {{-- <td class="text-center">
                        @if(isset($ipd->ipd_admission_discharged_id))
                            <p class="btn btn-secondary" style="width: 46px;"> Yes</p>
                            @else
                            <p class="btn btn-success" style="width: 46px;"> No </p>
                        @endif

                    </td> --}}
                    @if(in_array('ipd-admission-edit', $authRolePermissions))
                    <td class="text-center">
                        <a href="{{ route('ipd-admissions.edit', $ipd->id) }}"  class="btn btn-warning text-white" title="Edit">
                            <i class="bi bi-pencil-square"></i>
                        </a>
                    </td>
                    @endif
                    {{-- @if(in_array('ipd-admission-delete', $authRolePermissions))
                    <td class="text-center">
                        <a data-url="{{route('ipd-admissions.destroy')}}"  data-id="{{$ipd->id}}" class="btn btn-danger deleteDataCustom" href="#" class="text-danger" data-bs-toggle="tooltip" data-bs-placement="bottom" title="Delete">
                            <i class="bi bi-trash text-white"></i>
                        </a>
                    </td>
                    @endif --}}
                </tr>
                @endforeach
            @else
                <tr>
                    <td colspan="11">No Data Found</td>
                </tr>
            @endif
        </tbody>
    </table>
</div>
<nav class="mt-2 row">
    <div class="col-lg-8 col-md-8 col-8 float-start">
    {!! $data->onEachSide(1)->render('ipd-admissions.pagination') !!}
    </div>
    <div class="col-lg-2 col-md-2 col-2" style="text-align: right;">
        <div class="btn-group">
            <a href="{{ route('bed.status') }}" class="btn btn-success"><i class="bi bi-file-earmark-medical"></i> Bed Status</a>
        </div>
    </div>
    <div class="col-lg-2 col-md-2 col-2 dataTables_length" id="example_length" style="text-align: right;">
        <i class="bi bi-funnel" style=" top: 11px;left: 20px;"></i>
        <select class="form-select sorting sorting-box" id="" name="example_length" aria-controls="example" style="padding-left: 30px;">
            <option value="10">Show: 10</option>
            <option value="30">Show: 30</option>
            <option value="50">Show: 50</option>
        </select>
    </div>
</nav>
</div>
