@extends ('layouts/template')

@section('content')

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

        <div class="card">

            <div class="card-header px-2 py-1 d-flex align-items-center">
                <strong class="mr-4 font-size-14">
                    <i class="fas fa-user"></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->name_first}} {{$_pro->name_last}}</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">
                <div class="stag-table-container stag-table-container-lg stag-table-container-no-x-scroll border-bottom">
                <table class="table table-sm table-striped 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">Created</th>
                        <th class="border-0">HCP</th>
                        <th class="border-0">Signed?</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
                    </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="d-flex align-items-baseline">
                                    <a {{ request()->input('popupmode') ? 'native target="_blank"' : '' }} href="/patients/view/{{ $note->client->uid }}/notes/view/{{ $note->uid }}"
                                       class="d-block text-nowrap mt-1 font-weight-bold">
                                        {{ friendly_date_time($note->effective_dateest, false) }}
                                    </a>
                                    <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 ml-3"
                                       title="View in popup">
                                        <i class="fa fa-eye"></i>
                                    </a>
                                </div>
                                <span class="ml-1">{{ $note->is_cancelled ? '[cancelled]' : '' }}</span>
                            </td>
                            <td class="">
                                {{ friendly_date_time($note->created_at, true) }}
                            </td>
                            <td>{{$note->hcpPro?$note->hcpPro->displayName():''}}</td>
                            <td>{{$note->is_signed_by_hcp?'Yes':'No'}}</td>
                            @if($performer->pro->pro_type == 'ADMIN')
                                <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>
                            @endif
                            @if($performer->pro->pro_type == 'ADMIN')
                                <td class="p-0">
                                    @if($note->claims->count())
                                        <table class="table table-sm tabe-striped m-0 border-left border-bottom-0">
                                            <thead class="bg-light">
                                            <tr>
                                                <th class="border-0 pl-2">IID</th>
                                                <th class="border-0">Details</th>
                                                <th class="border-0">Current Version</th>
                                                <th class="border-0">Status</th>
                                                <th class="border-0">Submissions</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            @foreach ($note->claims as $claim)
                                                @if($claim->status !== 'CANCELLED' || request('claims-filter') === 'all')
                                                    <tr class="bg-secondary on-hover-opaque">
                                                        <td colspan="5" class="pt-0 pb-1"></td>
                                                    </tr>
                                                    <tr class="{{ $claim->status === 'CANCELLED' ? 'text-secondary bg-light on-hover-opaque' : '' }}">
                                                    <td class="pl-2">
                                                        <div>{{ $claim->iid }}</div>
                                                        @if($claim->status === 'CANCELLED')
                                                            <div class="text-secondary font-weight-bold text-sm mt-1">[CANCELLED]</div>
                                                        @endif
                                                    </td>
                                                    <td class="p-0 border-left border-right">
                                                        @if($claim->lines->count())
                                                            <table class="table table-sm table-condensed border-0 mb-0">
                                                                <thead>
                                                                <tr class="bg-light">
                                                                    <th class="border-0">CPT</th>
                                                                    <th class="border-0">DOS</th>
                                                                    <th class="border-0">ICDs</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody class="pb-3">
                                                                @foreach($claim->lines as $line)
                                                                    <tr class="claim-line">
                                                                        <td>{{$line->cpt}}</td>
                                                                        <td class="text-nowrap">{{friendlier_date($line->date_of_service)}}</td>
                                                                        <td>
                                                                            @if(count($line->claimLineIcds))
                                                                                @foreach($line->claimLineIcds as $icd)
                                                                                    <div>
                                                                                        <b class="c-pointer border-secondary border-bottom" title="{{$icd->description}}">{{$icd->code}}</b>
                                                                                    </div>
                                                                                @endforeach
                                                                            @else
                                                                                <p>No ICDs set</p>
                                                                            @endif
                                                                        </td>
                                                                    </tr>
                                                                @endforeach
                                                                </tbody>
                                                            </table>
                                                        @else
                                                            <p>No lines for this claim</p>
                                                        @endif
                                                    </td>
                                                    <td class="border-right">
                                                        {{$claim->currentVersion ? friendlier_date_time($claim->currentVersion->created_at) : '-'}}
                                                    </td>
                                                    <td class="border-right p-0">

                                                        <!-- status -->
                                                        <div class="p-1 border-bottom">
                                                            <div>
                                                                <span class="text-secondary text-sm">Status: </span>{{$claim->status ? $claim->status : '(not set)'}}
                                                            </div>
                                                            @if($claim->status_memo)
                                                                <div class="text-secondary font-italic text-sm">{{$claim->status_memo}}</div>
                                                            @endif
                                                        </div>

                                                        <!-- payer, company, location -->
                                                        <div class="p-1">
                                                            <div class="mt-1">
                                                                <span class="text-secondary text-sm">Payer (1°): </span>
                                                                <b>{{$claim->primaryPayer ? $claim->primaryPayer->name : '-'}}</b>
                                                            </div>

