<?php $parsed = $page->data ? json_decode($page->data) : false; ?>
@if(!!$parsed)
<div class="p-3 border-top mt-3 mcp-theme-1" id="undo-remove-medication-container-{{$page->id}}">
    <form action="">
        <p>Undo removal of this medication?</p>
        <hr class="m-neg-4">
        <div class="d-flex align-items-center justify-content-center">
            <button class="btn btn-sm btn-danger px-3 mr-2 btn-save" type="button">Yes</button>
            <button class="btn btn-sm btn-default border btn-close-stag-popup px-3" type="button">No</button>
        </div>
    </form>
</div>
<script>
    (function() {
        function init() {
            let parent = $('#undo-remove-medication-container-{{$page->id}}'),
                form = parent.find('form').first();
            parent.find('.btn-save')
                .off('click')
                .on('click', function() {
                    if(!form[0].checkValidity()) {
                        form[0].reportValidity();
                        return false;
                    }
                    showMask();
                    let newData = {!! $page->data !!};
                    newData.removed_during_note_uid = null;
                    newData.removal_memo = null;
                    let payload = {
                        uid: '{{$page->uid}}',
                        newData: JSON.stringify(newData)
                    };
                    $.post('/api/page/change', payload, _data => {
                        if(!hasResponseError(_data)) {
                            toastr.success('Medication updated!');
                            closeStagPopup();
                        }
                    }, 'json');
                    return false;
                });
        }
        addMCInitializer('undo-remove-medication-{{$page->id}}', init, '#undo-remove-medication-container-{{$page->id}}');
    }).call(window);
</script>
@else
    <div class="p-3 border-top mt-3 mcp-theme-1" id="undo-remove-medication-container-{{$page->id}}">
        <div class="text-secondary">Invalid/corrupt input!</div>
    </div>
@endif