@extends ('layouts/template')

@section('content')

    <div id="practice-packs-multi-print" class="p-3 mcp-theme-1 only-screen">
    <div class="card border-bottom-0">
        <div class="card-header px-2 py-1 d-flex align-items-center bg-white">
            <strong class="font-size-14">
                Print Pack Labels
            </strong>
        </div>

        <div class="card-header px-3 py-1 d-flex align-items-center bg-white">
            <span id="num-selected" class="font-weight-bold text-secondary py-2">0 packs selected.</span>
            <button id="print-selected" class="btn btn-sm btn-info ml-3 d-none" type="button">Generate PDF with selected pack labels</button>
        </div>

        <div class="card-body p-0 border-0">
            <table class="table table-sm table-striped table-hover p-0 m-0 border-0" style="table-layout: fixed">
                <thead class="bg-light">
                <tr>
                    <th class="border-0 width-50px">
                        <label class="d-flex align-items-center m-0">
                            <input type="checkbox" class="my-0 mr-1" id="select-all">
                            <span class="font-weight-normal">All</span>
                        </label>
                    </th>
                    <th class="border-0">Patient</th>
                    <th class="border-0">Address</th>
                    <th class="border-0">Courier</th>
                    <th class="border-0">Tracking #</th>
                    <th class="border-0">Label</th>
                    <th class="border-0">Shipment</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($packs as $pack)
                    <tr data-pack-uid="{{$pack->uid}}"
                        data-pack-id="{{$pack->id}}">
                        <th class="border-right border-left-0">
                            <label class="d-flex align-items-center m-0">
                                <input type="checkbox" class="my-0 mr-1 row-select">
                            </label>
                        </th>
                        <td>{{$pack->shipment->client->displayName()}}</td>
                        <td>
                            {!! $pack->shipment->mailing_address_full ? implode(" ", [$pack->shipment->mailing_address_line1 . ' ' . $pack->shipment->mailing_address_line2, $pack->shipment->mailing_address_city . ' ' . $pack->shipment->mailing_address_state . ' ' . $pack->shipment->mailing_address_zip]) : '-'  !!}
                        </td>
                        <td>{{$pack->courier}}</td>
                        <td>{{$pack->tracking_number}}</td>
                        <td>
                            <a class="pdf-viewer-trigger" native target="_blank"
                               href="/api/pack/downloadLabel/{{ $pack->uid }}"
                               title="View">
                                <i class="fa fa-file-pdf text-danger on-hover-opaque"></i>
                                View
                            </a>
                        </td>
                        <td>{{$pack->shipment->status}}</td>
                    </tr>
                @endforeach
                </tbody>
                </table>
            </div>
        </div>
        </div>

        <div id="print-view" class="only-print"></div>

        <script>
            (function() {

                function updatePrintButton() {
                    let num = $('.row-select:checked').length;
                    $('#num-selected').text(num + ' pack' + (num === 1 ? '' : 's') + ' selected');
                    if(num) {
                        $('#print-selected').removeClass('d-none');
                    }
                    else {
                        $('#print-selected').addClass('d-none');
                    }
                }

                function hasError (_data) {
                    let msg = 'Unknown error!';
                    if (_data) {
                        if (_data.success) return false;
                        else if (_data.message) msg = _data.message;
                    }
                    toastr.error(msg);
                    return true;
                }

                function init() {
                    $('#select-all')
                        .off('change')
                        .on('change', function() {
                            $('.row-select').prop('checked', this.checked);
                            updatePrintButton();
                        });
                    $('.row-select')
                        .off('change')
                        .on('change', function() {
                            $('#select-all').prop('checked', $('.row-select:not(:checked)').length === 0);
                            updatePrintButton();
                        });
                    $('#print-selected')
                        .off('click')
                        .on('click', function() {
                            let uids = [];
                            $('.row-select:checked').each(function() {
                                uids.push($(this).closest('tr').attr('data-pack-uid'));
                            });
                            uids = uids.join('|');
                            let form = $('<form target="_blank" method="POST" action="/api/pack/bulkLabelPDF" />');
                            form.append($('<input type="hidden" name="packUids" />').val(uids));
                            form.appendTo('body').submit().remove();
                            return false;
                        });
                }

                addMCInitializer('practice-packs-multi-print', init, '#practice-packs-multi-print')

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