@extends ('layouts/template')

@section('content')

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

<div id="practice-shipments" class="p-3 mcp-theme-1">
    <div class="card">
        <div class="card-header px-2 py-1 d-flex align-items-center bg-white">
            <strong class="font-size-14">
                Shipments ({{@$counts['shipments']}})
            </strong>
            <span class="mx-3 text-secondary">|</span>
            <a href="{{route('practice-management.shipments-ready-to-print')}}">Ready to Print ({{@$counts['shipmentsReadyToPrint']}})</a>
            <span class="mx-3 text-secondary">|</span>
            <a href="{{route('practice-management.shipments-waiting-for-picker')}}">Printed &amp; Waiting for Picker ({{@$counts['shipmentsWaitingForPicker']}})</a>
            @if(count(request()->all()))
            <a href="{{route('practice-management.shipments')}}" class="ml-auto">Clear Filters</a>
            @endif
            <div class="ml-auto">
                @include('app.practice-management.shipment-scan')
            </div>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm table-striped p-0 m-0">
                    <thead class="bg-light text-nowrap">
                        <tr>
                            <th class="border-0"></th>
                            <th class="border-0">Client</th>
                            <th class="border-0 w-25">Supply Orders</th>
                            <th class="border-0">Courier</th>
                            <th class="border-0">Tracking #</th>
                            <th class="border-0">Label File</th>
                            <th class="border-0">Status</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"></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">
                                    <option value="">All</option>
                                    @foreach($shClients as $shClient)
                                    <option value="{{$shClient->id}}" {{$filters['client'] == $shClient->id ? 'selected' : ''}}>{{$shClient->displayName()}}</option>
                                    @endforeach
                                </select>
                            </th>
                            <th class="p-0 border-bottom-0 border-right"></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="courier" value="{{$filters['courier']}}" 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="tracking_num" value="{{$filters['tracking_num']}}" 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="label">
                                    <option value="">All</option>
                                    <option {{$filters['label'] == 'yes' ? 'selected' : ''}} value="yes">Present</option>
                                    <option {{$filters['label'] == 'no' ? 'selected' : ''}} value="no">Absent</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="status">
                                    <option value="">All</option>
                                    <?php $statuses = ['CREATED', 'PRINTED', 'BEING_PICKED', 'PICKED', 'READY_FOR_FULFILMENT', 'READY_FOR_DISPATCH', 'DISPATCHED']; ?>
                                    @foreach($statuses as $iStatus)
                                    <option {{$filters['status'] == $iStatus ? 'selected' : ''}} value="{{$iStatus}}">{{cleanStatus($iStatus)}}</option>
                                    @endforeach
                                </select>
                            </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 ($shipments as $shipment)
                        <tr class="{{$shipment->is_cancelled ? 'bg-light' : ''}}">
                            <td class="border-right"><a href="{{route('practice-management.shipment', $shipment->uid)}}">Open</a></td>
                            <td>{{$shipment->client->displayName()}}</a></td>
                            <td>
                                @if($shipment->supplyOrders && count($shipment->supplyOrders))
                                <table class="table table-sm table-striped table-bordered mb-0 bg-white">
                                    <thead>
                                        <tr class="">
                                            <th class="text-nowrap text-secondary border-bottom-0">Supply Order</th>
                                            <th class="text-nowrap text-secondary border-bottom-0">IMEI</th>
                                            <th class="text-nowrap text-secondary border-bottom-0">Lot #</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($shipment->supplyOrders as $iSupplyOrder)
                                        <tr class="">
                                            <td class="">{{ $iSupplyOrder->product->title }}</td>
                                            <td class="">{{ $iSupplyOrder->imei }}</td>
                                            <td class="">{{ $iSupplyOrder->lot_number }}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                                @else
                                None
                                @endif
                            </td>
                            <td>{{$shipment->courier}}</td>
                            <td>{{$shipment->tracking_number}}</td>
                            <td>
                                @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
                                </a>
                                @endif
                            </td>
                            <td>
                                {{ ucwords(strtolower($shipment->status)) }}
                                @if($shipment->status === 'DELIVERED' && $shipment->delivered_date)
                                <div class="text-secondary text-sm mt-1">
                                    on {{friendlier_date_time($shipment->delivered_date)}}
                                </div>
                                @endif
                            </td>
                            <td>
                                {{friendlier_date_time($shipment->created_at)}}
                                <div class="text-secondary text-sm mt-1">
                                    By {{$shipment->createdSession->pro->displayName()}}
                                </div>
                            </td>
                            <td>{!! $shipment->is_cancelled ? '<b class="text-warning-mellow">Yes</b>' : 'No' !!}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

        </div>
    </div>
    <div class="mt-3">
        {{$shipments->links()}}
    </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/shipments?' + 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-shipments', init, '#practice-shipments')

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