<?php

use App\Models\Page;

$page = Page::where('note_id', $note->id)->where('category', 'CHIEF_COMPLAINT')->first();


$contentData = [
    'value'=>''
];

if($page){
    $contentData = json_decode($page->data, true);
}
?>
<div id="ccSection">
    <div class="form-group mb-2">
        <input type="hidden" name="data" id="cc-data">
        <div note-rte note-rte-ros data-content="{{isset($contentData['value'])?$contentData['value']:'' }}" class="form-group mb-2 border-left border-right rte-holder"></div>
    </div>
    <div class="form-group mb-2">
        <button class="btn btn-sm btn-outline-primary" @click="save">Submit</button>
    </div>
</div>

<script>
    (function() {
        function init() {
            window.clientVitalsApp = new Vue({
                el: '#ccSection',
                data: {
                    value: '',
                },
                mounted: function() {
                    $(this.$el).closest('#ccSection').find('[name="data"]').val(this.value);
                },
                watch: {
                    value: {
                        handler: function(val, oldVal) {
                            $(this.$el).closest('#ccSection').find('[name="data"]').val(this.value);
                        },
                        deep: true
                    },
                },
                methods: {

                    save: function(){
                        console.log(this.value);
                        showMask();
                        console.log("DATA", this.value);

                        var payload = {
                            noteUid:  '{{$note->uid}}',
                            parentPageUid: null,
                            clientUid: '{{$patient->uid}}',
                            category: 'CHIEF_COMPLAINT',
                            key: null,
                            data: $(this.$el).closest('#ccSection').find('[name="data"]').val()
                        }
                        console.log("PAYLOAD:  ", payload);
                        $.post('/api/page/changeOrCreateNoteScoped', payload, _data => {
                            if(!hasResponseError(_data)) {
                                toastr.success('Medication updated!');
                                closeStagPopup();
                            }
                        }, 'json');
                        hideMask();
                        fastReload();
                        return false;

                    }
                }
            });
        }
        addMCInitializer('client-cc', init, '#ccSection');
    })();
</script>

