<!-- To change the layout of the detail view form, we recommend using JS code in hooks/mfbo-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="mfbo_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="mfbo_dv_container">
	<!-- child links -->
	<div class="row vspacer-md">
		<div class="col-xs-12 children-links">
			<input type="hidden" id="flugbuch_verein_hclink" value="flugbuch_verein_view.php?filterer_mfbo=<%%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="mfbo_dv_form">
			<fieldset class="form-horizontal">

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

			</fieldset>
		</div>

		<!-- DV action buttons -->
		<hr class="hidden-md hidden-lg">
		<div class="col-md-4 col-lg-2 dv-action-buttons" id="mfbo_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="mfbo-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);
			}


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

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

			$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');
						}
					);
				}
				setInterval(append_embedded, 1000);
			}

			/* 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: 'mfbo',
						SelectedID: '<%%VALUE(id)%%>',
						Operation: 'show-children'
					},
					'mfbo-children'
				);
			}

			if(true /* !embedded_mode */) {
				$j('.add_new_parent').click(function() {
					const fieldId = $j(this).prevAll('input:hidden').eq(0).attr('id');
					const pt = $j(this).attr('id').replace(/_add_new$/, '');

					modal_window({
						url: `${pt}_view.php?addNew_x=1&Embedded=1`,
						close: function() {
							$j(`#${fieldId}-container`).select2('focus').select2('focus');

							/* if a new parent was added, select it */
							var new_id = AppGini.localStorage.getItem(`${pt}_last_added_id`);
							if(new_id === null) return;

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

			AppGini.hideViewParentLinks();

			$j('form').on('click', '.view_parent', function() {
				const pt = $j(this).attr('id').replace(/_view_parent$/, '');
				const fieldId = $j(this).prevAll('input:hidden').eq(0).attr('id');

				let parent_id = '{empty_value}';
				if(fieldId) {
					parent_id = $j(`#${fieldId}`).val();
				} else if($j(this).prevAll('[type=radio]').length) { /* handle radio lookups */
					parent_id = $j(this).prevAll('[type=radio]').eq(0).val();
				} else {
					parent_id = $j(this).prevAll().children('input:hidden').eq(0).val();
				}

				if(parent_id != '{empty_value}' && parent_id !== undefined && parent_id !== '') {
					const 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() {
									$j(`#${fieldId}-container`).select2('focus').select2('focus');

									// update lookup text in case the parent record was updated
									AppGini.lookupText({
										id: parent_id,
										table: AppGini.currentTableName(),
										field: fieldId,
										cache: false,
										callback: function(data) {
											$j(`#${fieldId}-container`).select2('data', data, true);

											// update auto-fills
											const updaterName = `${fieldId}_update_autofills`;
											if(typeof(window[updaterName]) == 'function')
												window[updaterName]();
										}
									});
								},
								beforeClose: AppGini.confirmBeforeClosingModal,
								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;
					}

				}
			});

			mfboUniqueFields.forEach(fieldName => enforce_uniqueness('mfbo', fieldName));

			/* 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=mfbo&id=<%%URLVALUE(id)%%>',
					function(resp) {
						if(!resp.length) return;
						$j.ajax('ajax_admin_tools.php?action=get_admin_tools_js&table=mfbo&id=<%%URLVALUE(id)%%>');
					}
				);
			}
		});

		var mfboUniqueFields = [
		];
	</script>