@extends ('layouts/template')

@section('content')
<div class="p-3 mcp-theme-1" id="patients-list">
    <div class="card">

        <div class="card-header px-2 py-1 d-flex align-items-center">
            <strong class="mr-4">
                <i class="fas fa-file-prescription"></i>
                ERx & Orders
            </strong>
        </div>

        <div class="card-body p-0">
            <div class="p-3">
                @include('app.mcp.erx_and_orders_filters')
            </div>
            <table class="table table-sm table-striped border-top p-0 m-0">
                <thead class="bg-light">
                    <tr>
                        <th class="border-0">Date</th>
                        <th class="border-0">Patient</th>
                        <th class="border-0">Category</th>
                        <th class="border-0">Content</th>
                        <th class="border-0">Status</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($erxAndOrders as $row)
                    <tr>
                        <td>{{ friendly_date_time($row->created_at) }}</td>
                        <td>
                            <a target="_blank" native href="{{route('patients.view.dashboard', $row->client)}}">
                                {{$row->client->displayName()}}
                            </a>
                        </td>
                        <td>{{ ucwords($row->erx_category) }}</td>
                        <td>
                            <?php $parsed = @$row->clinical_detail_json ? json_decode($row->clinical_detail_json) : false; ?>
                            @if($row->erx_category === 'DRUG')
                            <?php
                            $drugs = ($parsed && @$parsed->items) ? $parsed->items : [];
                            ?>
                            @foreach($drugs as $drug)
                            <div class="d-flex align-items-baseline mb-1">
                                <b class="mr-2">{{$drug->medication ?? '-'}}</b>
                                <span class="text-secondary mr-1">{{$drug->frequency ?? ''}}</span>
                            </div>
                            @endforeach
                            @elseif($row->erx_category === 'LAB' || $row->erx_category === 'IMAGING')
                            <?php
                            $tests = ($parsed && @$parsed->tests) ? $parsed->tests : [];
                            ?>
                            @foreach($tests as $test)
                            <div class="d-flex align-items-baseline mb-1">
                                <b class="mr-2">{{$test ?? '-'}}</b>
                            </div>
                            @endforeach
                            @elseif($row->erx_category === 'REFERRAL')
                            <b class="mr-2">{{@$parsed->to ?? '-'}}</b>
                            <span class="text-secondary mr-1">{{@$parsed->memo ?? '-'}}</span>
                            @elseif($row->erx_category === 'SUPPLY')
                            <b class="mr-2">{{@$parsed->item ?? '-'}}</b>
                            <span class="text-secondary mr-1">{{@$parsed->quantity ?? '-'}}</span>
                            @elseif($row->erx_category === 'OTHER')
                            <b class="mr-2">{{@$parsed->title ?? '-'}}</b>
                            <span class="text-secondary mr-1">{{@$parsed->memo ?? '-'}}</span>
                            @endif
                        </td>
                        <td>{{ $row->pro_declared_status ?: '-' }}</td>
                    </tr>
                    @endforeach

                    @if(count($erxAndOrders) === 0)
                    <tr>
                        <td colspan="5">No records found!</td>
                    </tr>
                    @endif
                </tbody>

            </table>

        </div>
    </div>
    <div class="mt-3">
        {{ $erxAndOrders->appends(request()->input())->links() }}
    </div>
</div>
@endsection