@extends ('layouts/template')

@section('content')

    <div class="p-3 mcp-theme-1" id="my-favorites">
        <div class="card">

            <div class="card-header px-2 py-1 d-flex align-items-center">
                <strong class="">
                    <i class="fas fa-user"></i>
                    Favorites
                </strong>
                <span class="ml-4">Show:</span>
                <select class="ml-2 width-150px form-control form-control-sm"
                        onchange="fastLoad('/practice-management/my-favorites/' + this.value, true, false, false)">
                    <option value="allergy" {{ $filter === 'allergy' ? 'selected' : '' }}>Allergy</option>
                    <option value="medication" {{ $filter === 'medication' ? 'selected' : '' }}>Medication</option>
                    <option value="problem" {{ $filter === 'problem' ? 'selected' : '' }}>Problem</option>
                    <option value="all" {{ $filter === 'all' ? 'selected' : '' }}>All favorites</option>
                </select>
                <span class="ml-4 mr-2">+ New Favorite:</span>
                <div moe relative>
                    <a start show href="#">Allergy</a>
                    <form url="/api/proFavorite/create">
                        <input type="hidden" name="proUid" value="{{$pro->uid}}">
                        <input type="hidden" name="category" value="allergy">
                        <input type="hidden" name="data" value="">
                        <div class="mb-2">
                            <label class="text-sm mb-0 font-weight-bold">Allergy *</label>
                            <input type="text" class="form-control form-control-sm"
                                   stag-suggest allergy stag-suggest-ep="/fdb-allergy-suggest/json" required>
                        </div>
                        <div class="form-group m-0">
                            <button submit class="btn btn-primary btn-sm mr-2" disabled>Save</button>
                            <button cancel class="btn btn-default border btn-sm mr-2">Cancel</button>
                        </div>
                    </form>
                </div>
                <span class="mx-2 text-secondary text-sm">|</span>
                <div moe relative>
                    <a start show href="#">Medication</a>
                    <form url="/api/proFavorite/create">
                        <input type="hidden" name="proUid" value="{{$pro->uid}}">
                        <input type="hidden" name="category" value="medication">
                        <input type="hidden" name="data" value="">
                        <div class="mb-2">
                            <label class="text-sm mb-0 font-weight-bold">Medication *</label>
                            <input type="text" class="form-control form-control-sm"
                                   stag-suggest medication stag-suggest-ep="/fdb-med-suggest-v2/json" required>
                        </div>
                        <div class="form-group m-0">
                            <button submit class="btn btn-primary btn-sm mr-2" disabled>Save</button>
                            <button cancel class="btn btn-default border btn-sm mr-2">Cancel</button>
                        </div>
                    </form>
                </div>
                <span class="mx-2 text-secondary text-sm">|</span>
                <div moe relative>
                    <a start show href="#">Problem</a>
                    <form url="/api/proFavorite/create">
                        <input type="hidden" name="proUid" value="{{$pro->uid}}">
                        <input type="hidden" name="category" value="problem">
                        <input type="hidden" name="data" value="">
                        <div class="mb-2">
                            <label class="text-sm mb-0 font-weight-bold">Problem *</label>
                            <input type="text" class="form-control form-control-sm"
                                   stag-suggest problem stag-suggest-ep="/fdb-dx-suggest-v2/json" required>
                        </div>
                        <div class="form-group m-0">
                            <button submit class="btn btn-primary btn-sm mr-2" disabled>Save</button>
                            <button cancel class="btn btn-default border btn-sm mr-2">Cancel</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="card-body p-0">
                <table class="table table-sm table-striped p-0 m-0">
                    <thead class="bg-light">
                    <tr>
                        <th class="px-3 border-0 width-100px">Category</th>
                        <th class="border-0">Content</th>
                        <th class="border-0 min-width-500px">&nbsp;</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($myFavorites as $favorite)
                        <?php $parsed = json_decode($favorite->data); ?>
                        <tr>
                            <td class="px-3">{{ ucwords($favorite->category) }}</td>
                            <td class="text-nowrap">
                                {{ @$parsed->name }}
                            </td>
                            <td>
                                <div class="d-flex align-items-center">
                                    <div moe relative class="mr-3">
                                        <a start show class="text-primary">
                                            <i class="fa fa-edit on-hover-opaque"></i>
                                        </a>
                                        <form url="/api/proFavorite/updateBasic">
                                            <input type="hidden" name="uid" value="{{$favorite->uid}}">
                                            <input type="hidden" name="category" value="{{$favorite->category}}">
                                            <input type="hidden" name="data" value="{{$favorite->data}}">
                                            <div class="mb-2">
                                            @if($favorite->category === 'allergy')
                                                <input type="text" class="form-control form-control-sm"
                                                       value="{{ @$parsed->name }}"
                                                       stag-suggest allergy stag-suggest-ep="/fdb-allergy-suggest/json" required>
                                            @elseif($favorite->category === 'medication')
                                                <input type="text" class="form-control form-control-sm"
                                                       value="{{ @$parsed->name }}"
                                                       stag-suggest medication stag-suggest-ep="/fdb-med-suggest-v2/json" required>
                                            @elseif($favorite->category === 'problem')
                                                <input type="text" class="form-control form-control-sm"
                                                       value="{{ @$parsed->name }}"
                                                       stag-suggest problem stag-suggest-ep="/fdb-dx-suggest-v2/json" required>
                                            @endif
                                            </div>
                                            <div class="form-group m-0">
                                                <button submit class="btn btn-primary btn-sm mr-2" disabled>Save</button>
                                                <button cancel class="btn btn-default border btn-sm mr-2">Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                    <div moe relative wide class="mr-2">
                                        <a start show class="text-danger">
                                            <i class="fa fa-trash-alt on-hover-opaque"></i>
                                        </a>
                                        <form url="/api/proFavorite/remove" right>
                                            <input type="hidden" name="uid" value="{{$favorite->uid}}">
                                            <p>
                                                Are you sure you want to remove this from your favorites?
                                            </p>
                                            <div class="form-group m-0">
                                                <button submit class="btn btn-danger btn-sm mr-2">Yes</button>
                                                <button cancel class="btn btn-default border btn-sm mr-2">No</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script>
        (function () {
            function init() {
                let parentSegment = $('#my-favorites');
                parentSegment.find('input[stag-suggest][allergy]')
                    .off('stag-suggest-selected')
                    .on('stag-suggest-selected', (_e, _input, _data) => {
                        $(_input).closest('form').find('[name="data"]').val(JSON.stringify({
                            name: _data.text,
                            damConceptId: _data.dam_concept_id,
                            damConceptIdType: _data.dam_concept_id_typ
                        }));
                        $(_input).closest('form').find('[submit]').prop('disabled', false);
                    });
                parentSegment.find('input[stag-suggest][medication]')
                    .off('stag-suggest-selected')
                    .on('stag-suggest-selected', (_e, _input, _data) => {
                        $(_input).closest('form').find('[name="data"]').val(JSON.stringify({
                            name: _data.text,
                            medId: _data.medid,
                            routedMedId: _data.routed_med_id,
                            routedDosageFormMedId: _data.routed_dosage_form_med_id,
                            gcnSeqno: _data.gcn_seqno,
                        }));
                        $(_input).closest('form').find('[submit]').prop('disabled', false);
                    });
                parentSegment.find('input[stag-suggest][problem]')
                    .off('stag-suggest-selected')
                    .on('stag-suggest-selected', (_e, _input, _data) => {
                        $(_input).closest('form').find('[name="data"]').val(JSON.stringify({
                            name: _data.text,
                            dxid: _data.dxid,
                            icd: _data.sub_text,
                            "icd-type": ""
                        }));
                        $(_input).closest('form').find('[submit]').prop('disabled', false);
                    });

            }
            addMCInitializer('my-favorites', init, '#my-favorites');
        }).call(window);
    </script>

@endsection
