{namespace neos=Neos\Neos\ViewHelpers}
<f:layout name="BackendSubModule" />

<f:section name="subtitle">
</f:section>

<f:section name="content">

	<f:if condition="{siteChanges}">
		<f:then>
			<f:form action="publishOrDiscardNodes">
				<f:form.hidden name="selectedWorkspace" value="{selectedWorkspace}"/>
				<legend>{neos:backend.translate(id: 'workspaces.unpublishedChanges', source: 'Modules', package: 'Neos.Neos', arguments: {0: selectedWorkspaceLabel})}</legend>
				<br />
				<div class="neos-row-fluid">
					<table class="neos-table">
						<thead>
							<tr>
								<th class="check neos-priority1">
									<label for="check-all" class="neos-checkbox">
										<input type="checkbox" id="check-all" /><span></span>
									</label>
								</th>
								<th></th>
								<th class="neos-action"></th>
							</tr>
						</thead>
						<tbody>
						<f:for each="{siteChanges}" as="site">
							<f:for each="{site.documents}" key="documentPath" as="document">
								<tr class="neos-document" data-nodepath="{document.documentNode.path}" data-ismoved="{f:if(condition: document.isMoved, then: 'true', else: 'false')}" data-isnew="{f:if(condition: document.isNew, then: 'true', else: 'false')}">
									<f:if condition="{document.changes -> f:count()} > 1">
										<f:then>
											<td class="check neos-priority1">
												<label for="check-document-{document.documentNode.identifier}" class="neos-checkbox"><f:form.checkbox id="check-document-{document.documentNode.identifier}" class="neos-check-document" value="{document.documentNode.identifier}"/><span></span></label>
											</td>
											<td class="neos-priority1 path-caption">
										</f:then>
										<f:else>
											<td colspan="2" class="neos-priority1 path-caption">
										</f:else>
									</f:if>
										<div class="neos-row-fluid">
											<div class="neos-span2">
												{neos:backend.translate(id: 'pathCaption', source: 'Main', package: 'Neos.Neos')}:
												<f:render partial="Module/Shared/DocumentBreadcrumb" arguments="{node: document.documentNode, showIcon: true}"></f:render>
											</div>

											<div class="neos-span2 neos-pull-right neos-aRight">
												<f:if condition="{document.documentNode.removed}">
													<f:else>
														<neos:link.node node="{document.documentNode}" absolute="1" target="neosPreview" class="neos-button" title="{neos:backend.translate(id: 'workspaces.openPageInWorkspace', source: 'Modules', package: 'Neos.Neos', value: 'Open page in \"{0}\" workspace', arguments: {0: selectedWorkspaceLabel})}" additionalAttributes="{data-neos-toggle: 'tooltip'}"><i class="fas fa-external-link-alt icon-white"></i></neos:link.node><button form="postHelper" formaction="{f:uri.action(action: 'rebaseAndRedirect', arguments: {targetNode: document.documentNode, targetWorkspace: selectedWorkspace})}" type="submit" class="neos-button" title="{neos:backend.translate(id: 'edit', source: 'Main', package: 'Neos.Neos', value: 'Edit')}" data-neos-toggle="tooltip"><i class="fas fa-pencil-alt icon-white"></i></button>
													</f:else>
												</f:if>
											</div>
										</div>
									</td>
									<td class="neos-action neos-folder">
										<i class="fold-toggle fas fa-chevron-up icon-white" data-toggle="fold-{document.documentNode.identifier}"></i>
									</td>
								</tr>
								<f:for each="{document.changes}" key="relativePath" as="change">
									<tr class="neos-change fold-{document.documentNode.identifier} document-{document.documentNode.identifier}" data-nodepath="{change.node.path}" data-ismoved="{f:if(condition: change.isMoved, then: 'true', else: 'false')}" data-isnew="{f:if(condition: change.isNew, then: 'true', else: 'false')}">
										<td class="check neos-priority1">
											<label for="{change.node.identifier}" class="neos-checkbox"><f:form.checkbox name="nodes[]" value="{change.node.contextPath}" id="{change.node.identifier}" /><span></span></label>
										</td>
										<td id="change-{change.node.identifier}" {f:render(partial: 'Module/Management/Workspaces/ContentChangeAttributes', arguments: {change: change})} data-neos-toggle="tooltip" data-placement="left" data-container="body">
											<f:render partial="Module/Management/Workspaces/ContentChangeDiff" arguments="{change: change, contentDimensions: contentDimensions}"/>
										</td>
										<td class="neos-action">
											<f:if condition="{canPublishToBaseWorkspace}">
												<button form="postHelper" formaction="{f:uri.action(action: 'publishNode', arguments: {node: change.node.contextPath, selectedWorkspace: selectedWorkspace})}" type="submit" class="neos-button neos-button-success neos-pull-right" title="{neos:backend.translate(id: 'publish', source: 'Main', package: 'Neos.Neos')}" data-neos-toggle="tooltip">
													<i class="fas fa-check icon-white"></i>
												</button>
											</f:if>
											<button form="postHelper" formaction="{f:uri.action(action: 'discardNode', arguments: {node: change.node.contextPath, selectedWorkspace: selectedWorkspace})}" type="submit" class="neos-button neos-button-danger neos-pull-right" title="{neos:backend.translate(id: 'discard', source: 'Main', package: 'Neos.Neos')}" data-neos-toggle="tooltip" data-placement="bottom">
												<i class="fas fa-trash-alt icon-white"></i>
											</button>
										</td>
									</tr>
								</f:for>
							</f:for>
						</f:for>
						</tbody>
					</table>
				</div>
				<f:render section="actions" arguments="{_all}" />
			</f:form>

			<div class="neos-hide" id="discard">
				<div class="neos-modal-centered">
					<div class="neos-modal-content">
						<div class="neos-modal-header">
							<button type="button" class="neos-close neos-button" data-dismiss="modal"></button>
							<div class="neos-header">{neos:backend.translate(id: 'workspaces.discardAllChangesInWorkspaceConfirmation', arguments: {0: selectedWorkspaceLabel}, source: 'Modules', package: 'Neos.Neos')}</div>
						</div>
						<div class="neos-modal-footer">
							<a href="#" class="neos-button" data-dismiss="modal">{neos:backend.translate(id: 'cancel', source: 'Main', package: 'Neos.Neos')}</a>
							<button form="postHelper" formaction="{f:uri.action(action: 'discardWorkspace', arguments: {workspace: selectedWorkspace})}" type="submit" class="neos-button neos-button-danger">
								<i class="fas fa-trash-alt icon-white"></i>
								{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Modules', package: 'Neos.Neos')}
							</button>
						</div>
					</div>
				</div>
				<div class="neos-modal-backdrop neos-in"></div>
			</div>

			<f:form action="index" id="postHelper" method="post"></f:form>

			<script>
				(function($) {
					$('#check-all').change(function() {
						var value = false;
						if ($(this).is(':checked')) {
							value = true;
							$('.batch-action').removeClass('neos-hidden').removeClass('neos-disabled').removeAttr('disabled');
						} else {
							$('.batch-action').addClass('neos-hidden').addClass('neos-disabled').attr('disabled', 'disabled');
						}
						$('tbody input[type="checkbox"]').prop('checked', value);
					});

					$('.neos-check-document').change(function() {
						var documentIdentifier = $(this).val();
						var checked = $(this).prop('checked');
						$(this).closest('table').find('tr.neos-change.document-' + documentIdentifier + ' td.check input').prop('checked', checked);
					});

					$('tbody input[type="checkbox"]').change(function() {
						if ($(this).closest('tr').data('ismoved') === true || $(this).closest('tr').data('isnew') === true) {
							var currentNodePath = $(this).closest('tr').attr('data-nodepath') + '/';
							var checked = $(this).prop('checked');

							function nodePathStartsWith(nodePath) {
								return function(index, element) {
									return nodePath.indexOf($(element).data('nodepath')) === 0;
								}
							}
							var movedOrNewParentDocuments = $(this).closest('table').find('.neos-document[data-ismoved="true"], .neos-document[data-isnew="true"]').filter(nodePathStartsWith(currentNodePath));
							$(movedOrNewParentDocuments).each(function(index, movedParentDocument) {
								$('tr[data-nodepath^="' + $(movedParentDocument).data('nodepath') + '"] td.check input').prop('checked', checked);
							});
						}

						if ($('tbody input[type="checkbox"]:checked').length > 0) {
							$('.batch-action').removeClass('neos-hidden').removeClass('neos-disabled').removeAttr('disabled')
						} else {
							$('.batch-action').addClass('neos-hidden').addClass('neos-disabled').attr('disabled', 'disabled');
						}
					});

					$('.fold-toggle').click(function() {
						$(this).toggleClass('fas fa-chevron-down fas fa-chevron-up');
						$('tr.' + $(this).data('toggle')).toggle();
					});
				})(jQuery);
			</script>

		</f:then>
		<f:else>
			<legend>{neos:backend.translate(id: 'workspaces.unpublishedChanges', source: 'Modules', package: 'Neos.Neos', arguments: {0: selectedWorkspaceLabel})}</legend>
			<p>{neos:backend.translate(id: 'workspaces.thereAreNoUnpublishedChanges', source: 'Modules', package: 'Neos.Neos')}</p>
			<div class="neos-footer">
				<div class="pull-left">
					<f:link.action action="index" class="neos-button">{neos:backend.translate(id: 'back', source: 'Main', package: 'Neos.Neos')}</f:link.action>
				</div>
			</div>
		</f:else>
	</f:if>
</f:section>

<f:section name="actions">
	<div class="neos-footer">
		<div class="neos-pull-left">
			<f:link.action action="index" class="neos-button">{neos:backend.translate(id: 'back', source: 'Main', package: 'Neos.Neos')}</f:link.action>
		</div>
		<div class="neos-pull-right">
			<f:if condition="{canPublishToBaseWorkspace}">
				<f:then>
					<button type="submit" name="moduleArguments[action]" value="publish" class="neos-button neos-hidden neos-disabled batch-action" disabled="disabled">{neos:backend.translate(id: 'workspaces.publishSelectedChanges', source: 'Modules', package: 'Neos.Neos')}</button>
					<button type="submit" name="moduleArguments[action]" value="discard" class="neos-button neos-hidden neos-disabled batch-action" disabled="disabled">{neos:backend.translate(id: 'workspaces.discardSelectedChanges', source: 'Modules', package: 'Neos.Neos')}</button>
					<button class="neos-button neos-button-danger" data-toggle="modal" href="#discard">{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Modules', package: 'Neos.Neos')}</button>
					<button form="postHelper" formaction="{f:uri.action(action: 'publishWorkspace', arguments: {workspace: selectedWorkspace})}" type="submit" class="neos-button neos-button-primary">{neos:backend.translate(id: 'workspaces.publishAllChangesTo', source: 'Modules', package: 'Neos.Neos', arguments: {0: baseWorkspaceLabel})}</button>
				</f:then>
				<f:else>
					<button type="submit" name="moduleArguments[action]" value="publish" class="neos-button neos-hidden neos-disabled batch-action" disabled="disabled">{neos:backend.translate(id: 'workspaces.publishSelectedChanges', source: 'Modules', package: 'Neos.Neos')}</button>
					<button type="submit" name="moduleArguments[action]" value="discard" class="neos-button neos-hidden neos-disabled batch-action" disabled="disabled">{neos:backend.translate(id: 'workspaces.discardSelectedChanges', source: 'Modules', package: 'Neos.Neos')}</button>
					<button class="neos-button neos-button-danger" data-toggle="modal" href="#discard">{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Modules', package: 'Neos.Neos')}</button>
				</f:else>
			</f:if>
		</div>
	</div>
</f:section>
