@extends ('layouts/template-no-mc')
@section('content')

    <?php
    function cleanStatus($_status)
    {
        return ucwords(strtolower(str_replace("_", " ", $_status)));
    }
    ?>

    @foreach($shipments as $shipment)
    <div style="page-break-after: always">
    <div id="practice-shipment" class="p-3 mcp-theme-1">
        <div class="card">
            <div class="card-header px-3 py-3 d-flex align-items-center bg-white">
                <strong class="font-size-14">
                    Shipment
                </strong>
                <span class="text-sm text-secondary ml-2">
                    (Created: {{friendlier_date_time($shipment->created_at)}})
                </span>
                @if($shipment->status === 'CANCELLED')
                    <div class="ml-5 font-weight-bold text-danger on-hover-opaque">-- CANCELLED --</div>
                @endif
                <div class="ml-auto d-inline-flex align-items-center only-screen">
                    <button id="print-shipment" class="btn btn-sm btn-info">Generate PDF</button>
                    <div moe relative class="d-block ml-3">
                        <a start show class="btn btn-default border btn-sm text-secondary">Cancel Shipment</a>
                        <form url="/api/shipment/setStatus" right>
                            <input type="hidden" name="uid" value="{{ $shipment->uid }}">
                            <input type="hidden" name="status" value="CANCELLED">
                            <p>Cancel this 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>
                </div>
            </div>
            <div class="only-screen">
                <div class="card-header px-3 py-3 d-flex align-items-center bg-light">
                    <span class="mr-3">Status:</span>
                    <?php $statuses = ['CREATED', 'PRINTED', 'BEING_PICKED', 'PICKED', 'READY_FOR_FULFILMENT', 'READY_FOR_DISPATCH', 'DISPATCHED']; ?>
                    @foreach($statuses as $status)
                        <button class="btn btn-sm {{ $shipment->status === $status ? 'btn-info font-weight-bold' : 'btn-outline-info change-status bg-white' }}"
                                data-status="{{$status}}">
                            {{cleanStatus($status)}}
                        </button>
                        @if($status !== 'DISPATCHED')
                            <span class="text-secondary text-sm mx-2"><i class="fa fa-chevron-right on-hover-opaque"></i></span>
                        @endif
                    @endforeach
                </div>
            </div>

            <div class="card-header p-3 only-print bg-white">
                <div class="d-flex justify-content-center">
                    <div>
                        <?php
                        $generator = new Picqer\Barcode\BarcodeGeneratorPNG();
                        echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode($shipment->uid, $generator::TYPE_CODE_128, 1, 60)) . '">';
                        ?>
                        <div class="text-center text-secondary text-sm mt-1">{{$shipment->uid}}</div>
                    </div>
                </div>

            </div>

            <div class="card-body p-3 border-0">

                <div class="mb-3 d-flex align-items-center">
                    <span class="text-secondary">Patient: </span>
                    <a href="{{route('patients.view.dashboard', $shipment->client->uid)}}">
                        <span class="font-size-14">{{$shipment->client->displayName()}}</span>
                    </a>
                    <span class="text-secondary ml-auto">Address: </span>
                    <span class="font-size-14">{!! $shipment->mailing_address_full ? implode(" ", [$shipment->mailing_address_line1 . ' ' . $shipment->mailing_address_line2, $shipment->mailing_address_city . ' ' . $shipment->mailing_address_state . ' ' . $shipment->mailing_address_zip]) : '-'  !!}</span>
                </div>

                <div class="mb-3">
                    <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 only-print">Picked</th>
                                <th class="px-2 text-nowrap text-secondary border-bottom-0">Item</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 only-screen">&nbsp;</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach($shipment->supplyOrders as $iSupplyOrder)
                                <tr class="">
                                    <td class="px-2 only-print">&nbsp;</td>
                                    <td class="px-2">{{ $iSupplyOrder->product->title }}
                                        <span class="text-sm text-secondary ml-2">
                                            (Created: {{friendlier_date_time($iSupplyOrder->created_at)}})
                                        </span>
                                    </td>
                                    <td class="px-2">
                                        <div class="only-screen">
                                        <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>
                                        </div>
                                        &nbsp;
                                    </td>
                                    <td class="px-2">
                                        <div moe relative class="d-block">
                                            <div class="only-screen">
                                            @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>
                                            &nbsp;
                                        </div>
                                    </td>
                                    <td class="px-2 only-screen">
                                        <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 items in this shipment!</td>
                            </tr>
                            </tbody>
                        @endif
                    </table>
                </div>

                <div class="bg-light border px-2 pt-2">

                    <div class="mb-2 d-flex align-items-baseline">
                        <label class="text-secondary 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 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 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 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 mb-0 width-90px mr-2">Status</label>
                        <span>{{cleanStatus($shipment->status)}}</span>
                    </div>

                    @if($shipment->status === 'DELIVERED')
                        <div class="mb-2 d-flex align-items-baseline">
                            <label class="text-secondary 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

                </div>

            </div>
        </div>
    </div>
    </div>
    @endforeach


@endsection
