<div id="incoming-reports">
    @if(!request()->input('documentViewFromNote'))
    <div class="d-flex align-items-center pb-2">
        <h4 class="font-weight-bold text-secondary m-0 font-size-14">Incoming Reports</h4>
        <span class="mx-2 text-secondary">|</span>
        <div moe>
            <a start show class="">Upload</a>
            <form url="/api/incomingReport/create"
                  method="post"
                  enctype="multipart/form-data">
                <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                @if(request()->input('noteUid'))
                    <input type="hidden" name="noteUid" value="{{ request()->input('noteUid') }}">
                @endif
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Category</label>
                    <input type="text" class="form-control form-control-sm" name="category"
                           value="" placeholder="Category">
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Sub-category</label>
                    <input type="text" class="form-control form-control-sm" name="subcategory"
                           value="" placeholder="Sub-category">
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Report Date</label>
                    <input type="text" class="form-control form-control-sm stag-dp" name="reportDate"
                           value="" placeholder="Report Date">
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Title *</label>
                    <input type="text" class="form-control form-control-sm" name="title"
                           value="" placeholder="Title" required>
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">File *</label>
                    <input type="file" class="form-control form-control-sm" name="pdf"
                           value="" placeholder="File" required>
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Ordering Pro</label>
                    <select name="hcpProUid" provider-search data-pro-uid="{{$pro->uid}}" data-pro-name="{{$pro->displayName()}}"
                            class="form-control form-control-sm">
                    </select>
                </div>
                <div class="mb-2">
                    <label for="" class="text-sm text-secondary mb-1">Memo</label>
                    <input type="text" class="form-control form-control-sm" name="memo"
                           value="" placeholder="Memo">
                </div>
                <div class="d-flex align-items-center">
                    <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
                    <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                </div>
            </form>
        </div>
    </div>
    @endif
    <div class="d-flex align-items-start h-100">
        <div class="flex-grow-1">
            @if(!request()->input('documentViewFromNote'))
            <table class="table table-striped table-sm table-bordered mb-0">
                <thead class="bg-light">
                <tr>
                    <th class="border-0 text-secondary">Created</th>
                    <th class="border-0 text-secondary">Pro</th>
                    <th class="border-0 text-secondary">Title</th>
                    <th class="border-0 text-secondary">Report Date</th>
                    <th class="border-0 text-secondary">Sign</th>
                    <th class="border-0 text-secondary">Category</th>
                </tr>
                </thead>
                <tbody>
                @foreach($patient->incomingReports as $document)
                    <tr class="{{$document->is_entry_error ? 'entry-error' : ''}} {{@$currentReport->uid === $document->uid ? 'bg-aliceblue' : ''}}">
                        <td>
                            {{ friendly_date_time($document->created_at, false) }}
                            @if(request()->input('noteUid') && $document->note && $document->note->uid === request()->input('noteUid'))
                                <div class="mt-1 text-info text-sm">Created on this note</div>
                            @endif
                        </td>
                        <td>
                            {{$document->hcp ? $document->hcp->displayName() : '-'}}
                        </td>
                        <td>
                            @if(@$note)
                                <a open-in-stag-popup
                                   popup-style="stag-popup-md overflow-visible"
                                   mc-initer="incoming-reports"
                                   update-parent
                                   title="Incoming Report: {{ $document->title ? $document->title : '(not set)' }}"
                                   href="{{route('patients.view.incoming-reports', ['patient' => $patient, 'currentReport' => $document])}}?noteUid={{request()->input('noteUid') ?: ''}}&{{ @$note ? 'documentViewFromNote=1' : '' }}"
                                   class="{{@$currentReport->uid === $document->uid ? 'font-weight-bold' : ''}}">
                                    {{ $document->title ? $document->title : '(not set)' }}
                                </a>
                            @else
                                <a mc-initer="incoming-reports"
                                   update-parent
                                   href="{{route('patients.view.incoming-reports', ['patient' => $patient, 'currentReport' => $document])}}{{request()->input('noteUid') ? '?noteUid=' . request()->input('noteUid') : ''}}"
                                   class="{{@$currentReport->uid === $document->uid ? 'font-weight-bold' : ''}}">
                                    {{ $document->title ? $document->title : '(not set)' }}
                                </a>
                            @endif
                        </td>
                        <td>{{ friendly_date_time($document->report_date, false) }}</td>
                        <td>
                            @if(!$document->has_hcp_pro_signed)
                                -
                            @else
                                <div class="text-success mb-1">
                                    <i class="fa fa-check"></i>
                                    Signed
                                </div>
                                @if($document->hcp)
                                    <div class="text-secondary">
                                        <div class="text-sm font-weight-bold">{{$document->hcp->displayName()}}</div>
                                        <div class="text-sm">{{friendlier_date_time($document->hcp_pro_signed_at)}}</div>
                                    </div>
                                @endif
                            @endif
                        </td>
                        <td>{{ $document->category }}{{ $document->subcategory ? ' / ' . $document->subcategory : '' }}</td>
                    </tr>
                @endforeach
                @if(!count($patient->incomingReports))
                    <tr>
                        <td colspan="6">No records found.</td>
                    </tr>
                @endif
                </tbody>
            </table>
            @endif
        </div>
        @if(@$currentReport)
            <div class="{{ request()->input('documentViewFromNote') ? '' : 'min-width-700px max-width-700px ml-2' }} border align-self-stretch p-3">
                <div class="d-flex align-items-center">
                    <h3 class="font-size-16 m-0 flex-grow-1 text-ellipsis">{{$currentReport->title ? $currentReport->title : '(not set)'}}</h3>
                    <div class="d-inline-flex align-items-center ml-auto flex-nowrap mr-2 border-right">
                        <a class="on-hover-opaque mr-2 pr-1 d-inline-block" native target="_blank"
                           href="/api/incomingReport/download/{{ $currentReport->uid }}"
                           title="Download"><i class="fa fa-download"></i></a>
                        @if(!$currentReport->has_hcp_pro_signed && $currentReport->hcp_pro_id === $pro->id)
                            <div moe relative class="mr-2 pr-1">
                                <a href="#" start show class="" title="Sign"><i class="fa fa-signature text-success"></i></a>
                                <form url="/api/incomingReport/signAsHcp" right>
                                    <input type="hidden" name="uid" value="{{ $currentReport->uid }}">
                                    <p class="small">Are you sure you want to sign this report as HCP?</p>
                                    <div class="d-flex align-items-center">
                                        <button class="btn btn-sm btn-success mr-2" submit>Sign</button>
                                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                    </div>
                                </form>
                            </div>
                        @endif
                        @if(!$currentReport->has_hcp_pro_signed)
                            <div moe relative class="mr-2 pr-1">
                                <a href="#" start show class="on-hover-opaque" title="Edit"><i class="fa fa-edit text-primary"></i></a>
                                <form url="dummy" action="/api/incomingReport/updateBasic"
                                      right
                                      method="post"
                                      onsubmit="return submitIncomingReport(this)"
                                      enctype="multipart/form-data">
                                    <input type="hidden" name="uid" value="{{ $currentReport->uid }}">

                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">Category</label>
                                        <input type="text" class="form-control form-control-sm" name="category"
                                               value="{{$currentReport->category}}" placeholder="Category">
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">Sub-category</label>
                                        <input type="text" class="form-control form-control-sm" name="subcategory"
                                               value="{{$currentReport->subcategory}}" placeholder="Sub-category">
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">Report Date</label>
                                        <input type="date" class="form-control form-control-sm" name="reportDate"
                                               value="{{$currentReport->reportDate}}" placeholder="Report Date">
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">Title *</label>
                                        <input type="text" class="form-control form-control-sm" name="title"
                                               value="{{$currentReport->title}}" placeholder="Title" required>
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">File *</label>
                                        <input type="file" class="form-control form-control-sm" name="pdf"
                                               value="" placeholder="File">
                                        <span class="text-sm text-secondary">Leave blank if not changing</span>
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">HCP Pro</label>
                                        <select name="hcpProUid" provider-search
                                                data-pro-name="{{$currentReport->hcp ? $currentReport->hcp->displayName() : ''}}"
                                                class="form-control form-control-sm">
                                        </select>
                                    </div>
                                    <div class="mb-2">
                                        <label for="" class="text-sm text-secondary mb-1">Memo</label>
                                        <input type="text" class="form-control form-control-sm" name="memo"
                                               value="{{$currentReport->memo}}" placeholder="Memo">
                                    </div>

                                    <div class="d-flex align-items-center">
                                        <button class="btn btn-sm btn-primary mr-2" type="submit">Save</button>
                                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                    </div>
                                </form>
                            </div>
                            @if(!$currentReport->is_entry_error)
                                <div moe relative class="mr-2 pr-1">
                                    <a href="#" start show class="on-hover-opaque" title="Mark as entry-error"><i class="fa fa-ban text-danger"></i></a>
                                    <form url="/api/incomingReport/setIsEntryErrorToTrue" right>
                                        <input type="hidden" name="uid" value="{{ $currentReport->uid }}">
                                        <p class="small">Are you sure you want to set this report as entry error?</p>
                                        <div class="d-flex align-items-center">
                                            <button class="btn btn-sm btn-primary mr-2" submit>Save</button>
                                            <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            @else
                                <div moe relative class="mr-2">
                                    <a href="#" start show class="on-hover-opaque" title="Undo mark as entry-error"><i class="fa fa-undo"></i></a>
                                    <form url="/api/incomingReport/setIsEntryErrorToFalse" right>
                                        <input type="hidden" name="uid" value="{{ $currentReport->uid }}">
                                        <p class="small">Are you sure you want to set unset this report as entry error?</p>
                                        <div class="d-flex align-items-center">
                                            <button class="btn btn-sm btn-primary mr-2" submit>Save</button>
                                            <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                        </div>
                                    </form>
                                </div>
                            @endif
                        @endif
                    </div>
                    @if(!request()->input('documentViewFromNote'))
                    <a class="ml-auto" mc-initer="incoming-reports" href="{{route('patients.view.incoming-reports', ['patient' => $patient, 'currentReport' => null])}}{{request()->input('noteUid') ? '?noteUid=' . request()->input('noteUid') : ''}}">
                        <i class="fa fa-times-circle on-hover-opaque"></i>
                    </a>
                    @endif
                </div>
                <hr class="mt-3 mb-0 row">
                <div class="pdf-viewer-auto row px-3" pdf-url="/api/incomingReport/download/{{ $currentReport->uid }}"></div>
            </div>
        @endif
    </div>
</div>
@include('app/pdf/viewer')
<script>
    (function() {
        function init() {
            $('#incoming-reports').find('input.stag-dp')
                .prop('readonly', true)
                .addClass('bg-white')
                .datepicker({
                    dateFormat: 'yy-mm-dd'
                });
            runMCInitializer('inline-pdf-viewer');
        }
        addMCInitializer('incoming-reports', init, '#incoming-reports')
    }).call(window);
</script>