@extends ('layouts.patient')
@section('inner-content')
    <div class="" id="patient-supply-orders">
        <div class="d-flex align-items-center pb-2">
            <h4 class="font-weight-bold m-0">Supply Orders</h4>
            @if($pro->pro_type === 'ADMIN' || request()->input('note-uid'))
                <span class="mx-2 text-secondary">|</span>
                <div moe large>
                    <a start show href="#">Add</a>
                    <form url="/api/supplyOrder/create" redir="/patients/view/{{ $patient->uid }}/supply-orders/[data]">
                        <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                        <input type="hidden" name="noteUid" value="{{ request()->input('note-uid') }}">
                        <label class="text-secondary font-weight-bold">Add Supply Order</label>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Product</label>
                            <select name="productUid" class="form-control form-control-sm">
                                <option value=""> --select--</option>
                                @foreach($products as $product)
                                    <option
                                        value="{{$product->uid}}">{{$product->title}}</option>
                                @endforeach
                            </select>
                        </div>
                        <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="{{$patient->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="{{$patient->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="{{$patient->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="{{$patient->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="{{$patient->mailing_address_zip}}">
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Reason</label>
                            <input type="text" class="form-control form-control-sm" name="reason">
                        </div>
                        <div class="mb-2">
                            <label class="text-secondary text-sm">Client Understanding Memo</label>
                            <input type="text" class="form-control form-control-sm" name="clientUnderstandingMemo">
                        </div>
                        <div class="d-flex align-items-center">
                            <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
                        </div>
                    </form>
                </div>
            @endif
            <select class="ml-auto max-width-300px form-control form-control-sm supply-orders-filter">
                <option value="active" {{ !request()->input('filter') || request()->input('filter') === 'active' ? 'selected' : '' }}>Active Supply Orders</option>
                <option value="cancelled" {{ request()->input('filter') === 'cancelled' ? 'selected' : '' }}>Cancelled Supply Orders</option>
                <option value="all" {{ request()->input('filter') === 'all' ? 'selected' : '' }}>All Supply Orders</option>
            </select>
        </div>
        <div class="d-flex align-items-start h-100">
            <div class="flex-grow-1">
                <table class="table table-sm table-bordered mb-0" style="table-layout: fixed">
                    <?php
                    $supplyOrdersList = $patient->activeSupplyOrders;
                    if(request()->input('filter')) {
                        switch(request()->input('filter')) {
                            case 'cancelled':
                                $supplyOrdersList = $patient->cancelledSupplyOrders;
                                break;
                            case 'all':
                                $supplyOrdersList = $patient->supplyOrders;
                                break;
                        }
                    }
                    ?>

                    @if($supplyOrdersList && count($supplyOrdersList))
                        <thead>
                        <tr class="bg-light">
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Title</div></th>
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Reason</div></th>
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Created At</div></th>
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Pro Signed?</div></th>
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Cancelled?</div></th>
                            <th class="px-2 text-nowrap text-secondary border-bottom-0"><div class="text-ellipsis">Shipment</div></th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($supplyOrdersList as $iSupplyOrder)
                            <tr class="{{@$supplyOrder && @$supplyOrder->uid === $iSupplyOrder->uid ? 'bg-aliceblue' : ''}} {{$iSupplyOrder->is_cancelled ? 'on-hover-opaque' : ''}}">
                                <td class="px-2">
                                    @if(request()->input('note-uid') && request()->input('popupmode'))
                                        <a class="c-pointer" onclick="return refreshDynamicStagPopup('{{route('patients.view.supply-orders', ['patient' => $patient, 'supplyOrder' => $iSupplyOrder])}}?{{request()->input('filter') ? 'filter=' . request()->input('filter') : ''}}&popupmode=1&{{request()->input('note-uid') ? 'note-uid=' . request()->input('note-uid') : ''}}')">
                                            {{ $iSupplyOrder->product->title }}
                                        </a>
                                    @else
                                        <a href="{{route('patients.view.supply-orders', ['patient' => $patient, 'supplyOrder' => $iSupplyOrder])}}{{request()->input('filter') ? '?filter=' . request()->input('filter') : ''}}">
                                            {{ $iSupplyOrder->product->title }}
                                        </a>
                                    @endif
                                    @if($iSupplyOrder->is_cancelled)
                                        <div class="font-weight-bold text-sm text-secondary"><i class="fa fa-ban"></i> CANCELLED</div>
                                    @endif
                                </td>
                                <td class="px-2">{{ $iSupplyOrder->reason }}</td>
                                <td class="px-2">{{ friendlier_date_time($iSupplyOrder->created_at) }}</td>
                                <td class="px-2">{{ $iSupplyOrder->is_signed_by_pro ? $iSupplyOrder->signedPro->displayName() : '-' }}</td>
                                <td class="px-2">{{ $iSupplyOrder->is_cancelled ? 'Yes' : 'No' }}</td>
                                <td class="px-2">
                                    @if($iSupplyOrder->shipment_id)
                                        <i class="fa fa-building"></i>
                                        {{$iSupplyOrder->shipment->status ? $iSupplyOrder->shipment->status : 'CREATED'}}
                                    @elseif($iSupplyOrder->is_cleared_for_shipment)
                                        <span class="text-info">
                                            <i class="fa fa-user-nurse"></i>
                                            Cleared for shipment
                                        </span>
                                    @else
                                        <span class="text-warning-mellow">
                                            <i class="fa fa-user-nurse"></i>
                                            Not cleared for shipment
                                        </span>
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    @else
                        <tbody>
                        <tr>
                            <td class="text-secondary p-3">No supply orders matching the criteria</td>
                        </tr>
                        </tbody>
                    @endif
                </table>
            </div>
            @if($supplyOrder)
                <div class="min-width-500px ml-2 border align-self-stretch p-3">
                    <div class="d-flex align-items-center">
                        <h3 class="font-size-16 m-0">{{$supplyOrder->product->title}}</h3>
                        <a class="ml-auto" href="/patients/view/{{$patient->uid}}/supply-orders{{request()->input('filter') ? '?filter=' . request()->input('filter') : ''}}">
                            <i class="fa fa-times-circle on-hover-opaque"></i>
                        </a>
                    </div>
                    <hr class="my-3">
                    <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($pro->pro_type === 'ADMIN' && $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>
                    @if(!$supplyOrder->is_cancelled)
                        <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="mb-2">
                                        <label class="text-secondary mb-1 text-sm">Memo</label>
                                        <textarea type="text" class="form-control form-control-sm"
                                                  name="memo"></textarea>
                                    </div>
                                    <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-secondary font-weight-bold">
                            <i class="fa fa-ban"></i>
                            CANCELLED
                        </div>
                        @if($supplyOrder->cancellation_memo)
                            <div class="text-sm text-secondary font-italic mt-1">{{$supplyOrder->cancellation_memo}}</div>
                        @endif
                        @if(!$supplyOrder->is_cancellation_acknowledged)
                            <div moe bottom relative="" class="mt-1">
                                <a start show class="py-0">Acknowledge Cancellation</a>
                                <form url="/api/supplyOrder/acknowledgeCancellation">
                                    <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                    <p class="small text-nowrap mb-2">Acknowledge cancellation?</p>
                                    <div class="mb-2">
                                        <label class="text-secondary mb-1 text-sm">Memo</label>
                                        <textarea type="text" class="form-control form-control-sm"
                                                  name="memo"></textarea>
                                    </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>
                        @else
                            <div class="text-secondary font-weight-bold mt-1">
                                <i class="fa fa-check"></i>
                                Cancellation Acknowledged
                            </div>
                            @if($supplyOrder->cancellation_acknowledgement_memo)
                                <div class="text-sm text-secondary font-italic mt-1">{{$supplyOrder->cancellation_acknowledgement_memo}}</div>
                            @endif
                            <div moe bottom relative="" class="mt-1">
                                <a start show class="py-0">Undo Cancellation Acknowledgement</a>
                                <form url="/api/supplyOrder/undoAcknowledgeCancellation">
                                    <input type="hidden" name="uid" value="{{ $supplyOrder->uid }}">
                                    <p class="small text-nowrap mb-2">Undo cancellation acknowledgement?</p>
                                    <div class="mb-2">
                                        <label class="text-secondary mb-1 text-sm">Memo</label>
                                        <textarea type="text" class="form-control form-control-sm"
                                                  name="memo"></textarea>
                                    </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>
                        @endif
                    @endif
                </div>
            @endif
        </div>
    </div>

    <script>
        (function() {
            function init() {
                $(document)
                    .off('change', '.supply-orders-filter')
                    .on('change', '.supply-orders-filter', function() {
                        @if(request()->input('note-uid') && request()->input('popupmode'))
                        refreshDynamicStagPopup('/patients/view/{{$patient->uid}}/supply-orders{{@$supplyOrder ? '/' . $supplyOrder->uid : '' }}?' +
                            'popupmode=1&' +
                            'note-uid={{request()->input('note-uid')}}&' +
                            'filter=' + this.value);
                        @else
                        fastLoad('/patients/view/{{$patient->uid}}/supply-orders{{@$supplyOrder ? '/' . $supplyOrder->uid : '' }}?'+
                            'filter=' + this.value, true, false, false);
                        @endif
                        return false;
                    });

                // overflow visible for this specific stag-popup-content
                if($('#patient-supply-orders').closest('.stag-popup-content').length) {
                    $('#patient-supply-orders').closest('.stag-popup-content')[0].style.overflow = 'visible';
                }

            }
            addMCInitializer('patient-supply-orders', init);
        }).call(window);
    </script>
@endsection
