<?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 */

$careTeamMembers = Point::getPlanPointsOfCategory($patient, 'CARE_TEAM_MEMBER', $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">Care Team Member</th>
                <th class="border-bottom-0">Most Recent Subjective</th>
                <th class="border-bottom-0">Most Recent Plan</th>
            </tr>
            </thead>
            <?php foreach($careTeamMembers as $careTeamMember): ?>
                <?php $point = $careTeamMember; ?>
                <?php $rel = $point->relevanceToNote($note); ?>
                <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-point-uid="{{$point->uid}}"
                                   data-rel-uid="{{$rel ? $rel->uid : ''}}"
                                    {{$rel ? 'checked' : ''}}>
                            <span>&nbsp;</span>
                        </label>
                    </td>
                    <td>
                        <div class="d-flex align-items-baseline">
                            <div class="<?= $careTeamMember->is_removed ? 'strike-through' : '' ?>">
                                <b><?= !!@($careTeamMember->data->name) ? @($careTeamMember->data->name) : '-' ?></b>
                                <?= !!@($careTeamMember->data->specialty) ? '/&nbsp;' . @($careTeamMember->data->specialty) : '' ?>
                                <?= !!@($careTeamMember->data->organization) ? '/&nbsp;' . @($careTeamMember->data->organization) : '' ?>
                                <br>
                                <?= !!@($careTeamMember->data->phone) ? 'Ph: ' . @($careTeamMember->data->phone) : '' ?>
                                <?= !!@($careTeamMember->data->fax) ? '/&nbsp;Fax: ' . @($careTeamMember->data->fax) : '' ?>
                                <br>
                                <?= !!@($careTeamMember->data->date) ? 'Date: ' . @($careTeamMember->data->date) : '' ?>
                            </div>

                            <!-- common actions -->
                            <div class="ml-auto d-inline-flex align-items-baseline pr-2">
                                <?php
                                $label = 'Care Team Member';
                                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="CARE_TEAM_MEMBER">
                                            <input type="hidden" name="data">

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

                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Name</label>
                                                <input type="text" data-name="name" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Specialty</label>
                                                <input type="text" data-name="specialty" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Organization</label>
                                                <input type="text" data-name="organization" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Phone</label>
                                                <input type="text" data-name="phone" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Fax</label>
                                                <input type="text" data-name="fax" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Date</label>
                                                <input type="date" data-name="date" class="form-control form-control-sm">
                                            </div>
                                            <div class="mb-2">
                                                <label class="text-sm text-secondary mb-1">Memo</label>
                                                <textarea type="text" data-name="memo" 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 = $careTeamMember;
                                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<?= !!@($careTeamMember->data->name) ? ' for ' . @($careTeamMember->data->name) : '' ?>"
                               href="/point/review-log/<?= $careTeamMember->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>
                            <div class="d-inline-flex flex-nowrap">
                                <a class="px-1 view-review-log"
                                   native target="_blank"
                                   open-in-stag-popup
                                   popup-style="stag-popup-md"
                                   title="Plan log<?= !!@($careTeamMember->data->name) ? ' for ' . @($careTeamMember->data->name) : '' ?>"
                                   href="/point/plan-log/<?= $careTeamMember->uid ?>?popupmode=1">
                                    <i class="fa fa-history"></i>
                                </a>
                                <?php
                                include resource_path('views/app/patient/segment-templates/_child_plan/edit-plan.php');
                                ?>
                            </div>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>

    <div class="d-flex align-items-center">
        <div visit-moe large class="mt-1 w-100 border p-2 bg-white rounded">
            <!--<a href="#" start show class="btn btn-sm btn btn-outline-primary">+ Add new care team member, added during this visit</a>-->
            <form show url="/api/visitPoint/addTopLevelDuringVisit" class="mcp-theme-1 w-100">
                <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
                <input type="hidden" name="category" value="CARE_TEAM_MEMBER">
                <input type="hidden" name="data">

                <p class="mb-2"><b>Add Care Team Member (plan)</b></p>

                <div class="d-flex align-items-start">

                <div class="mb-2">
                    <label class="text-sm text-secondary mb-1">Name</label>
                    <input type="text" data-name="name" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2">
                    <label class="text-sm text-secondary mb-1">Specialty</label>
                    <input type="text" data-name="specialty" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2">
                    <label class="text-sm text-secondary mb-1">Organization</label>
                    <input type="text" data-name="organization" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2">
                    <label class="text-sm text-secondary mb-1">Phone</label>
                    <input type="text" data-name="phone" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2">
                    <label class="text-sm text-secondary mb-1">Fax</label>
                    <input type="text" data-name="fax" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2">
                    <label class="text-sm text-secondary mb-1">Date</label>
                    <input type="date" data-name="date" class="form-control form-control-sm">
                </div>
                <div class="mb-2 ml-2 flex-grow-1">
                    <label class="text-sm text-secondary mb-1">Memo</label>
                    <textarea rows="1" data-name="memo" class="form-control form-control-sm"></textarea>
                </div>

                </div>

                <div>
                    <button submit class="btn btn-sm btn-primary mr-2">Submit</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>