<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	<h3 id="popupModalLabel"><?php echo AdminController::authorized('write') ? 'Edit' : 'View'; ?> item</h3>
</div>
<div class="modal-body"><?php
	$protected = array();
	$entity = $this->entity->getRawData();

	foreach ($this->protectedKeys as $key) {
		if (isset($entity[$key])) {
			$protected[$key] = $entity[$key];
			unset($entity[$key]);
		}
	}
	foreach ($this->hiddenKeys as $key) {
		unset($entity[$key]);
	}

	print '<div class="form_protect">';
	if (isset($protected['log_stamp'])) {
		$log_stamp = $protected['log_stamp'];
	}
	foreach ($protected as $k => $v) {
		print '<span class="label label-info">' . $k . '</span>';
		if ($k == "file") {
			print '<a href="#LogPopup" data-toggle="modal" data-remote=""><span id="form' . $k . '"class="label label-inverse">' . $v . '</span></a><br />';
		} else {
			print '<span id="form' . $k . '"class="label label-inverse">' . $v . '</span><br />';
		}
	}
	print '<span id="form_type" class="hidden">' . $this->type . '</span>';
	print '<span id="form_coll" class="hidden">' . $this->collectionName . '</span>';
	print '</div>';
	?>
	<script type="text/javascript">
				var entityJson = <?php echo json_encode($entity); ?>;
				var _jsonData = entityJson;
				var opt = {
					change: function (data) {
						/* called on every change */
						_jsonData = data;
					},
					propertyclick: function (path) { /* called when a property is clicked with the JS path to that property */
						jsonPath = JSON.parse(path);
						var protectedKeys = <?php echo json_encode($this->protectedKeys); ?>;
						for (p in jsonPath) {
							if ($.inArray(jsonPath[p], protectedKeys) !== -1) {
								$('input[title="' + jsonPath[p] + '"]').attr('disabled', 'disabled');
							}
						}
					}
				};
				/* opt.propertyElement = '<textarea>'; */ // element of the property field, <input> is default
				/* opt.valueElement = '<textarea>'; */  // element of the value field, <input> is default
				var _jsonEditor = $('#jsondiv').jsonEditor(entityJson, opt);
				$('#save').unbind();
				$('#save').removeAttr("disabled");
				$('#save').click(function saveClick() {
					$(this).attr('disabled', 'disabled');
					$(this).unbind("click", saveClick);
					console.log(_jsonData);
					var _ajaxOpts = {
						async: true,
						cache: false,
						//			dataType:'json',
						type: 'POST',
						data: {
							id: $('#form_id').html(),
							coll: $('#form_coll').html(),
							type: $('#form_type').html(),
							duplicate_rates: $('#duplicate_rates').is(':checked'),
							data: JSON.stringify(_jsonData)
						},
						success: function (data, textStatus, jqXHR) {
							if (data != 'null') {
								alert(data);
								$('#save').removeAttr("disabled");
								$('#save').bind("click", saveClick);
								return;
							}
							window.location.reload();
						},
						error: function (jqXHR, textStatus, errorThrown) {
							alert('some error occured');
						}
					};
					$.ajax('<?php echo $this->baseUrl; ?>/admin/save', _ajaxOpts);
				});

				$('#formfile').click(function () {
					if ($('#formfile').html().length) {
						$('#formfile').parent().data('remote', '<?php echo $this->baseUrl; ?>/admin/logDetails?type=logDetails&coll=log&stamp=' + $('#formlog_stamp').html());
					}
				});
	</script>
	<div id="jsondiv" class="json-editor"></div>
	<!-- ./modal-body-->
</div>
<div class="modal-footer">
	<?php if ($this->collectionName == 'plans' && $this->type == 'duplicate') : ?>
		<div class="edit_checkboxes">
			<input class="span1" id="duplicate_rates" name="duplicate_rates" type="checkbox" value="on" checked> <label class="span1" for="duplicate_rates">Duplicate rates</label>
		</div>
	<?php endif; ?>
	<button id="cancelPopup" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
	<?php if (AdminController::authorized('write') && $this->type == 'update') : ?>
		<button id="save" class="btn btn-primary">Save changes</button>
	<?php endif; ?>
</div>
