<!-- To change the layout of the detail view form, we recommend using JS code in hooks/patients-dv.js rather than editing this file -->

<div id="detail-view"></div>
<div class="panel-heading"><h3 class="panel-title"><strong><%%DETAIL_VIEW_TITLE%%></strong></h3></div>

<div id="patients_dv_container-loading" class="text-center text-info text-bold vspacer-lg"><i class="glyphicon glyphicon-refresh loop-rotate"></i> <%%TRANSLATION(Loading ...)%%></div>

<div class="panel-body hidden" id="patients_dv_container">
	<!-- child links -->
	<div class="row vspacer-md">
		<div class="col-xs-12 children-links">
			<a class="hidden-xs btn btn-default btn-lg pull-right hidden hspacer-sm vspacer-sm" href="medical_records_view.php?filterer_patient=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="medical_records_link"><img src="resources/table_icons/cash_terminal.png"> Medical Records</a>
			<a class="visible-xs btn btn-default btn-lg btn-block hidden vspacer-sm" href="medical_records_view.php?filterer_patient=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="xs_medical_records_link"><img src="resources/table_icons/cash_terminal.png"> Medical Records</a>
			<input type="hidden" id="medical_records_hclink" value="medical_records_view.php?filterer_patient=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>">
			<input type="hidden" id="events_hclink" value="events_view.php?filterer_name_patient=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>">
			<div class="clearfix"></div>
		</div>
	</div>
	<hr class="hidden-xs hidden-sm">

	<div class="row">
		<!-- form inputs -->
		<div class="col-md-8 col-lg-10" id="patients_dv_form">
			<fieldset class="form-horizontal">

				<!-- Field: ID -->
				<div class="form-group patients-id">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="id">ID</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="id"><%%VALUE(id)%%></div>
					</div>
				</div>

				<!-- Field: Last name -->
				<div class="form-group patients-last_name">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="last_name">Last name<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<input maxlength="40" type="text" class="form-control" name="last_name" id="last_name" value="<%%VALUE(last_name)%%>" required>
					</div>
				</div>

				<!-- Field: First name -->
				<div class="form-group patients-first_name">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="first_name">First name<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<input maxlength="40" type="text" class="form-control" name="first_name" id="first_name" value="<%%VALUE(first_name)%%>" required>
					</div>
				</div>

				<!-- Field: Gender -->
				<div class="form-group patients-gender">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="gender">Gender<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<%%COMBO(gender)%%>
					</div>
				</div>

				<!-- Field: Sexual orientation -->
				<div class="form-group patients-sexual_orientation">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="sexual_orientation">Sexual orientation<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<%%COMBO(sexual_orientation)%%>
					</div>
				</div>

				<!-- Field: Birth date -->
				<div class="form-group patients-birth_date">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="birth_date-mm">Birth date</label>
					<div class="col-lg-9">
						<%%COMBO(birth_date)%%>
					</div>
				</div>

				<!-- Field: Age -->
				<div class="form-group patients-age">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Age <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#age-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="age-description"><div class="alert alert-info bspacer-xs">Automatically calculated from birth date.</div></span>
						<div class="form-control-static" id="age"><%%VALUE(age)%%></div>
					</div>
				</div>

				<!-- Field: Image -->
				<div class="form-group patients-image">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Image <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#image-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="image-description"><div class="alert alert-info bspacer-xs">Maximum file size allowed: 1000 KB.<br>Allowed file types: jpg, jpeg, gif, png</div></span>
						<div class="form-control-static well well-sm">
							<div class="row" style="margin-right: 0; margin-left: 0;">
								<div class="col-lg-8 col-md-6" style="padding-left: 0;"><a href="<%%BASE_UPLOAD_PATH%%><%%VALUE(image)%%>" data-lightbox="patients_dv"><img src="thumbnail.php?i=<%%VALUE(image)%%>&t=patients&f=image&v=dv" class="img-thumbnail" id="image-image"></a></div>
								<div class="hidden-md hidden-lg vspacer-lg"></div>
								<div class="col-lg-4 col-md-6 well upload-toolbox">
									<%%REMOVEFILE(image)%%>
									<%%UPLOADFILE(image)%%>
									<div class="text-bold text-danger file-type-error hidden"><%%TRANSLATION(invalid file type)%%></div>
									<div class="text-bold text-danger file-size-error hidden"><%%TRANSLATION(file too large)%%></div>
								</div>
							</div>
						</div>
						<script>/* disable empty links */ $j(() => { const e_image = '<%%VALUE(image)%%>'; if(e_image == '' || e_image == '&nbsp;' || e_image == 'blank.gif') { $j('#image-link').hide(); $j('#image-image').parents('a').attr('href', '').on('click', (e) => e.preventDefault()); } });</script>
					</div>
				</div>

				<!-- Field: State -->
				<div class="form-group patients-state">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="state">State</label>
					<div class="col-lg-9">
						<%%COMBO(state)%%>
					</div>
				</div>

				<!-- Field: Mobile -->
				<div class="form-group patients-mobile">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="mobile">Mobile</label>
					<div class="col-lg-9">
						<input maxlength="40" type="text" class="form-control" name="mobile" id="mobile" value="<%%VALUE(mobile)%%>">
					</div>
				</div>

				<!-- Field: Tobacco usage -->
				<div class="form-group patients-tobacco_usage">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="tobacco_usage">Tobacco usage<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<%%COMBO(tobacco_usage)%%>
					</div>
				</div>

				<!-- Field: Alcohol Intake -->
				<div class="form-group patients-alcohol_intake">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="alcohol_intake">Alcohol Intake<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<%%COMBO(alcohol_intake)%%>
					</div>
				</div>

				<!-- Field: History -->
				<div class="form-group patients-history">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="history">History<span class="text-danger"><%%TRANSLATION(*)%%></span></label>
					<div class="col-lg-9">
						<%%COMBO(history)%%>
					</div>
				</div>

				<!-- Field: Surgical history -->
				<div class="form-group patients-surgical_history">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="surgical_history">Surgical history</label>
					<div class="col-lg-9">
						<textarea class="form-control" name="surgical_history" id="surgical_history" rows=" 5"><%%VALUE(surgical_history)%%></textarea>
					</div>
				</div>

				<!-- Field: Obstetric history -->
				<div class="form-group patients-obstetric_history">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="obstetric_history">Obstetric history</label>
					<div class="col-lg-9">
						<textarea class="form-control" name="obstetric_history" id="obstetric_history" rows=" 5"><%%VALUE(obstetric_history)%%></textarea>
					</div>
				</div>

				<!-- Field: Genetic diseases -->
				<div class="form-group patients-genetic_diseases">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="genetic_diseases">Genetic diseases</label>
					<div class="col-lg-9">
						<textarea class="form-control" name="genetic_diseases" id="genetic_diseases" rows=" 5"><%%VALUE(genetic_diseases)%%></textarea>
					</div>
				</div>

				<!-- Field: Contact person in case of Emergency -->
				<div class="form-group patients-contact_person">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="contact_person">Contact person in case of Emergency</label>
					<div class="col-lg-9">
						<input maxlength="100" type="text" class="form-control" name="contact_person" id="contact_person" value="<%%VALUE(contact_person)%%>">
					</div>
				</div>

				<!-- Field: Other details -->
				<div class="form-group patients-other_details">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="other_details">Other details</label>
					<div class="col-lg-9">
						<textarea class="form-control" name="other_details" id="other_details" rows=" 5"><%%VALUE(other_details)%%></textarea>
					</div>
				</div>

				<!-- Field: Comments -->
				<div class="form-group patients-comments">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="comments">Comments</label>
					<div class="col-lg-9 has-nicedit">
						<%%HTMLAREA(comments)%%>
					</div>
				</div>

				<!-- Field: Filed -->
				<div class="form-group patients-filed">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Filed <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#filed-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="filed-description"><div class="alert alert-info bspacer-xs">This field is automatically populated with the date and time when this record was created.</div></span>
						<div class="form-control-static" id="filed"><%%VALUE(filed)%%></div>
					</div>
				</div>

				<!-- Field: Last modified -->
				<div class="form-group patients-last_modified">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Last modified <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#last_modified-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="last_modified-description"><div class="alert alert-info bspacer-xs">This field is automatically populated with the date and time when this record was last modified.</div></span>
						<div class="form-control-static" id="last_modified"><%%VALUE(last_modified)%%></div>
					</div>
				</div>

			</fieldset>
		</div>

		<!-- DV action buttons -->
		<hr class="hidden-md hidden-lg">
		<div class="col-md-4 col-lg-2" id="patients_dv_action_buttons">
			<div class="btn-toolbar">
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%UPDATE_BUTTON%%>
				</div><p></p>
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%DESELECT_BUTTON%%>
					<%%DVPRINT_BUTTON%%>
					<%%DELETE_BUTTON%%>
				</div><p></p>
				<div class="btn-group-vertical btn-group-lg" style="width: 100%;">
					<%%INSERT_BUTTON%%>
				</div>
			</div>
		</div>
	</div>


	<!-- child records -->
	<hr>
	<div class="row">
		<div class="col-lg-10 col-lg-offset-1">
			<div id="patients-children" class="children-tabs">
				<div class="text-center"><i class="glyphicon glyphicon-refresh loop-rotate"></i> <%%TRANSLATION(Loading ...)%%></div>
			</div>
		</div>
	</div>

