@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 ({{$counts['supplyOrders']}})
            </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-shipment-underway')}}">Shipment Underway ({{$counts['supplyOrdersShipmentUnderway']}})</a>
            @if($counts['supplyOrdersHanging'])
                <span class="mx-3 text-secondary">|</span>
                <a href="{{route('practice-management.supply-orders-hanging')}}" class="text-warning-mellow font-weight-bold"><i class="fa fa-exclamation-triangle"></i>&nbsp;Hanging ({{$counts['supplyOrdersHanging']}})</a>
            @endif
            @if(count(request()->all()))
                <a href="{{route('practice-management.supply-orders')}}" class="ml-auto">Clear Filters</a>
            @endif
        </div>
        <div class="card-body p-0">
            <table class="table table-sm table-condensed p-0 m-0">
                <thead class="bg-light">
                <tr>
                    <th class="border-0">Client</th>
                    <th class="border-0">Product</th>
                    <th class="border-0">Reason</th>
                    <th class="border-0">Client Understanding Memo</th>
                    <th class="border-0">Pro Sign</th>
                    <th class="border-0">Client Sign</th>
                    <th class="border-0">Shipment</th>
                    <th class="border-0">Lot #</th>
                    <th class="border-0">IMEI</th>
                    <th class="border-0">Created At</th>
                    <th class="border-0">Cancelled?</th>
                </tr>
                <tr>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="client">
                            <option value="">All</option>
                            @foreach($soClients as $soClient)
                                <option value="{{$soClient->id}}"
                                    {{$filters['client'] == $soClient->id ? 'selected' : ''}}>{{$soClient->displayName()}}</option>
                            @endforeach
                        </select>
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="product">
                            <option value="">All</option>
                            @foreach($soProducts as $soProduct)
                                <option value="{{$soProduct->id}}"
                                    {{$filters['product'] == $soProduct->id ? 'selected' : ''}}>{{$soProduct->title}}</option>
                            @endforeach
                        </select>
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <input type="text" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                               data-filter="reason"
                               value="{{$filters['reason']}}" placeholder="Any">
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <input type="text" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                               data-filter="cu_memo"
                               value="{{$filters['cu_memo']}}" placeholder="Any">
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="pro_sign">
                            <option value="">All</option>
                            <option {{$filters['pro_sign'] == 'not_signed' ? 'selected' : ''}} value="not_signed">Not Signed</option>
                            <option {{$filters['pro_sign'] == 'signed' ? 'selected' : ''}} value="signed">Signed</option>
                        </select>
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="client_sign">
                            <option value="">All</option>
                            <option {{$filters['client_sign'] == 'not_signed' ? 'selected' : ''}} value="not_signed">Not Signed</option>
                            <option {{$filters['client_sign'] == 'signed' ? 'selected' : ''}} value="signed">Signed</option>
                            <option {{$filters['client_sign'] == 'waived' ? 'selected' : ''}} value="waived">Waived</option>
                        </select>
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="shipment">
                            <option value="">All</option>
                            <optgroup label="Pro">
                                <option {{$filters['shipment'] == 'not_cleared_for_shipment' ? 'selected' : ''}} value="not_cleared_for_shipment">Not Cleared for Shipment</option>
                                <option {{$filters['shipment'] == 'cleared_for_shipment' ? 'selected' : ''}} value="cleared_for_shipment">Cleared for Shipment</option>
                            </optgroup>
                            <optgroup label="Warehouse">
                                <option {{$filters['shipment'] == 'CREATED' ? 'selected' : ''}} value="CREATED">Created</option>
                                <option {{$filters['shipment'] == 'SHIPPED' ? 'selected' : ''}} value="SHIPPED">Shipped</option>
                                <option {{$filters['shipment'] == 'DELIVERED' ? 'selected' : ''}} value="DELIVERED">Delivered</option>
                                <option {{$filters['shipment'] == 'RETURNED_TO_SENDER' ? 'selected' : ''}} value="RETURNED_TO_SENDER">Returned to Sender</option>
                                <option {{$filters['shipment'] == 'CANCELLED' ? 'selected' : ''}} value="CANCELLED">Cancelled</option>
                            </optgroup>
                        </select>
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <input type="text" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                               data-filter="lot_number"
                               value="{{$filters['lot_number']}}" placeholder="Any">
                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <input type="text" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                               data-filter="imei"
                               value="{{$filters['imei']}}" placeholder="Any">
                    </th>
                    <th class="p-0 border-bottom-0 border-right">

                    </th>
                    <th class="p-0 border-bottom-0 border-right">
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="cancelled">
                            <option value="">All</option>
                            <option {{$filters['cancelled'] == 'not_cancelled' ? 'selected' : ''}} value="not_cancelled">Not Cancelled</option>
                            <option {{$filters['cancelled'] == 'cancelled' ? 'selected' : ''}} value="cancelled">Cancelled</option>
                        </select>
                    </th>
                </tr>
                </thead>
                <tbody>
                @foreach ($supplyOrders as $supplyOrder)
                    <tr class="{{$supplyOrder->is_cancelled ? 'bg-light' : ''}}">
                        <td><a href="{{route('patients.view.supply-orders',['patient'=>$supplyOrder->client, 'supplyOrder'=>$supplyOrder])}}">{{$supplyOrder->client->displayName()}}</a></td>
                        <td>{{$supplyOrder->product->title}}</td>
                        <td>{{$supplyOrder->reason}}</td>
                        <td>{{$supplyOrder->client_understanding_memo}}</td>
                        <td>
                            @if($supplyOrder->is_signed_by_pro)
                                <b>Signed</b>
                                <div class="text-secondary text-sm mt-1">
                                    By {{$supplyOrder->signedPro->displayName()}}<br>
                                    {{friendlier_date_time($supplyOrder->pro_signed_at)}}
                                </div>
                            @else
                                Not Signed
                            @endif
                        </td>
                        <td>
                            @if($supplyOrder->is_signed_by_client)
                                <b>Signed</b>
                                <div class="text-secondary text-sm mt-1">
                                    By {{$patient->displayName()}}<br>
                                    On {{friendlier_date_time($supplyOrder->client_signed_at)}}
                                </div>
                            @elseif($supplyOrder->is_client_signature_waived)
                                <b>Waived</b>
                                <div class="text-secondary text-sm mt-1">
                                    By {{$supplyOrder->waiverPro->displayName()}}<br>
                                    on {{friendlier_date_time($supplyOrder->client_signature_waived_at)}}
                                </div>
                            @else
                                Not Signed
                            @endif
                        </td>
                        <td>
                            @if($supplyOrder->shipment_id)
                                <i class="fa fa-building"></i>
                                {{$supplyOrder->shipment->status}}
                                @if($supplyOrder->shipment && $supplyOrder->shipment->status === 'DELIVERED' && $supplyOrder->shipment->delivered_date)
                                    <div class="text-secondary text-sm mt-1">
                                    on {{friendlier_date_time($supplyOrder->shipment->delivered_date)}}
                                    </div>
                                @endif
                            @elseif($supplyOrder->is_cleared_for_shipment)
                                <i class="fa fa-user-nurse"></i>
                                Cleared for shipment
                                <div class="text-secondary text-sm mt-1">
                                    By {{$supplyOrder->clearedForShipmentPro->displayName()}}<br>
                                    on {{friendlier_date_time($supplyOrder->cleared_for_shipment_at)}}
                                </div>
                            @else
                                <i class="fa fa-user-nurse"></i>
                                Not cleared for shipment
                            @endif
                        </td>
                        <td>{{$supplyOrder->lot_number}}</td>
                        <td>{{$supplyOrder->imei}}</td>
                        <td>
                            {{friendlier_date_time($supplyOrder->created_at)}}
                            <div class="text-secondary text-sm mt-1">
                                By {{$supplyOrder->createdSession->pro->displayName()}}
                            </div>
                        </td>
                        <td>{!! $supplyOrder->is_cancelled ? '<b class="text-warning-mellow">Yes</b>' : 'No' !!}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <div>
                {{$supplyOrders->links()}}
            </div>
        </div>
    </div>
    </div>

    <script>
        (function() {

            function applyFilters() {
                let params = {}, queryLine = [];
                $('[data-filter]').each(function() {
                    if($.trim($(this).val())) {
                        params[$(this).attr('data-filter')] = $.trim($(this).val());
                    }
                });
                for(let x in params) {
                    if(params.hasOwnProperty(x)) {
                        queryLine.push(x + '=' + encodeURIComponent(params[x]));
                    }
                }
                queryLine = queryLine.join('&');

                fastLoad('/practice-management/supply-orders?' + queryLine);
            }

            function init() {
                $('select[data-filter]')
                    .off('change')
                    .on('change', applyFilters);
                $('input[data-filter]')
                    .off('keyup')
                    .on('keyup', function(_event) {
                        if(_event.which === 13) {
                            applyFilters();
                            return false;
                        }
                    });
            }

            addMCInitializer('practice-supply-orders', init, '#practice-supply-orders')

        }).call(window);
    </script>
@endsection
