@extends ('layouts/template')

@section('content')
    <style>
      .iframes iframe {
        width: 100%;
        height: 800px;
      }
    </style>
    <div id="notes-processing-center" class="p-3 mcp-theme-1">
        <div class="card">

            <div class="card-header p-2 d-flex align-items-center">
                <strong class="mr-4">
                    <i class="fas fa-user"></i>
                    Notes Processing Center
                </strong>
            </div>

            <div class="card-header px-2 pt-2 pb-0 align-items-center bg-white">
                <form id="filter-form" class="d-flex flex-wrap align-items-center">
                    <div class="mr-2 mb-2 width-150px min-width-unset">
                        <label class="text-secondary mb-1">Pro</label>
                        <select class="form-control form-control-sm min-width-unset"
                                name="proUid"
                                provider-search
                                data-pro-uid="{{request()->input('proUid')}}"
                                required>
                            <option value="">-- Select Pro --</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Pro signed</label>
                        <select class="form-control form-control-sm min-width-unset" name="proSigned">
                            <option value="">All</option>
                            <option {{request()->input('proSigned') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('proSigned') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Billing marked done</label>
                        <select class="form-control form-control-sm min-width-unset" name="billingMarkedDone">
                            <option value="">All</option>
                            <option {{request()->input('billingMarkedDone') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('billingMarkedDone') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Billing Closed</label>
                        <select class="form-control form-control-sm min-width-unset" name="billingClosed">
                            <option value="">All</option>
                            <option {{request()->input('billingClosed') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('billingClosed') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Claiming Closed</label>
                        <select class="form-control form-control-sm min-width-unset" name="claimingClosed">
                            <option value="">All</option>
                            <option {{request()->input('claimingClosed') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('claimingClosed') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">All claims submitted</label>
                        <select class="form-control form-control-sm min-width-unset" name="allClaimsSubmitted">
                            <option value="">All</option>
                            <option {{request()->input('allClaimsSubmitted') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('allClaimsSubmitted') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Good/Bad</label>
                        <select class="form-control form-control-sm min-width-unset" name="goodBad">
                            <option value="">All</option>
                            <option {{request()->input('goodBad') === 'good' ? 'selected' : ''}} value="good">Good</option>
                            <option {{request()->input('goodBad') === 'bad' ? 'selected' : ''}} value="bad">Bad</option>
                            <option {{request()->input('goodBad') === 'unclassified' ? 'selected' : ''}} value="unclassified">Unclassified</option>
                        </select>
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Date From</label>
                        <input type="date" name="startDate" class="form-control form-control-sm min-width-unset"
                               value="{{request()->input('startDate')}}">
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">Date To</label>
                        <input type="date" name="endDate" class="form-control form-control-sm min-width-unset"
                               value="{{request()->input('endDate')}}">
                    </div>
                    <div class="mr-2 mb-2 width-150px">
                        <label class="text-secondary mb-1">MC Part B</label>
                        <select class="form-control form-control-sm min-width-unset" name="mcPartB">
                            <option value="">All</option>
                            <option {{request()->input('mcPartB') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('mcPartB') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                </form>



            </div>
            <div class="card-body p-0">

                <table class="table table-sm table-striped border-0 p-0 m-0">
                    <thead class="bg-light">
                    <tr>
                        <th>Patient</th>
                        <th>Date</th>
                        <th>HCP</th>
                        <th>New/FU</th>  <!-- and method -->
                        <th>ICDs</th>
                        <th>HCP <br>signed?</th>
                        <th>Billing <br>marked done?</th>
                        <th>Billing <br>closed?</th>
                        <th>Claiming <br>closed?</th>
                        <th>Bills</th>
                        <th>Claims</th>
                        <th>Pickup</th>
                        <th>Preview</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($notes as $note)
                        <?php $isCurrent = $note->currentNotePickupForProcessing && $note->currentNotePickupForProcessing->note && $note->currentNotePickupForProcessing->note->id === $note->id; ?>
                        <tr class="{{$isCurrent ? 'bg-aliceblue' : ''}}">
                            <td>
                                <a href="/patients/view/{{ $note->client->uid }}">
                                    {{ $note->client->displayName() }}
                                </a>
                            </td>
                            <td>
                                <a href="/patients/view/{{ $note->client->uid }}/notes/view/{{ $note->uid }}" class="font-weight-bold">
                                    {{ friendly_date_time($note->effective_dateest, false) }}
                                </a>
                            </td>
                            <td>
                                {{$note->hcpPro ? $note->hcpPro->displayName() : '-'}}
                            </td>
                            <td>
                                {{$note->new_or_fu_or_na}}
                                @if($note->method)
                                    <span class="text-secondary text-sm ml-1">({{noteMethodDisplay($note->method)}})</span>
                                @endif
                            </td>
                            <td>
                                <div class="d-flex flex-wrap width-100px">
                                    @foreach($note->reasons() as $reason)
                                        <span class="border-bottom c-pointer mr-2 mb-1" title="{{$reason->description}}">{{$reason->code}}</span>
                                    @endforeach
                                </div>
                            </td>
                            <td>
                                @if($note->is_signed_by_hcp)
                                    <i class="fa fa-check text-info"></i>
                                @else
                                    <i class="fa fa-times text-warning-mellow"></i>
                                @endif
                            </td>
                            <td>
                                @if($note->is_billing_marked_done)
                                    <i class="fa fa-check text-info"></i>
                                @else
                                    <i class="fa fa-times text-warning-mellow"></i>
                                @endif
                            </td>
                            <td>
                                @if($note->is_bill_closed)
                                    <i class="fa fa-check text-info"></i>
                                @else
                                    <i class="fa fa-times text-warning-mellow"></i>
                                @endif
                            </td>
                            <td>
                                @if($note->is_claim_closed)
                                    <i class="fa fa-check text-info"></i>
                                @else
                                    <i class="fa fa-times text-warning-mellow"></i>
                                @endif
                            </td>
                            <td>
                                @foreach($note->bills as $bill)
                                    @if(!$bill->is_cancelled)
                                        <div class="mb-1 d-flex align-items-baseline">
                                            <b>{{$bill->code}}</b>
                                            <?php
                                            $mins = round($bill->number_of_units * 60);
                                            ?>
                                            <span class="ml-2">{{$mins}} min{{$mins !== 1 ? 's' : ''}}</span>
                                        </div>
                                    @endif
                                @endforeach
                            </td>
                            <td>
                                @foreach($note->claims as $claim)
                                    @if(!$claim->is_cancelled && $claim->lines->count())
                                        @foreach($claim->lines as $line)
                                        <div class="mb-1 d-flex align-items-baseline">
                                            <b>{{$line->cpt}}</b>
                                            <span class="ml-2">{{friendlier_date($line->date_of_service)}}</span>
                                            <span class="ml-2">
                                                @if(count($line->claimLineIcds))
                                                    @foreach($line->claimLineIcds as $icd)
                                                        <span class="c-pointer border-bottom" title="{{$icd->description}}">{{$icd->code}}</span>
                                                    @endforeach
                                                @endif
                                            </span>
                                        </div>
                                        @endforeach
                                    @endif
                                @endforeach
                            </td>
                            <td>
                                @if($isCurrent)
                                    <span class="text-info font-weight-bold">CURRENT</span>
                                @else
                                    <a href="#" data-uid="{{$note->uid}}" class="pick-up-for-processing">Pickup</a>
                                @endif
                            </td>
                            <td>
                                <a native target="_blank"
                                   open-in-stag-popup
                                   update-parent
                                   mc-initer="note-single"
                                   title="Note Details"
                                   href="/patients/view/{{ $note->client->uid }}/notes/view/{{ $note->uid }}">
                                    Preview
                                </a>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>

                {{ $notes->withQueryString()->links() }}

            </div>
        </div>
        @if($pro->currentNotePickupForProcessing && $pro->currentNotePickupForProcessing->note)
            <?php
            $cnPatientUid = $pro->currentNotePickupForProcessing->note->client->uid;
            $cnNoteUid = $pro->currentNotePickupForProcessing->note->uid;
            ?>
            <div class="row iframes pt-5">
                <div class="col-md-5">
                    <iframe id="target-chart" data-src="/patients/view/{{$cnPatientUid}}/notes" native></iframe>
                </div>
                <div class="col-md-7">
                    <iframe id="target-note" data-src="/patients/view/{{$cnPatientUid}}/notes/view/{{$cnNoteUid}}" native></iframe>
                </div>
            </div>
        @endif
    </div>

    <script>
        (function () {
            function init() {
                $('.pick-up-for-processing')
                    .off('click')
                    .on('click', function() {
                         $.post('/api/note/pickUpForProcessing', {
                             uid: $(this).attr('data-uid')
                         }, _data => {
                             if(!hasResponseError(_data)) {
                                 fastReload();
                             }
                         }, 'json');
                         return false;
                    });
                $('#filter-form select,#filter-form  input')
                    .off('change')
                    .on('change', function() {
                        let params = $('#filter-form').serialize();
                        fastLoad('/practice-management/notes-processing-center?' + params);
                        return false;
                    });
                @if($pro->currentNotePickupForProcessing && $pro->currentNotePickupForProcessing->note)
                    $('#target-chart').attr('src', $('#target-chart').attr('data-src'));
                    $('#target-note').attr('src', $('#target-note').attr('data-src'));
                    window.setTimeout(() => {
                        $('.row.iframes')[0].scrollIntoView();
                    }, 250);
                @endif
            }
            addMCInitializer('notes-processing-center', init, '#notes-processing-center')
        }).call(window);
    </script>

@endsection