</div><!-- /div class="panel-body" -->
	<script>
		$j(function() {
			var add_new_mode = (!$j('input[name=SelectedID]').val());
			var embedded_mode = ($j('input[name=Embedded]').val() == 1);

			/* auto-close embedded page? */
			var autoclose_mode = ($j('input[name=AutoClose]').val() == 1);
			var record_added_ok = (location.search.split('record-added-ok=').length == 2);
			var record_updated_ok = (location.search.split('record-updated-ok=').length == 2);
			if((record_added_ok || record_updated_ok) && embedded_mode && autoclose_mode) {
				setTimeout(AppGini.closeParentModal, 2000);
			}

			AppGini.once({
				condition: function() { return bkLib != undefined; },
				action: function() {
					if($j('textarea#comments').length) new nicEditor({ fullPanel : true }).panelInstance('comments');
				}
			});

			/* enable DV action buttons to float on scrolling down the form */
			enable_dvab_floating();

			$j('#patients_dv_container-loading').addClass('hidden');
			$j('#patients_dv_container').removeClass('hidden');
			if(!$j('.table_view').length || $j('[name=SelectedID]').val().length) AppGini.focusFormElement('patients', 'last_name');

			$j('form').eq(0).change(function() {
				if($j(this).data('already_changed')) return;
				if($j('#deselect').length) $j('#deselect').removeClass('btn-default').addClass('btn-warning').get(0).lastChild.data = " <%%TRANSLATION(Cancel)%%>";
				$j(this).data('already_changed', true);
			});

			// try to detect rich text changes by adding 'input' handler after nicedit is loaded ...
			AppGini.detectContentEditableChanges();

			$j('a[href="<%%BASE_UPLOAD_PATH%%>"]').click(function() { return false; });

			if(embedded_mode) {
				/* hide links to children tables in embedded mode */
				$j('.detail_view a[id$=_link]').remove();

				/* append embedded mode to all links */
				var append_embedded = function() {
					$j('a[href]:not([href^="#"]):not([href^="javascript"]):not([href$="Embedded=1"]):not([href*="link.php?t="]):not([href^="mailto:"]):not([href$=".jpg"]):not([href$=".png"]):not([href$=".gif"])').each(
						function() {
							$j(this).attr('href', $j(this).attr('href') + '&Embedded=1');
						}
					);
				}
				setTimeout(append_embedded, 500);
				setTimeout(append_embedded, 1500);
				setTimeout(append_embedded, 2500);
				setTimeout(append_embedded, 3500);
			}

			/* when no record is selected ('add new' mode) */
			if(add_new_mode) {
				/* hide links to children tables */
				$j('.detail_view a[id$=_link]').remove();
			} else {
				post(
					'parent-children.php', {
						ParentTable: 'patients',
						SelectedID: '<%%VALUE(id)%%>',
						Operation: 'show-children'
					},
					'patients-children'
				);
			}

			if(true /* !embedded_mode */) {
				$j('.add_new_parent').click(function() {
					var pt = $j(this).attr('id').replace(/_add_new$/, '');
					modal_window({
						url: pt + '_view.php?addNew_x=1&Embedded=1',
						close: function() {
							var field_id = $j('#' + pt + '_add_new').prevAll('input:hidden').eq(0).attr('id');
							$j('#' + field_id + '-container').select2('focus').select2('focus');

							/* if a new parent was added, select it */
							var new_id = localStorage.getItem(pt + '_last_added_id');
							if(new_id === null) return;

							localStorage.removeItem(pt + '_last_added_id');
							AppGini.lookupText({
								id: new_id,
								table: 'patients',
								field: field_id,
								callback: function(data) {
									$j('#' + field_id + '-container').select2('data', data, true);
								}
							});
						},
						size: 'full',
						title: $j(this).attr('title')
					});
				});
			}

			AppGini.hideViewParentLinks();

			$j('form').on('click', '.view_parent', function() {
				var pt = $j(this).attr('id').replace(/_view_parent$/, '');
				if($j(this).prevAll('input:hidden').length) {
					var parent_id = $j(this).prevAll('input:hidden').eq(0).val();
				} else {
					var parent_id = $j(this).prevAll().children('input:hidden').eq(0).val();
				}

				/* handle radio lookups */
				if($j(this).prevAll('[type=radio]').length) {
					parent_id = $j(this).prevAll('[type=radio]').eq(0).val();
				}

				if(parent_id != '{empty_value}' && parent_id != undefined && parent_id != '') {
					var parent_link = $j(this).parents('.form-group').children('label').data('parent_link');
					switch(parent_link) {
						case 'view_parent_hidden':
							break;
						case 'view_parent_modal':
							modal_window({
								url: pt + '_view.php?Embedded=1&SelectedID=' + encodeURIComponent(parent_id),
								close: function() {
									var field_id = $j('#' + pt + '_view_parent').prevAll('input:hidden').eq(0).attr('id');
									$j('#' + field_id + '-container').select2('focus').select2('focus');

									// in case the parent record was updated, we should update auto-fills
									var updaterName = field_id + '_update_autofills';
									if(typeof(window[updaterName]) == 'function')
										window[updaterName]();
								},
								size: 'full',
								title: $j(this).attr('title')
							});
							break;
						case 'view_parent_same_window':
							window.location = pt + '_view.php?Embedded=' + (embedded_mode ? '1' : '') + '&SelectedID=' + encodeURIComponent(parent_id);
							break;
						case 'view_parent_new_window':
							window.open(pt + '_view.php?SelectedID=' + encodeURIComponent(parent_id));
							break;
					}

				}
			});


			/* load admin tools for admin user */
			if(!add_new_mode) {
				$j('body').append('<div id="admin-tools-appendix"></div>');
				$j('#admin-tools-appendix').load(
					'ajax_admin_tools.php?table=patients&id=<%%URLVALUE(id)%%>',
					function(resp) {
						if(!resp.length) return;
						$j.ajax('ajax_admin_tools.php?action=get_admin_tools_js&table=patients&id=<%%URLVALUE(id)%%>');
					}
				);
			}

			/* warn user if 'add new' clicked while unsaved changes exist */
			$j('#addNew').click(function() {
				if($j('#deselect.btn-warning').length) {
					return confirm('<%%TRANSLATION(discard changes confirm)%%>');
				}
			});

		});
	</script>