{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 id="publishOrDiscardNodes" action="publishOrDiscardNodes">
				<f:form.hidden name="selectedWorkspace" value="{selectedWorkspace.workspaceName.value}"/>
				<legend>{neos:backend.translate(id: 'workspaces.unpublishedChanges', source: 'Main', package: 'Neos.Workspace.Ui', arguments: {0: selectedWorkspaceLabel.value})}</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>{neos:backend.translate(id: 'workspaces.selectAllCurrentChanges', source: 'Main', package: 'Neos.Workspace.Ui')}</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="{documentPath}" data-identifier="{document.documentNode.identifier}" 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.aggregateId.value}" class="neos-checkbox"><f:form.checkbox id="check-document-{document.documentNode.aggregateId.value}" class="neos-check-document" value="{document.documentNode.aggregateId.value}"/><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.Workspace.Ui')}:
												<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: 'Main', package: 'Neos.Workspace.Ui', value: 'Open page in \"{0}\" workspace', arguments: {0: selectedWorkspaceLabel.value})}" 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.workspaceName.value})}" type="submit" class="neos-button" title="{neos:backend.translate(id: 'edit', source: 'Main', package: 'Neos.Workspace.Ui', 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.aggregateId.value}"></i>
									</td>
								</tr>
								<f:for each="{document.changes}" key="relativePath" as="change">
									<tr class="neos-change fold-{document.documentNode.aggregateId.value} document-{document.documentNode.aggregateId.value}" data-nodepath="{relativePath}" 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.serializedNodeAddress}" class="neos-checkbox"><f:form.checkbox name="nodes[]" value="{change.serializedNodeAddress}" id="{change.serializedNodeAddress}" /><span></span></label>
										</td>
										<td id="change-{change.serializedNodeAddress}" {f:render(partial: 'Workspaces/ContentChangeAttributes', arguments: {change: change, nodepath: relativePath})} data-neos-toggle="tooltip" data-placement="left" data-container="body">
											<f:render partial="Workspaces/ContentChangeDiff" arguments="{change: change, contentDimensions: contentDimensions}"/>
										</td>
										<td class="neos-action">
											<f:if condition="{canPublishToBaseWorkspace}">
												<f:if condition="{document.isNew}">
													<f:then>
														<div class="neos-button neos-disabled neos-button-primary neos-pull-right"
																 title="{neos:backend.translate(id: 'cantPublishSingleNodeInNewPage', source: 'Main', package: 'Neos.Workspace.Ui')}">
															<i class="fas fa-check icon-white"></i>
														</div>
													</f:then>
													<f:else>
														<button form="postHelper" formaction="{f:uri.action(action: 'publishNode', arguments: {nodeAddress: change.serializedNodeAddress, selectedWorkspace: selectedWorkspace.workspaceName.value})}" type="submit" class="neos-button neos-button-primary neos-pull-right" title="{neos:backend.translate(id: 'publish', source: 'Main', package: 'Neos.Workspace.Ui')}" data-neos-toggle="tooltip">
															<i class="fas fa-check icon-white"></i>
														</button>
													</f:else>
												</f:if>
											</f:if>
											<button form="postHelper" formaction="{f:uri.action(action: 'discardNode', arguments: {nodeAddress: change.serializedNodeAddress, selectedWorkspace: selectedWorkspace.workspaceName.value})}" type="submit" class="neos-button neos-button-danger neos-pull-right" title="{neos:backend.translate(id: 'discard', source: 'Main', package: 'Neos.Workspace.Ui')}" 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.value}, source: 'Main', package: 'Neos.Workspace.Ui')}</div>
						</div>
						<div class="neos-modal-footer">
							<button href="#" class="neos-button" data-dismiss="modal">{neos:backend.translate(id: 'cancel', source: 'Main', package: 'Neos.Workspace.Ui')}</button>
							<button form="postHelper" formaction="{f:uri.action(action: 'discardWorkspace', arguments: {workspace: selectedWorkspace.workspaceName.value})}" type="submit" class="neos-button neos-button-danger">
								<i class="fas fa-trash-alt icon-white"></i>
								{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
							</button>
						</div>
					</div>
				</div>
				<div class="neos-modal-backdrop neos-in"></div>
			</div>

			<div class="neos-hide" id="discardSelected">
				<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.discardSelectedChangesInWorkspaceConfirmation', arguments: {0: selectedWorkspaceLabel.value}, source: 'Main', package: 'Neos.Workspace.Ui')}</div>
						</div>
						<div class="neos-modal-footer">
							<button href="#" class="neos-button" data-dismiss="modal">{neos:backend.translate(id: 'cancel', source: 'Main', package: 'Neos.Workspace.Ui')}</button>
							<button form="publishOrDiscardNodes" name="moduleArguments[action]" value="discard" type="submit" class="neos-button neos-button-danger">
								<i class="fas fa-trash-alt icon-white"></i>
								{neos:backend.translate(id: 'workspaces.discardSelectedChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
							</button>
						</div>
					</div>
				</div>
				<div class="neos-modal-backdrop neos-in"></div>
			</div>

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

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

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

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

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

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

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

		</f:then>
		<f:else>
			<legend>{neos:backend.translate(id: 'workspaces.unpublishedChanges', source: 'Main', package: 'Neos.Workspace.Ui', arguments: {0: selectedWorkspaceLabel.value})}</legend>
			<p>{neos:backend.translate(id: 'workspaces.thereAreNoUnpublishedChanges', source: 'Main', package: 'Neos.Workspace.Ui')}</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.Workspace.Ui')}</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.Workspace.Ui')}</f:link.action>
		</div>
		<div class="neos-pull-right">
			<f:if condition="{canPublishToBaseWorkspace}">
				<f:then>
					<button type="submit" class="review-button-action neos-button neos-button-danger" data-toggle="modal" href="#discard">
						<i class="fas fa-trash-alt icon-white"></i>
						{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
					</button>
					<button form="postHelper" formaction="{f:uri.action(action: 'publishWorkspace', arguments: {workspace: selectedWorkspace.workspaceName.value})}" type="submit" class="review-button-action neos-button neos-button-primary">
						<i class="fas fa-check-double icon-white"></i>
						{neos:backend.translate(id: 'workspaces.publishAllChangesTo', source: 'Main', package: 'Neos.Workspace.Ui', arguments: {0: baseWorkspaceLabel.value})}
					</button>
					<button type="submit" name="moduleArguments[action]" value="discard" class="neos-button neos-button-danger neos-hidden neos-disabled batch-action" disabled="disabled" data-toggle="modal" href="#discardSelected">
						<i class="fas fa-trash-alt icon-white"></i>
						{neos:backend.translate(id: 'workspaces.discardSelectedChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
					</button>
					<button type="submit" name="moduleArguments[action]" value="publish" class="neos-button neos-button-primary neos-hidden neos-disabled batch-action" disabled="disabled">
						<i class="fas fa-check icon-white"></i>
						{neos:backend.translate(id: 'workspaces.publishSelectedChangesTo', source: 'Main', package: 'Neos.Workspace.Ui', arguments: {0: baseWorkspaceLabel.value})}
					</button>
				</f:then>
				<f:else>
					<button type="submit" name="moduleArguments[action]" value="discard" class="neos-button neos-button-danger neos-hidden neos-disabled batch-action" disabled="disabled" data-toggle="modal" href="#discardSelected">
						<i class="fas fa-trash-alt icon-white"></i>
						{neos:backend.translate(id: 'workspaces.discardSelectedChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
					</button>
					<button type="submit" class="review-button-action neos-button neos-button-danger" data-toggle="modal" href="#discard">
						<i class="fas fa-trash-alt icon-white"></i>
						{neos:backend.translate(id: 'workspaces.discardAllChanges', source: 'Main', package: 'Neos.Workspace.Ui')}
					</button>
				</f:else>
			</f:if>
		</div>
	</div>
</f:section>
