@extends ('layouts/template')

@section('content')

	<style>tr.bgyellow>td, tr.bgyellow>th { background-color: lightyellow; }</style>
	<style>tr.bggreen>td, tr.bggreen>th { background-color: #caffca; }</style>
	<style>tr.bgblue>td, tr.bgblue>th { background-color: #d0f2fd; }</style>
	<style>tr.claim-closed>td:not(:first-child), tr.claim-closed>th:not(:first-child) { background-color: lightgray; }</style>

    <div id="notes-resolution-center" class="px-3 pt-3 mcp-theme-1" v-cloak>

        <div class="d-flex align-items-baseline mb-3">
            <h6 class="font-size-16 font-weight-bold text-secondary my-0 mr-4">Notes Resolution Center</h6>
            <!--<form class="ml-5 d-inline-flex align-items-center" action="{{route('practice-management.notes-resolution-center-v2')}}" method="GET">
                <span class="">Farah:</span>
                <select name="f" class="form-control form-control-sm min-width-unset max-width-110px ml-2"
                        onchange="fastLoad('{{route('practice-management.notes-resolution-center-v2')}}?' + $(this).closest('form').serialize()); return false;">
                    <option value="">Any</option>
                    <option {{ request()->input('f') === 'green' ? 'selected' : '' }} value="green">Green</option>
                    <option {{ request()->input('f') === 'red' ? 'selected' : '' }} value="red">Red</option>
                </select>
                <span class=" ml-3">Shawn:</span>
                <select name="s" class="form-control form-control-sm min-width-unset max-width-110px ml-2"
                        onchange="fastLoad('{{route('practice-management.notes-resolution-center-v2')}}?' + $(this).closest('form').serialize()); return false;">
                    <option value="">Any</option>
                    <option {{ request()->input('s') === 'green' ? 'selected' : '' }} value="green">Green</option>
                    <option {{ request()->input('s') === 'red' ? 'selected' : '' }} value="red">Red</option>
                </select>
            </form>-->
            <form class="d-inline-flex flex-grow-1 align-items-stretch" onsubmit="return fastLoad('{{route('practice-management.notes-resolution-center-v2')}}?' + $(this).serialize());">

                <div class="d-inline-flex align-items-baseline mr-2">
                    <label class="text-nowrap mb-0 text-sm mr-1 {{request()->input('f_pb_primary') && request()->input('f_pb_primary') !== 'any' ? 'text-info' : 'text-secondary'}}">PB Primary?</label>
                    <div class="d-flex align-items-start">
                        <select name="f_pb_primary" class="mr-2 form-control form-control-sm min-width-unset width-70px pl-0">
                            <option {{!request()->input('f_pb_primary') || request()->input('f_pb_primary') === 'any' ? 'selected' : ''}} value="any">All</option>
                            <option {{request()->input('f_pb_primary') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('f_pb_primary') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                </div>

                <div class="d-inline-flex align-items-baseline mr-2">
                    <label class="text-nowrap mb-0 text-sm mr-1 {{request()->input('f_deductible_zero') && request()->input('f_deductible_zero') !== 'any' ? 'text-info' : 'text-secondary'}}">Deductible 0?</label>
                    <div class="d-flex align-items-start">
                        <select name="f_deductible_zero" class="mr-2 form-control form-control-sm min-width-unset width-70px pl-0">
                            <option {{!request()->input('f_deductible_zero') || request()->input('f_deductible_zero') === 'any' ? 'selected' : ''}} value="any">All</option>
                            <option {{request()->input('f_deductible_zero') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('f_deductible_zero') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                </div>

                <div class="d-inline-flex align-items-baseline mr-2">
                    <label class="text-nowrap mb-0 text-sm mr-1 {{request()->input('f_note_hcp') ? 'text-info' : 'text-secondary'}}">Note HCP:</label>
                    <select name="f_note_hcp" class="form-control form-control-sm min-width-unset max-width-110px" provider-search provider-type="hcp" data-pro-uid="{{request()->input('f_note_hcp')}}">
                        <option value="">--select--</option>
                    </select>
                </div>

                <div class="d-inline-flex align-items-baseline mr-2">
                    <label class="text-nowrap mb-0 text-sm mr-1 {{request()->input('f_2022_patient') && request()->input('f_2022_patient') !== 'any' ? 'text-info' : 'text-secondary'}}">2022 Pt.?</label>
                    <div class="d-flex align-items-start">
                        <select name="f_2022_patient" class="mr-2 form-control form-control-sm min-width-unset width-70px pl-0">
                            <option {{!request()->input('f_2022_patient') || request()->input('f_2022_patient') === 'any' ? 'selected' : ''}} value="any">All</option>
                            <option {{request()->input('f_2022_patient') === 'yes' ? 'selected' : ''}} value="yes">Yes</option>
                            <option {{request()->input('f_2022_patient') === 'no' ? 'selected' : ''}} value="no">No</option>
                        </select>
                    </div>
                </div>

                <div class="d-flex align-items-start">
                    <button type="button" class="btn-apply btn btn-sm btn-primary mr-2"
                            onclick="return fastLoad('{{route('practice-management.notes-resolution-center-v2')}}?' + $(this).closest('form').serialize());">
                        <i class="fa fa-check"></i>
                    </button>
                    <button type="button" class="btn-reset btn btn-sm btn-default bg-white text-secondary border"
                            onclick="return fastLoad('{{route('practice-management.notes-resolution-center-v2')}}');">
                        <i class="fa fa-times"></i>
                    </button>
                </div>
            </form>
        </div>

        <div class="d-flex align-items-start">
            <div class="overflow-overlay-on-hover width-300px max-width-300px min-width-300px border-bottom row-scroller" style="height: calc(100vh - 170px);">
                <table class="table table-sm table-bordered table-striped m-0 w-100">
                    <thead>
                    <tr>
                        <th class="border-bottom-0 text-nowrap width-22px"></th>
                        <th class="border-bottom-0 text-nowrap width-30px text-right pr-2">V#</th>
                        <th class="border-bottom-0 text-nowrap">Details</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($rows as $row)
                        @php
                            $parsedDetailJson = new stdClass();
                            if($row->detail_json) {
                                try {
                                    $parsedDetailJson = json_decode($row->detail_json);
                                }
                                catch (Exception $e) {}
                            }
                        @endphp
                        <tr class="{{request('uid') === $row->uid ? 'current-row' : ''}} {{ $row->visit_number > 2 ? 'bgyellow':'' }} {{ $row->visit_number == 1 ? 'bggreen first-visit':'' }} {{ $row->visit_number == 2 ? 'bgblue':'' }} {{ $row->is_claim_closed ? 'claim-closed' : '' }}"
                            data-uid="{{$row->uid}}">
                            <td class="{{request('uid') === $row->uid ? 'bg-info' : ''}}"></td>
                            <td class="text-nowrap text-right pr-2">{{$row->visit_number}}</td>
                            <td class="p-0">
                                <div class="p-1 {{request('uid') === $row->uid ? 'border border-info' : ''}}">
                                    <div class="text-nowrap d-flex align-items-baseline" >
                                        @if(request('uid') === $row->uid)
                                            <b>{{$row->client_name}}</b>
                                        @else
                                            <a class="text-nowrap select-row" href="/practice-management/notes-resolution-center-v2?uid={{$row->uid}}&{{queryLineExcept(['uid'])}}" data-uid="{{$row->uid}}">{{$row->client_name}}</a>
                                        @endif
                                        <a href="/patients/view/{{ $row->client_uid }}/notes/view/{{ $row->uid }}?suggestion_mode=on" target="top" native class="ml-2"><i class="fa fa-arrow-right"></i></a>
                                    </div>
                                    <a href="/patients/view/{{$row->client_uid}}" class="text-nowrap d-block pl-2" target="top" native>Chart #: {{$row->chart_number}}</a>
                                    <span class="text-nowrap d-block pl-2">Pro: {{$row->hcp_name}}</span>
                                    <span class="text-nowrap d-block pl-2">Date: {{friendly_date($row->effective_dateest)}}</span>
                                    <span class="text-nowrap d-block pl-2">Minutes: {{$row->minutes}}</span>
					@for($i = 1; $i < 5; $i++)
                                    		<span class="text-nowrap d-block pl-2">
							@if($row->{"icd".$i}) 
								{{ $row->{"icd".$i} }} {{$row->{"icd".$i."description"} }}
							@endif
						</span>
					@endfor
                                </div>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
            <div class="flex-grow-1 border-top">
                @if(request('uid'))
                    <div class="bg-white">
                        @php
                            $note = \App\Models\Note::where('uid', request('uid'))->first();
                            $patient = $note->client;
                        @endphp
                        @include('app.patient.note.partials.resolve-content')
                    </div>
                @endif
            </div>
        </div>

        <div class="d-flex align-items-baseline mt-3">
            {!! $paginator->withQueryString()->links() !!}
            <div class="mb-3">
                <div class="ml-4 mb-3">Showing <b>{{$paginator->firstItem()}}</b> to <b>{{$paginator->lastItem()}}</b> (page {{$paginator->currentPage()}}) of <b>{{$paginator->total()}}</b> notes</div>
            </div>
        </div>

    </div>

    <script>
        (function() {
            function init() {
                @php $nrcPage = request()->input('page') ?: 1; @endphp
                let parent = $('#notes-resolution-center');
                parent.find('.row-scroller')
                    .off('scroll')
                    .on('scroll', function() {
                        localStorage.nrcv2RowScrollTop_{{$nrcPage}} = $(this).scrollTop();
                    });
                @if(request('uid'))
                window.setTimeout(() => {
                    if(localStorage.nrcv2RowScrollTop_{{$nrcPage}}) {
                        parent.find('.row-scroller').scrollTop(+localStorage.nrcv2RowScrollTop_{{$nrcPage}});
                    }
                    else {
                        let selectedTr = parent.find('tr[data-uid="{{request('uid')}}"]').first();
                        if (selectedTr.length) {
                            selectedTr[0].scrollIntoView({behavior: "auto", block: "center"});
                        }
                    }
                    parent.removeAttr('v-cloak');
                }, 0);
                @else
                localStorage.nrcv2RowScrollTop_{{$nrcPage}} = '';
                parent.find('tr[data-uid]').first().find('.select-row').trigger('click');
                @endif
                parent.removeAttr('v-cloak');

                parent
                    .off('click.next-row', '.btn-next-row')
                    .on('click.next-row', '.btn-next-row', function () {
                        let currentRow = parent.find('tr.current-row').first(),
                            nextRow = false;
                        while(currentRow.length) {
                            if(currentRow.next().is('.first-visit:not(.claim-closed)')) {
                                nextRow = currentRow.next();
                                break;
                            }
                            currentRow = currentRow.next();
                        }
                        if(nextRow && nextRow.length) {
                            localStorage.nrcv2RowScrollTop_{{$nrcPage}} = '';
                            nextRow.find('.select-row').trigger('click');
                        }
                        else {
                            toastr.error('No can do!');
                        }
                        return false;
                    });

                parent
                    .off('click.prev-row', '.btn-prev-row')
                    .on('click.prev-row', '.btn-prev-row', function () {
                        let currentRow = parent.find('tr.current-row').first(),
                            prevRow = false;
                        while(currentRow.length) {
                            if(currentRow.prev().is('.first-visit:not(.claim-closed)')) {
                                prevRow = currentRow.prev();
                                break;
                            }
                            currentRow = currentRow.prev();
                        }
                        if(prevRow && prevRow.length) {
                            localStorage.nrcv2RowScrollTop_{{$nrcPage}} = '';
                            prevRow.find('.select-row').trigger('click');
                        }
                        else {
                            toastr.error('No can do!');
                        }
                        return false;
                    });
            }
            addMCInitializer('notes-resolution-center', init, '#notes-resolution-center');
        }).call(window);
    </script>

@endsection
