@extends ('layouts/template')

@section('content')

<div class="p-3 mcp-theme-1" id="processing-bill-matrix">

    <div class="card">

        <div class="card-header px-2 py-1">
            <div class="font-weight-bold text-nowrap mb-2">
                <i class="fas fa-user"></i>
                Processing Bills (RPM)
            </div>
            <div class="d-flex align-items-center mb-2">
                <div class="max-width-110px">
                    <label class="text-sm text-secondary mb-1">Pro</label>
                    <select provider-search data-pro-uid="{{ @$targetPro->uid }}" name="proUid" class="form-control form-control-sm mr-auto width-200px min-width-unset" onchange="return billsFilter('', this.value, true)">
                        <option value="" {{!@$targetPro ? 'selected' : ''}}>All Pros</option>
                    </select>
                </div>
                <div class="ml-2">
                    <label class="text-sm text-secondary mb-1">Client</label>
                    <input  type="text" class="form-control form-control-sm" name="clientName"value="{{request()->input('clientName')}}" onchange="return billsFilter('clientName', this.value)">
                </div>
                <div class="max-width-110px ml-2">
                    <label class="text-sm text-secondary mb-1">Verification</label>
                    <select name="filter" class="form-control form-control-sm min-width-unset flex-grow-1" onchange="return billsFilter('f', this.value)">
                        <option {{request()->input('f') === '' ? 'selected' : ''}} value="">All</option>
                        <option {{request()->input('f') === 'verified' ? 'selected' : ''}} value="verified">Verified Only</option>
                        <option {{request()->input('f') === 'not-verified' ? 'selected' : ''}} value="not-verified">Not Verified Only</option>
                    </select>
                </div>
                <div class="ml-2">
                    <label class="text-sm text-secondary mb-1">Effective Date</label>
                    <div class="d-flex align-items-center">
                        <input type="date" value="{{request()->input('bs')}}" onchange="return billsFilter('bs', this.value)" class="form-control form-control-sm width-150px" name="bpdFrom">
                        <span class="mx-2">to</span>
                        <input type="date" value="{{request()->input('be')}}" onchange="return billsFilter('be', this.value)" class="form-control form-control-sm width-150px" name="bpdTo">
                    </div>
                </div>
                <div class="max-width-110px ml-2">
                    <label class="text-sm text-secondary mb-1">Claims</label>
                    <select name="filter" class="form-control form-control-sm min-width-unset flex-grow-1" onchange="return billsFilter('c', this.value)">
                        <option {{request()->input('c') === '' ? 'selected' : ''}} value="">All</option>
                        <option {{request()->input('c') === 'no-claims' ? 'selected' : ''}} value="no-claims">No Claims</option>
                        <option {{request()->input('c') === 'has-claims' ? 'selected' : ''}} value="has-claims">Has Claims</option>
                    </select>
                </div>
                <div class="max-width-110px ml-2">
                    <label class="text-sm text-secondary mb-1">99454</label>
                    <select name="filter" class="form-control form-control-sm min-width-unset flex-grow-1" onchange="return billsFilter('c4', this.value)">
                        <option {{request()->input('c4') === '' ? 'selected' : ''}} value="">All</option>
                        <option {{request()->input('c4') === 'no' ? 'selected' : ''}} value="no">No 99454</option>
                        <option {{request()->input('c4') === 'yes' ? 'selected' : ''}} value="yes">Has 99454</option>
                    </select>
                </div>
                <div class="max-width-110px ml-2">
                    <label class="text-sm text-secondary mb-1">99457</label>
                    <select name="filter" class="form-control form-control-sm min-width-unset flex-grow-1" onchange="return billsFilter('c7', this.value)">
                        <option {{request()->input('c7') === '' ? 'selected' : ''}} value="">All</option>
                        <option {{request()->input('c7') === 'no' ? 'selected' : ''}} value="no">No 99457</option>
                        <option {{request()->input('c7') === 'yes' ? 'selected' : ''}} value="yes">Has 99457</option>
                    </select>
                </div>
                <div class="max-width-110px ml-2">
                    <label class="text-sm text-secondary mb-1">99458</label>
                    <select name="filter" class="form-control form-control-sm min-width-unset flex-grow-1" onchange="return billsFilter('c8', this.value)">
                        <option {{request()->input('c8') === '' ? 'selected' : ''}} value="">All</option>
                        <option {{request()->input('c8') === 'no' ? 'selected' : ''}} value="no">No 99458</option>
                        <option {{request()->input('c8') === 'yes' ? 'selected' : ''}} value="yes">Has 99458</option>
                    </select>
                </div>
                @if(@$targetPro || count(array_filter(request()->all(), function($_x) { return !!$_x && $_x !== 'na' && $_x !== 'hcp';})))
                    <div class="ml-3">
                        <label class="text-sm text-secondary mb-1">&nbsp;</label>
                        <a href="/practice-management/processing-bill-matrix2" class="d-block">Clear Filters</a>
                    </div>
                @endif
            </div>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-sm table-striped table-hover p-0 m-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="border-bottom-0 border-top-0">Care Month</th>
                            <th class="border-bottom-0 border-top-0">Effective Date</th>
                            @if(!request()->input('t') || request()->input('t') === 'hcp')
                            <th class="border-bottom-0 border-top-0">Pro</th>
                            @elseif(request()->input('t') === 'na')
                            <th class="border-bottom-0 border-top-0">Pro</th>
                            @endif
                            <th class="border-bottom-0 border-top-0">Client</th>
                            <th class="border-bottom-0 border-top-0">Insurance</th>
                            <th class="border-bottom-0 border-top-0">State</th>
                            <th class="border-bottom-0 border-top-0">Company Pro</th>
{{--                            <th class="border-bottom-0 border-top-0">Code</th>--}}
                            <th class="border-bottom-0 border-top-0">Units</th>
                            <th class="border-bottom-0 border-top-0">Verified?</th>
                            <th class="border-bottom-0 border-top-0">Cancelled?</th>
                            <th class="border-bottom-0 border-top-0">Has Claim?</th>
                            @if(!request()->input('t') || request()->input('t') === 'hcp')
                            <th class="border-bottom-0 border-top-0">HCP Expected Amount</th>
                            @elseif(request()->input('t') === 'na')
                            <th class="border-bottom-0 border-top-0">NA Expected Amount</th>
                            @endif
                            <th class="border-bottom-0 border-top-0">Pro Signed?</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($bills as $row)
                        <tr>

                            <td>
                                @if($row->careMonth)
                                    <div>
                                        <a target="_blank" native href="/mc/patients/view/{{$row->client->uid}}/care-months/view/{{$row->careMonth->uid}}">
                                            Care Month
                                        </a>
                                    </div>
                                @endif
                            </td>
                            <td>{{is_null($row->effective_date) ? 'NULL' : friendly_date($row->effective_date)}}</td>
                            <td>
                                {{$row->genericPro->name_last}}, {{$row->genericPro->name_first}}
                            </td>
                            <td>
                                @if($row->client)
                                {{$row->client->name_last}}, {{$row->client->name_first}}
                                @endif
                            </td>
                            <td>
                            @if($row->client)
                                @include('app.patient.coverage_column_renderer', ['patient'=>$row->client])
                            @endif
                            </td>
                            <td>
                                @if($row->client)
                                {{$row->client->mailing_address_state}}
                                @endif
                            </td>
                            <td>
                                    
