@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>
        <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">Report</th>
                <th class="px-2 text-secondary">Sign</th>
                <th class="px-2 text-secondary">Title</th>
                <th class="px-2 text-secondary">Category</th>
                <th class="px-2 text-secondary">Memo</th>
                <th class="px-2"></th>
            </tr>
            </thead>
            <tbody>
            @foreach($patient->incomingReports as $document)
                <tr class="{{$document->is_entry_error ? 'entry-error' : ''}}">
                    <td class="px-2">{{ friendly_date_time($document->created_at) }}</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)
                            @if($document->hcp_pro_id === $pro->id)
                                <div moe relative class="mr-2">
                                    <a start show class="">Sign</a>
                                    <form url="/api/incomingReport/signAsHcp" right>
                                        <input type="hidden" name="uid" value="{{ $document->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>
                            @else
                                -
                            @endif
                        @else
                            <span class="text-secondary">
                                <i class="fa fa-check"></i>
                                Signed
                            </span>
                        @endif
                    </td>
                    <td class="px-2">{{ $document->title }}</td>
                    <td class="px-2">{{ $document->category }}{{ $document->subcategory ? ' / ' . $document->subcategory : '' }}</td>
                    <td class="px-2">{{ $document->memo }}</td>
                    <td class="px-2">
                        <div class="d-flex align-items-center">
                            <a class="on-hover-opaque mr-3" native target="_blank"
                               href="/api/incomingReport/download/{{ $document->uid }}"
                               title="Download"><i class="fa fa-download"></i></a>
                            @if(!$document->has_hcp_pro_signed)
                                <div moe relative class="mr-3">
                                    <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="{{ $document->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="{{$document->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="{{$document->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="{{$document->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="{{$document->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="{{$document->hcp ? $document->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="{{$document->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(!$document->is_entry_error)
                                    <div moe relative class="mr-3">
                                        <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="{{ $document->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-3">
                                        <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="{{ $document->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>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </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
