@extends ('layouts/template')

@section('content')

    <?php
    $showProgramsColumn = false;
    foreach($patients as $patient) {
        if(count($patient->clientPrograms)) {
            if($pro->pro_type === 'ADMIN') {
                $showProgramsColumn = true;
                break;
            }
            else {
                foreach($patient->clientPrograms as $clientProgram) {
                    if(in_array($pro->id, [$clientProgram->mcp_pro_id, $clientProgram->manager_pro_id])) {
                        $showProgramsColumn = true;
                        break;
                    }
                }
                if($showProgramsColumn) break;
            }
        }
    }
    ?>

    <div class="p-3 mcp-theme-1">
    <div class="card">

        <div class="card-header px-3 py-2 d-flex align-items-center">
            <strong class="mr-4">
                <i class="fas fa-user-injured"></i>
                Patients
            </strong>
            <select class="ml-auto max-width-300px form-control form-control-sm" onchange="fastLoad('/patients/' + this.value, true, false, false)">
                <option value="" {{ $filter === '' ? 'selected' : '' }}>All patients</option>
                <option value="not-yet-seen" {{ $filter === 'not-yet-seen' ? 'selected' : '' }}>Patients I have not seen yet</option>
            </select>
        </div>
        <div class="card-body p-0">
            <table class="table table-condensed p-0 m-0">
                <thead class="bg-light">
                <tr>
                    @if($pro->pro_type === 'ADMIN')
                    <th class="border-0"></th>
                    @endif
                    <th class="px-3 border-0">Chart #</th>
                    <th class="border-0">Patient</th>
                    <th class="border-0">Created At</th>
                    @if($showProgramsColumn)<th class="border-0">Program(s)</th>@endif
                    <th class="border-0">MCN</th>
                    <th class="border-0">PCP</th>
                    {{--<th class="border-0">RMM</th>--}}
                    <th class="border-0">Upcoming Appointments</th>
                </tr>
                </thead>
                <tbody>
                @foreach($patients as $patient)
                    <tr>
                        @if($pro->pro_type === 'ADMIN')
                        <td>{{$loop->index + 1}}</td>
                        @endif
                        <td class="px-3">
                            <a href="{{route('patients.view.dashboard', $patient)}}">
                                {{$patient->chart_number}}
                            </a>
                        </td>
                        <td>
                            {{$patient->displayName()}}
                            @if($patient->has_mcp_done_onboarding_visit !== 'YES')
                            <span title="MCP Onboarding Visit Pending"><i class="fa fa-exclamation-triangle"></i></span>
                            @endif
                            <div>{{ friendly_date_time($patient->dob, false) }}{{ $patient->sex === 'M' ? ', Male' : ($patient->sex === ', F' ? 'Female' : '') }}</div>
                        </td>
                        <td>{{friendly_date_time_short_with_tz($patient->created_at, true, 'EASTERN')}}</td>
                        @if($showProgramsColumn)
                        <td>
                            <?php $programNumber = 0; ?>
                            @foreach($patient->clientPrograms as $clientProgram)
                                <?php
                                    if($pro->pro_type === 'ADMIN' ||
                                        in_array($pro->id, [$clientProgram->mcp_pro_id, $clientProgram->manager_pro_id])
                                    ) {
                                        // $program = $clientProgram->program;
                                        $programNumber++;
                                ?>
                                <div class="mb-1 text-nowrap">
                                    {{ $programNumber }}. {{ $clientProgram->title }}
                                    @if($clientProgram->has_mcp_done_onboarding_visit !== 'YES')
                                        <span title="Onboarding Pending" class="ml-1"><i class="fa fa-exclamation-triangle"></i></span>
                                    @else
                                        <span title="Onboarding Complete" class="ml-1 text-secondary"><i class="fa fa-check"></i></span>
                                    @endif
                                </div>
                                <?php } ?>
                            @endforeach
                        </td>
                        @endif
                        <td>
                            @if($patient->was_medicare_validation_successful && $patient->is_part_b_primary == 'YES')
                            Covered <span style="color:green"><i class="fa fa-check-circle"></i></span>
                            @elseif($patient->was_medicare_validation_successful)
                                <div>Valid Medicare Number</div>
                                    <div class="text-secondary">Not Medicare Part B</div>
                                    @if($patient->is_medicare_advantage == 'YES')
                                        <div class="text-secondary">Medicare Advantage: <b>{{$patient->medicare_advantage_plan}}</b></div>
                                    @endif
                            @else
                                @if($patient->mcn)
                                    <div>Invalid MCN</div>
                                @else
                                    <div>No Info Provided</div>
                                @endif
                            @endif
                        </td>
                        <td>
                            {{ $patient->mcp ? $patient->mcp->displayName() : '-' }}
                        </td>
                        {{--<td>
                            {{ $patient->rmm ? $patient->rmm->displayName() : '-' }}
                        </td>--}}
                        <td>
                            <table class="table table-sm border-0 my-0">
                                <tbody>
                                <?php $numAppts = 0; ?>
                                @foreach($patient->upcomingAppointments as $appointment)
                                    @if($appointment->status !== 'CANCELLED' && $appointment->status !== 'ABANDONED')
                                    <tr>
                                        <td class="text-black p-0 border-0">
                                            <div class="pb-0">
                                                {{ friendly_date_time($appointment->start_time, false) }}, {{ friendly_time($appointment->raw_start_time) }}
                                                <span class="d-inline-block text-secondary text-sm">({{ $appointment->timezone }})</span>
                                                &nbsp;/&nbsp;
                                                <b class="mr-1">{{$appointment->pro->displayName()}}</b>
                                                <span class="text-secondary text-sm">({{ $appointment->status }})</span>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php $numAppts++; ?>
                                    @endif
                                @endforeach
                                @if(!$numAppts)
                                    <tr>
                                        <td class="text-secondary p-0 border-0">
                                            No upcoming appointments
                                        </td>
                                    </tr>
                                @endif
                                </tbody>
                            </table>
                        </td>
                    </tr>
                @endforeach
                </tbody>

            </table>
            <div class="ml-2 mt-2">
             {{$patients->links()}}
            </div>
        </div>
    </div>
    </div>

@endsection