{{--                                    @if(!request()->input('t') || request()->input('t') === 'hcp')--}}
{{--                                        <div class="mt-1 text-secondary">--}}
{{--                                            @if($row->hcpCompanyPro)--}}
{{--                                                {{$row->hcpCompanyPro ? $row->hcpCompanyPro->company->name : '-'}}--}}
{{--                                            @else --}}
{{--                                                <i>[company pro is not set]</i>--}}
{{--                                            @endif--}}
{{--                                            <div moe class="ml-1">--}}
{{--                                                <a class="text-primary" href="" show start><i class="fa fa-edit"></i></a>--}}
{{--                                                <form url="/api/bill/swapHcpCompanyPro">--}}
{{--                                                    <input type="hidden" name="uid" value="{{$row->uid}}">--}}
{{--                                                    <div class="mb-2">--}}
{{--                                                        <select name="hcpCompanyProUid" class="form-control form-control-sm">--}}
{{--                                                            <option value="">-- select --</option>--}}
{{--                                                            @foreach($row->hcp->companyPros as $companyPro)--}}
{{--                                                                <option value="{{$companyPro->uid}}">{{$companyPro->company->name}}</option>--}}
{{--                                                            @endforeach--}}
{{--                                                        </select>--}}
{{--                                                    </div>--}}
{{--                                                    <div class="mb-0">--}}
{{--                                                        <button class="btn btn-primary btn-sm" submit>Submit</button>--}}
{{--                                                        <button class="btn btn-default border btn-sm" cancel>Cancel</button>--}}
{{--                                                    </div>--}}
{{--                                                </form>--}}
{{--                                            </div>--}}
{{--                                        </div>--}}
{{--                                    @elseif(request()->input('t') === 'na')--}}
                                        
                                        <div class="mt-1 text-secondary">
                                            @if($row->genericCompanyPro && $row->genericCompanyPro->company)
                                                {{$row->genericCompanyPro->company->name}}
                                            @else 
                                                <i class="text-warning-dark">[company pro is not set]</i>
                                            @endif
                                            @if(count($row->genericPro->companyPros))
                                                <div moe class="ml-1">
                                                    <a class="text-primary" href="" show start><i class="fa fa-edit"></i></a>
                                                    <form url="/api/bill/swapGenericCompanyPro">
                                                        <input type="hidden" name="uid" value="{{$row->uid}}">
                                                        <div class="mb-2">
                                                            <select name="genericCompanyProUid" class="form-control form-control-sm">
                                                                <option value="">-- select --</option>
                                                                @foreach($row->genericPro->companyPros as $companyPro)
                                                                    <option value="{{$companyPro->uid}}">{{$companyPro->company->name}}</option>
                                                                @endforeach
                                                            </select>
                                                        </div>
                                                        <div class="mb-0">
                                                            <button class="btn btn-primary btn-sm" submit>Submit</button>
                                                            <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            @else
                                                <div class="text-warning-dark">No company associations for
                                                    {{$row->genericPro->displayName()}}</div>
                                            @endif
                                        </div>
                                        
