@extends ('layouts/template')

@section('content')

    <div class="p-3 mcp-theme-1" id="practice-billing-manager">

        <div class="card">

            <div class="card-header px-3 py-2 d-flex align-items-center">
                <strong class="mr-4 font-size-14">
                    <i class="fas fa-user-injured"></i>
                    Billing Manager
                    @if(request()->input('date'))
                        - Notes on {{friendlier_date(request()->input('date'))}}
                    @endif
                </strong>
                @if(!request()->input('popupmode'))
                <select class="ml-auto max-width-300px form-control form-control-sm"
                        onchange="fastLoad('/practice-management/billing-manager/' + this.value, true, false, false)">
                    <option value="" {{ $proUid === '' ? 'selected' : '' }}>All Pros</option>
                    @foreach($allPros as $_pro)
                        <option value="{{$_pro->uid}}" {{ $proUid === $_pro->uid ? 'selected' : '' }}>{{$_pro->displayName()}}</option>
                    @endforeach
                </select>
                @endif
            </div>
            @if(!request()->input('popupmode'))
            <div class="bg-aliceblue border-bottom px-3 py-2 d-flex align-items-center">
                <div class="d-inline-flex align-items-center">
                    <span class="border-bottom c-pointer" title="Filter notes with/without bills on them">Bills Created</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['bills_created'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="bills_created">
                        <option value="">All</option>
                        <option value="yes" {{$filters['bills_created'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['bills_created'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <div class="d-inline-flex align-items-center ml-3">
                    <span class="border-bottom c-pointer" title="Filter notes with/without billing marked as done">Billing Marked As Done</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['is_billing_marked_done'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="is_billing_marked_done">
                        <option value="">All</option>
                        <option value="yes" {{$filters['is_billing_marked_done'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['is_billing_marked_done'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <div class="d-inline-flex align-items-center ml-3">
                    <span class="border-bottom c-pointer" title="Filter notes with/without any bills that are neither cancelled nor verified">Bills Resolved</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['bills_resolved'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="bills_resolved">
                        <option value="">All</option>
                        <option value="yes" {{$filters['bills_resolved'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['bills_resolved'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <div class="d-inline-flex align-items-center ml-3">
                    <span class="border-bottom c-pointer" title="Filter notes with/without billing closed">Bills Closed</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['bills_closed'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="bills_closed">
                        <option value="">All</option>
                        <option value="yes" {{$filters['bills_closed'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['bills_closed'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <div class="d-inline-flex align-items-center ml-3">
                    <span class="border-bottom c-pointer" title="Filter notes with/without claims on them">Claims Created</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['claims_created'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="claims_created">
                        <option value="">All</option>
                        <option value="yes" {{$filters['claims_created'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['claims_created'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <div class="d-inline-flex align-items-center ml-3">
                    <span class="border-bottom c-pointer" title="Filter notes with/without claiming closed">Claims Closed</span>
                    <select class="form-control form-control-sm min-width-unset width-70px ml-2 {{ !!$filters['claims_closed'] ? 'border-info font-weight-bold text-info' : 'text-secondary on-hover-opaque' }}"
                            data-filter="claims_closed">
                        <option value="">All</option>
                        <option value="yes" {{$filters['claims_closed'] === 'yes' ? 'selected' : ''}}>Yes</option>
                        <option value="no" {{$filters['claims_closed'] === 'no' ? 'selected' : ''}}>No</option>
                    </select>
                </div>
                <a href="/practice-management/billing-manager" class="ml-auto">Clear Filters</a>
            </div>
            @endif
            <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">Patient</th>
                        <th class="border-0">Note Eff. Date</th>
                        <th class="border-0">HCP</th>
                        <th class="border-0">Signed?</th>
                        <th class="border-0">Bills</th>
                        @if($performer->pro->pro_type == 'ADMIN')
                            <th class="border-0">Assessments</th>
                        @endif
                        @if($performer->pro->pro_type == 'ADMIN')
                            <th class="border-0">Claims</th>
                        @endif
                        <th class="px-3 border-0">Created</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($notes as $note)
                        <tr class="{{ $note->is_cancelled ? 'cancelled-item always-clickable' : '' }}">
                            <td class="">
                                <a {{ request()->input('popupmode') ? 'native target="_blank"' : '' }} href="/patients/view/{{ $note->client->uid }}">{{ $note->client->displayName() }}</a>
                            </td>
                            <td class="">
                                <div class="font-weight-bold">{{ friendly_date_time($note->effective_dateest, false) }}</div>
                                <a href="/patients/view/{{ $note->client->uid }}/notes/view/{{ $note->uid }}?popupmode=1"
                                   native target="_blank"
                                   class="note-popup-trigger d-block text-nowrap mt-1">
                                    <i class="fa fa-eye"></i>
                                    View
                                </a>
                                <a {{ request()->input('popupmode') ? 'native target="_blank"' : '' }} href="/patients/view/{{ $note->client->uid }}/notes/view/{{ $note->uid }}"
                                   class="d-block text-nowrap mt-1">
                                    <i class="fa fa-external-link-alt"></i>
                                    Open
                                </a>
                                <span class="ml-1">{{ $note->is_cancelled ? '[cancelled]' : '' }}</span>
                            </td>
                            <td>{{$note->hcpPro->displayName()}}</td>
                            <td>{{$note->is_signed_by_hcp?'Yes':'No'}}</td>
                            <td>
                                <table class="table table-sm table-condensed table-stripe">
                                    <thead>
                                    <tr>
                                        <th>Service</th>
                                        <th># of units</th>
                                        <th>Signed?</th>
                                        <th>Cancelled?</th>
                                        <th>HCP ex. amount</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    @foreach($note->bills as $bill)
                                        <tr>
                                            <td>{{$bill->code}}</td>
                                            @if($bill->code == 'Treatment Services')
                                                <td>{{floor($bill->number_of_units*60)}}m</td>
                                            @else
                                                <td>{{$bill->number_of_units}}</td>
                                            @endif
                                            <td>{{$bill->is_signed_by_hcp?'Yes':'No'}}</td>
                                            <td>{{ $bill->is_cancelled ?'Yes':'No'}}</td>
                                            <td>{{ $bill->hcp_expected_payment_amount }}</td>
                                        </tr>
                                    @endforeach
                                    </tbody>
                                </table>
                            </td>
                            @if($performer->pro->pro_type == 'ADMIN')
                                <td>
                                    <table class="table table-sm table-condensed table-stripe">
                                        <thead>
                                        <tr>
                                            <th>Code</th>
                                            <th>Description</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        @foreach($note->reasons as $reason)
                                            <tr>
                                                <td>{{$reason->code}}</td>
                                                <td>{{$reason->description}}</td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                </td>
                            @endif
                            @if($performer->pro->pro_type == 'ADMIN')
                                <td>
                                    <table class="table table-sm table-condensed table-stripe">
                                        <thead>
                                        <tr>
                                            <th>Identifier</th>
                                            <th>Submitted?</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        @foreach($note->claims as $claim)
                                            <tr>
                                                <td>{{$claim->iid}}</td>
                                                <td>{{$claim->was_submitted?'Yes':'No'}}</td>
                                            </tr>
                                            <tr>
                                                <td colspan="2">
                                                    <strong>Claim Lines</strong>
                                                    <table class="table table-sm table-condensed table-striped">
                                                        <tr>
                                                            <th>CPT</th>
                                                            <th>ICDs</th>
                                                            <th>Date of Service</th>
                                                        </tr>
                                                        @foreach($claim->lines as $claimLine)
                                                            <tr>
                                                                <td>{{$claimLine->cpt}}</td>
                                                                <td>{{$claimLine->icds()}}</td>
                                                                <td>{{$claimLine->date_of_service}}</td>
                                                            </tr>
                                                        @endforeach
                                                    </table>
                                                </td>
                                            </tr>
                                        @endforeach
                                        </tbody>
                                    </table>
                                </td>
                            @endif
                            <td class="px-3">
                                {{ friendly_date_time($note->created_at, true) }}
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                <div>
                    {{$notes->withQueryString()->links()}}
                </div>
            </div>
        </div>
    </div>

    <div class="stag-popup stag-popup-lg mcp-theme-1" stag-popup-key="note-popup">
        <form method="POST" action="" class="p-0">
            <h3 class="stag-popup-title mb-0 mt-3 mx-3 pb-0 border-bottom-0">
                <span>Note Details</span>
                <a href="#" class="ml-auto text-secondary"
                   onclick="return closeStagPopup()"><i class="fa fa-times-circle"></i></a>
            </h3>
            <div class="pb-3 note-popup-content">
                <div class="px-3 pt-3 text-secondary font-italic">Loading ...</div>
            </div>
        </form>
    </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/billing-manager?' + queryLine);
            }

            function init() {
                $('select[data-filter]')
                    .off('change')
                    .on('change', applyFilters);
                $('.note-popup-trigger')
                    .off('click')
                    .on('click', function() {
                        showMask();
                        window.noMc = true;
                        $.get(this.href, (_data) => {
                            $('.note-popup-content').html(_data);
                            // not readonly anymore
                            // $('.note-popup-content')
                            //     .find('a, button, input, select')
                            //         .attr('disabled', 'disabled')
                            //         .attr('tabindex', '-1')
                            //         .css('pointer-events', 'none');
                            showStagPopup('note-popup');
                            // run note single initer and allow edits
                            runMCInitializer('note-single');
                            hideMask();
                        });
                        return false;
                    });
            }

            addMCInitializer('practice-billing-manager', init, '#practice-billing-manager')

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