<?php
/** @var App\Models\Note $note */
/** @var App\Models\Pro $pro */
/** @var App\Models\Section $section */
/** @var $allSections */
?>
@extends ('layouts.patient')

@section('inner-content')

    <?php
    $hasBills = false;
    if($note->bills->count()) {
        foreach ($note->bills as $bill) {
            if(!$bill->is_cancelled) {
                $hasBills = true;
                break;
            }
        }
    }
    ?>

    <div class="pt-2 pb-3 d-flex align-items-start">
        <h6 class="my-0 text-secondary d-flex align-items-start w-100">
            <a href="/patients/view/{{ $patient->uid }}/notes" class="small text-decoration-none mr-3">
                <i class="fa fa-chevron-left"></i>
            </a>
            <div class="mr-auto">
                <span class="text-dark font-weight-bold">Note
                    <span class="text-secondary font-weight-normal">{{ $note->is_cancelled ? '(CANCELLED)' : '' }}</span>
                </span>
            </div>
            @if($patient->has_mcp_done_onboarding_visit !== 'YES')
                <div class="ml-auto">
                    <div moe relative>
                        <a start show class="font-weight-bold">Mark as Onboarding Visit Note</a>
                        <form url="/api/client/updateMcpOnboardingVisitInfo" class="mcp-theme-1" right>
                            <input type="hidden" name="uid" value="{{$patient->uid}}">
                            <input type="hidden" name="mcpOnboardingVisitNoteUid" value="{{$note->uid}}">
                            <input type="hidden" name="hasMcpDoneOnboardingVisit" value="YES">
                            <input type="hidden" name="mcpOnboardingVisitDate" value="{{$note->effective_dateest}}">
                            <p class="min-width-200px">Mark this patient's onboarding as completed using this note?</p>
                            <div>
                                <button submit class="btn btn-sm btn-success mr-1">Yes</button>
                                <button cancel class="btn btn-sm btn-default border">No</button>
                            </div>
                        </form>
                    </div>

                </div>
            @endif
        </h6>
    </div>

    <div class="card mb-0 {{ $note->is_cancelled ? 'cancelled-item' : '' }}">
        <div class="card-header d-flex align-items-start px-3 py-2 border-bottom">
            <div class="pr-2">
                {{$note->title}}
                @if(!$note->is_signed_by_hcp)
                <div moe>
                    <a href="" show start><i class="fa fa-edit"></i></a>
                    <form url="/api/note/putTitle">
                        <input type="hidden" name="uid" value="{{$note->uid}}">
                        <div class="mb-2">
                            <input type="text" name="title" value="{{$note->title}}" class="form-control form-control-sm"
                                   placeholder="Title">
                        </div>
                        <div class="form-group 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>
                @endif
            </div>
            <div class="px-2 border-left border-right">
                <div class="d-flex">
                    <span class="mr-2">{{friendly_date_time($note->effective_dateest, false, '')}}</span>
                    @if(!$note->is_signed_by_hcp)
                    <span moe class="ml-auto">
                        <a href="" show start><i class="fa fa-calendar"></i></a>
                        <form url="/api/note/putEffectiveDateEST">
                            <input type="hidden" name="uid" value="{{$note->uid}}">
                            <div class="mb-2">
                                <label for="" class="text-secondary text-sm">Effective date EST</label>
                                <input type="date" name="effectiveDateEST" value="{{$note->effective_dateest}}" class="form-control form-control-sm">
                            </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>
                    </span>
                    @endif
                </div>
            </div>

            {{--<div class="px-2 border-left border-right">
                <div class="d-flex">
                    <span class="mr-2">{{friendly_time($note->effective_time, '')}}</span>
                    @if(!$note->is_signed_by_hcp)
                        <span moe class="ml-auto">
                            <a href="" show start><i class="fa fa-clock"></i></a>
                            <form url="/api/note/putEffectiveTime">
                                <input type="hidden" name="uid" value="{{$note->uid}}">
                                <div class="mb-2">
                                    <label for="" class="text-secondary text-sm">Effective time </label>
                                    <input type="time" name="effectiveTime" value="{{ date("H:i", strtotime($note->effective_time)) }}" class="form-control form-control-sm">
                                </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>
                        </span>
                    @endif
                </div>
            </div>--}}

            <div class="px-2 border-right">
                <div class="d-flex">
                    <span class="mr-2"><span class="text-secondary">HCP:</span> {{$note->hcpPro->name_display}}</span>
                    @if(!$note->is_signed_by_hcp)
                    <div moe class="ml-auto">
                        <a href="" show start><i class="fa fa-edit"></i></a>
                        <form url="/api/note/putHcp">
                            <input type="hidden" name="uid" value="{{$note->uid}}">
                            <div class="mb-2">
                                <select provider-search data-pro-uid="{{ @$note->hcpPro->uid }}"
                                        name="hcpProUid" class="form-control form-control-sm">
                                    <option value="">-- Select HCP Pro --</option>
                                    @foreach ($pros as $iPro)
                                        <option value="{{$iPro->uid}}" {{ $note->hcpPro && $iPro->id === $note->hcpPro->id ? 'selected' : '' }}>{{$iPro->name_display}}</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>
                    @endif
                </div>
            </div>

            <div class="px-2 border-right">
                <div class="d-flex">
                    <span class="mr-2"><span class="text-secondary">Type:</span> {{$note->new_or_fu_or_na ? ($note->new_or_fu_or_na === 'NEW' ? 'New Patient' : $note->new_or_fu_or_na) : '-'}}</span>
                    @if(!$note->is_signed_by_hcp)
                        <div moe class="ml-auto">
                            <a href="" show start><i class="fa fa-edit"></i></a>
                            <form url="/api/note/updateNewOrFuOrNa">
                                <input type="hidden" name="uid" value="{{$note->uid}}">
                                <div class="mb-2">
                                    <select name="newOrFuOrNa" class="form-control form-control-sm" required>
                                        <option value="">-- select --</option>
                                        <option value="NEW" {{ $note->new_or_fu_or_na === "NEW" ? "selected" : "" }}>New</option>
                                        <option value="FU" {{ $note->new_or_fu_or_na === "FU" ? "selected" : "" }}>Follow-up</option>
                                        <option value="NA" {{ $note->new_or_fu_or_na === "NA" ? "selected" : "" }}>N/A</option>
                                    </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>
                    @endif
                </div>
            </div>

            <div class="px-2">
                <div class="d-flex">
                    <span class="mr-2"><span class="text-secondary">Method:</span> {{$note->method ? $note->method : '-'}}</span>
                    @if(!$note->is_signed_by_hcp && $note->new_or_fu_or_na !== 'NEW')
                        <div moe class="ml-auto">
                            <a href="" show start><i class="fa fa-edit"></i></a>
                            <form url="/api/note/updateMethod">
                                <input type="hidden" name="uid" value="{{$note->uid}}">
                                <div class="mb-2">
                                    <select name="method" class="form-control form-control-sm" required>
                                        <option value="">-- select --</option>
                                        <option value="VIDEO" {{ $note->method === "VIDEO" ? "selected" : "" }}>Video</option>
                                        <option value="AUDIO" {{ $note->method === "AUDIO" ? "selected" : "" }}>Audio</option>
                                        <option value="IN_CLINIC" {{ $note->method === "IN_CLINIC" ? "selected" : "" }}>In-Clinic</option>
                                        <option value="HOUSE_CALL" {{ $note->method === "HOUSE_CALL" ? "selected" : "" }}>House Call</option>
                                    </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>
                    @endif
                </div>
            </div>

            <div class="ml-auto d-flex align-items-start">
                <div class="">
                    <div>
                        @if($note->is_signed_by_hcp)
                            <span class="text-secondary">
                        <i class="fa fa-check"></i>
                        Note Signed
                    </span>
                        @else
                            <div moe
                                 class="{{ $note->hcp_pro_id !== $pro->id ? 'moe-disabled' : '' }}"
                                 title="{{ $note->hcp_pro_id !== $pro->id ? 'Only the note\'s HCP can sign' : '' }}">
                                <a class="" href="" show start>Sign Note</a>
                                <form url="/api/note/signAsHcp" right>
                                    <input type="hidden" name="uid" value="{{$note->uid}}">
                                    <p>Sign this note 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>
                            </div>
                        @endif
                    </div>
                </div>
                @if($note->is_cancelled || (!$note->is_cancelled && !$note->is_signed_by_hcp))
                <div class="ml-2 pl-2 border-left">
                    <div class="d-flex justify-content-end">
                        @if(!$note->is_cancelled)
                        <div moe relatve
                             class="{{ $hasBills ? 'moe-disabled' : '' }}"
                             title="{{ $hasBills ? 'Cannot cancel note since it has un-cancelled bills in it' : '' }}">
                            <a class="text-danger" href="" show start>Cancel</a>
                            <form url="/api/note/cancel" right>
                                <input type="hidden" name="uid" value="{{$note->uid}}">
                                <p class="small mb-2">Are you sure you want to cancel this note?</p>
                                <div class="mb-2">
                                    <textarea name="memo" id="" cols="30" rows="5" placeholder="Memo" class="memo-textarea form-control form-control-sm"></textarea>
                                </div>
                                <div class="d-flex align-items-center">
                                    <button class="btn btn-sm btn-danger mr-2" submit>Yes</button>
                                    <button class="btn btn-sm btn-default mr-2 border" cancel>No</button>
                                </div>
                            </form>
                        </div>
                        @else
                            <span class="text-secondary font-weight-bold">CANCELLED</span>
                        @endif
                    </div>
                </div>
                @endif

            </div>
        </div>
        <div class="card-body p-0">
            <div>
                <div class="mb-3">
                    <div>
                        <?php
                        $shortCutsObject = [];
                        foreach ($pro->shortcuts as $shortcut) {

                            // %replaceables%
                            $shortcut->text = str_replace("%AGE%", $patient->age_in_years, $shortcut->text);
                            $shortcut->text = str_replace("%GENDER%", $patient->sex, $shortcut->text);
                            $shortcut->text = str_replace("%NAME%", $patient->displayName(), $shortcut->text);

                            $shortCutsObject[] = [
                                "name" => $shortcut->shortcut,
                                "value" => $shortcut->text
                            ];
                        }
                        ?>
                        <script>window.userShortcuts = <?= json_encode($shortCutsObject); ?>;</script>
                        <?php
                        $shortcuts = "";
                        $latestSectionTS = 0;
                        ?>
                        <div class="{{ $note->is_signed_by_hcp ? 'note-signed-by-hcp' : '' }}">
                        @include('app.patient.note.note-section-list')
                        </div>
                    </div>
                </div>

                <span class="d-none latest-section-ts">{{ $latestSectionTS }}</span>

                <div class="border-top p-3">
                    <div class="">
                        <div class="d-flex align-items-center mb-2">
                            <p class="font-weight-bold text-secondary m-0 mr-2">ICDs</p>
                            @include('app/patient/note/_create-note-reason')
                        </div>
                        <div>
                        @if(count($note->reasons))
                             @foreach ($note->reasons as $reason)
                                <span class="badge badge-info p-2">
                                    {{ $reason->description}} ({{ $reason->code }})
                                </span>
                            @endforeach
                        @else
                            <div class="alert alert-info">No note reasons</div>
                        @endif
                        </div>
                    </div>
                </div>

                {{-- bills --}}
                @if($pro->pro_type === 'ADMIN' || ($note->hcpPro && $pro->id === $note->hcpPro->id))
                @if($note->bills->count())
                    <div class="mt-2 px-3">
                        <div class="d-flex align-items-center mb-2">
                            <p class="font-weight-bold text-secondary m-0">Bills</p>
                            @include('app/patient/note/_create-bill')
                        </div>
                        <table class="table table-sm tabe-striped mb-3 border-left border-right border-bottom">
                            <thead class="bg-light">
                            <tr>
                                <th class="border-bottom-0 pl-3">HCP</th>
                                <th class="border-bottom-0 w-25 pl-3">Service</th>
                                {{--<th class="border-bottom-0 w-25 pl-2">Reason</th>--}}
                                <th class="border-bottom-0 pl-3">Billable</th>
                                <th class="border-bottom-0 pl-2">Amount</th>
                                <th class="border-bottom-0"></th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach ($note->bills as $bill)
                                <tr>
                                    <td class="pl-3">{{ $bill->hcp->displayName() }}</td>
                                    <td class="pl-3 {{ $bill->is_cancelled ? 'text-secondary' : '' }}">{{$bill->code}}</td>
                                    {{--<td class="pl-2">
                                        {{ $bill->reason1 }}
                                        <div class="text-secondary small">{{ $bill->reason2 }}</div>
                                    </td>--}}
                                    <td class="pl-3">
                                        @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>
                                    <td class="pl-2">
                                        @if($bill->has_hcp_been_paid)
                                            <span class="text-dark">Processed:</span><span class="font-weight-bold text-success ml-2">${{ $bill->hcp_payment_amount }}</span>
                                        @else
                                            <span class="text-dark">Expected:</span><span class="font-weight-bold text-dark ml-2">{{ $bill->hcp_expected_payment_amount ? '$' . $bill->hcp_expected_payment_amount : '-' }}</span>
                                        @endif
                                    </td>
                                    <td>
                                        <div class="d-flex align-items-center">
                                            @if(!$bill->is_cancelled)
                                                @if($bill->is_signed_by_hcp)
                                                    <span class="d-block text-secondary">
                                                        <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
                                                @if(!$bill->has_hcp_been_paid && $pro->pro_type === 'ADMIN')
                                                    <span class="mx-2 text-secondary">|</span>
                                                    <span class="d-block" moe>
                                                    <a class="text-danger" href="" show start>Submit Payment</a>
                                                    <form url="/api/bill/payHcpAmount">
                                                        <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
                                            @if($bill->is_cancelled)
                                                <span class="mx-2 text-secondary">|</span>
                                                <span class="d-block text-secondary">
                                                    Cancelled
                                                </span>
                                            @else
                                                <span class="mx-2 text-secondary">|</span>
                                                <span class="d-block" moe>
                                                    <a class="text-danger" href="" show start>Cancel</a>
                                                    <form url="/api/bill/markCancelled">
                                                        <input type="hidden" name="uid" value="{{$bill->uid}}">
                                                        <p>Cancel this bill?</p>
                                                        <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
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="my-3 px-3 d-flex">
                        <p class="font-weight-bold mb-0 text-secondary">No bills in this note</p>
                        @include('app/patient/note/_create-bill')
                    </div>
                @endif
                @endif

                {{-- claims --}}
                @if($pro->pro_type === 'ADMIN')
                @if($note->claims->count())
                    <div class="mt-2 px-3">
                        <div class="d-flex align-items-center mb-2">
                            <p class="font-weight-bold text-secondary m-0">Claims</p>
                            @include('app/patient/note/_create-claim')
                        </div>

                        <table class="table table-sm tabe-striped mb-3 border-left border-right border-bottom">
                            <thead class="bg-light">
                            <tr>
                                <th class="border-bottom-0 pl-2">IID</th>
                                <th class="border-bottom-0 w-50">Details</th>
                                <th class="border-bottom-0 text-center">Submitted?</th>
                                <th class="border-bottom-0">Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach ($note->claims as $claim)
                                <tr>
                                    <td class="pl-2">{{ $claim->iid }}</td>
                                    <td class="p-0">
                                        @if($claim->lines->count())
                                            <table class="table table-sm table-condensed border-left border-right mb-0">
                                                <thead>
                                                    <tr class="bg-light">
                                                        <th class="border-0">CPT</th>
                                                        <th class="border-0">Date of Service</th>
                                                        <th class="border-0">ICDs</th>
                                                    </tr>
                                                </thead>
                                                <tbody class="pb-3">
                                                    @foreach($claim->lines as $line)
                                                    <tr class="claim-line">
                                                        <td>{{$line->cpt}}</td>
                                                        <td>{{$line->date_of_service}}</td>
                                                        <td>
                                                            @if(count($line->claimLineIcds))
                                                                @foreach($line->claimLineIcds as $icd)
                                                                    <div>
                                                                        <b>{{$icd->code}}</b> <span class="text-secondary">({{$icd->description}})</span>
                                                                    </div>
                                                                @endforeach
                                                            @else
                                                                <p>No ICDs set</p>
                                                            @endif
                                                        </td>
                                                    </tr>
                                                    @endforeach
                                                </tbody>
                                            </table>
                                        @else
                                            <p>No lines for this claim</p>
                                        @endif
                                    </td>
                                    <td class="text-center">{{$claim->was_submitted? 'Yes':'No'}}</td>

                                    <td>
                                        <div class="d-flex align-items-center">
                                            @if($claim->was_submitted)
                                                <span class="d-block text-secondary">
                                                    Submitted
                                                </span>
                                            @else
                                                @include('app/patient/note/_update-claim')
                                                <span class="mx-2 text-secondary">|</span>
                                                <span moe class="d-block" title="Submit Claim">
                                                    <a class="" href="" show start>Submit</a>
                                                    <form url="/api/claim/submit">
                                                        <input type="hidden" name="uid" value="{{$claim->uid}}">
                                                        <p>Submit claim?</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
                                            @if($claim->is_cancelled)
                                                <span class="mx-2 text-secondary">|</span>
                                                <span class="d-block text-secondary">
                                                    Cancelled
                                                </span>
                                            @else
                                                <span class="mx-2 text-secondary">|</span>
                                                <span class="d-block" moe>
                                                    <a class="text-danger" href="" show start>Cancel</a>
                                                    <form url="/api/claim/cancel">
                                                        <input type="hidden" name="uid" value="{{$claim->uid}}">
                                                        <p>Cancel this claim?</p>
                                                        <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
                                        </div>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                @else
                    <div class="my-3 px-3 d-flex">
                        <p class="font-weight-bold mb-0 text-secondary">No claims in this note</p>
                        @include('app/patient/note/_create-claim')
                    </div>
                @endif
                @endif


                <div class="border-top p-3">
                    @if($note->addendums->count())
                        <div class="">
                            <div class="d-flex align-items-center mb-2">
                                <p class="font-weight-bold text-secondary m-0">Addendums</p>
                                @include('app/patient/note/_create-addendum')
                            </div>
                            <table class="table table-sm tabe-striped border-left border-right border-bottom m-0">
                                <thead class="bg-light">
                                <tr>
                                    <th class="border-bottom-0 pl-2 w-75">Addendum</th>
                                    <th class="border-bottom-0">Created</th>
                                    <th class="border-bottom-0 delete-column"></th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach ($note->addendums as $addendum)
                                    <tr>
                                        <td class="pl-2">{{ $addendum->memo }}</td>
                                        <td class="">{{ friendly_date_time($addendum->created_at, false)}}</td>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <div moe relative class="mr-2">
                                                    <a href="" show start><i class="fa fa-pencil-alt"></i></a>
                                                    <form url="/api/noteAddendum/updateMemo" right>
                                                        <input type="hidden" name="uid" value="{{$addendum->uid}}">
                                                        <div class="mb-2 d-block">
                                                            <label class="d-block text-sm text-secondary mb-1">Addendum</label>
                                                            <textarea class="form-control form-control-sm" name="memo" rows="5">{{$addendum->memo}}</textarea>
                                                        </div>
                                                        <div class="form-group mb-0">
                                                            <button class="btn btn-primary btn-sm mr-1" submit>Submit</button>
                                                            <button class="btn btn-default border btn-sm" cancel>Cancel</button>
                                                        </div>
                                                    </form>
                                                </div>
                                                <div moe relative class="mr-1">
                                                    <a href="" show start class="text-danger"><i class="fa fa-trash"></i></a>
                                                    <form url="/api/noteAddendum/remove" right>
                                                        <input type="hidden" name="uid" value="{{$addendum->uid}}">
                                                        <p>Remove this addendum?</p>
                                                        <div class="form-group mb-0">
                                                            <button class="btn btn-danger btn-sm mr-1" submit>Yes</button>
                                                            <button class="btn btn-default border btn-sm" cancel>No</button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    @else
                        <div class="d-flex">
                            <p class="font-weight-bold mb-0 text-secondary">No addendums in this note</p>
                            @include('app/patient/note/_create-addendum')
                        </div>
                    @endif
                </div>

                @include('app/patient/note/_cancel-signed-note')

            </div>
        </div>
    </div>
    <div class="note-templates-underlay"></div>
    @include('app.patient.note.dashboard_script')
