<?php

$keyToInputMap = [

	'generalAppearance' => [
		'Appropriate',
		'Disheveled',
		'Emaciated',
		'Obese',
		'Poor Hygiene',
		'Not Assessed'
	],

	'dressInput' => [
		'Appropriate',
		'Eccentric',
		'Seductive',
		'Bizarre',
		'Not Assessed'
	],

	'motorActivity' => [
		'Unremarkable',
		'Agitation',
		'Retardation',
		'Posturing',
		'Repetitive actions',
		'Tics',
		'Tremor',
		'Unusual Gait',
		'Not Assessed'
	],

	'insight' => [
		'Excellent',
		'Good',
		'Fair',
		'Poor',
		'Nil',
		'Not Assessed'
	],

	'judgment' => [
		'Excellent',
		'Good',
		'Fair',
		'Poor',
		'Nil',
		'Not Assessed'
	],

	'affect' => [
		'Congruent',
		'Incongruent',
		'Appropriate',
		'Inappropriate',
		'Reactive',
		'Constricted',
		'Blunted',
		'Flat',
		'Not Assessed'
	],

	'mood' => [
		'Euthymic',
		'Dysphoric',
		'Depressed',
		'Euphoric',
		'Grandiose',
		'Anxious',
		'Labile',
		'Irritable',
		'Angry',
		'Not Assessed'
	],

	'orientation' => [
		'X3: Oriented to Person, Place, and Time',
		'X2: Oriented to Person, Place; Impaired to Time',
		'X2: Oriented to Person, Time; Impaired to Place',
		'X2: Oriented to Time, Place; Impaired to Person',
		'X1: Oriented to Person; Impaired to Place, Time',
		'X1: Oriented to Place; Impaired to Person, Time',
		'X1: Oriented to Time; Impaired to Person, Place',
		'X0: Impaired to Person, Place, and Time',
		'Not Assessed'
	],

	'memory' =>[
		'Intact',
		'Poor Remote',
		'Poor Recent',
		'Not Assessed'
	],

	'attentionConcentration' => [
		'Good',
		'Distractible',
		'Variable',
		'Not Assessed',
    ],

	'thoughtContent' => [
		'Appropriate',
		'Preoccupied',
		'Obsessions',
		'Delusions: Persecutory',
		'Delusions: Bizarre',
		'Delusions: Grandeur',
		'Delusions: Guilt',
		'Delusions: Somatic',
		'Delusions: Ideas of Reference',
		'Delusions: Thought Broadcasting',
		'Delusions: Thought Control',
		'Not Assessed'
	],

	'perception' => [
		'Unremarkable',
		'Auditory Hallucinations',
		'Visual Hallucinations',
		'Olfactory Hallucinations',
		'Tactile Hallucinations',
		'Gustatory Hallucinations',
		'Not Assessed'
	],

	'thoughtProcess' => [
		'Unremarkable',
		'Blocking',
		'Circumstantial',
		'Flight of Ideas',
		'Loose Associations',
		'Perseveration',
		'Tangential',
		'Not Assessed'
	],

	'interviewBehavior' => [
		'Appropriate',
		'Aggressive',
		'Angry',
		'Apathetic',
		'Argumentative',
		'Child-like',
		'Demanding',
		'Drastic',
		'Evasive',
		'Hostile',
		'Irritable',
		'Passive',
		'Manipulative',
		'Withdrawn',
		'Uncooperative',
		'Not Assessed'
	],

	'speech' =>[
		'Normal',
		'Hesitant',
		'Pressured',
		'Slurred',
		'Soft',
		'Stuttering',
		'Mute',
		'Verbose',
		'Not Assessed'
	],

	'functionalStatus' => [
		'Intact',
		'Mildly Impaired',
		'Moderately Impaired',
		'Severely Impaired',
		'Variably Impaired',
		'Not Assessed'
	]

];

$keyToLabelMap = [
	'orientation' => 'Orientation',
	'generalAppearance' => 'General Appearance',
	'dressInput' => 'Dress',
	'motorActivity' => 'Motor Activity',
	'interviewBehavior' => 'Interview Behavior',
	'speech' => 'Speech',
	'mood' => 'Mood',
	'affect' => 'Affect',
	'insight' => 'Insight',
	'judgment' => 'Judgment/Impulse Control',
	'memory' => 'Memory',
	'attentionConcentration' => 'Attention / Concentration',
	'thoughtProcess' => 'Thought Process',
	'thoughtContent' => 'Thought Content',
	'perception' => 'Perception',
	'functionalStatus' => 'Functional Status'
];

$allNormal = [
	'Orientation' => 'X3: Oriented to Person, Place, and Time',
	'Insight' => 'Excellent',
	'General Appearance' => 'Appropriate',
	'Judgment/Impulse Control' => 'Excellent',
	'Dress' => 'Appropriate',
	'Memory' => 'Intact',
	'Motor Activity' => 'Unremarkable',
	'Attention/Concentration' => 'Good',
	'Interview Behavior' => 'Appropriate',       
	'Thought Process' => 'Unremarkable',
	'Speech' => 'Normal',
	'Thought Content' => 'Appropriate',
	'Mood' => 'Euthymic',
	'Perception' => 'Unremarkable',
	'Affect' => 'Congruent',
	'Functional Status' => 'Intact'
];

