<?php

// add handlebars macro
// add text to existing views
// generate $res['fields'] data
// include css and js files

// handle fieldsets, tabs, custom field types
// handle fieldsizes, placeholders, hint, helptext, move cursor to first field


class FormEdit_View extends View {
//{{#macro "fields" fieldlist=""}}
	
	function handlebars_template() {
		$html = <<<BLOCK
	{{#if helplink}}
		<a class="helpicon" href="{{helplink}}" target="pkp"></a></div>
	{{/if}}
	{{#each fieldlist}}
		{{#ifeq type 'hidden'}}
			<input type="hidden" id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" value="{{indirect @root name}}">
		{{else}}{{#ifeq type 'comment'}}
				<div>{{{caption}}}</div>
			{{else}}
				<div class="control-group cf-{{id}}{{#if required}} required{{/if}}">
				{{#ifeq type 'string'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><input class="{{#if required}}required{{/if}} {{class}}"{{#if hint}} title="{{hint}}"{{/if}}{{#if placeholder}} placeholder="{{placeholder}}"{{/if}} 
							id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" size="30" type="text" value="{{indirect @root name}}"></div>
				{{/ifeq}}{{#ifeq type 'email'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><input class="{{#if required}}required{{/if}} {{class}}"{{#if hint}} title="{{hint}}"{{/if}}{{#if placeholder}} placeholder="{{placeholder}}"{{/if}}
							id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" size="30" type="email" value="{{indirect @root name}}"></div>
				{{/ifeq}}{{#ifeq type 'tel'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><input class="bind-tel {{#if required}}required{{/if}} {{class}}"{{#if hint}} title="{{hint}}"{{/if}}{{#if placeholder}} placeholder="{{placeholder}}"{{/if}} 
							id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" size="30" type="text" value="{{indirect @root name}}"></div>
				{{/ifeq}}{{#ifeq type 'select'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><select id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}"{{#if hint}} title="{{hint}}"{{/if}} class="{{class}}">
						{{#each options}}<option value="{{@key}}"{{select @root ../name @key}}>{{this}}</option>
						{{/each}}</select></div>
				{{/ifeq}}{{#ifeq type 'text'}}
<label for="{{id}}">{{caption}}</label>
<div class="controls">
<textarea id="{{id}}" class="cf-textarea {{class}}" name="{{name}}"{{#if hint}} title="{{hint}}"{{/if}} wrap="physical">{{indirect @root name}}</textarea></div>
				{{/ifeq}}{{#ifeq type 'int'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><input class="{{#if required}}required{{/if}} short {{class}}"{{#if hint}} title="{{hint}}"{{/if}} 
						id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" size="10" type="text" value="{{indirect @root name}}"></div>
				
				{{/ifeq}}{{#ifeq type 'boolean'}}
					<label>&nbsp;</label>
					<div class="controls">
						<div class="control-checkbox"><input type="checkbox" id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" class="{{class}}"  value="1"
							name="{{name}}"{{#if hint}} title="{{hint}}"{{/if}}{{#ifindirect @root name}} checked{{/ifindirect}}> 
							<label for="{{id}}">{{caption}}</label>
						</div>
					</div>
				{{/ifeq}}{{#ifeq type 'password'}}
					<label for="{{id}}">{{caption}}</label>
					<div class="controls"><input class="{{#if required}}required {{/if}}admin-password {{class}}"{{#if hint}} title="{{hint}}"{{/if}} 
						id="{{#if id}}{{id}}{{else}}{{name}}{{/if}}" name="{{name}}" size="30" 
						type="password" value="{{indirect @root name}}"></div>
				{{/ifeq}}{{#ifeq type 'custom'}}
					<label for="{{id}}">{{caption}}</label>
					{{{custom}}}
				{{/ifeq}}{{#ifeq type 'submit'}}
					<div class="form-actions right">
						<button>{{caption}}</button>
					</div>
				{{/ifeq}}{{#ifeq type 'button'}}
					<div class="form-actions right">
					{{{custom}}}
					</div>
				{{/ifeq}}
				<div class="status-message" for="{{id}}">{{{status}}}</div></div>
			{{/ifeq}}
		{{/ifeq}}
	{{/each}}

BLOCK;
//{{/macro}}
/*
      <input data-toggle="password"
        data-placement="after"
        class="form-control"
        type="password"
        value="123"
        placeholder="password"
        data-eye-class="material-icons"
        data-eye-open-class="visibility"
        data-eye-close-class="visibility_off"
        data-eye-class-position-inside="true">
*/

		$res = $this('fieldlist_id')->html($html)->macro('fields');

		// {{> cannot have leading whitespace
		$html = <<<BLOCK
{{#if fieldset}}
	{{#each fieldset}}<fieldset style="position: relative">
		<legend>&nbsp;{{name}}&nbsp;</legend>
{{> fields fieldlist=fieldlist}}
		</fieldset>
	{{/each}}
{{else}}
{{> fields fieldlist=fieldlist}}
{{/if}}
BLOCK;
		
		$this('formedit_id')->html($html)->macro('formedit');
		return $res;
	}

	function formedit_selection() {
		hook_execute('component.add', FALSE, 'datatables');	
		$html = <<<BLOCK
<div class="indented">
	<h3 class="templateheader">{{page_title}}</h3>

	<p>Please select a form from the list below, or create a <a href="/admin/forms/new?auth={{newauth}}">new</a> one.</p>
	<table class="display data-class" ajax="/admin/forms/list">
	<thead>
	<tr>{{#each header}}
		<th>{{caption}}</th>
	{{/each}}</tr>
	</thead>
	</table>

</div>

BLOCK;
		$this('formedit_selection')->html($html)->title('Forms');

	}

	// based on FormWizard wordpress plugin
	function formedit_detail() {
		$html = <<<BLOCK
	<h3 class="templateheader">{{page_title}}</h3>
		
{{tabs 'formedit.tab' tabauth}}
BLOCK;
		$this('formedit_detail')->html($html)->title('Forms');
	}
	
	function formedit_general() {
		$button = hook_execute_late('html.button', '', 'Save Changes', 'far fa-check-circle', 'cc-blue');
		$macro = hook_execute('component.add', '', 'formedit');	
		$html = <<<BLOCK

<form class="frm json" method="POST" action="/admin/forms">
	<input type="hidden" name="auth" value="{{auth}}">
	<input type="hidden" name="action" value="set-general">
	$macro
	<div class="form-actions right">
		<a class="submit" href="#">$button</a>
	</div>
	<br /><br />
</form>

BLOCK;
		$this('formedit_general')->html($html);
	}

	function formedit_fields() {
		$button = hook_execute_late('html.button', '', 'Save Changes', 'far fa-check-circle', 'cc-blue');
		$html = <<<BLOCK

<form class="frm json formwizpage-fields" method="POST" action="/admin/forms" onsubmit="return formwizard_preparesubmit()">
	<input type="hidden" name="auth" value="{{auth}}">
	<input type="hidden" name="action" value="set-fields">
	<input type="hidden" name="fields" value="">

	<p>To assemble your form, drag fields from the collection on the right, to the area below</p>
	<div id="column1-wrap">
		<div id="formwizard_target">{{{used}}}</div>
	</div>
	<div id="formwizard_available">
		<h5>Available Fields</h5>
	{{{available}}}
	</div>
	<p>Remember to include a Submit button</p>
	
	<div style="clear:both"></div>
	<div class="form-actions right">
		<a class="submit" href="#">$button</a>
	</div>
	<br /><br />
</form>

BLOCK;
		$this('formedit_fields')->html($html);
	}

	function formedit_preview() {
		$button = hook_execute_late('html.button', '', 'Set Defaults', 'far fa-check-circle', 'cc-blue');
		$html = <<<BLOCK
<form class="frm json" method="POST" action="/admin/forms">
	<input type="hidden" name="auth" value="{{auth}}">
	<input type="hidden" name="action" value="set-preview">
	
	This preview may show changes you have made, but not yet saved. Anything you enter 
	in this form will become the default values.
	<div id="formwizard_preview"></div>
	<p>Please fill in the form, and choose "Set Defaults" to make these values the default for the form. 
	Required Fields do not need a value here.</p>

	<div class="form-actions right">
	{{#if preview}}
	{{else}}
		<a class="submit" href="#">$button</a>
	{{/if}}
	</div>
	<br /><br />
</form>

BLOCK;
		$this('formedit_preview')->html($html);
	}	
	
	function formedit_notification() {
		$button = hook_execute_late('html.button', '', 'Save Changes', 'far fa-check-circle', 'cc-blue');
		$macro = hook_execute('component.add', '', 'formedit');	
		
		$html = <<<BLOCK

<form class="frm json" method="POST" action="/admin/forms">
	<input type="hidden" name="auth" value="{{auth}}">
	<input type="hidden" name="action" value="set-notification">
	<p>This tab will allow you to be notified by email whenever the form is submitted. Leave the Recipients blank if you do not wish to be notified.</p>
	<div style="width:650px">
$macro
		<div class="form-actions right">
			<a class="submit" href="#">$button</a>
		</div>
	</div>
	<br /><br />
</form>

BLOCK;
		$this('formedit_notification')->html($html);
	}
	
}