<?php

use App\Models\Point;

$category = 'OBJECTIVE_SYSTEM';
$endPoint = 'upsertNoteSingleton';

$point = Point::where('added_in_note_id', $note->id)->where('category', $category)->orderBy('id', 'DESC')->first();
$contentData = [
    "reviewed_relevant_labs" => null,
    "lab_findings" => null,
    "reviewed_relevant_rpm_data" => null,
    "rpm_findings" => null,
    "rpm_data_unavailable" => null
];
if (!!@$point->data) {
    $contentData = json_decode($point->data, true);
}
?>
<div visit-moe close-on-save close-on-cancel class="d-block px-2 py-1">
    <form show url="/api/visitPoint/<?= $endPoint ?>" class="mcp-theme-1">
        <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
        <input type="hidden" name="category" value="<?= $category ?>">
        <input type="hidden" name="data" value="{{json_encode($contentData)}}">

        <label class="d-flex align-items-center mb-2">
            <input type="checkbox" data-name="reviewed_relevant_labs" {{@$contentData["reviewed_relevant_labs"] ? 'checked' : ''}} class="mr-2">
            <span>Reviewed relevant labs.</span>
        </label>

        <div class="mb-2 align-items-baseline {{ @$contentData["reviewed_relevant_labs"] ? 'd-flex' : 'd-none' }}" if-reviewed-labs>
            <span class="text-dark mr-2 text-nowrap">Lab Findings:</span>
            <input type="text" class="form-control form-control-sm flex-grow-1" data-name="lab_findings" value="{{@$contentData["lab_findings"]}}">
        </div>

        <label class="d-flex align-items-center mb-2">
            <input type="checkbox" data-name="reviewed_relevant_rpm_data" {{@$contentData["reviewed_relevant_rpm_data"] ? 'checked' : ''}} class="mr-2">
            <span>Reviewed relevant remote measurements.</span>
            <?php
            // resolve care month from the effective date of this note
            $cmStartDate = date('Y-m-01', strtotime($note->effective_dateest));
            $careMonth = \App\Models\CareMonth::where('client_id', $note->client->id)->where('start_date', $cmStartDate)->first();
            ?>
            @if($careMonth)
                <a href="/patients/view/{{ $note->client->uid }}/care-months/view/{{$careMonth->uid}}"
                   class="text-nowrap ml-3"
                   native target="_blank"
                   open-in-stag-popup
                   popup-style="tall overflow-visible"
                   mc-initer="care-month-dashboard-{{$note->client->uid}}"
                   title="Current Care Month RPM Data"
                >Current Care Month RPM Data</a>
            @endif
        </label>

        <div class="mb-2 align-items-baseline {{ @$contentData["reviewed_relevant_rpm_data"] ? 'd-flex' : 'd-none' }}" if-reviewed-rpm>
            <span class="text-dark mr-2 text-nowrap">RPM Findings:</span>
            <input type="text" class="form-control form-control-sm flex-grow-1" data-name="rpm_findings" value="{{@$contentData["rpm_findings"]}}">
        </div>

        <label class="d-flex align-items-center mb-2">
            <input type="checkbox" data-name="rpm_data_unavailable" {{@$contentData["rpm_data_unavailable"] ? 'checked' : ''}} class="mr-2">
            <span>Remote monitoring data not available.</span>
        </label>

        <div class="pt-2">
            <button submit class="btn btn-sm btn-primary mr-2"><i class="fa fa-save"></i></button>
            <div class="d-inline-flex align-self-stretch align-items-center">
                <span class="autosave-indicator saving text-sm text-secondary">Saving changes &hellip;</span>
                <span class="autosave-indicator saved text-sm text-secondary">
                    <i class="fa fa-check"></i>
                    Saved
                </span>
            </div>
        </div>
    </form>
</div>

<script>
    (function() {
        window.segmentInitializers.<?= !!@$segment ? $segment->segmentTemplate->internal_name : $segmentInternalName ?> = function () {
            let parentSegment = $('[data-segment-template-name="<?= $segment->segmentTemplate->internal_name ?>"] ');
            parentSegment.find('[data-name="reviewed_relevant_labs"]')
                .off('change')
                .on('change', function() {
                    if(this.checked) {
                        parentSegment.find('[if-reviewed-labs]').removeClass('d-none').addClass('d-flex');
                    }
                    else {
                        parentSegment.find('[if-reviewed-labs]').removeClass('d-flex').addClass('d-none');
                    }
                });
            parentSegment.find('[data-name="reviewed_relevant_rpm_data"]')
                .off('change')
                .on('change', function() {
                    if(this.checked) {
                        parentSegment.find('[data-name="rpm_data_unavailable"]').prop('checked', false);
                        parentSegment.find('[if-reviewed-rpm]').removeClass('d-none').addClass('d-flex');
                    }
                    else {
                        parentSegment.find('[if-reviewed-rpm]').removeClass('d-flex').addClass('d-none');
                    }
                });
            parentSegment.find('[data-name="rpm_data_unavailable"]')
                .off('change')
                .on('change', function() {
                    if(this.checked) {
                        parentSegment.find('[data-name="reviewed_relevant_rpm_data"]').prop('checked', false);
                        parentSegment.find('[if-reviewed-rpm]').removeClass('d-flex').addClass('d-none');
                    }
                });
        };
    }).call(window);
</script>