@section('header')
	@include('common.header')
@stop

@section('content')

	@include('workflow::actions.send_email')

	<script type="text/ng-template" id="workflow/filter_template.html">
		<div class="workflow-filter">
			<span class="workflow-filter-element" editable-select="filter.logic" e-ng-options="o.value as o.name for o in logical_operators" ng-show="!$first"><span class="text-warning">{[{ getLogicalOperator(filter) }]}</span></span>
			<span class="workflow-filter-element" editable-select="filter.attribute_prefix" e-ng-options="o as o for o in prefixes"><span class="text-success">{[{ filter.attribute_prefix }]}</span></span>
			<span class="workflow-filter-element" editable-select="filter.attribute" e-ng-options="o as o for o in attributes">{[{ filter.attribute || 'select a model first' }]}</span>
			<span class="workflow-filter-element" editable-select="filter.operator" e-ng-options="o.value as o.name for o in operators"><span class="text-warning">{[{ getOperator(filter) }]}</span></span>
			<span class="workflow-filter-element" editable-text="filter.value" ng-show="textfieldVisible(filter.operator)">{[{ filter.value || 'not set' }]}</span>
			<div class="pull-right">
				<i class="fa fa-times workflow-filter-remove" tooltip="Remove filter" ng-click="removeFilter(filter);"></i>
			</div>
		</div>
	</script>

	<script type="text/ng-template" id="workflow/group_template.html">
		<div class="workflow-group row">
			<div class="col-sm-1 text-center">
				<span class="workflow-filter-element workflow-group-logic" editable-select="filter.logic" e-ng-options="o.value as o.name for o in logical_operators" ng-show="!$first"><span class="text-warning">{[{ getLogicalOperator(filter) }]}</span></span>
			</div>
			<div class="col-sm-11">
				<div class="workflow-group-inner">
					<div ng-repeat="filter in filter.filters" ng-switch="filter.group">
						<div ng-switch-when="true"><span ng-include="'workflow/group_template.html'"></span></div>
						<div ng-switch-default><span ng-include="'workflow/filter_template.html'"></span></div>
					</div>
					<div class="buttons" style="margin-top: 10px;">
						<span class="btn btn-default" ng-click="addFilter(filter)">Add a condition</span>
						<span class="btn btn-default" ng-click="addGroup(filter)">Add a condition group</span>
						<span class="btn btn-danger" ng-click="removeFilter(filter)">Delete group</span>
					</div>
				</div>
			</div>
		</div>
	</script>

	<div class="widget" ng-controller="NewWorkflowCtrl" ng-cloak>
		<span ng-init="models = {{ json_encode($models) }}"></span>
		<span ng-init="actions = {{ json_encode($actions) }}"></span>
		<span ng-init="data = {{ json_encode($data) }}"></span>

		<div class="widget-content">
			<h4>Create a new workflow</h4>
			<hr>


				<h4>When</h4>
				<label>
					Select a model:
					<select ng-model="data.model">
						@foreach ($models as $model)
							<option value="{!! $model['name'] !!}">{!! $model['short_name'] !!}</option>
						@endforeach
					</select>
				</label>
				<label>
					Select an event:
					<select ng-model="data.event">
						@foreach ($events as $event)
							<option value="{!! $event !!}">{!! ucwords($event) !!}</option>
						@endforeach
					</select>
				</label>
				<div class="well">
					<div ng-repeat="filter in data.filters" ng-switch="filter.group">
						<div ng-switch-when="true"><span ng-include="'workflow/group_template.html'"></span></div>
						<div ng-switch-default><span ng-include="'workflow/filter_template.html'"></span></div>
					</div>
					<div class="buttons" style="margin-top: 10px;">
						<span class="btn btn-default" ng-click="addFilter()">Add a condition</span>
						<span class="btn btn-default" ng-click="addGroup()">Add a condition group</span>
					</div>
				</div>




				<h4>Then</h4>
				<div class="well">
					<p ng-show="data.condition_true.length == 0">Do nothing</p>
					<div class="workflow-filter" ng-repeat="action in data.condition_true">
						<span class="workflow-filter-element" editable-select="action.action" onaftersave="updateAction(action)" e-ng-options="o.value as o.name for o in actions">{[{ action.action || 'not set' }]}</span>
						<div class="pull-right">
							<i class="fa fa-times workflow-filter-remove" tooltip="Remove action" ng-click="removeAction(action)"></i>
						</div>

						<div class="headline" style="margin-top: 5px; margin-left: 15px;" ng-show="action.action">
							<h4><i class="fa fa-cog"></i> Settings</h4>
						</div>
						<div class="workflow-action-settings" ng-include="action.template"></div>

					</div>
					<span class="btn btn-default" ng-click="addAction(data.condition_true)">Add action</span>
				</div>






				<h4>Else</h4>
				<div class="well">
					<p ng-show="data.condition_false.length == 0">Do nothing</p>
					<div class="workflow-filter" ng-repeat="action in data.condition_false">
						<span class="workflow-filter-element" editable-select="action.action" onaftersave="updateAction(action)" e-ng-options="o.value as o.name for o in actions">{[{ action.action || 'not set' }]}</span>
						<div class="pull-right">
							<i class="fa fa-times workflow-filter-remove" tooltip="Remove action" ng-click="removeAction(action)"></i>
						</div>

						<div class="headline" style="margin-top: 5px; margin-left: 15px;" ng-show="action.action">
							<h4><i class="fa fa-cog"></i> Settings</h4>
						</div>
						<div class="workflow-action-settings" ng-include="action.template"></div>

					</div>
					<span class="btn btn-default" ng-click="addAction(data.condition_false)">Add action</span>
				</div>

				<br><br>
				<label class="checkbox">
					<input type="checkbox" ng-model="data.active" ng-init="data.active = 1" ng-checked="data.active"> Active
				</label>

				<hr>
				<button class="btn btn-primary" ng-click="save()">Save</button>
		</div>
	</div>

@stop