@extends ('layouts/template')

@section('content')
    <div class="p-3 mcp-theme-1">
        <div class="card">
            <div class="card-header px-2 py-1 d-flex align-items-center">
                <strong class="mr-4">
                    <i class="fas fa-user"></i>
                    Coverage Center
                </strong>
            </div>
            <div class="card-header p-2">
                <form class="d-flex align-items-start" onsubmit="return fastLoad('{{route('practice-management.coverages')}}?' + $(this).serialize());">

                    <div>
                        <label class="mb-0 text-sm text-secondary">Patient</label>
                        <input type="text" class="mr-2 form-control form-control-sm min-width-unset max-width-110px" name="f_name" value="{{request()->input('f_name')}}">
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">Covered</label>
                        <div class="d-flex align-items-start">
                            <select name="f_covered" class="mr-2 form-control form-control-sm min-width-unset max-width-110px pl-0">
                                <option {{!request()->input('f_covered') || request()->input('f_covered') === 'any' ? 'selected' : ''}} value="any">Any</option>
                                <option {{request()->input('f_covered') === 'covered' ? 'selected' : ''}} value="covered">Covered</option>
                                <option {{request()->input('f_covered') === 'not-covered' ? 'selected' : ''}} value="not-covered">Not Covered</option>
                                <option {{request()->input('f_covered') === 'unknown' ? 'selected' : ''}} value="unknown">Unknown</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">Type</label>
                        <div class="d-flex align-items-start">
                            <select name="f_plan_type" class="mr-2 form-control form-control-sm min-width-unset max-width-110px pl-0">
                                <option {{!request()->input('f_plan_type') || request()->input('f_plan_type') === 'any' ? 'selected' : ''}} value="any">Any</option>
                                <option {{request()->input('f_plan_type') === 'medicare' ? 'selected' : ''}} value="medicare">Medicare</option>
                                <option {{request()->input('f_plan_type') === 'medicaid' ? 'selected' : ''}} value="medicaid">Medicaid</option>
                                <option {{request()->input('f_plan_type') === 'commercial' ? 'selected' : ''}} value="commercial">Commercial</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="mb-0 text-sm 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 max-width-110px pl-0">
                                <option {{!request()->input('f_pb_primary') || request()->input('f_pb_primary') === 'any' ? 'selected' : ''}} value="any">Any</option>
                                <option {{request()->input('f_pb_primary') === 'primary' ? 'selected' : ''}} value="primary">Primary</option>
                                <option {{request()->input('f_pb_primary') === 'not-primary' ? 'selected' : ''}} value="not-primary">Not Primary</option>
                                <option {{request()->input('f_pb_primary') === 'unknown' ? 'selected' : ''}} value="unknown">Unknown</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">PB Active:</label>
                        <div class="d-flex align-items-start">
                            <select name="f_pb_active" class="mr-2 form-control form-control-sm min-width-unset max-width-110px pl-0">
                                <option {{!request()->input('f_pb_active') || request()->input('f_pb_active') === 'any' ? 'selected' : ''}} value="any">Any</option>
                                <option {{request()->input('f_pb_active') === 'active' ? 'selected' : ''}} value="active">Active</option>
                                <option {{request()->input('f_pb_active') === 'not-active' ? 'selected' : ''}} value="not-active">Not Active</option>
                                <option {{request()->input('f_pb_active') === 'unknown' ? 'selected' : ''}} value="unknown">Unknown</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">PC Active:</label>
                        <div class="d-flex align-items-start">
                            <select name="f_pc_active" class="mr-2 form-control form-control-sm min-width-unset max-width-110px pl-0">
                                <option {{!request()->input('f_pc_active') || request()->input('f_pc_active') === 'any' ? 'selected' : ''}} value="any">Any</option>
                                <option {{request()->input('f_pc_active') === 'active' ? 'selected' : ''}} value="active">Active</option>
                                <option {{request()->input('f_pc_active') === 'not-active' ? 'selected' : ''}} value="not-active">Not Active</option>
                                <option {{request()->input('f_pc_active') === 'unknown' ? 'selected' : ''}} value="unknown">Unknown</option>
                            </select>
                        </div>
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">Comm. Payer</label>
                        <input type="text" class="mr-2 form-control form-control-sm min-width-unset max-width-110px" name="f_comm_payer" value="{{request()->input('f_comm_payer')}}">
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">Comm. Member ID:</label>
                        <input type="text" class="mr-2 form-control form-control-sm min-width-unset max-width-110px" name="f_comm_member_id" value="{{request()->input('f_comm_member_id')}}">
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">Comm. Group #:</label>
                        <input type="text" class="mr-2 form-control form-control-sm min-width-unset max-width-110px" name="f_comm_group_num" value="{{request()->input('f_comm_group_num')}}">
                    </div>

                    <div>
                        <label class="mb-0 text-sm text-secondary">&nbsp;</label>
                        <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.coverages')}}?' + $(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.coverages')}}');">
                                <i class="fa fa-times"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="card-body p-0">
                <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">DOB</th>
                            <th class="border-0">Covered?</th>
                            <th class="border-0">Effective Coverage</th>
                            <th class="border-0 width-500px">Latest Auto Refresh</th>
                            <th class="border-0 width-500px">Latest Manual Verif.</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($rows as $row)
                            <?php
                            $lcpc = null;
                            $lacpc = null;
                            $lmcpc = null;
                            $lmcpc_payer = null;
                            if($row->latest_client_primary_coverage) {
                                $lcpc = json_decode($row->latest_client_primary_coverage);
                            }
                            if($row->latest_auto_client_primary_coverage) {
                                $lacpc = json_decode($row->latest_auto_client_primary_coverage);
                            }
                            if($row->latest_manual_client_primary_coverage) {
                                $lmcpc = json_decode($row->latest_manual_client_primary_coverage);
                            }

                            $coveredState = -1;
                            $effective = '-';
                            if($lmcpc && !$lmcpc->is_cancelled) {

                                if($lmcpc->manual_determination_category == 'COVERED') {
                                    $coveredState = 1;
                                }
                                else if($lmcpc->manual_determination_category == 'NOT_COVERED') {
                                    $coveredState = 2;
                                }
                                else {
                                    $coveredState = 3; // unknown
                                }

                                $effective = 'Latest Manual';
                                if($row->latest_manual_client_primary_coverage_payer) {
                                    $lmcpc_payer = json_decode($row->latest_manual_client_primary_coverage_payer);
                                }
                                $lmcpc->lmcpc_payer = $lmcpc_payer;
                            }
                            else if($lacpc && !$lacpc->is_cancelled) {

                                if($lacpc->auto_medicare_is_partbprimary == 'YES') {
                                    $coveredState = 1;
                                }
                                else if($lacpc->auto_medicare_is_partbprimary == 'NO') {
                                    $coveredState = 2;
                                }
                                else {
                                    $coveredState = 3; // unknown
                                }

                                $effective = 'Latest Auto';
                            }
                            ?>
                            <tr>
                                <td>
                                    <div class="d-flex align-items-baseline">
                                        <a href="/mc/patients/view/primary-coverage/{{$row->uid}}"
                                           native
                                           target="_blank">{{$row->client_name}}</a>
                                        <span class="ml-1 text-nowrap">({{$row->sex}})</span>
                                    </div>
                                </td>
                                <td>
                                    {{$row->dob}} ({{$row->age_in_years}} y)
                                </td>
                                <td>
                                    @if($coveredState === 1)
                                        <div class="text-success">
                                            <i class="fa fa-check"></i>
                                            Covered
                                        </div>
                                    @elseif($coveredState === 2)
                                        <div class="text-secondary">
                                            <i class="fa fa-times"></i>
                                            Not Covered
                                        </div>
                                    @else
                                        <div class="text-warning-dark">
                                            <i class="fa fa-exclamation-triangle"></i>
                                            Unknown
                                        </div>
                                    @endif
                                </td>
                                <td>{{$effective}}</td>
                                <td class="p-0">
                                    @if($lacpc && !$lacpc->is_cancelled)
                                        @include('app.practice-management._cpc', ['cpc' => $lacpc])
                                    @else
                                        -
                                    @endif
                                </td>
                                <td class="p-0">
                                    @if($lmcpc && !$lmcpc->is_cancelled)
                                        @include('app.practice-management._cpc', ['cpc' => $lmcpc])
                                    @else
                                        -
                                    @endif
                                </td>

                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </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> coverages</div>
            </div>
        </div>
    </div>
@endsection