{{--                                    @endif--}}
                                        
                                </td>
{{--                            <td>{{$row->code}}--}}
{{--                                @if($row->generic_target_entity_type && $row->generic_target_entity_uid)--}}
{{--                                <div class="mt-1 text-sm text-secondary">{{$row->generic_target_entity_type}}</div>--}}
{{--                                @endif--}}
{{--                            </td>--}}
                            <td>{{str_contains($row->code, 'Treatment Services') || str_contains($row->code, 'Administrative Services') ? ceil((float) $row->number_of_units * 60) . ' mins' : $row->number_of_units}}</td>
                            <td>
                                {!! $row->is_verified ? '<i class="fa fa-check text-success"></i> ' . friendly_date($row->marked_verified_at, true) : 'No' !!} 
                                @if(!$row->is_cancelled)
                                    @if(!$row->is_verified)
                                        <div class="d-inline-block ml-1" moe>
                                            <a href="" show start>Mark Verified</a>
                                            <form url="/api/bill/markAsVerified">
                                                <input type="hidden" name="uid" value="{{$row->uid}}">
                                                <p>Mark As Verified?</p>
                                                <div class="mb-0">
                                                    <button class="btn btn-success btn-sm" submit>Submit</button>
                                                    <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                </div>
                                            </form>
                                        </div>
                                    @else
                                        <div class="d-inline-block ml-1" moe>
                                            <a class="" href="" show start>Undo</a>
                                            <form url="/api/bill/undoMarkAsVerified">
                                                <input type="hidden" name="uid" value="{{$row->uid}}">
                                                <p>Undo Mark As Verified?</p>
                                                <div class="mb-0">
                                                    <button class="btn btn-success btn-sm" submit>Submit</button>
                                                    <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                </div>
                                            </form>
                                        </div>
                                    @endif
                                @endif
                            </td>

                            <td>
                                @if($row->is_cancelled)
                                    <div class="text-warning-mellow font-weight-bold">Cancelled</div>
                                    @if($row->cancellation_memo)
                                        <div class="text-dark text-sm font-italic my-1">{{$row->cancellation_memo}}</div>
                                    @endif
                                    @if($row->is_cancelled_by_administrator)
                                        <div class="text-secondary text-sm text-nowrap">(by Administrator)</div>
                                    @endif
                                    <div moe class="mt-1">
                                        <a class="text-nowrap" href="" show start>Update Memo</a>
                                        <form url="/api/bill/updateCancellationMemo">
                                            <input type="hidden" name="uid" value="{{$row->uid}}">
                                            <p>Update Cancellation Memo</p>
                                            <div class="mb-2">
                                                <textarea class="text form-control form-control-sm" name="cancellationMemo"
                                                        placeholder="">{{$row->cancellation_memo ? $row->cancellation_memo : ($pro->pro_type === 'ADMIN' ? 'Please contact billing.' : '')}}</textarea>
                                            </div>
                                            <div>
                                                <button class="btn btn-success btn-sm" submit>Submit</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                @else
                                    <div class="d-block" moe relative="">
                                        <a class="text-danger" href="" show start>Cancel</a>
                                        <form url="/api/bill/markCancelled" right="">
                                            <input type="hidden" name="uid" value="{{$row->uid}}">
                                            <p class="mb-2">Cancel this bill?</p>
                                            <div class="mb-2">
                                                <label class="mb-1 text-secondary">Cancellation Memo</label>
                                                <textarea type="text" name="memo" placeholder="Memo" class="form-control form-control-sm">@if($pro->pro_type === 'ADMIN')Please contact billing.@endif</textarea>
                                            </div>
                                            <div class="mb-0">
                                                <button class="btn btn-danger btn-sm" submit>Yes</button>
                                                <button class="btn btn-default border btn-sm" cancel>No</button>
                                            </div>
                                        </form>
                                    </div>
                                @endif
                            </td>
                            <td>
                                @if($row->careMonth)
                                    @if(count($row->careMonth->claims))
                                        <span class="text-dark">Yes</span>
                                        <?php
                                        $cptCodes = [];
                                        foreach($row->careMonth->claims as $claim) {
                                            foreach ($claim->lines as $line) {
                                                $cptCodes[] = $line->cpt;
                                            }
                                        }
                                        ?>
                                        @if(count($cptCodes))
                                            ({{implode(', ', $cptCodes)}})
                                        @endif
                                    @else
                                        <span class="text-secondary">No</span>
                                    @endif
                                @else
                                    N/A
                                @endif
                            </td>
                            @if(!request()->input('t') || request()->input('t') === 'hcp')
                            <td>${{$row->hcp_expected_payment_amount}}</td>
                            @elseif(request()->input('t') === 'na')
                            <td>${{number_format($row->generic_pro_expected_payment_amount, 2)}}</td>
                            @endif
                            @if(!request()->input('t') || request()->input('t') === 'hcp')
                            <td>{{$row->is_signed_by_hcp ? 'Yes' : 'No'}}</td>
                            @elseif(request()->input('t') === 'na')
                            <td>{{$row->is_signed_by_generic_pro ? 'Yes' : 'No'}}</td>
                            @endif
                           
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="mt-3">
        {{$bills->withQueryString()->links()}}
    </div>
