@extends ('layouts.patient')
@section('inner-content')
    <div class="">
        <div class="d-flex align-items-center mb-3">
            <h4 class="font-weight-bold m-0">Shipments</h4>
            <span class="mx-2 text-secondary">|</span>
            <div moe>
                <a start show href="#">Add</a>
                <form url="/api/shipment/create" redir="/patients/view/{{ $patient->uid }}/shipments/[data]">
                    <input type="hidden" name="clientUid" value="{{ $patient->uid }}">
                    <label class="text-secondary font-weight-bold">Add Shipment</label>
<!--                    <div class="mb-2">
                        <label class="text-secondary text-sm">Requested Ship Date *</label>
                        <input type="date" class="form-control form-control-sm" name="requestedShipDate" required>
                    </div>-->
                    <p>Add a new shipment?</p>
                    <div class="d-flex align-items-center">
                        <button class="btn btn-sm btn-primary mr-2" type="button" submit>Submit</button>
                        <button class="btn btn-sm btn-default border mr-2" type="button" cancel>Cancel</button>
                    </div>
                </form>
            </div>
        </div>

        <div class="d-flex align-items-start h-100">
            <div class="flex-grow-1">

            @if($patient->readyToShipSupplyOrders && count($patient->readyToShipSupplyOrders))
            {{--<div class="mb-3 p-2 border bg-aliceblue">
                <div class="font-weight-bold mb-2 text-info">Ready to Ship Supply Orders</div>
                <table class="table table-sm table-striped table-bordered mb-0 bg-white">
                    @if($patient->readyToShipSupplyOrders && count($patient->readyToShipSupplyOrders))
                        <thead class="bg-light">
                        <tr class="">
                            <th class="border-0 text-nowrap text-secondary">Title</th>
                            <th class="border-0 text-nowrap text-secondary">Memo</th>
                            @if(!$shipment)
                                <th class="border-0 text-nowrap text-secondary">Created At</th>
                                <th class="border-0 text-nowrap text-secondary">Pro Signed?</th>
                                <th class="border-0 text-nowrap text-secondary">Cancelled?</th>
                                <th class="border-0 text-nowrap text-secondary">Shipment</th>
                            @endif
                        </tr>
                        </thead>
                        <tbody>
                        @foreach($patient->readyToShipSupplyOrders as $iSupplyOrder)
                            <tr class="">
                                <td>
                                    <a href="{{route('patients.view.supply-orders', ['patient' => $patient, 'supplyOrder' => $iSupplyOrder])}}">
                                        {{ $iSupplyOrder->product->title }}
                                    </a>
                                </td>
                                <td>{{ $iSupplyOrder->reason }}</td>
                                @if(!$shipment)
                                    <td>{{ friendlier_date_time($iSupplyOrder->created_at) }}</td>
                                    <td>{{ $iSupplyOrder->is_signed_by_pro ? $iSupplyOrder->signedPro->displayName() : '-' }}</td>
                                    <td>{{ $iSupplyOrder->is_cancelled ? 'Yes' : 'No' }}</td>
                                    <td>{{ $iSupplyOrder->shipment_id ? $iSupplyOrder->shipment->status : '-' }}</td>
                                @endif
                            </tr>
                        @endforeach
                        </tbody>
                    @else
                        <tbody>
                        <tr>
                            <td class="text-secondary p-3">No unshipped supply orders for this patient</td>
                        </tr>
                        </tbody>
                    @endif
                </table>
            </div>--}}
                <div class="alert alert-info px-3 rounded-0">
                    There {{ count($patient->readyToShipSupplyOrders) === 1 ? 'is' : 'are' }}
                    <b>{{ count($patient->readyToShipSupplyOrders) }}</b>
                    supply order{{ count($patient->readyToShipSupplyOrders) === 1 ? '' : 's' }}
                    cleared for shipment
                </div>
            @endif
            <table class="table table-sm table-striped table-bordered mb-0" style="table-layout: fixed">
                @if($patient->shipments && count($patient->shipments))
                    <thead>
                    <tr class="bg-light">
                        <th class="text-nowrap text-secondary border-0"><div class="text-ellipsis">Created At</div></th>
                        <th class="text-nowrap text-secondary border-0"><div class="text-ellipsis">Supply Orders</div></th>
                        <th class="text-nowrap text-secondary border-0"><div class="text-ellipsis">Status</div></th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($patient->shipments as $iShipment)
                        <tr class="{{@$shipment && @$shipment->uid === $iShipment->uid ? 'bg-aliceblue' : ''}}">
                            <td>
                                <a href="{{route('patients.view.shipments', ['patient' => $patient, 'shipment' => $iShipment])}}">
                                    {{ friendlier_date_time($iShipment->created_at) }}
                                </a>
                            </td>
                            <td>{{count($iShipment->supplyOrders)}}</td>
