@extends ('layouts/template')

@section('content')

    <div id="practice-supply-orders" 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">
                    Supply Orders - Shipment Underway ({{$counts['supplyOrdersShipmentUnderway']}})
                </strong>
                <span class="mx-3 text-secondary">|</span>
                <a href="{{route('practice-management.supply-orders-ready-to-ship')}}">Ready to Ship ({{$counts['supplyOrdersReadyToShip']}})</a>
                <span class="mx-3 text-secondary">|</span>
                <a href="{{route('practice-management.supply-orders')}}">All ({{$counts['supplyOrders']}})</a>
<!--                <a href="{{route('practice-management.supply-orders-ready-to-ship')}}" class="ml-auto">Clear Filters</a>-->
            </div>
            <div class="card-body p-0">
                <table class="table table-sm table-condensed p-0 m-0" style="table-layout: fixed">
                    <thead class="bg-light">
                    <tr>
                        <th class="border-0">Patient</th>
                        <th class="border-0">Address</th>
                        <th class="border-0">Item(s)</th>
                        <th class="border-0">Shipment</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $prevClientAndAddress = null;
                    $shipmentIndex = 0;
                    ?>
                    @foreach ($supplyOrders as $supplyOrder)
                        <?php
                        $currentClientAndAddress = implode('|', [$supplyOrder->client_id, $supplyOrder->mailing_address_full]);
                        $sameClientAndAddress = false;
                        ?>
                        @if($currentClientAndAddress !== $prevClientAndAddress)
                            <tr class="sep"><td colspan="4"></td></tr>
                            <?php
                            $shipmentIndex++;
                            $prevClientAndAddress = $currentClientAndAddress;
                            ?>
                        @else
                            <?php $sameClientAndAddress = true; ?>
                        @endif
                        <tr class="{{$supplyOrder->is_cancelled ? 'bg-light' : ''}}"
                            data-client-uid="{{$supplyOrder->client->uid}}"
                            data-address-line1="{{$supplyOrder->mailing_address_line1}}"
                            data-address-line2="{{$supplyOrder->mailing_address_line2}}"
                            data-address-city="{{$supplyOrder->mailing_address_city}}"
                            data-address-state="{{$supplyOrder->mailing_address_state}}"
                            data-address-zip="{{$supplyOrder->mailing_address_zip}}"
                            data-shipment-index="{{$shipmentIndex}}"
                            data-supply-order-uid="{{$supplyOrder->uid}}">
                            <td class="align-top border-top-0">
                                @if(!$sameClientAndAddress)
                                    <a href="{{route('patients.view.supply-orders',['patient'=>$supplyOrder->client, 'supplyOrder'=>$supplyOrder])}}"> {{$supplyOrder->client->displayName()}}</a>
                                @endif
                            </td>
                            <td class="align-top border-top-0">
                                @if(!$sameClientAndAddress)
                                    {!! $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]) : '-'  !!}
                                @endif
                            </td>
                            <td class="align-top border-top-0">
                                {{$supplyOrder->product->title}}
                            </td>
                            <td class="align-top border-top-0">
                                @if(!$sameClientAndAddress)
                                    <a href="{{route('practice-management.shipment', $supplyOrder->shipment)}}">
                                        {{$supplyOrder->shipment->status ? $supplyOrder->shipment->status : 'CREATED'}}
                                        <span class="text-secondary text-sm">(Generated: {{friendlier_date_time($supplyOrder->shipment->created_at, false)}})</span>
                                    </a>
                                @endif
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <div>
                    {{$supplyOrders->links()}}
                </div>
            </div>
        </div>
    </div>

@endsection
