<?php $noteRates = $note->hcpPro ? $note->hcpPro->noteRates() : []; ?>
@if(!$note->is_bill_closed && !$note->is_billing_marked_done)
    @if($note->hasUnacknowledgedCancelledBillsByPro($pro))
        <b class="ml-3 text-warning-dark">
            <i class="fa fa-exclamation-triangle"></i>
            There are unacknowledged cancelled bills in this note!
        </b>
    @else
    <span class="mx-2 text-secondary">|</span>
    <div moe relative wide bottom class="">
        <a class="" href="" show start>Create Bill</a>
        <form url="/api/bill/createForNote">
            <?php $maxMinutes = 30; ?>
            @if($noteRates && count($noteRates))
                <input type="hidden" name="noteUid" value="{{$note->uid}}">
                <div class="mb-2">
                    <label for="" class="text-secondary text-sm">Effective Date</label>
                    <input type="date" name="effectiveDate" class="form-control form-control-sm" value="{{$note->effective_dateest ? $note->effective_dateest : get_current_date($pro->display_timezone)}}" required>
                </div>
                <div class="mb-2">
                    @if($noteRates && count($noteRates))
                        @if(count($noteRates) === 1)
                            <input type="hidden" name="code" value="{{$noteRates[0]->code}}">
                            <p class="mb-2">Service: <b>{{ $noteRates[0]->code }} (${{ $noteRates[0]->amount }}/hr)</b></p>
                        @else
                            <select autofocus class="form-control" name="code" onchange="switchNumberOfUnitsByType(this)">
                                <option value="">-- Select Code --</option>
                                @foreach($noteRates as $noteRate)
                                    <option data-amount="{{ $noteRate->amount }}" value="{{ $noteRate->code }}">
                                        {{ $noteRate->code }} (${{ $noteRate->amount }}{{ $noteRate->code === 'Treatment Services' ? '/hr' : '' }})
                                    </option>
                                @endforeach
                            </select>
                        @endif
                    @endif
                </div>

                <?php
                    $maxMinutes = 120;
                    if($note->new_or_fu_or_na === 'NEW') {
                        $maxMinutes = 90;
                    }
                    else if($note->new_or_fu_or_na === 'FU') {
                        if($note->method === 'VIDEO') {
                            $maxMinutes = 90;
                        }
                        else if($note->method === 'AUDIO') {
                            $maxMinutes = 90;
                        }
                    }
                ?>

                @if($noteRates && count($noteRates) && count($noteRates) === 1)
                    @if(strpos(strtolower($noteRates[0]->code), "treatment services") !== FALSE)
                        <div class="mb-2">
                            <select name="numberOfUnits" class="form-control form-control-sm"
                                    onchange="calculateBillAmount(this)">
                                <option value=""> -- select -- </option>
                                <?php for ($i = 5; $i <= $maxMinutes; $i+=5) { ?>
                                    @if($i == 20) {
                                        <option value="{{ 21/60 }}" selected>{{ 21 }} minutes</option>
                                    @else 
                                        <option value="{{ $i/60 }}">{{ $i }} minutes</option>        
                                    @endif
                                <?php } ?>
                            </select>
                        </div>
                        <div class="mb-2" calculated-amount></div>
                    @else
                        <input type="hidden" name="numberOfUnits" value="1">
                    @endif
                @endif
                <div class="bill-conditional">

                </div>

                @if(@$note && $note->hcpPro->id === $pro->id)
                <div class="my-3">
                    <input type="checkbox" name="signAndMarkBillingDone">&nbsp;Sign and Mark Billing Done
                </div>
                @endif

                <div class="">
                    <button class="btn btn-primary btn-sm" submit>Submit</button>
                    <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                </div>
            @else
                <div class="border rounded bg-aliceblue p-2 width-200px">
                    <i class="fa fa-exclamation-triangle text-warning-mellow mr-1"></i>
                    Payment rates not yet set for Treatment Services. Please contact admin.
                </div>
            @endif
        </form>
        <div class="d-none" hourly-template>
            <div class="mb-2">
                <select name="numberOfUnits" class="form-control form-control-sm"
                        onchange="calculateBillAmount(this)">
                    <option value=""> -- select -- </option>
                    <?php for ($i = 5; $i <= $maxMinutes; $i+=5) { ?>
                        @if($i == 20) {
                            <option value="{{ 21/60 }}" selected>{{ 21 }} minutes</option>
                        @else 
                            <option value="{{ $i/60 }}">{{ $i }} minutes</option>         
                        @endif
                    <?php } ?>
                </select>
            </div>
            <div class="mb-2" calculated-amount></div>
        </div>
        <div class="d-none" non-hourly-template>
            <input type="hidden" name="numberOfUnits" value="1">
        </div>
    </div>
    @endif
@endif
@if($note->is_bill_closed)
    <b class="ml-2 text-info">[BILLING CLOSED]</b>
@endif
@if($pro->pro_type === 'ADMIN')
    @if(!$note->is_bill_closed)
        <span class="mx-2 text-secondary">|</span>
        <span class="d-block" moe wide relative bottom>
            <a class="text-danger" href="" show start>Close Billing</a>
            <form url="/api/note/closeBilling">
                <input type="hidden" name="uid" value="{{$note->uid}}">
                <p>Close billing?</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>
    @elseif($note->is_bill_closed)
        <span class="mx-2 text-secondary">|</span>
        <span class="d-block" moe wide relative bottom>
            <a class="text-danger" href="" show start>Reopen Billing</a>
            <form url="/api/note/reopenBilling">
                <input type="hidden" name="uid" value="{{$note->uid}}">
                <p>Reopen billing?</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
@if(!$note->hasUnacknowledgedCancelledBillsByPro($pro))
@if(!$note->is_billing_marked_done)
    <span class="mx-2 text-secondary">|</span>
    <span class="d-block" moe wide relative bottom>
        <a class="text-danger" href="" show start>Mark Billing Done</a>
        <form url="/api/note/setIsBillingMarkedDoneToTrue">
            <input type="hidden" name="uid" value="{{$note->uid}}">
            <p>Mark billing on this note as done?</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
    <span class="mx-2 text-secondary">|</span>
    <span class="d-block" moe wide relative bottom>
        <i class="fa fa-check"></i>
        <a class="text-danger" href="" show start>Undo Mark Billing Done</a>
        <form url="/api/note/setIsBillingMarkedDoneToFalse">
            <input type="hidden" name="uid" value="{{$note->uid}}">
            <p>Mark billing on this note as not done?</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

<script>
    (function() {
        let selectedCodeHourlyRate = 0;
        @if($noteRates && count($noteRates) && count($noteRates) === 1)
            selectedCodeHourlyRate = {{ $noteRates[0]->amount }};
        @endif
        window.switchNumberOfUnitsByType = function(_trigger) {
            let container = $('.bill-conditional').empty();
            let selected  = $(_trigger).find('option:selected');
            if(!selected.length) return;
            let clone = null;
            selectedCodeHourlyRate = +selected.attr('data-amount');
            $('[calculated-amount]').text('');
            if(selected.text().toLowerCase().indexOf('treatment services') !== -1) {
                clone = $('[hourly-template]').clone()
                    .removeAttr('hourly-template')
                    .removeClass('d-none');
            }
            else {
                clone = $('[non-hourly-template]').clone()
                    .removeAttr('non-hourly-template')
                    .removeClass('d-none');
            }
            clone.appendTo(container)
            clone.focus();
            $('[name="numberOfUnits"]').trigger('change');
        }
        window.calculateBillAmount = function(_trigger) {
            let amountTarget = $('[calculated-amount]');
            _trigger = $(_trigger);
            if(!_trigger.find('option:selected').length) {
                amountTarget.text('');
                return;
            }
            let hours = +_trigger.find('option:selected').attr('value');
            amountTarget.html('<b>Reimbursable Amount:</b> $' + (hours * selectedCodeHourlyRate).toFixed(2));
        }
    })();

</script>