<!--                            <td>
                                @if($iShipment->label_system_file_id)
                                    <a class="pdf-viewer-trigger" native="" target="_blank"
                                       href="/api/shipment/downloadLabel/{{$iShipment->uid}}" title="View">
                                        <i class="fa fa-file-pdf text-danger on-hover-opaque"></i>
                                        View
                                    </a>
                                @endif
                            </td>
                            <td>{{ $iShipment->courier }}</td>
                            <td>{{ $iShipment->tracking_number }}</td>-->
                            <td>{{ $iShipment->status }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                @else
                    <tbody>
                        <tr>
                            <td class="text-secondary p-3">No shipments have been created for this patient</td>
                        </tr>
                    </tbody>
                @endif
            </table>

            </div>

            @if($shipment)
                <div class="min-width-700px ml-2 border align-self-stretch p-3">
                    <div class="d-flex align-items-center mb-3">
                        <h3 class="font-size-16 m-0">Shipment (created on {{ friendlier_date_time($shipment->created_at) }}) #{{$shipment->id}}</h3>
                        <a class="ml-auto" href="{{route('patients.view.shipments', ['patient' => $patient])}}">
                            <i class="fa fa-times-circle on-hover-opaque"></i>
                        </a>
                    </div>
                    <hr class="my-3">

{{--                    <div class="mb-4 d-flex align-items-baseline">--}}
{{--                        <label class="text-secondary mb-0 min-width-140px mr-2">Requested Ship Date</label>--}}
{{--                        <div moe bottom class="d-block">--}}
{{--                            <a start show>{{friendlier_date($shipment->requested_ship_date)}}</a>--}}
{{--                            <form url="/api/supplyOrder/setRequestedShipDate">--}}
{{--                                <input type="hidden" name="uid" value="{{ $shipment->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" required--}}
{{--                                           name="requestedShipDate" value="{{$shipment->requested_ship_date}}">--}}
{{--                                </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>--}}

                    @if($patient->readyToShipSupplyOrders && count($patient->readyToShipSupplyOrders))
                        <div class="mb-4">
                            <label class="mb-2 font-weight-bold text-secondary">Supply orders cleared for shipment</label>
                            <table class="table table-sm table-striped table-bordered mb-0 bg-white">
                                <thead>
                                <tr class="">
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">Title</th>
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">Requested Ship Date</th>
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">&nbsp;</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($patient->readyToShipSupplyOrders as $iSupplyOrder)
                                    <tr class="">
                                        <td class="px-2">{{ $iSupplyOrder->product->title }}</td>
                                        <td class="px-2 text-secondary">-todo-</td>
                                        <td class="px-2">
                                            <div moe relative>
                                                <a start show class="py-0 font-weight-bold">Add</a>
                                                <form url="/api/supplyOrder/putInShipment" right>
                                                    <input type="hidden" name="uid" value="{{ $iSupplyOrder->uid }}">
                                                    <input type="hidden" name="shipmentUid" value="{{ $shipment->uid }}">
                                                    <p class="small">Add this supply order?</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>
                                            @if($iSupplyOrder->note && !$iSupplyOrder->note->is_signed_by_hcp)
                                                <small class="text-danger text-sm">
                                                <i class="fas fa-exclamation-triangle text-danger"></i> Note not signed by HCP.
                                                </small>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @endif

                    <div class="mb-3">
<!--                        <label class="mb-2 font-weight-bold text-secondary">Supply Orders in this Shipment</label>-->
                        <table class="table table-sm table-striped table-bordered mb-0 bg-white">
                            @if($shipment->supplyOrders && count($shipment->supplyOrders))
                                <thead>
                                <tr class="">
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">Supply Order</th>
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">IMEI</th>
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">Lot #</th>
                                    <th class="px-2 text-nowrap text-secondary border-bottom-0">&nbsp;</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($shipment->supplyOrders as $iSupplyOrder)
                                    <tr class="">
                                        <td class="px-2">{{ $iSupplyOrder->product->title }}</td>
                                        <td class="px-2">
                                            <div moe relative class="d-block">
                                                <a start show>{{ $iSupplyOrder->imei ? $iSupplyOrder->imei : '(not set)' }}</a>
                                                <form url="/api/supplyOrder/associateImei" right>
                                                    <input type="hidden" name="uid" value="{{ $iSupplyOrder->uid }}">
                                                    <div class="mb-2">
                                                        <label class="text-secondary mb-1 text-sm">IMEI *</label>
                                                        <input type="text" class="form-control form-control-sm" required
                                                               name="imei" value="{{ $iSupplyOrder->imei }}">
                                                    </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>
                                        </td>
                                        <td class="px-2">
                                            <div moe relative class="d-block">
                                                @if($iSupplyOrder->product->has_lot)
                                                <a start show>{{ $iSupplyOrder->lot_number ? $iSupplyOrder->lot_number : '(not set)' }}</a>
                                                <form url="/api/supplyOrder/updateLotNumber" right>
                                                    <input type="hidden" name="uid" value="{{ $iSupplyOrder->uid }}">
                                                    <div class="mb-2">
                                                        <label class="text-secondary mb-1 text-sm">Lot # *</label>
                                                        <input type="text" class="form-control form-control-sm" required
                                                               name="lotNumber" value="{{ $iSupplyOrder->lot_number }}">
                                                    </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>
                                                @else
                                                    <span class="text-secondary">N/A</span>
                                                @endif
                                            </div>
                                        </td>
                                        <td class="px-2">
                                            <div moe relative>
                                                <a start show class="py-0 text-danger">Remove</a>
                                                <form url="/api/supplyOrder/removeFromShipment" right>
                                                    <input type="hidden" name="uid" value="{{ $iSupplyOrder->uid }}">
                                                    <p class="small">Remove this supply order?</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>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            @else
                                <tbody>
                                <tr>
                                    <td class="text-secondary px-3 py-2">No supply orders in this shipment yet</td>
                                </tr>
                                </tbody>
                            @endif
                        </table>
                    </div>

                    @include('app.patient.partials.packs')

{{--
                    <div class="mb-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">Ship Date</label>
                        <div moe bottom relative class="d-block">
                            <a start show>{{$shipment->ship_date ? friendlier_date($shipment->ship_date) : '(not set)'}}</a>
                            <form url="/api/shipment/setShipDate">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Ship Date *</label>
                                    <input type="date" class="form-control form-control-sm" required
                                           name="shipDate" value="{{$shipment->ship_date}}">
                                </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-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">Courier</label>
                        <div moe bottom relative class="d-block">
                            <a start show>{{$shipment->courier ? $shipment->courier : '(not set)'}}</a>
                            <form url="/api/shipment/setCourier">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Courier</label>
                                    <input type="text" class="form-control form-control-sm"
                                           name="courier" value="{{$shipment->courier}}">
                                </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-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">Label File</label>
                        @if($shipment->label_system_file_id)
                            <a class="pdf-viewer-trigger" native target="_blank"
                               href="/api/shipment/downloadLabel/{{ $shipment->uid }}"
                               title="View">
                                <i class="fa fa-file-pdf text-danger on-hover-opaque"></i>
                                View
                                --}}
{{--{{ $shipment->labelFile->file_name }}--}}{{--

                            </a>
                            <span class="mx-2 text-secondary">|</span>
                        @endif
                        <div moe bottom relative class="d-block">
                            <a start show>Upload</a>
                            <form url="/api/shipment/setLabelSystemFile">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Label File</label>
                                    <input type="file" class="form-control form-control-sm"
                                           name="labelSystemFile">
                                </div>
                                <div class="d-flex align-items-center">
                                    <button class="btn btn-sm btn-primary mr-2" submit>Upload</button>
                                    <button class="btn btn-sm btn-default mr-2 border" cancel>Cancel</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <div class="mb-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">Tracking #</label>
                        <div moe bottom relative class="d-block">
                            <a start show>{{$shipment->tracking_number ? $shipment->tracking_number : '(not set)'}}</a>
                            <form url="/api/shipment/setTrackingNumber">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Tracking #</label>
                                    <input type="text" class="form-control form-control-sm"
                                           name="trackingNumber" value="{{$shipment->tracking_number}}">
                                </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-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">FedEx Status</label>
                        <div moe bottom relative class="d-block">
                            <a start show>{{$shipment->fedex_shipment_status ?: '(not set)'}}</a>
                            <form url="/api/shipment/updateFedexStatus">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Status *</label>
                                    <input type="text" class="form-control form-control-sm" name="fedexStatus" value="{{$shipment->fedex_shipment_status}}" required>
                                </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-2 d-flex align-items-baseline">
                        <label class="text-secondary text-sm mb-0 width-90px mr-2">Status</label>
                        <div moe bottom relative class="d-block">
                            <a start show>{{$shipment->status ? $shipment->status : '(not set)'}}</a>
                            <form url="/api/shipment/setStatus">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Status *</label>
                                    <select class="form-control form-control-sm"
                                           name="status">
                                        <option value=""> -- select -- </option>
                                        <?php
                                        $allStatuses = ['CREATED', 'PRINTED', 'BEING_PICKED', 'PICKED', 'BEING_SCANNED', 'SCANNED', 'BEING_FULFILLED', 'FULFILLED', 'DISPATCHED'];
                                        foreach($allStatuses as $iAllStatuses):
                                        ?>
                                        <option value="{{$iAllStatuses}}" {{$shipment->status === $iAllStatuses ? 'selected' : '' }} >
                                            {{$iAllStatuses}}
                                        </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="mb-2">
                                    <label class="text-secondary mb-1 text-sm">Memo</label>
                                    <input type="text" class="form-control form-control-sm"
                                           name="statusMemo" value="{{$shipment->status_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>

                    @if($shipment->status === 'DELIVERED')
                        <div class="mb-2 d-flex align-items-baseline">
                            <label class="text-secondary text-sm mb-0 width-90px mr-2">Delivered Date</label>
                            <div moe bottom relative class="d-block">
                                <a start show>{{$shipment->delivered_date ? friendlier_date($shipment->delivered_date) : '(not set)'}}</a>
                                <form url="/api/shipment/setDeliveredDate">
                                    <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                    <div class="mb-2">
                                        <label class="text-secondary mb-1 text-sm">Delivered Date</label>
                                        <input type="date" class="form-control form-control-sm"
                                               name="deliveredDate" value="{{$shipment->delivered_date}}">
                                    </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>
                    @endif

                    <hr class="m-neg-3">
                    <div class="mb-3">
                        <label class="text-secondary text-sm mb-1">Was Client Called and Has Client Acknowledged?</label>
                        <div class="d-flex align-items-center">
                            @if($shipment->was_client_called)
                                <div>
                                    <div class="mb-1 text-info">Client was called</div>
                                    <div class="mb-1 text-info">Client has acknowledged receipt of product: {{$shipment->has_client_acknowledged}}</div>
                                    @if($shipment->has_client_acknowledged === 'YES' && $shipment->client_acknowledgement_memo)
                                        <div class="mb-1 text-secondary font-italic">{{$shipment->client_acknowledgement_memo}}</div>
                                    @endif
                                </div>
                            @else
                                <div class="mb-1 text-warning-mellow font-weight-bold">Client has not been called</div>
                            @endif
                        </div>
                        <div class="" moe>
                            <a start show class="py-0">Update</a>
                            <form url="/api/shipment/{{$shipment->was_client_called ? 'setWasClientCalledToFalse' : 'setWasClientCalledToTrue'}}">
                                <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                                @if($shipment->was_client_called)
                                    <p>Set to client was not called?</p>
                                @else
                                    <p>Set to client was called?</p>
                                    <div class="mb-2">
                                        <label class="text-secondary text-sm">Has Client Acknowledged Receipt of Product?</label>
                                        <select name="hasClientAcknowledged"
                                                class="form-control form-control-sm">
                                            <option value="">-- Select Status --</option>
                                            <option value="YES">YES</option>
                                            <option value="NO">NO</option>
                                            <option value="UNKNOWN">UNKNOWN</option>
                                        </select>
                                    </div>
                                    <div class="mb-2">
                                        <label class="text-secondary text-sm">Client Acknowledgement Memo</label>
                                        <input type="text" class="form-control form-control-sm" name="clientAcknowledgementMemo">
                                    </div>
                                @endif
                                <div class="d-flex align-items-center">
                                    <button class="btn btn-sm btn-primary mr-2" type="button" submit>Save</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <hr class="m-neg-3">
                    @include('app.generic-bills.inline', ['patient' => $patient, 'entityType' => 'Shipment', 'entityUid' => $shipment->uid])

                </div>
            @endif

        </div>
    </div>
@endsection
