${ util.include('header', [title:'Setup: ' + page.title]) }
<% def fieldFn = { q, label, valueControl -> %>
<div class="form-group">
    <label class="col-sm-4 control-label" for='${q.getName()}'><%= label %></label>
    <div class="col-sm-8">
        <% valueControl(q); %>
    </div>
</div>
<% } %>
<div class="panel panel-default">
    <div class="panel-heading panel-xs">
<h4>This panel offer advanced configuration options. Please do not change them unless you know what you are doing.</h4>
    </div>
</div>
<script>
	var checkACS = function() {
		var acsRequired = false;
		["pubsub","muc"].forEach(function(name) {
			var el = document.getElementById("id-" + name);
			if (el != null) {
				acsRequired |= el.checked || false;
			}
		});
		document.getElementById("acsComponent").disabled = !document.getElementById("clusterMode").checked || (document.getElementById("clusterMode").checked && acsRequired);

		if (document.getElementById("acsComponent").checked) {
			document.getElementById("acsComponent").checked = document.getElementById("clusterMode").checked;
		} else {
			document.getElementById("acsComponent").checked = document.getElementById("clusterMode").checked && acsRequired;
		}
		document.getElementById("acs-warn").style.display = (document.getElementById("clusterMode").checked && acsRequired) ? "inline" : "none";
	};
	var exclusiveComponents = [
	    [ 'message-archive', 'unified-archive' ]
	];
	var checkComponent = function() {
	    checkACS();

	    exclusiveComponents.forEach(function(list) {
            var sel = list.map(function(cmpId) {
                return document.getElementById('id-' + cmpId);
            });
            sel = sel.reduce(function(prev, el) {
                if (!prev) {
                    prev = 0;
                }
                if (el && el.checked) {
                    prev++;
                }
                return prev;
            },0);

            var error = sel > 1;
            list.forEach(function(cmpId) {
                var el = document.getElementById('id-' + cmpId);
                if (el) {
                    if (error) {
                    var listCopy = list.slice();
                    listCopy = listCopy.map(function(id) {
                        return document.getElementById('id-' + id);
                    });
                    listCopy = listCopy.filter(function(x) { return !!x; });
                    listCopy = listCopy.map(function(x) { return x.parentElement.previousElementSibling.innerHTML; });
                    el.nextElementSibling.innerHTML = 'Only one of components ' + listCopy.join(', ') + ' should be active at the same time!';
                    } else {
                    el.nextElementSibling.innerHTML = null;
                    }
                }
            });
        });

	};
</script>
<form class="form-horizontal" method='POST' action="?step=${ page.nextPage() }">
    <div class="panel panel-default">
        <div class="panel-heading panel-xs">
            <h4>Select optional components to run:</h4>
        </div>
        <div class="panel-body">
<% page.getOptionalComponents().each { %>
    <% fieldFn(it, it.getLabel(), { q1 -> %>
    <input class="form-control" name='${q1.getName()}' type='checkbox' id='id-${q1.getBeanName()}' ${ q1.isSelected(it.getBeanName()) ? 'checked' : '' } onclick='checkComponent();'/>
    <span style='color: red; font-size:0.8em;'></span>
    <% }); %>
<% } %>
        </div>
    </div>
    <form class="form-horizontal" method='POST' action="?step=${ page.nextPage() }">
        <div class="panel panel-default">
            <div class="panel-heading panel-xs">
                <h4>Cluster configuration</h4>
            </div>
            <div class="panel-body">
<% def q = page.getQuestion('clusterMode') %>
    <% fieldFn(q, "Do you want your server to run in the cluster mode?", { q1 -> %>
    <input class="form-control" name='${q1.getName()}' id="clusterMode" type='checkbox' ${ q1.isSelected("true") ? 'checked' : '' } onclick='checkACS();'/>
    <% }); %>

    <% q = page.getQuestion('acsComponent') %>
    <% fieldFn(q, "Tigase Advanced Clustering Strategy (ACS) Component", { q1 -> %>
        <input name='${q1.getName()}' id='acsComponent' type="checkbox" ${ q1.isSelected("true") ? "" : "disabled"} ${ q1.isSelected("true") ? 'checked' : '' } /><span id="acs-warn" style="color: red; display: none;"><br/>Warning: Tigase ACS is required for PubSub and MUC components to work and cannot be disabled!</span>
    <% }); %>
            </div>
        </div>
    
<button class="btn btn-default" type="submit" name="next">Next</button>

</form>
<script>
	checkACS();
</script>
${ util.include('footer') } 