@extends ('layouts/template')

@section('content')

    <div id="practice-bills" 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>
                Bills
            </strong>
            <a href="/practice-management/bill-matrix" class="ml-auto">Clear Filters</a>
        </div>
        <div class="card-body p-0 border-0">
            <table class="table table-sm table-condensed table-bordered border-0 p-0 m-0">
                <thead class="bg-light">
                <tr>
                    <th class="border-bottom-0 border-top-0 border-left-0">Date</th>
                    <th class="border-bottom-0 border-top-0">Client</th>
                    <th class="border-bottom-0 border-top-0">Note</th>
                    <th class="border-bottom-0 border-top-0">Service</th>
                    <th class="border-bottom-0 border-top-0">Billable</th>
                    <th class="border-bottom-0 border-top-0">HCP</th>
                    <th class="border-bottom-0 border-top-0">Expected</th>
                    <th class="border-bottom-0 border-top-0">Paid</th>
                    <th class="border-bottom-0 border-top-0">Sign</th>
                    <th class="border-bottom-0 border-top-0">Bal Post Date</th>
                    <th class="border-bottom-0 border-top-0">Verification</th>
                    <th class="border-bottom-0 border-top-0">Cancellation</th>
                    <th class="border-bottom-0 border-top-0 border-right-0">Payment</th>
                </tr>
                <tr class="filter-head">
                    <th class="p-0 border-bottom-0 border-right border-left-0"></th> <!-- date -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- client -->
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="client">
                            <option value="">All</option>
                            @foreach($bClients as $bClient)
                                <option value="{{$bClient->id}}"
                                    {{$filters['client'] == $bClient->id ? 'selected' : ''}}>{{$bClient->displayName()}}</option>
                            @endforeach
                        </select>
                    </th> <!-- Client -->
                    <th class="p-0 border-bottom-0 border-right"></th> <!-- Note -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- service type -->
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="service">
                            <option value="">All</option>
                        </select>
                    </th> <!-- Service -->
                    <th class="p-0 border-bottom-0 border-right"></th> <!-- billable -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- hcp, hcp_paid -->
                        <div class="d-flex align-items-center">
                            <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-left-0 border-top-0 border-bottom-0 border-right w-50"
                                    data-filter="hcp">
                                <option value="">All</option>
                                @foreach($bHCPPros as $bHCPPro)
                                    <option value="{{$bHCPPro->id}}"
                                        {{$filters['hcp'] == $bHCPPro->id ? 'selected' : ''}}>{{$bHCPPro->displayName()}}</option>
                                @endforeach
                            </select>
                            <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0 w-50"
                                    data-filter="hcp_paid">
                                <option value="">All</option>
                                <option {{$filters['hcp_paid'] == 'yes' ? 'selected' : ''}} value="yes">Paid</option>
                                <option {{$filters['hcp_paid'] == 'no' ? 'selected' : ''}} value="no">Not Paid</option>
                            </select>
                        </div>
                    </th> <!-- HCP -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- expected -->
                        <div class="d-flex align-items-center">
                            <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-left-0 border-top-0 border-bottom-0 border-right width-40px p-0"
                                    data-filter="expected_op">
                                <option value=""></option>
                                <option value="<=" {{$filters['expected_op'] == '<=' ? 'selected' : ''}}>&lt;=</option>
                                <option value=">=" {{$filters['expected_op'] == '>=' ? 'selected' : ''}}>&gt;=</option>
                                <option value="=" {{$filters['expected_op'] == '=' ? 'selected' : ''}}>=</option>
                            </select>
                            <input type="number" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                   data-filter="expected_value"
                                   value="{{$filters['expected_value']}}" placeholder="Amount">
                        </div>
                    </th> <!-- Expected -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- paid -->
                        <div class="d-flex align-items-center">
                            <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-left-0 border-top-0 border-bottom-0 border-right width-40px p-0"
                                    data-filter="paid_op">
                                <option value=""></option>
                                <option value="<=" {{$filters['paid_op'] == '<=' ? 'selected' : ''}}>&lt;=</option>
                                <option value=">=" {{$filters['paid_op'] == '>=' ? 'selected' : ''}}>&gt;=</option>
                                <option value="=" {{$filters['paid_op'] == '=' ? 'selected' : ''}}>=</option>
                            </select>
                            <input type="number" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                   data-filter="paid_value"
                                   value="{{$filters['paid_value']}}" placeholder="Amount">
                        </div>
                    </th> <!-- Paid -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- hcp signed -->
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="hcp_sign">
                            <option value="">All</option>
                            <option {{$filters['hcp_sign'] == 'yes' ? 'selected' : ''}} value="yes">Signed</option>
                            <option {{$filters['hcp_sign'] == 'no' ? 'selected' : ''}} value="no">Not Signed</option>
                        </select>
                    </th> <!-- Sign -->
                    <th class="p-0 border-bottom-0 border-right">
                        <div class="d-flex align-items-center">
                            <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-left-0 border-top-0 border-bottom-0 border-right width-40px p-0"
                                    data-filter="bal_post_date_op">
                                <option value=""></option>
                                <option value="<=" {{$filters['bal_post_date_op'] == '<=' ? 'selected' : ''}}>&lt;=</option>
                                <option value=">=" {{$filters['bal_post_date_op'] == '>=' ? 'selected' : ''}}>&gt;=</option>
                                <option value="=" {{$filters['bal_post_date_op'] == '=' ? 'selected' : ''}}>=</option>
                            </select>
                            <input type="date" class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                   data-filter="bal_post_date_value"
                                   value="{{$filters['bal_post_date_value']}}" placeholder="Amount">
                        </div>
                    </th> <!-- Bal Post Date -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- verified -->
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="verified">
                            <option value="">All</option>
                            <option {{$filters['verified'] == 'yes' ? 'selected' : ''}} value="yes">Verified</option>
                            <option {{$filters['verified'] == 'no' ? 'selected' : ''}} value="no">Not Verified</option>
                        </select>
                    </th> <!-- verification -->
                    <th class="p-0 border-bottom-0 border-right"> <!-- cancelled -->
                        <select class="form-control form-control-sm bg-aliceblue min-width-unset rounded-0 shadow-none border-0"
                                data-filter="cancelled">
                            <option value="">All</option>
                            <option {{$filters['cancelled'] == 'yes' ? 'selected' : ''}} value="yes">Cancelled</option>
                            <option {{$filters['cancelled'] == 'no' ? 'selected' : ''}} value="no">Not Cancelled</option>
                        </select>
                    </th> <!-- cancellation -->
                    <th class="p-0 border-bottom-0 border-right-0"></th> <!-- payment -->
                </tr>
                </thead>
                <tbody>
                @foreach ($bills as $bill)
                    <tr class="{{$bill->is_cancelled ? 'bg-light' : ''}}">
                        <td class="text-nowrap border-left-0">{{friendlier_date_time($bill->effective_date, false)}}</td>
                        <td>
                            <a href="/patients/view/{{$bill->client->uid}}">{{$bill->client->displayName()}}</a>
                        </td> <!-- client -->
                        <td>
                            @if($bill->note)
                                <a href="/patients/view/{{$bill->client->uid}}/notes/view/{{$bill->note->uid}}" class="font-weight-bold">View</a>
                            @else
                                -
                            @endif
                        </td> <!-- note -->
                        <td>{{$bill->code}}</td> <!-- service type / code -->
                        <td class="">
                            @if(strpos(strtolower($bill->code), 'treatment services') !== FALSE)
                                <?php
                                $totalSeconds = $bill->number_of_units * 3600;
                                $remainder = $totalSeconds % 60;
                                if ($remainder !== 0) {
                                    if ($remainder < 30) {
                                        $totalSeconds = $totalSeconds - $remainder;
                                    }
                                    else {
                                        $totalSeconds = $totalSeconds + (60 - $remainder);
                                    }
                                }
                                ?>
                                {{ time_in_hrminsec($totalSeconds) }}
                            @else
                                @if(!!$bill->number_of_units)
                                    {{ $bill->number_of_units }} unit(s)
                                @else
                                    -
                                @endif
                            @endif
                        </td> <!-- minutes / units -->
                        <td class="">
                            <div class="text-nowrap font-weight-bold text-secondary">{{ $bill->hcp->displayName() }}</div>
                            <div class="text-nowrap mt-1">
                                <span class="text-secondary">Paid: </span>
                                <span>{{ $bill->has_hcp_been_paid ? 'Yes' : 'No' }}</span>
                            </div>
                            @if(!$bill->has_hcp_been_paid)
                                <div class="text-nowrap mt-1">
                                    <span class="text-secondary">Expected: </span>
                                    <span class="">${{ friendly_money($bill->hcp_expected_payment_amount) }}</span>
                                </div>
                            @else
                                <div class="text-nowrap mt-1">
                                    <span class="text-secondary">Amount: </span>
                                    <span class="">${{ friendly_money($bill->hcp_payment_amount) }}</span>
                                </div>
                            @endif
                        </td>
                        <td>
                            <span class="">${{ friendly_money($bill->total_expected) }}</span>
                        </td>
                        <td>
                            <span class="">${{ friendly_money($bill->total_paid) }}</span>
                        </td>
                        <td>
                            @if(!$bill->is_cancelled)
                                @if($bill->is_signed_by_hcp)
                                    <span class="d-block text-secondary text-nowrap">
                                        <i class="fa fa-check"></i>
                                        HCP Signed
                                    </span>
                                @else
                                    <span moe
                                          class="d-block {{ $bill->hcp_pro_id !== $pro->id ? 'moe-disabled' : '' }}"
                                          title="{{ $bill->hcp_pro_id !== $pro->id ? 'Only the bill\'s HCP can sign' : '' }}">
                                        <a class="" href="" show start>Sign</a>
                                        <form url="/api/bill/signAsHcp">
                                            <input type="hidden" name="uid" value="{{$bill->uid}}">
                                            <p>Sign this bill as HCP?</p>
                                            <div class="mb-0">
                                                <button class="btn btn-success btn-sm" submit>Sign</button>
                                                <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                            </div>
                                        </form>
                                    </span>
                                @endif
                            @endif
                        </td>
                        <td>
                            @if(!$bill->is_cancelled)
                                <span class="d-block" moe>
                                                <a class="" href="" show start>
                                                    {{$bill->balance_post_date ? friendlier_date_time($bill->balance_post_date, false) : '(not set)' }}
                                                </a>
                                                <form url="/api/bill/updateBalancePostDate">
                                                    <input type="hidden" name="uid" value="{{$bill->uid}}">
                                                    <p>Update Balance Post Date</p>
                                                    <div class="mb-0">
                                                        <input type="date" class="text form-control form-control-sm" name="balancePostDate" value="{{$bill->balance_post_date}}" placeholder="balance post date"><br>
                                                        <button class="btn btn-success btn-sm" submit>Submit</button>
                                                        <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                    </div>
                                                </form>
                                            </span>
                            @endif
                        </td>
                        <td> <!-- verification -->
                            @if($pro->pro_type === 'ADMIN' && !$bill->is_cancelled)
                                @if(!$bill->is_verified)
                                    <div class="text-warning-mellow font-weight-bold">Not Verified</div>
                                    <span class="d-block mt-1" moe>
                                                    <a href="" show start>Mark Verified</a>
                                                    <form url="/api/bill/markAsVerified">
                                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                                        <p>Mark As Verfified?</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>
                                                </span>
                                @else
                                    <div class="text-success font-weight-bold"><i class="fa fa-check"></i> Verified</div>
                                    <span class="d-block mt-1" moe>
                                                    <a class="" href="" show start>Undo</a>
                                                    <form url="/api/bill/undoMarkAsVerified">
                                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                                        <p>Undo Mark As Verfified?</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>
                                                </span>
                                @endif
                            @endif
                        </td>
                        <td>  <!-- cancellation -->
                            @if($bill->is_cancelled)
                                <div class="text-warning-mellow font-weight-bold">Cancelled</div>
                                @if($bill->is_cancelled_by_administrator)
                                    <div class="text-secondary text-sm">(by Administrator)</div>
                                @endif
                                <div moe class="mt-1">
                                    <a class="" href="" show start>Update Memo</a>
                                    <form url="/api/bill/updateCancellationMemo">
                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                        <p>Update Cancellation Memo</p>
                                        <div class="mb-0">
                                            <input type="text" class="text form-control form-control-sm" name="updateCancellationMemo" value="{{$bill->cancellation_memo}}" placeholder=""><br>
                                            <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
                                <span 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="{{$bill->uid}}">
                                                    <p class="mb-2">Cancel this bill?</p>
                                                    <div class="mb-2">
                                                        <label class="mb-1 text-secondary">Cancellation Memo</label>
                                                        <input type="text" name="memo" placeholder="Memo" class="form-control form-control-sm">
                                                    </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>
                                            </span>
                            @endif

                            @if($bill->is_cancelled && !$bill->is_cancellation_acknowledged)
                                <div class="mt-2 text-secondary">
                                    <i class="fa fa-exclamation-triangle"></i>
                                    Not Acknowledged
                                </div>
                                <div class="d-block mt-1" moe>
                                    <a class="" href="" show start>Ack. Cancellation</a>
                                    <form url="/api/bill/acknowledgeCancellation">
                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                        <p>Acknowledge Cancellation?</p>
                                        <div class="mb-0">
                                            <input type="text" class="text form-control form-control-sm" name="cancellationMemo" value="{{$bill->cancellation_memo}}" placeholder=""><br>
                                            <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

                            @if($bill->is_cancellation_acknowledged)
                                <div class="mt-2 text-secondary">
                                    <i class="fa fa-check"></i>
                                    Acknowledged
                                </div>
                                <div class="d-block mt-1" moe>
                                    <a class="" href="" show start>Undo Cancellation Ack.</a>
                                    <form url="/api/bill/undoAcknowledgeCancellation">
                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                        <p>Undo Acknowledge Cancellation?</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

                        </td>
                        <td class="border-right-0"> <!-- submit payment -->
                            @if($pro->pro_type === 'ADMIN' && !$bill->is_cancelled)
                                <span class="d-block" moe relative="">
                                                <a class="font-weight-bold" href="" show start>Submit Payment</a>
                                                <form url="/api/bill/payHcpAmount" right>
                                                    <input type="hidden" name="uid" value="{{$bill->uid}}">
                                                    <p>Submit Payment</p>
                                                    <div class="mb-0">
                                                        <input type="text" class="text form-control form-control-sm" name="hcpPaymentAmount" value="{{$bill->hcp_expected_payment_amount}}" placeholder="amount"><br>
                                                        <button class="btn btn-success btn-sm" submit>Submit</button>
                                                        <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                    </div>
                                                </form>
                                            </span>
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <div>
                {{$bills->links()}}
            </div>
        </div>
    </div>
    </div>

    <script>
        (function() {

            function applyFilters() {
                let params = {}, queryLine = [];
                $('[data-filter]').each(function() {
                    if($.trim($(this).val())) {
                        params[$(this).attr('data-filter')] = $.trim($(this).val());
                    }
                });
                for(let x in params) {
                    if(params.hasOwnProperty(x)) {
                        queryLine.push(x + '=' + encodeURIComponent(params[x]));
                    }
                }
                queryLine = queryLine.join('&');

                fastLoad('/practice-management/bill-matrix?' + queryLine);
            }

            function init() {
                $('select[data-filter]')
                    .off('change')
                    .on('change', applyFilters);
                $('input[data-filter]')
                    .off('keyup')
                    .on('keyup', function(_event) {
                        if(_event.which === 13) {
                            applyFilters();
                            return false;
                        }
                    });
            }

            addMCInitializer('practice-bills', init, '#practice-bills')

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