@extends ('layouts/template')

@section('content')

    @if($pro->id === 1 || $pro->id === 16)
    <link href="/c3/c3.min.css" rel="stylesheet">
    <script src="/c3/d3.v5.min.js" charset="utf-8"></script>
    <script src="/c3/c3.min.js"></script>
    @endif

    <?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" id="patients-list">
    <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>
            <div class="ml-auto d-flex align-items-center search-form">
                <form action="" method="get" class="mr-2" id="patients-search">
                    <input type="text" name="q" value="{{request()->input('q')}}" class="form-control form-control-sm"
                           placeholder="Name / Email / Tags">
                </form>
                <select class="ml-auto max-width-300px form-control form-control-sm"
                        id="patients-filter">
                    <option value="" {{ $filter === '' ? 'selected' : '' }}>All patients</option>
                    <option value="not-yet-seen" {{ $filter === 'not-yet-seen' ? 'selected' : '' }}>Patients I have not seen yet</option>
                    <option value="having-birthday-today" {{ $filter === 'having-birthday-today' ? 'selected' : '' }}>Patients having birthday today</option>
                </select>
            </div>
        </div>

        @if($pro->id === 1 || $pro->id === 16)
        <div class="card-header px-3 py-4 d-flex align-items-center bg-white justify-content-center">
            <div id="patientAcquisitionChart" class="w-75"></div>
        </div>
        @endif

        <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 px-1">OB</th>
                    <th class="border-0">Signed<br>Notes</th>
                    <th class="border-0">Created At</th>
		            <th class="border-0">Address</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">Appointments</th>
                    <th class="border-0">Account</th>
                    <th class="border-0">Tags</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()}}
                            <div>{{ friendly_date_time($patient->dob, false) }}{{ $patient->sex === 'M' ? ', Male' : ($patient->sex === ', F' ? 'Female' : '') }}</div>
                        </td>
                        <td class="px-1">
                            @if($patient->has_mcp_done_onboarding_visit !== 'YES')
                                <span title="MCP Onboarding Visit Pending"><i class="fa fa-exclamation-triangle"></i></span>
                            @else
                                <i class="fa fa-check text-secondary on-hover-opaque"></i>
                            @endif
                        </td>
                        <td>
                            <?php $numSignedNotes = $patient->numSignedNotes(); ?>
                            <span class="{{$numSignedNotes && $patient->has_mcp_done_onboarding_visit !== 'YES' ? 'font-weight-bold text-warning-mellow' : 'text-secondary'}}">
                                {{$numSignedNotes ? $numSignedNotes :'-'}}
                            </span>
                        </td>
                        <td>
                            {{ friendly_date_time_short_with_tz($patient->created_at, true, 'EASTERN') }}
                            <hr>
                            @if($pro->pro_type === 'ADMIN')
                                {{ $patient->initiative }}
                            @endif
                        </td>
                        <td>
                            <?php
                            $addressParts = [];
                            if (!!$patient->mailing_address_line1) $addressParts[] = $patient->mailing_address_line1;
                            if (!!$patient->mailing_address_line2) $addressParts[] = $patient->mailing_address_line2;
                            $addressParts = implode(", ", $addressParts) . "<br/>";
                            $addressPart2 = [];
                            if (!!$patient->mailing_address_city) $addressPart2[] = $patient->mailing_address_city;
                            if (!!$patient->mailing_address_state) $addressPart2[] = $patient->mailing_address_state;
                            $addressParts .= implode(", ", $addressPart2);
                            echo $addressParts;
                            ?>
                        </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
                                    @if($patient->is_coverage_manually_verified)
                                        {{$patient->coverage_manual_verification_memo}} <span style="color:green"><i class="fa fa-check-circle">
                                    @else
                                        <div>No Info Provided</div>
                                    @endif

                                @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>
                        <td>
                            @if($patient->linkedAccounts && count($patient->linkedAccounts))
                                <span class="font-weight-bold text-info"><i class="fa fa-check"></i></span>
                            @else
                                <span class="text-secondary">-</span>
                            @endif
                        </td>
                        <td>
                            @if($patient->tags)
                                <?php
                                $tags = explode("|", $patient->tags);
                                $tags = array_filter($tags, function($_x) {
                                    return !empty($_x);
                                });
                                $tags = implode(", ", $tags);
                                ?>
                                {{ $tags }}
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>

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

    <script>
        (function() {
            function init() {
                function submit() {
                    let url = '/patients' +
                        ($('#patients-filter').val() ? '/' + $('#patients-filter').val() : '') +
                        ($('#patients-search input').val() ? '?q=' + encodeURIComponent($('#patients-search input').val()) : '');
                    fastLoad(url);
                    return false;
                }
                $('#patients-search').off('submit').on('submit', submit);
                $('#patients-filter').off('change').on('change', submit);

                @if($pro->id === 1 || $pro->id === 16)
                    patientAcquisitionChart();
                @endif
            }

            @if($pro->pro_type === 'ADMIN')
            <?php
            $dates = [];
            $acquisitions = [];
            for ($i = count($patientAcquisitionData) - 1; $i >= 0; $i--) {
                $dates[] = $patientAcquisitionData[$i]->date;
                $acquisitions[] = $patientAcquisitionData[$i]->count;
            }
            ?>
            function patientAcquisitionChart() {
                var chart = c3.generate({
                    bindto: '#patientAcquisitionChart',
                    data: {
                        x: 'x',
                        // xFormat: '%Y%m%d', // 'xFormat' can be used as custom format of 'x'
                        columns: [
                            ['x', <?= implode(", ", array_map(function($_x) { return "'" . $_x . "'"; }, $dates)) ?>],
                            ['New Patients', <?= implode(", ", array_map(function($_x) { return "'" . $_x . "'"; }, $acquisitions)) ?>],
                        ]
                    },
                    axis: {
                        x: {
                            type: 'timeseries',
                            tick: {
                                format: '%Y-%m-%d',
                                multiline: true,
                                fit: true,
                                rotate: -45
                            },
                        },
                    }
                });
            }
            @endif

            addMCInitializer('patients-list', init, '#patients-list');
        }).call(window);
    </script>
@endsection
