<?php

use App\Models\Point;
use \App\Models\Client;
use \App\Models\Note;
use \App\Models\Segment;

/** @var Client $patient */
/** @var Note $note */
/** @var Segment $segment */

$interventions = Point::getPlanPointsOfCategory($patient, 'INTERVENTION', $note);
$intakeOrVisit = 'PLAN';

?>
<div>

        <table class="table table-sm table-bordered table-striped mb-0 bg-white mb-2">
            <thead>
            <tr class="">
                <th class="border-bottom-0 width-100px text-info">Relevant to<br>this visit</th>
                <th class="border-bottom-0">Category</th>
                <th class="border-bottom-0">Intervention</th>
                <th class="border-bottom-0">Most Recent Subjective</th>
                <th class="border-bottom-0">Most Recent Plan</th>
            </tr>
            </thead>
            <?php $previousCategory = null; ?>
            <?php foreach($interventions as $intervention): ?>
                <?php $point = $intervention; ?>
                <tr>
                    <td class="p-0">
                        <label class="d-flex align-items-center px-2 py-1 m-0 c-pointer">
                            <input type="checkbox" class="m-0 chk-relevance"
                                   data-uid="{{$intervention->uid}}" {{$rel ? 'checked' : ''}}>
                            <span>&nbsp;</span>
                        </label>
                    </td>
                    <td>
                        <?php if($previousCategory === $intervention->data->category): ?>
                            <span class="text-secondary"><?= $intervention->data->category ?></span>
                        <?php else: ?>
                            <span class="font-weight-bold"><?= $intervention->data->category ?></span>
                        <?php endif; ?>
                    </td>
                    <td>
                        <div class="d-flex align-items-baseline">
                            <div class="<?= $intervention->is_removed ? 'strike-through' : '' ?>">
                                <?= !!@($intervention->data->intervention) ? @($intervention->data->intervention) : '' ?>
                                <br>
                                <span class="text-secondary"><?= !!@($intervention->data->description) ? @($intervention->data->description) : '' ?></span>
                            </div>

                            <!-- common actions -->
                            <div class="ml-auto d-inline-flex align-items-baseline pr-2">
                                <?php
                                $label = 'Intervention';
                                include resource_path('views/app/patient/segment-templates/_common_actions/remove-undo.php');
                                ?>
                                @if($point->added_in_note_id === $note->id && $point->addition_reason_category == 'DURING_VISIT')
                                    <div visit-moe class="ml-2">
                                        <a href="#" start show class="">Swap</a>
                                        <form url="/api/visitPoint/swapTopLevelEntryError" class="mcp-theme-1">
                                            <input type="hidden" name="uid" value="<?= $point->uid ?>">
                                            <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
                                            <input type="hidden" name="category" value="INTERVENTION">
                                            <input type="hidden" name="data">

                                            <p class="mb-2"><b>Add Intervention (on intake)</b></p>

                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Name</label>
                                                <select type="text" data-name="category" class="form-control form-control-sm">
                                                    <option value="">-- select --</option>
                                                    <option value="Self-Monitoring">Self-Monitoring</option>
                                                    <option value="Labs">Labs</option>
                                                    <option value="Radiology / Imaging">Radiology / Imaging</option>
                                                    <option value="Screenings">Screenings</option>
                                                    <option value="Medication Rx">Medication Rx</option>
                                                    <option value="Physical Therapy Rx">Physical Therapy Rx</option>
                                                    <option value="Occupational Therapy Rx">Occupational Therapy Rx</option>
                                                    <option value="Referral to Specialist / HCP">Referral to Specialist / HCP</option>
                                                    <option value="Clinical Instructions / Other">Clinical Instructions / Other</option>
                                                    <option value="SMS Reminders">SMS Reminders</option>
                                                    <option value="Handouts / Education Provided">Handouts / Education Provided</option>
                                                </select>
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Intervention</label>
                                                <input type="text" data-name="intervention" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Description</label>
                                                <textarea type="text" data-name="description" class="form-control form-control-sm"></textarea>
                                            </div>

                                            <div>
                                                <button submit class="btn btn-sm btn-primary mr-2">Submit</button>
                                                <button cancel class="btn btn-sm btn-default border">Cancel</button>
                                            </div>
                                        </form>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </td>
                    <td>
                        <div class="d-flex align-items-start">
                            <div class="flex-grow-1">
                                <?php
                                $point = $intervention;
                                include resource_path('views/app/patient/segment-templates/_child_review/last-review.php');
                                ?>
                            </div>
                            <a class="px-1 view-review-log"
                               native target="_blank"
                               open-in-stag-popup
                               popup-style="stag-popup-md"
                               title="Subjective log<?= !!@($intervention->data->intervention) ? ' for ' . @($intervention->data->intervention) : '' ?>"
                               href="/point/review-log/<?= $intervention->uid ?>?popupmode=1">
                                <i class="fa fa-history"></i>
                            </a>
                        </div>
                    </td>
                    <td>
                        <div class="d-flex align-items-start">
                            <div class="flex-grow-1">
                                <?php
                                include resource_path('views/app/patient/segment-templates/_child_plan/last-plan.php');
                                ?>
                            </div>
                            <a class="px-1 view-review-log"
                               native target="_blank"
                               open-in-stag-popup
                               popup-style="stag-popup-md"
                               title="Plan log<?= !!@($intervention->data->intervention) ? ' for ' . @($intervention->data->intervention) : '' ?>"
                               href="/point/plan-log/<?= $intervention->uid ?>?popupmode=1">
                                <i class="fa fa-history"></i>
                            </a>
                        </div>
                    </td>
                    <td>
                        <?php
                        include resource_path('views/app/patient/segment-templates/_child_plan/edit-plan.php');
                        ?>
                    </td>
                </tr>
                <?php $previousCategory = $intervention->data->category; ?>
            <?php endforeach; ?>
        </table>

    <div class="d-flex align-items-center">
        <div visit-moe large class="max-width-500px">
            <a href="#" start show class="btn btn-sm btn btn-outline-primary ml-2 mb-2">+ Add new intervention, added during this visit</a>
            <form url="/api/visitPoint/addTopLevelDuringVisit" class="mcp-theme-1">
                <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
                <input type="hidden" name="category" value="INTERVENTION">
                <input type="hidden" name="data">

                <p class="mb-2"><b>Add Intervention (on intake)</b></p>

                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Name</label>
                    <select type="text" data-name="category" class="form-control form-control-sm">
                        <option value="">-- select --</option>
                        <option value="Self-Monitoring">Self-Monitoring</option>
                        <option value="Labs">Labs</option>
                        <option value="Radiology / Imaging">Radiology / Imaging</option>
                        <option value="Screenings">Screenings</option>
                        <option value="Medication Rx">Medication Rx</option>
                        <option value="Physical Therapy Rx">Physical Therapy Rx</option>
                        <option value="Occupational Therapy Rx">Occupational Therapy Rx</option>
                        <option value="Referral to Specialist / HCP">Referral to Specialist / HCP</option>
                        <option value="Clinical Instructions / Other">Clinical Instructions / Other</option>
                        <option value="SMS Reminders">SMS Reminders</option>
                        <option value="Handouts / Education Provided">Handouts / Education Provided</option>
                    </select>
                </div>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Intervention</label>
                    <input type="text" data-name="intervention" class="form-control form-control-sm">
                </div>
                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Description</label>
                    <textarea type="text" data-name="description" class="form-control form-control-sm"></textarea>
                </div>

                <div>
                    <button submit class="btn btn-sm btn-primary mr-2">Submit</button>
                    <button cancel class="btn btn-sm btn-default border">Cancel</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    window.segmentInitializers.<?= $segment->segmentTemplate->internal_name ?> = function () {
        // any JS can come here
        // will be run on page-load as well as whenever this segment is refreshed

        let parentSegment = $('[data-segment-template-name="<?= $segment->segmentTemplate->internal_name ?>"] ');

    };
</script>