@extends ('layouts.patient')
@section('inner-content')
    <div>
        <div class="d-flex align-items-center pb-2">
            <h4 class="font-weight-bold m-0">Incoming Reports</h4>
            <span class="mx-2 text-secondary">|</span>
            <div moe>
                <a start show class="">Upload</a>
                <form url="dummy" action="/api/incomingReport/create"
                      method="post"
                      onsubmit="return submitIncomingReport(this)"
                      enctype="multipart/form-data">
                    <input type="hidden" name="clientUid" value="{{ $patient->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="" 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="date" class="form-control form-control-sm" 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">HCP 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="submit">Save</button>
                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                    </div>
                </form>
            </div>
        </div>
        <div class="d-flex align-items-start h-100">
            <div class="flex-grow-1">
                <table class="table table-striped table-sm table-bordered mb-0">
                    <thead>
                    <tr>
                        <th class="px-2 text-secondary">Created</th>
                        <th class="px-2 text-secondary">Title</th>
<!--                        <th class="px-2 text-secondary">Report</th>-->
                        <th class="px-2 text-secondary">Sign</th>
                        <th class="px-2 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 class="px-2">{{ friendly_date_time($document->created_at, false) }}</td>
                            <td class="px-2">
                                <a href="{{route('patients.view.incoming-reports', ['patient' => $patient, 'currentReport' => $document])}}"
                                   class="{{@$currentReport->uid === $document->uid ? 'font-weight-bold' : ''}}">
                                    {{ $document->title ? $document->title : '(not set)' }}
                                </a>
                            </td>
{{--                            <td class="px-2">--}}
{{--                                <div class="d-flex align-items-center">--}}
{{--                                    <a class="pdf-viewer-trigger" native target="_blank"--}}
{{--                                       href="/api/incomingReport/download/{{ $document->uid }}"--}}
{{--                                       title="View">View</a>--}}
{{--                                    @if($document->is_entry_error)--}}
{{--                                        <span class="ml-auto text-danger on-hover-opaque" title="Entry Error">--}}
{{--                                    <i class="fa fa-exclamation-triangle"></i>--}}
{{--                                </span>--}}
{{--                                    @endif--}}
{{--                                </div>--}}
{{--                            </td>--}}
                            <td class="px-2">
                                @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 class="px-2">{{ $document->category }}{{ $document->subcategory ? ' / ' . $document->subcategory : '' }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
            @if(@$currentReport)
                <div class="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 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 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>
                        <a class="ml-auto" href="{{route('patients.view.incoming-reports', ['patient' => $patient, 'currentReport' => null])}}">
                            <i class="fa fa-times-circle on-hover-opaque"></i>
                        </a>
                    </div>
                    <hr class="mt-3 mb-0 row">
                    <?php /*
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Reason</label>
                        <div moe class="d-block">
                            <a start show>{{$supplyOrder->reason ? $supplyOrder->reason : '(not set)'}}</a>
                            <form url="/api/supplyOrder/updateReason">
                                <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Reason *</label>
                                    <input type="text" class="form-control form-control-sm" required
                                           name="reason" value="{{$supplyOrder->reason}}">
                                </div>
                                <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>
                    </div>
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Address</label>
                        <div moe class="d-block">
                            <a start show>
                                {{$supplyOrder->mailing_address_full ? implode(" ", [$supplyOrder->mailing_address_line1, $supplyOrder->mailing_address_line2, $supplyOrder->mailing_address_city, $supplyOrder->mailing_address_state, $supplyOrder->mailing_address_zip]) : '(not set)'}}
                            </a>
                            <form url="/api/supplyOrder/updateMailingAddress">
                                <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary text-sm">Address Line1</label>
                                    <input type="text" class="form-control form-control-sm" name="mailingAddressLine1" value="{{$supplyOrder->mailing_address_line1}}">
                                </div>
                                <div class="mb-2">
                                    <label class="text-secondary text-sm">Address Line2</label>
                                    <input type="text" class="form-control form-control-sm" name="mailingAddressLine2" value="{{$supplyOrder->mailing_address_line2}}">
                                </div>
                                <div class="mb-2">
                                    <label class="text-secondary text-sm">Address City</label>
                                    <input type="text" class="form-control form-control-sm" name="mailingAddressCity" value="{{$supplyOrder->mailing_address_city}}">
                                </div>
                                <div class="mb-2">
                                    <label class="text-secondary text-sm">Address State</label>
                                    <input type="text" class="form-control form-control-sm" name="mailingAddressState" value="{{$supplyOrder->mailing_address_state}}">
                                </div>
                                <div class="mb-2">
                                    <label class="text-secondary text-sm">Address Zip</label>
                                    <input type="text" class="form-control form-control-sm" name="mailingAddressZip" value="{{$supplyOrder->mailing_address_zip}}">
                                </div>
                                <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>
                    </div>
                    <div class="mb-3 d-none">
                        <label class="text-secondary text-sm mb-1">Requested Ship Date</label>
                        <div moe class="d-block">
                            <a start show>{{$supplyOrder->requested_date ? friendlier_date_time($supplyOrder->requested_date) : '(not set)'}}</a>
                            <form url="/api/supplyOrder/updateRequestedDate">
                                <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Requested Ship Date</label>
                                    <input type="date" class="form-control form-control-sm"
                                           name="requestedDate" value="">
                                </div>
                                <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>
                    </div>
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Client Understanding Memo</label>
                        <div moe class="d-block">
                            @if($supplyOrder->client_understanding_memo)
                                <a start show>{{$supplyOrder->client_understanding_memo}}</a>
                            @else
                                <a start show>(not set)</a>
                            @endif
                            <form url="/api/supplyOrder/updateClientUnderstandingMemo">
                                <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Client Understanding Memo</label>
                                    <input type="text" class="form-control form-control-sm"
                                           name="clientUnderstandingMemo" value="{{$supplyOrder->client_understanding_memo}}">
                                </div>
                                <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>

                    </div>
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Pro Signature</label>
                        <div class="d-flex align-items-center">
                            @if($supplyOrder->is_signed_by_pro)
                                <div class="text-info">
                                    Signed by <b>{{$supplyOrder->signedPro->displayName()}}</b>
                                    on {{friendlier_date_time($supplyOrder->pro_signed_at)}}
                                </div>
                            @else
                                <div class="text-warning-mellow font-weight-bold">Not Signed</div>
                                <div class="ml-3">
                                    <div moe>
                                        <a start show class="py-0">Sign</a>
                                        <form url="/api/supplyOrder/signAsPro">
                                            <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                            <p class="small">Sign this supply order?</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>
                                </div>
                            @endif
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Patient Signature</label>
                        <div class="d-flex align-items-center">
                            @if($supplyOrder->is_signed_by_client)
                                <div class="text-info">
                                    Signed by <b>{{$patient->displayName()}}</b>
                                    on {{friendlier_date_time($supplyOrder->client_signed_at)}}
                                </div>
                            @elseif($supplyOrder->is_client_signature_waived)
                                <div class="text-info">
                                    Waived by <b>{{$supplyOrder->waiverPro->displayName()}}</b>
                                    on {{friendlier_date_time($supplyOrder->client_signature_waived_at)}}
                                </div>
                                <div class="ml-3">
                                    <div moe bottom relative="">
                                        <a start show class="py-0">Undo</a>
                                        <form url="/api/supplyOrder/undoWaiveClientSignature" right>
                                            <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                            <p class="small">Undo waiving patient signature?</p>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-primary mr-2" submit>Yes</button>
                                                <button class="btn btn-sm btn-default mr-2 border" cancel>No</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            @else
                                <div class="text-warning-mellow font-weight-bold">Not Signed</div>
                                <div class="ml-3">
                                    <div moe bottom relative="">
                                        <a start show class="py-0">Email</a>
                                        <form url="/api/supplyOrder/sendClientSignatureRequestByEmail">
                                            <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                            <p class="small">Request patient signature via email?</p>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-primary mr-2" submit>Submit</button>
                                                <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                                <span class="mx-2 text-secondary">|</span>
                                <div class="">
                                    <div moe bottom relative="">
                                        <a start show class="py-0">SMS</a>
                                        <form url="/api/supplyOrder/sendClientSignatureRequestBySms">
                                            <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                            <p class="small">Request patient signature via SMS?</p>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-primary mr-2" submit>Submit</button>
                                                <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                                <span class="mx-2 text-secondary">|</span>
                                <div class="">
                                    <a href="#" class="text-secondary on-hover-opaque">Problem Signing</a>
                                </div>
                                <span class="mx-2 text-secondary">|</span>
                                <div class="">
                                    <div moe bottom relative="">
                                        <a start show class="py-0">Waive</a>
                                        <form url="/api/supplyOrder/waiveClientSignature" right>
                                            <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                            <p class="small mb-2">Waive patient signature?</p>
                                            <div class="mb-2">
                                                <label class="text-secondary mb-1 text-sm">Memo</label>
                                                <input type="text" class="form-control form-control-sm"
                                                       name="memo" value="{{$supplyOrder->client_signature_waiver_memo}}">
                                            </div>
                                            <div class="d-flex align-items-center">
                                                <button class="btn btn-sm btn-primary mr-2" submit>Submit</button>
                                                <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            @endif
                        </div>
                        @if($supplyOrder->client_signature_last_requested_at &&
                            !$supplyOrder->is_signed_by_client &&
                            !$supplyOrder->is_client_signature_waived)
                            <div class="text-sm my-1 text-info">Client signature last requested on {{friendlier_date_time($iSupplyOrder->client_signature_last_requested_at)}}</div>
                        @endif
                        @if($supplyOrder->does_client_have_problem_signing &&
                            !$supplyOrder->is_signed_by_client &&
                            !$supplyOrder->is_client_signature_waived)
                            <div class="text-sm my-1 text-warning-mellow">
                                Client has problem signing
                                @if($supplyOrder->client_problem_signing)
                                    <i class="text-sm">{{$supplyOrder->client_problem_signing}}</i>
                                @endif
                            </div>
                        @endif
                    </div>
                    <hr class="my-3">
                    <div class="d-flex align-items-center">
                        @if($supplyOrder->is_cleared_for_shipment)
                            <div class="text-info mb-2">
                                <i class="fa fa-check"></i> Cleared for shipment by <b>{{$supplyOrder->clearedForShipmentPro->displayName()}}</b>
                                on {{friendlier_date_time($supplyOrder->cleared_for_shipment_at)}}
                            </div>
                        @elseif($supplyOrder->is_signed_by_pro && ($supplyOrder->is_signed_by_client || $supplyOrder->is_client_signature_waived))
                            <div moe bottom relative="" class="mb-2">
                                <a start show class="py-0 text-success font-weight-bold">Clear for Shipment</a>
                                <form url="/api/supplyOrder/clearForShipment">
                                    <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                    <p class="small mb-2">Clear this supply order for shipment?</p>
                                    <div class="d-flex align-items-center">
                                        <button class="btn btn-sm btn-primary mr-2" submit>Submit</button>
                                        <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                    </div>
                                </form>
                            </div>
                        @endif
                    </div>
                    <div class="d-flex align-items-center">
                        <div moe bottom relative="">
                            <a start show class="py-0 text-danger">Cancel Supply Order</a>
                            <form url="/api/supplyOrder/cancel">
                                <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                <p class="small text-nowrap mb-2">Cancel this supply order?</p>
                                <div class="d-flex align-items-center">
                                    <button class="btn btn-sm btn-primary mr-2" submit>Yes</button>
                                    <button class="btn btn-sm btn-default mr-2 border" cancel>No</button>
                                </div>
                            </form>
                        </div>
                    </div>
                    */ ?>
                    <div class="pdf-viewer-auto row px-3" pdf-url="/api/incomingReport/download/{{ $currentReport->uid }}"></div>
                </div>
            @endif
        </div>
    </div>
    <script>
        function submitIncomingReport(_form) {
            showMask();
            $.ajax({
                type: "POST",
                enctype: 'multipart/form-data',
                url: _form.action,
                data: new FormData(_form),
                processData: false,
                contentType: false,
                cache: false,
                timeout: 600000,
                success: function (data) {
                    hideMask();
                    if(data.success) {
                        fastReload();
                    }
                    else {
                        toastr.error(data.message);
                    }
                },
                error: function (e) {
                    hideMask();
                    toastr.error('Unable to upload document!');
                }
            });
            return false;
        }
    </script>
@endsection