</div>
<script>
    (function() {
        window.billsFilter = function(_key, _value, _isClient = false) {

            <?php
            $keys = ['f', 'bs', 'be', 't', 'c', 's', 'c4', 'c7', 'c8'];
            $currentParams = [];
            for ($i = 0; $i < count($keys); $i++) {
                if (!!request()->input($keys[$i])) {
                    $currentParams[$keys[$i]] = request()->input($keys[$i]);
                }
            }
            ?>

            let targetProUid = _isClient ? _value : $('[provider-search]').attr('data-pro-uid'),
                base = '/practice-management/processing-bill-matrix2' + (targetProUid ? '/' + targetProUid : ''),
                keys = ['f', 'bs', 'be', 't', 'c', 's'].filter(_x => _x !== _key),
                currentParams = {!!json_encode($currentParams) !!},
                url = [];

            // base
            url.push(base);
            url.push('?');

            // params
            let getParams = [];
            for (let i = 0; i < keys.length; i++) {
                if (currentParams[keys[i]]) {
                    getParams.push(keys[i] + '=' + encodeURIComponent(currentParams[keys[i]]));
                }
            }

            // new params
            getParams.push(_key + '=' + encodeURIComponent(_value));

            // generate url
            url.push(getParams.join('&'));

            // go
            fastLoad(url.join(''));

            return false;
        };
    }).call(window);
</script>
@endsection