<!--                                                            <div class="mt-1">
                                                                <span class="text-secondary text-sm">Payer (2°): </span>
                                                                <b>-</b>
                                                            </div>-->

                                                            <div class="mt-1">
                                                                <span class="text-secondary text-sm">Company: </span>
                                                                <b>{{$claim->companyPro && $claim->companyPro->company ? $claim->companyPro->company->name : '-'}}</b>
                                                            </div>

                                                            <div class="mt-1">
                                                                <span class="text-secondary text-sm">Location: </span>
                                                                <b>{{$claim->companyLocation ? $claim->companyLocation->line1 . ', ' . $claim->companyLocation->city : '-'}}</b>
                                                            </div>
                                                        </div>
                                                    </td>
                                                    <td class="p-0 border-left border-right">
                                                        <table class="table table-sm table-condensed border-0 mb-0">
                                                            @if(count($claim->mbClaims))
                                                                <thead>
                                                                <tr class="bg-light">
                                                                    <th class="border-0">Created</th>
                                                                    <th class="border-0">Status</th>
                                                                    <th class="border-0">&nbsp;</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody class="pb-3">
                                                                @foreach($claim->mbClaims as $mbClaim)
                                                                    <tr>
                                                                        <td>
                                                                            @if($mbClaim->claim_version_id !== $claim->current_version_id)
                                                                                <i class="fa fa-exclamation-triangle text-warning-mellow text-sm" title="This submission was generated using an older version of the claim"></i>
                                                                            @else
                                                                                <i class="fa fa-asterisk text-secondary text-sm" title="This submission is up to date"></i>
                                                                            @endif
                                                                            {{ friendlier_date_time($mbClaim->created_at) }}
                                                                        </td>
                                                                        <td>
                                                                            {{$mbClaim->status}}
                                                                            @if($mbClaim->status_memo)
                                                                                <div class="font-italic text-secondary text-sm">{{$mbClaim->status_memo}}</div>
                                                                            @endif
                                                                        </td>
                                                                        <td class="text-right pr-2">
                                                                            <a class="ml-1" target="_blank" native href="/api/mbClaim/downloadEDI/{{$mbClaim->uid}}" show start><i class="fa fa-file-download"></i></a>
                                                                        </td>
                                                                    </tr>
                                                                @endforeach
                                                                </tbody>
                                                            @else
                                                                <tbody>
                                                                <tr>
                                                                    <td colspan="3">
                                                                        <p class="p-1 text-secondary m-0">No submissions in this claim</p>
                                                                    </td>
                                                                </tr>
                                                                </tbody>
                                                            @endif
                                                        </table>
                                                    </td>
                                                    </tr>
                                                @endif
                                            @endforeach
                                            </tbody>
                                        </table>
                                    @else
                                        <p class="p-1 mb-0 text-secondary">No claims in this note</p>
                                    @endif
                                </td>
                            @endif
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                </div>
                <div class="pt-3 pl-3">
                    {{$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
