<?php
use App\Models\Client;
use App\Models\Note;
/** @var Client $patient */
/** @var Note $note */
?>
<div class="p-3 border-top mt-3 mcp-theme-1" id="medications-container">
    <?php $view = 'add'; ?>
    <a class="btn btn-sm btn-primary text-white btn-add-medication"
       native target="_blank"
       open-in-stag-popup
       mc-initer="add-medication"
       update-parent
       popup-style="narrow"
       title="Add Medication"
       href="{{route('patients.view.notes.view.section-view', compact('patient', 'note', 'section', 'view'))}}">
        + Add
    </a>
    <?php
        $items = $patient->pages('medication', true, $note);
        $sorted = [];

    ?>
    <table class="table table-striped table-sm table-bordered mb-0">
        @if($items && count($items))
            <thead>
            <tr>
                <th class="border-bottom-0 px-2 text-secondary w-25">Title</th>
                <th class="border-bottom-0 px-2 text-secondary">Strength/Form</th>
                <th class="border-bottom-0 px-2 text-secondary">Frequency</th>
                <th class="border-bottom-0 px-2 text-secondary w-25">Detail</th>
                <th class="border-bottom-0 px-2 text-secondary w-25">Created</th>
                <th class="border-bottom-0 px-2 text-secondary">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach($items as $page):
                $parsed = $page->data ? json_decode($page->data) : false; ?>
                @if($parsed)
                    <tr>
                        <td class="px-2">{{ $page->key }}
                            <?php if(!!@$parsed->removed_during_note_uid): ?>
                                <span class="text-warning-mellow text-sm mr-2 font-weight-bold">(Removed<?= @$parsed->removed_during_note_uid === $note->uid ? ' during this visit' : '' ?>)</span>
                                <?php if(!!@$parsed->removal_memo): ?>
                                    <div class="text-sm text-secondary font-italic mt-1"><?= $parsed->removal_memo ?></div>
                                <?php endif; ?>
                            <?php elseif(!!@$parsed->was_existing_at_visit_start): ?>
                                <span class="text-secondary text-sm mr-2 font-weight-bold">(Existing)</span>
                            <?php endif; ?>
                        </td>
                        <td class="px-2">{{ $parsed->strength ? $parsed->strength : '-' }}</td>
                        <td class="px-2">{{ $parsed->frequency ? $parsed->frequency : '-' }}</td>
                        <td class="px-2">{{ $parsed->detail ? $parsed->detail : '-' }}</td>
                        <td class="px-2">{{ friendlier_date_time($page->created_at) }}</td>
                        <td class="px-2 text-nowrap">
                            <?php $view = 'edit'; ?>
                            <a native target="_blank"
                               open-in-stag-popup
                               mc-initer="edit-medication-{{$page->id}}"
                               update-parent
                               popup-style="narrow"
                               title="Edit Medication"
                               href="{{route('patients.view.notes.view.section-view', compact('patient', 'note', 'section', 'view', 'page'))}}">
                                Edit
                            </a>
                            @if(!@$parsed->removed_during_note_uid)
                                <?php $view = 'remove'; ?>
                                <a class="text-danger ml-2"
                                   native target="_blank"
                                   open-in-stag-popup
                                   mc-initer="remove-medication-{{$page->id}}"
                                   update-parent
                                   popup-style="narrow"
                                   title="Remove Medication?"
                                   href="{{route('patients.view.notes.view.section-view', compact('patient', 'note', 'section', 'view', 'page'))}}">
                                    Remove
                                </a>
                            @else
                                <?php $view = 'undo-remove'; ?>
                                <a class="text-info ml-2"
                                   native target="_blank"
                                   open-in-stag-popup
                                   mc-initer="undo-remove-medication-{{$page->id}}"
                                   update-parent
                                   popup-style="narrow"
                                   title="Undo Remove Medication?"
                                   href="{{route('patients.view.notes.view.section-view', compact('patient', 'note', 'section', 'view', 'page'))}}">
                                    Undo-Removal
                                </a>
                            @endif
                        </td>
                    </tr>
                @endif
            <?php endforeach; ?>
            </tbody>
        @else
            <tbody>
            <tr>
                <td class="text-secondary p-3">No medications added yet!</td>
            </tr>
            </tbody>
        @endif
    </table>
</div>
<script>
    (function() {
        function init() {
            $('.stag-popup-content .stag-popup-title .btn-add-medication').remove();
            $('.btn-add-medication')
                .addClass('ml-3')
                .insertAfter(
                    $('#medications-container')
                        .closest('.stag-popup-content')
                        .find('.stag-popup-title>span')
                );
        }
        addMCInitializer('medications-{{$patient->id}}', init, '#medications-container');
    }).call(window);
</script>