@endsection

@section('left-nav-content')
    @if(!$note->is_signed_by_hcp)
    <div class="left-nav-content" id="note-sections-app">
        <div class="note-widget-title px-2 py-1">Note Sections</div>
        <input type="search" class="d-block w-100 border-0 outline-0 px-2 py-1"
               v-model="q" placeholder="Filter">
        <div class="border-top py-1">
            <div v-for="section in sections"
                 v-show="section.title.toLowerCase().indexOf(q.toLowerCase()) !== -1">
                <div class="d-flex pr-1 align-items-center note-widget-item c-pointer"
                     :title="section.title + ' - Click to remove'"
                     v-if="section.used"
                     v-on:click.once="remove(section.section_uid)">
                    <span class="mx-2">
                        <a class="font-12 text-secondary" href="#">
                            <i class="fa fa-minus-circle"></i>
                        </a>
                    </span>
                    <span class="font-smaller text-ellipsis">
                        @{{section.title}}
                    </span>
                </div>
                <div class="d-flex pr-1 align-items-center note-widget-item c-pointer"
                     :title="section.title + ' - Click to add'"
                     v-if="!section.used"
                     v-on:click.once="add(section.uid, section.internal_name)">
                    <span class="mx-2">
                        <a class="font-12" href="#">
                            <i class="fa fa-plus-circle"></i>
                        </a>
                    </span>
                    <span class="font-smaller text-ellipsis">
                        @{{section.title}}
                    </span>
                </div>
            </div>
        </div>
    </div>
    <script>
        (function() {
            function initSectionsWidget() {
                var allSections = <?= json_encode($allSections) ?>;
                new Vue({
                    el: '#note-sections-app',
                    delimiters: ['@{{', '}}'],
                    data: {
                        q: '',
                        sections: allSections
                    },
                    methods: {
                        add: function(_uid, _section) {
                                $.post('/api/section/create', {
                                    noteUid: '{{$note->uid}}',
                                    sectionTemplateUid: _uid,
                                }, function(_data) {
                                    if(_data) {
                                        if(_data.success) {
                                            fastReload();
                                        }
                                        else {
                                            toastr.error(_data.message);
                                        }
                                    }
                                    else {
                                        toastr.error('Unable to add section!');
                                    }
                                }, 'json');
                        },
                        remove: function(_uid) {
                            $.post('/api/section/deactivate', {
                                uid: _uid,
                                memo: 'Deactivated from note',
                            }, function(_data) {
                                if(_data) {
                                    if(_data.success) {
                                        fastReload();
                                    }
                                    else {
                                        toastr.error(_data.message);
                                    }
                                }
                                else {
                                    toastr.error('Unable to remove section!');
                                }
                            }, 'json');
                        }
                    }
                });

                $('div[embed]').each(function() {
                    $(this).load($(this).attr('embed'));
                });
            }
            addMCInitializer('note-sections-widget', initSectionsWidget, '#note-sections-app');
        })();
    </script>
    @endif
@endsection
