<!-- To change the layout of the detail view form, we recommend using JS code in hooks/units-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="units_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="units_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="applications_leases_view.php?filterer_unit=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="applications_leases_link"><img src="resources/table_icons/curriculum_vitae.png"> Applications/Leases</a>
			<a class="visible-xs btn btn-default btn-lg btn-block hidden vspacer-sm" href="applications_leases_view.php?filterer_unit=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="xs_applications_leases_link"><img src="resources/table_icons/curriculum_vitae.png"> Applications/Leases</a>
			<input type="hidden" id="applications_leases_hclink" value="applications_leases_view.php?filterer_unit=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>">
			<a class="hidden-xs btn btn-default btn-lg pull-right hidden hspacer-sm vspacer-sm" href="unit_photos_view.php?filterer_unit=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="unit_photos_link"><img src="resources/table_icons/camera_link.png"> Unit photos</a>
			<a class="visible-xs btn btn-default btn-lg btn-block hidden vspacer-sm" href="unit_photos_view.php?filterer_unit=<%%URLVALUE(id)%%>&<%%EMBEDDED%%>" id="xs_unit_photos_link"><img src="resources/table_icons/camera_link.png"> Unit photos</a>
			<input type="hidden" id="unit_photos_hclink" value="unit_photos_view.php?filterer_unit=<%%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="units_dv_form">
			<fieldset class="form-horizontal">

				<!-- Field: ID -->
				<div class="form-group units-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: Property -->
				<div class="form-group units-property">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="property" data-parent_link="view_parent_modal">Property</label>
					<div class="col-lg-9">
						<div class="lookup-flex"><%%COMBO(property)%%><%%PLINK(property)%%><%%ADDNEW(properties)%%></div>
					</div>
				</div>

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

				<!-- Field: Cover photo -->
				<div class="form-group units-photo">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Cover photo <i class="glyphicon glyphicon-info-sign text-info" data-toggle="collapse" data-target="#photo-description"></i></label>
					<div class="col-lg-9">
						<span class="help-block bspacer-xs collapse" id="photo-description"><div class="alert alert-info bspacer-xs">Maximum file size allowed: 2000 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(photo)%%>" data-lightbox="units_dv"><img src="thumbnail.php?i=<%%VALUE(photo)%%>&t=units&f=photo&v=dv" class="img-thumbnail" id="photo-image"></a></div>
								<div class="hidden-md hidden-lg vspacer-lg"></div>
								<div class="col-lg-4 col-md-6 well upload-toolbox">
									<%%REMOVEFILE(photo)%%>
									<%%UPLOADFILE(photo)%%>
									<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_photo = '<%%VALUE(photo)%%>'; if(e_photo == '' || e_photo == '&nbsp;' || e_photo == 'blank.gif') { $j('#photo-link').hide(); $j('#photo-image').parents('a').attr('href', '').on('click', (e) => e.preventDefault()); } });</script>
					</div>
				</div>

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

				<!-- Field: Area (sq. feet) -->
				<div class="form-group units-size">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="size">Area (sq. feet)</label>
					<div class="col-lg-9">
						<input maxlength="40" type="text" class="form-control" name="size" id="size" value="<%%VALUE(size)%%>">
					</div>
				</div>

				<!-- Field: Country -->
				<div class="form-group units-country">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Country</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="country"></div>
					</div>
				</div>

				<!-- Field: Street -->
				<div class="form-group units-street">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Street</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="street"></div>
					</div>
				</div>

				<!-- Field: City -->
				<div class="form-group units-city">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">City</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="city"></div>
					</div>
				</div>

				<!-- Field: State -->
				<div class="form-group units-state">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">State</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="state"></div>
					</div>
				</div>

				<!-- Field: Postal code -->
				<div class="form-group units-postal_code">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3">Postal code</label>
					<div class="col-lg-9">
						<div class="form-control-static" id="postal_code"></div>
					</div>
				</div>

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

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

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

				<!-- Field: Rental amount -->
				<div class="form-group units-rental_amount">
					<hr class="hidden-md hidden-lg">
					<label class="control-label col-lg-3" for="rental_amount">Rental amount</label>
					<div class="col-lg-9">
						<input maxlength="13" type="text" class="form-control" name="rental_amount" id="rental_amount" value="<%%VALUE(rental_amount)%%>">
					</div>
				</div>

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

			</fieldset>
		</div>

		<!-- DV action buttons -->
		<hr class="hidden-md hidden-lg">
		<div class="col-md-4 col-lg-2" id="units_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="units-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#description').length) new nicEditor({ fullPanel : true }).panelInstance('description');
				}
			});

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

			if(!$j('.table_view').length || $j('[name=SelectedID]').val().length) {
				AppGini.once({
					condition: function() {
						return $j('.form-group.units-property')
									.find('.nicEdit-main, .select2-container, input[type=radio]')
									.length > 0
								// or read-only DV
								|| (!$j('#update').length && !$j('#insert').length);
					},
					action: function() {
						$j('#units_dv_container-loading').addClass('hidden');
						$j('#units_dv_container').removeClass('hidden');
						AppGini.focusFormElement('units', 'property');
					},
					timeout:  5000,
					doActionOnTimeout:  true
				});
			} else {
				$j('#units_dv_container-loading').addClass('hidden');
				$j('#units_dv_container').removeClass('hidden');
			}

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

				}
			});

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

		var unitsUniqueFields = [
		];
	</script>