{extends file="admin-layout.tpl"}

{block name="page-title"}{intl l='Edit a mailing template'}{/block}

{block name="check-resource"}admin.configuration.message{/block}
{block name="check-access"}update{/block}

{block name="main-content"}
<div class="messages edit-message">

    <div id="wrapper" class="container">

        {loop name="message_edit" type="message" secured="*" id="$message_id" backend_context="1" lang="$edit_language_id"}

        <ul class="breadcrumb">
			<li><a href="{url path='/admin/home'}">{intl l="Home"}</a></li>
            <li><a href="{url path='/admin/configuration'}">{intl l="Configuration"}</a></li>
            <li><a href="{url path='/admin/configuration/messages'}">{intl l="Mailing templates"}</a></li>
            <li>{intl l='Editing mailing template "%name"' name={$NAME}}</li>
        </ul>

		<div class="row">
			<div class="col-md-12 general-block-decorator">
				<div class="row">

					<div class="col-md-12 title title-without-tabs">
					   {intl l='Edit mailing template "%name"' name={$NAME}}
					</div>

                    <div class="form-container">
                         <div class="col-md-12">
						    {form name="thelia.admin.message.modification"}
						    <form method="POST" action="{url path='/admin/configuration/messages/save'}" {form_enctype form=$form}>

                                {* Be sure to get the message ID, even if the form could not be validated *}
                                <input type="hidden" name="message_id" value="{$message_id}" />

                                {include file="includes/inner-form-toolbar.html" close_url="{url path='/admin/configuration/messages'}"}

						        {form_hidden_fields form=$form}

						        {form_field form=$form field='success_url'}
						        <input type="hidden" name="{$name}" value="{url path='/admin/configuration/messages'}" />
						        {/form_field}

                                {form_field form=$form field='id'}
                                <input type="hidden" name="{$name}" value="{$value}" />
                                {/form_field}

		                        {form_field form=$form field='locale'}
		                        <input type="hidden" name="{$name}" value="{{$edit_language_locale}}" />
		                        {/form_field}

					            {if $form_error}<div class="alert alert-danger">{$form_error_message}</div>{/if}

								{form_field form=$form field='name'}
								<div class="form-group {if $error}has-error{/if}">
									<label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} :
									<span class="label-help-block">{intl l="This the unique name of this message. Do not change this value unless you understand what you do."}</span>
									</label>
									<input type="text" id="{$label_attr.for}" {if $required}required="required"{/if} name="{$name}" value="{$value}" title="{intl l='Variable name'}" placeholder="{intl l='Variable name'}" class="form-control">
								</div>
					            {/form_field}

								{form_field form=$form field='secured'}
		                        <div class="checkbox {if $error}has-error{/if}">
		                            <label>
		                                <input type="checkbox" name="{$name}" value="1" {if $value == 1}checked="checked"{/if}>
		                                {$label}
									</label>
		                        </div>
		                        {/form_field}

								{form_field form=$form field='title'}
								<div class="form-group {if $error}has-error{/if}">
								    <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} :
								        <span class="label-help-block">{intl l="This is the message purpose, such as 'Order confirmation'."}</span>
								    </label>
									<input type="text" id="{$label_attr.for}" name="{$name}" {if $required}{if $required}required="required"{/if}{/if} title="{intl l='Title'}" placeholder="{intl l='Title'}" class="form-control" value="{$value}">
								</div>
								{/form_field}

                                {form_field form=$form field='subject'}
                                <div class="form-group {if $error}has-error{/if}">
                                    <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} :
                                        <span class="label-help-block">{intl l="This is the subject of the e-mail, such as 'Your order is confirmed'."}</span>
                                    </label>
									<input type="text" id="{$label_attr.for}" name="{$name}" {if $required}required="required"{/if} title="{intl l='Subject'}" placeholder="{intl l='Subject'}" class="form-control" value="{$value}">
								</div>
                                {/form_field}

                                <div class="panel panel-default">
                                    <div class="panel-heading"><h3 class="panel-title">{intl l="HTML version of this message"}</h3></div>

                                    <div class="panel-body">
	                                    <div class="row">
		                                    <div class="col-md-6">
			                                {form_field form=$form field='html_layout_file_name'}
				                                <div class="form-group {if $error}has-error{/if}">
				                                    <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
				                                    <select id="{$label_attr.for}" name="{$name}" {if $required}required="required"{/if} title="{$label}" class="form-control">
				                                        <option value="">{intl l='Use default layout'}</option>
				                                        {foreach $layout_list as $layout}
				                                            <option value="{$layout}" {if $layout == $value}selected="selected"{/if}>{$layout}</option>
				                                        {/foreach}
				                                    </select>
				                                </div>
			                                {/form_field}
			                                </div>

			                                <div class="col-md-6">
				                                {form_field form=$form field='html_template_file_name'}
				                                <div class="form-group {if $error}has-error{/if}">
				                                    <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
			                                        <select data-toggle-textarea="html-message-content" id="{$label_attr.for}" name="{$name}" {if $required}required="required"{/if} title="{$label}" class="textarea-toggle form-control">
			                                            <option value="">{intl l='Use HTML message defined below'}</option>
	                                                    {foreach $html_template_list as $template}
	                                                        <option value="{$template}" {if $template == $value}selected="selected"{/if}>{$template}</option>
	                                                    {/foreach}
			                                        </select>
				                                </div>
				                                {if ! empty($value)}{$disable_html='readonly="true"'}{/if}
			                                {/form_field}
			                                </div>
                                        </div>

                                        <div class="row">
											<div class="col-md-12">
												{form_field form=$form field='html_message'}
												<div class="form-group {if $error}has-error{/if}">
												    <label for="html-message-content" class="control-label">
												    	{$label}{if $required} <span class="required">*</span>{/if} :
												      <span class="label-help-block">{intl l="The mailing template in HTML format."}</span>
												    </label>
												    <textarea {$disable_html} {if $required}required="required"{/if} name="{$name}" id="html-message-content" rows="10" class="fixedfont form-control">{$value}</textarea>
												</div>
												{/form_field}
											</div>
										</div>
                                    </div>
                                </div>

                                <div class="panel panel-default">
                                    <div class="panel-heading"><h3 class="panel-title">{intl l="Text version of this message"}</h3></div>

                                    <div class="panel-body">
                                        <div class="row">
		                                    <div class="col-md-6">
			                                    {form_field form=$form field='text_layout_file_name'}
			                                    <div class="form-group {if $error}has-error{/if}">
			                                        <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
			                                        <select id="{$label_attr.for}" name="{$name}" {if $required}required="required"{/if} title="{$label}" class="form-control">
			                                            <option value="">{intl l='Use default layout'}</option>
	                                                    {foreach $layout_list as $layout}
	                                                        <option value="{$layout}" {if $layout == $value}selected="selected"{/if}>{$layout}</option>
	                                                    {/foreach}
			                                        </select>
			                                    </div>
			                                    {/form_field}
		                                    </div>

		                                    <div class="col-md-6">
			                                    {form_field form=$form field='text_template_file_name'}
			                                    <div class="form-group {if $error}has-error{/if}">
			                                        <label for="{$label_attr.for}" class="control-label">{$label}{if $required} <span class="required">*</span>{/if} : </label>
			                                        <select data-toggle-textarea="text-message-content" id="{$label_attr.for}" name="{$name}" {if $required}required="required"{/if} title="{$label}" class="textarea-toggle form-control">
			                                            <option value="">{intl l='Use Text message defined below'}</option>
	                                                    {foreach $text_template_list as $template}
	                                                        <option value="{$template}" {if $template == $value}selected="selected"{/if}>{$template}</option>
	                                                    {/foreach}
			                                        </select>
			                                    </div>
			                                    {if ! empty($value)}{$disable_text='readonly="true"'}{/if}
			                                    {/form_field}
		                                    </div>
		                                </div>

                                        <div class="row">
	                                        <div class="col-md-12">
				                                {form_field form=$form field='text_message'}
				                                <div class="form-group {if $error}has-error{/if}">
				                                    <label for="text-message-content" class="control-label">
				                                    	{$label}{if $required} <span class="required">*</span>{/if} :
				                                      <span class="label-help-block">{intl l="The mailing template in text-only format."}</span>
				                                    </label>
				                                    <textarea {$disable_text} id="text-message-content" {if $required}required="required"{/if} name="{$name}" rows="10" class="fixedfont form-control">{$value}</textarea>
				                                </div>
				                                {/form_field}
			                                </div>
			                            </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <p>{intl l='Message created on %date_create. Last modification: %date_change' date_create="{format_date date=$CREATE_DATE}" date_change="{format_date date=$UPDATE_DATE}"}</p>
                                </div>

							</form>
							{/form}
						</div>
					</div>
				</div>
			</div>

		</div>
		{/loop}

        {elseloop rel="message_edit"}
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-error">
                    {intl l="Sorry, message ID=%id was not found." id={$message_id}}
                </div>
            </div>
        </div>
        {/elseloop}

    </div>
</div>
{/block}

{block name="javascript-initialization"}
<script>
	(function($) {
	   $('select.textarea-toggle').change(function(ev) {
		   if ($(this).val() != '') {
			   $('#' + $(this).data('toggle-textarea')).addClass("disabled").prop('readonly', true);
		   }
		   else {
               $('#' + $(this).data('toggle-textarea')).removeClass('disabled').prop('readonly', false);
		   }
	   });

	 })(jQuery);
</script>
{/block}

{block name="javascript-last-call"}
    {module_include location='message-edit-js'}
{/block}