$displayColumns = [
	'left' => ['orientation', 'generalAppearance', 'dressInput', 'motorActivity', 'interviewBehavior', 'speech', 'mood', 'affect'],
	'right' => ['insight', 'judgement', 'memory', 'attentionConcentration', 'thoughtProcess', 'thoughtContent', 'perception', 'functionalStatus']
];

$point = App\Models\Point::getOrCreateOnlyTopLevelPointOfCategory($note, 'PSYCH_INTAKE_CURRENT_MENTAL_STATUS', $sessionKey, true);
$contentData = [
    'orientation' => null,
	'generalAppearance' => null,
	'dressInput' => null,
	'motorActivity' => null,
	'interviewBehavior' => null,
	'speech' => null,
	'mood' => null,
	'affect' => null,
	'insight' => null,
	'judgment' => null,
	'memory' => null,
	'attentionConcentration' => null,
	'thoughtProcess' => null,
	'thoughtContent' => null,
	'perception' => null,
	'functionalStatus' => null,
];
if ($point->lastChildReview && $point->lastChildReview->data) {
    $point->lastChildReview->data = json_decode($point->lastChildReview->data, true);
    $contentData = $point->lastChildReview->data;
}
$submitFormUrl = "/api/visitPoint/upsertChildReview";
?>

<div visit-moe close-on-save close-on-cancel class="d-block" id="psych-intake-current-mental-status-{{$note->id}}">
    <form id="pychIntakeForm" show url="{{ $submitFormUrl }}" class="mcp-theme-1 frm-upsert-review-typical-day-ls">
        <input type="hidden" name="uid" value="<?= $point->uid ?>">
        <input type="hidden" name="noteUid" value="<?= $note->uid ?>">
        <input type="hidden" name="segmentUid" value="<?= $segment->uid ?>">
        <input type="hidden" name="data" value="{{json_encode($contentData)}}">

        <div class="mb-2 d-flex align-items-center justify-content-between">
			<p class="mb-0 font-weight-bold">&nbsp;</p>
			<div>
				<a class="mr-2 font-weight-bold c-pointer" all-normal><i class="fas fa-thumbs-up"></i> All Normal</a>
				<a class="font-weight-bold c-pointer" all-not-assessed><i class="fas fa-times-circle"></i> All Not assessed</a>
			</div>
		</div>

        <div>
            <div class="row mx-0">
                
                <div class="col-md-6 pl-0">
                    @foreach($keyToInputMap as  $field => $options)
                        @if(in_array($field, $displayColumns['left']))
							<div class="row align-items-center mb-1 mx-0">
								<div class="col-md-4 pl-0">
									<label class="control-label mb-0">{{$keyToLabelMap[$field]}}:</label>
								</div>
								<div class="col-md-8 pr-0">
									<input type="text" list="{{$field}}s" data-option-list data-name="{{$field}}" name="{{$field}}" id="{{$field}}" class="form-control form-control-sm" value="{{ @$contentData[$field] }}">
									<div class="data-option-list">
										@foreach($options as $option)
											<div>{{$option}}</div>
										@endforeach
									</div>
								</div>
							</div>
                        @endif
                    @endforeach
                </div>
                    
                <div class="col-md-6 px-0">
                    @foreach($keyToInputMap as  $field => $options)
                        @if(in_array($field, $displayColumns['right']))
						<div class="row align-items-center mb-1 mx-0">
							<div class="col-md-4 pl-0">
								<label class="control-label mb-0">{{$keyToLabelMap[$field]}}:</label>
							</div>
							<div class="col-md-8 pr-0">
								<input type="text" list="{{$field}}s" data-option-list data-name="{{$field}}" name="{{$field}}" id="{{$field}}" class="form-control form-control-sm" value="{{ @$contentData[$field] }}">
								<div class="data-option-list">
									@foreach($options as $option)
										<div>{{$option}}</div>
									@endforeach
								</div>
							</div>
						</div>
                        @endif
                    @endforeach
                </div>
            </div>   
        </div>    
        <div>
            <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($){
		var pychIntakeComponent = {
			form: null,
			keyLabelMap: <?= json_encode($keyToLabelMap) ?>,
			allNormal:<?= json_encode($allNormal) ?>,
			NotAssessedValue: "Not Assessed",

			initOnSetAllNormal: function(){
				var self = this;
				
				$('[all-normal]').click(function(){
					self.resetFieldsToAllNormal();
					return;
				});
				$('[all-not-assessed]').click(function(){
					self.resetFieldsToAllNormal(true);
					return;
				});
			},
			resetFieldsToAllNormal: function(setAsNotAssessed){
				var self = this;
				
				var formData = {};
				var inputs = self.form.find('[data-name]');
				$.each(inputs, function(i, input){
					var key = $(input).data('name');
					var ref = self.keyLabelMap[key];
					var defaultValue = self.allNormal[ref];
					if(setAsNotAssessed){
						defaultValue = self.NotAssessedValue;
					}
					$(input).val(defaultValue);
					formData[key] = defaultValue;
				});
				self.submitForm(formData);
			},
			submitForm: function(data){
				this.form.find('input[name=data]').val(JSON.stringify(data));
                $.post('{{ $submitFormUrl }}', this.form.serializeArray(), function(response) {}, 'json');
			},
			init: function(){
				this.form = $('#pychIntakeForm');
				this.initOnSetAllNormal();
			}
		};
		pychIntakeComponent.init();
	})(jQuery);
</script>
