<div class="pathology-bill">
    <div class="row">
      <div class="col-2" style="width: 20%">
          <p>Date : {!! formatedDate(date('Y-m-d')) !!}</p>
      </div>
      <div class="col-7" style="text-align: center;">
          <p style="font-size: 14px;"><b>REFERRAL STATEMENT @if(!empty($labs) && isset( $labs->organization )) OF LAB {!! !empty($labs) && isset( $labs->organization ) ? $labs->organization : '' !!} @endif</b></p>
          <p style="padding-bottom: 5px;font-size: 11px;"><b><span style="color: #698aff;">Referral</span> List</b>
          <b>From <span style="color:#698aff;" id="">{!! formatedDate(date('Y-m-d', strtotime($start_date))) !!}</span> TO <span style="color:#698aff;" id="">{{formatedDate($end_date)}}</span>
                      </b></p>
      </div>
      <div class="col-2" style="text-align: right; width:20%">
          <p>Time : {!! formatedTime(date('H:i:s')) !!}</p>
      </div>
    </div>
     <div class="table-responsive">
        <table id="example" class="table table-bordered align-middle" style="width:100%">
            <thead class="table-light">
                <tr>
                    <!-- <th>Date</th>
                    <th>INV NO</th>
                    <th>Patient Name</th> -->
                    <th>Sl no</th>
                    @if($statement_by == 'patient')
                    <th>Invoice No.</th>
                    <th>Invoice Date</th>
                    <th>Patient Name</th>
                    @endif
                    <th>Item/Service</th>
                    <th>Category</th>
                    <th>Type</th>
                    <th style="text-align: right;">Quantity</th>
                    <th style="text-align: right;">Percentage</th>
                    <th style="text-align: right;">Basic Amount</th>
                    <th style="text-align: right;">Total Amount</th>
                </tr>
            </thead>
            <tbody>
                @php $auth_format = \Session::get('company_data')['date_format'] ?? ''; @endphp
                @php $i=1; $total_comission = 0; @endphp
                @if(isset($data) && count($data)>0)
                    @foreach($data as $key=> $row)
                        <tr>
                            {{-- <td>{{ formatedDate($row['sale_invoice_details']->created_at) }}</td>
                            <td>{{ $row['sale_invoice_details']->main_invoice_no }}</td>
                            <td>
                                {{ $row['sale_invoice_details']->patient_name }}<br>
                                ({{ $row['sale_invoice_details']->patient_code }})
                            </td>--}}
                            <td>{{ $i }}</td>
                            @if($statement_by == 'patient')
                                <td>{{ $row['sale_invoice_details']->voucher_type_invoice_no }}</td>
                                <td>{{ formatedDate($row['sale_invoice_details']->invoice_date) }}</td>
                                <td>{{ $row['sale_invoice_details']->patient_title }} {{ $row['sale_invoice_details']->patient_name }}</td>
                            @endif
                            <td>{{ $row['sale_invoice_details']->product }}</td>
                            <td>{{ $row['sale_invoice_details']->brand }}</td>
                            @php
                                $amount     = $row['sale_invoice_details']->basic_amount ?? 0 ;
                                $discount   = !empty($row['sale_invoice_details']->discount_amount) ? $row['sale_invoice_details']->discount_amount :
                                    (!empty($row['sale_invoice_details']->sale_discount_amount) ? $row['sale_invoice_details']->sale_discount_amount : '0');

                                $amount     = $amount - $discount;
                                $type = '';
                                $price = 0 ;

                                if (isset($row['incentive_slab_details']) && !empty($row['incentive_slab_details']->calculation_type)) {
                                    $type       = isset($row['incentive_slab_details']) &&
                                        isset($row['incentive_slab_details']->calculation_type) ? $row['incentive_slab_details']->calculation_type : 0 ;

                                    $percentage = isset($row['incentive_slab_details']) &&
                                        isset($row['incentive_slab_details']->percentage) ? $row['incentive_slab_details']->percentage : 0 ;

                                    $price      = isset($row['incentive_slab_details']) &&
                                        isset($row['incentive_slab_details']->price) ? $row['incentive_slab_details']->price : 0 ;
                                } else if (!empty($incentive_slab) && !empty($incentive_slab->calculation_type)) {
                                    $type       = $incentive_slab->calculation_type ?? '' ;
                                    $percentage = $incentive_slab->percentage ?? 0 ;
                                    $price      = $incentive_slab->price ?? 0 ;
                                }

                                $commission = 0 ;
                                $referral_count = $row['sale_invoice_details']->referral_count ?? 1 ;

                                if ($type == 'quantity') {
                                    $commission = $referral_count * $price;
                                } else if ($type == 'percentage') {
                                    $commission = ($percentage / 100) * $amount;
                                }
                            @endphp
                            <td>{{ ucfirst($type) }}</td>
                            <td style="text-align: right;">{{ $row['sale_invoice_details']->referral_count }}</td>
                            <td style="text-align: right;">{{ !empty($percentage) ? $percentage.'%' : $price }}</td>
                            <td style="text-align: right;">{{ $amount }}</td>
                            <td style="text-align: right;">{{ $commission }}</td>
                        </tr>
                        @php $i++; $total_comission += $commission; @endphp
                    @endforeach
                        <tr>
                            <td colspan="{!! $statement_by == 'patient' ? '10' : '7' !!}"><b>Total</b></td>
                            <td style="text-align: right;"><b>{!! $total_comission !!}</b></td>
                        </tr>
                @else
                    <tr>
                        <td colspan="10">No Data Found</td>
                    </tr>
                @endif
            </tbody>
        </table>
    </div>
    <nav class="float-start mt-2">
        <ul class="pagination">
            {{-- {!! $data->onEachSide(0)->render('financialyear.pagination') !!} --}}
        </ul>
    </nav>
</div>