<?php

use App\Models\Point;

$point = Point::where('added_in_note_id', $note->id)->where('category', 'SUBJECTIVE_GENERAL')->orderBy('id', 'DESC')->first();
$parsed = null;
if (!!@$point->data) {
    $parsed = json_decode($point->data);
}
$content = '';
if (!!$parsed && @$parsed->free_text) {
    $content = $parsed->free_text;
}
else {
    if($note->visit_number === 1) {
        $intakeDataFields = config('patient-intake-data');
        $patientCanvasData = json_decode($patient->canvas_data ?? null);
        $patientIntakeData = null;
        if ($patientCanvasData && @$patientCanvasData->INTAKE_DATA) {
            $patientIntakeData = @$patientCanvasData->INTAKE_DATA;
            foreach($intakeDataFields as $key=>$label) {
                if($key == 'ethnicity_checkboxes') {
                    foreach($label as $eKey => $eLabel) {
                        if(@$patientIntakeData->{$eKey}) {
                            $content .= $eLabel . ': ' . @$patientIntakeData->{$eKey} . '&nbsp;&nbsp;&nbsp;';
                        }
                    }
                }
                else {
                    if(@$patientIntakeData->{$key}) {
                        $content .= ($key !== 'stickyNote' ? $label . ': ' : '<br>') . @$patientIntakeData->{$key} . '&nbsp;&nbsp;&nbsp;';
                    }
                }
            }
        }
    }
}
?>
<div visit-moe close-on-save close-on-cancel class="d-block">
    <form show url="/api/visitPoint/upsertNoteSingleton" class="mcp-theme-1">
        <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
        <input type="hidden" name="category" value="SUBJECTIVE_GENERAL">
        <input type="hidden" name="data">
        <div note-rte
             class="form-group mb-2 border-left border-right rte-holder"
             data-field-name="free_text"
        ><?= $content ?></div>
        <div class="m-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>