@extends ('layouts.patient')

@section('inner-content')

    <div class="pt-2 d-flex align-items-start">
        <h6 class="my-0 text-secondary d-flex align-items-start">
            <span class="font-weight-bold text-secondary">Notes</span>
            <span class="mx-2 text-secondary">|</span>
            <a href="#" class="py-0 mb-0 create-auto-note-trigger"
               data-patient-uid="{{ $patient->uid  }}"
               data-hcp-uid="{{ $pro->uid }}"
               data-effective-date="{{ date('Y-m-d') }}">Add</a>
            <?php /*<div moe>
                <a start show class="py-0 mb-3">Add</a>
                <form url="/api/note/createUsingFreeTextHtml" class="mcp-theme-1">
                    <input type="hidden" name="clientUid" value="{{$patient->uid}}">
                    <div class="mb-2">
                        <select name="hcpProUid" class="form-control form-control-sm">
                            <option value="">-- Select HCP Pro --</option>
                            @foreach ($pros as $iPro)
                                <option
                                    value="{{$iPro->uid}}">{{$iPro->name_display}}</option>
                            @endforeach
                        </select>
                    </div>
                    {{--<div class="mb-2">
                        <select name="allyProUid" class="form-control form-control-sm">
                            <option value="">-- Select Ally Pro --</option>
                            @foreach ($pros as $iPro)
                                <option
                                    value="{{$iPro->uid}}">{{$iPro->name_display}}</option>
                            @endforeach
                        </select>
                    </div>--}}
                    <div class="mb-2">
                        <input type="text" name="title" placeholder="Title" class="form-control form-control-sm">
                    </div>
                    <div class="mb-2">
                        <label class="text-secondary text-sm">Effective Date</label>
                        <input type="date" name="effectiveDateEST" placeholder="Effective Date" class="form-control form-control-sm">
                    </div>
                    <div class="mb-2">
                        <label class="text-secondary text-sm">Effective Time</label>
                        <input type="time" name="effectiveTime" placeholder="Effective Time" class="form-control form-control-sm">
                    </div>
                    <div class="mb-2">
                        <textarea name="freeTextHtml" id="" cols="30" rows="5" placeholder="Content" class="form-control form-control-sm"></textarea>
                    </div>
                    <div>
                        <button submit class="btn btn-sm btn-primary mr-1">Submit</button>
                        <button cancel class="btn btn-sm btn-default border">Cancel</button>
                    </div>
                </form>
            </div> */ ?>
        </h6>
    </div>

    <table class="table table-sm table-striped table-bordered mb-0 mt-2">
        <thead>
        <tr>
            <th class="px-2 text-secondary border-bottom-0">Created</th>
            <th class="px-2 text-secondary border-bottom-0 w-50">Content</th>
            <th class="px-2 text-secondary border-bottom-0">Effective Date</th>
            <th class="px-2 text-secondary border-bottom-0">HCP</th>
            <th class="px-2 text-secondary border-bottom-0">&nbsp;</th>
        </tr>
        </thead>
        <tbody>
        @foreach ($patient->notes as $note)
            <tr class="{{ $note->is_cancelled ? 'cancelled-item always-clickable' : '' }}">
                <td class="px-2">
                    {{ friendly_date_time($note->created_at, true) }}
                </td>
                <td class="px-2">
                    <?php
                    $textContent = strip_tags($note->free_text_html);
                    if(strlen($textContent) > 200) {
                        $textContent = substr($textContent, 0, 200) . '…';
                    }
                    ?>
                    {!! $textContent !!}
                    @foreach($note->sections as $section)
                    <div p-2>
                        {!! $section->summary_html !!}
                    </div>
                    @endforeach
                </td>
                <td class="px-2">
                    <a href="/patients/view/{{ $patient->uid }}/notes/view/{{ $note->uid }}" class="font-weight-bold">
                        {{ friendly_date_time($note->effective_dateest, false) }}
                    </a>
                    <span class="ml-1">{{ $note->is_cancelled ? '[cancelled]' : '' }}</span>
                </td>
                <td class="px-2">
                    {{$note->hcpPro ? $note->hcpPro->name_display: '-'}}
                </td>
                <td class="px-2">
                    <a href="#" class="text-primary trigger-clone" data-uid="{{$note->uid}}">Clone</a>
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
    <script>
        (function() {
            function initNotesList() {
                $('.trigger-clone')
                    .off('click.clone')
                    .on('click.clone', function() {
                        $.post('/api/note/clone', {
                            uid: $(this).attr('data-uid')
                        }, function(_data) {
                            if(_data) {
                                if(_data.success) {
                                    toastr.success('Note cloned successfully');
                                    fastLoad('/patients/view/{{$patient->uid}}/notes/view/' + _data.data);
                                }
                                else {
                                    toastr.error(_data.message);
                                }
                            }
                            else {
                                toastr.error('Unable to clone the note!');
                            }
                        }, 'json');
                    })
            }
            addMCInitializer('notes-list', initNotesList);
        })();
    </script>
@endsection
