<?php defined('MW_PATH') || exit('No direct script access allowed');

/**
 * This file is part of the MailWizz EMA application.
 * 
 * @package MailWizz EMA
 * @author Serban George Cristian <cristian.serban@mailwizz.com> 
 * @link http://www.mailwizz.com/
 * @copyright 2013-2014 MailWizz EMA (http://www.mailwizz.com)
 * @license http://www.mailwizz.com/license/
 * @since 1.0
 */
 
?>

<?php
$form = $this->beginWidget('CActiveForm', array(
    'action' => array('campaigns/template', 'campaign_uid' => $campaign->campaign_uid, 'do' => 'create')
));
echo CHtml::hiddenField('selected_template_id', 0, array('id' => 'selected_template_id'));
?>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="pull-left">
            <h3 class="panel-title">
                <span class="glyphicon glyphicon-envelope"></span> <?php echo $pageHeading;?>
            </h3>
        </div>
        <div class="pull-right">
            <?php echo CHtml::link(Yii::t('email_templates', 'Upload template'), '#template-upload-modal', array('class' => 'btn btn-default btn-xs', 'data-toggle' => 'modal', 'title' => Yii::t('email_templates', 'Upload template')));?>
            <?php echo CHtml::link(Yii::t('campaigns', 'Change/Select template'), array('campaigns/template', 'campaign_uid' => $campaign->campaign_uid, 'do' => 'select'), array('class' => 'btn btn-default btn-xs', 'title' => Yii::t('campaigns', 'Change/Select template')));?>
            <?php if (!empty($template->content)) { ?>
            <?php echo CHtml::link(Yii::t('campaigns', 'Test template'), '#template-test-email', array('class' => 'btn btn-default btn-xs', 'title' => Yii::t('campaigns', 'Test template'), 'data-toggle' => 'modal'));?>
            <?php } ?>
            <?php echo CHtml::link(Yii::t('app', 'Cancel'), array('campaigns/index'), array('class' => 'btn btn-default btn-xs', 'title' => Yii::t('app', 'Cancel')));?>
        </div>
        <div class="clearfix"><!-- --></div>
    </div>
    <div class="panel-body">
        
        <div class="form-group col-lg-6">
            <?php echo $form->labelEx($template, 'inline_css');?>
            <?php echo $form->dropDownList($template, 'inline_css', $template->getInlineCssArray(), $template->getHtmlOptions('inline_css')); ?>
            <?php echo $form->error($template, 'inline_css');?>
        </div>
        
        <?php if (!empty($campaign->option) && $campaign->option->plain_text_email == CampaignOption::TEXT_YES) { ?>
        <div class="form-group col-lg-6">
            <?php echo $form->labelEx($template, 'auto_plain_text');?>
            <?php echo $form->dropDownList($template, 'auto_plain_text', $template->getAutoPlainTextArray(), $template->getHtmlOptions('auto_plain_text')); ?>
            <?php echo $form->error($template, 'auto_plain_text');?>
        </div>
        <?php } ?>
        
        <div class="clearfix"><!-- --></div>
        <hr />
        
        <div class="form-group">
            <?php echo $form->labelEx($template, 'content');?> [<a data-toggle="modal" href="#available-tags-modal"><?php echo Yii::t('lists', 'Available tags');?></a>]
            <?php echo $form->textArea($template, 'content', $template->getHtmlOptions('content', array('rows' => 15))); ?>
            <?php echo $form->error($template, 'content');?>
        </div>

        <div class="clearfix"><!-- --></div>
        
        <?php if (!empty($campaign->option) && $campaign->option->plain_text_email == CampaignOption::TEXT_YES) { ?>
        <div class="form-group plain-text-version" style="display: none;">
            <?php echo $form->labelEx($template, 'plain_text');?> [<a data-toggle="modal" href="#available-tags-modal"><?php echo Yii::t('lists', 'Available tags');?></a>]
            <?php echo $form->textArea($template, 'plain_text', $template->getHtmlOptions('plain_text', array('rows' => 20))); ?>
            <?php echo $form->error($template, 'plain_text');?>
        </div>
        <?php } ?>
        
        <div class="pull-right">
            <?php if (!empty($campaign->option) && $campaign->option->plain_text_email == CampaignOption::TEXT_YES) { ?>
            <button type="button" class="btn btn-default btn-plain-text" data-showtext="<?php echo Yii::t('campaigns', 'Show plain text version');?>" data-hidetext="<?php echo Yii::t('campaigns', 'Hide plain text version');?>"><?php echo Yii::t('campaigns', 'Show plain text version');?></button>
            <?php } ?>
            <button type="submit" id="is_next" name="is_next" value="0" class="btn btn-default btn-submit btn-go-next" data-loading-text="<?php echo Yii::t('app', 'Please wait, processing...');?>"><?php echo Yii::t('campaigns', 'Save template changes');?></button>
        </div>
        
        <div class="clearfix"><!-- --></div>
    </div>
    <div class="panel-footer">
        <div class="wizard">
            <ul class="steps">
                <li class="complete"><a href="<?php echo $this->createUrl('campaigns/update', array('campaign_uid' => $campaign->campaign_uid));?>"><?php echo Yii::t('campaigns', 'Name &amp; recipients');?></a><span class="chevron"></span></li>
                <li class="complete"><a href="<?php echo $this->createUrl('campaigns/setup', array('campaign_uid' => $campaign->campaign_uid));?>"><?php echo Yii::t('campaigns', 'Setup');?></a><span class="chevron"></span></li>
                <li class="active"><a href="<?php echo $this->createUrl('campaigns/template', array('campaign_uid' => $campaign->campaign_uid));?>"><?php echo Yii::t('campaigns', 'Template');?></a><span class="chevron"></span></li>
                <li><a href="<?php echo $this->createUrl('campaigns/confirm', array('campaign_uid' => $campaign->campaign_uid));?>"><?php echo Yii::t('campaigns', 'Confirmation');?></a><span class="chevron"></span></li>
                <li><a href="javascript:;"><?php echo Yii::t('app', 'Done');?></a><span class="chevron"></span></li>
            </ul>
            <div class="actions">
                <button type="submit" id="is_next" name="is_next" value="1" class="btn btn-default btn-submit btn-go-next" data-loading-text="<?php echo Yii::t('app', 'Please wait, processing...');?>"><?php echo Yii::t('campaigns', 'Save and next');?></button>
            </div>
        </div>
    </div>
</div>    
<?php $this->endWidget();?>

<div class="modal fade" id="available-tags-modal" tabindex="-1" role="dialog" aria-labelledby="available-tags-modal-label" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title"><?php echo Yii::t('lists', 'Available tags');?></h4>
        </div>
        <div class="modal-body" style="max-height: 300px; overflow-y:scroll;">
            <table class="table table-bordered table-hover table-striped">
                <tr>
                    <td><?php echo Yii::t('lists', 'Tag');?></td>
                    <td><?php echo Yii::t('lists', 'Required');?></td>
                </tr>
                <?php foreach ($template->getAvailableTags() as $tag) { ?>
                <tr>
                    <td><?php echo CHtml::encode($tag['tag']);?></td>
                    <td><?php echo $tag['required'] ? strtoupper(Yii::t('app', CampaignTemplate::TEXT_YES)) : strtoupper(Yii::t('app', CampaignTemplate::TEXT_NO));?></td>
                </tr>
                <?php } ?>
            </table>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo Yii::t('app', 'Close');?></button>
        </div>
      </div>
    </div>
</div>

<?php if(!empty($template->content)) { ?>
<div class="modal fade" id="template-test-email" tabindex="-1" role="dialog" aria-labelledby="template-test-email-label" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title"><?php echo Yii::t('campaigns', 'Send a test email');?></h4>
        </div>
        <div class="modal-body">
             <div class="callout">
                 <strong><?php echo Yii::t('app', 'Notes');?>: </strong><br />
                 <?php
                 $text = '* if multiple recipients, separate the email addresses by a comma.<br />
                 * the email tags will be parsed and we will pick a random subscriber to impersonate.<br />
                 * the tracking will not be enabled.<br />
                 * make sure you save the template changes before you send the test.';
                 echo Yii::t('campaigns', StringHelper::normalizeTranslationString($text));
                 ?>
             </div>
             <?php echo CHtml::form(array('campaigns/test', 'campaign_uid' => $campaign->campaign_uid), 'post', array('id' => 'template-test-form'));?>
             <?php echo CHtml::textField('email', null, array('class' => 'form-control'));?>
             <?php echo CHtml::endForm();?>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo Yii::t('app', 'Close');?></button>
          <button type="button" class="btn btn-primary btn-submit" onclick="$('#template-test-form').submit();"><?php echo Yii::t('campaigns', 'Send test');?></button>
        </div>
      </div>
    </div>
</div>
<?php } ?>

<div class="modal fade" id="template-upload-modal" tabindex="-1" role="dialog" aria-labelledby="template-upload-modal-label" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
          <h4 class="modal-title"><?php echo Yii::t('email_templates',  'Upload template archive');?></h4>
        </div>
        <div class="modal-body">
             <div class="callout">
                <?php
                $text = '
                Please see <a href="{templateArchiveHref}">this example archive</a> in order to understand how you should format your uploaded archive!
                Also, please note we only accept zip files.';
                echo Yii::t('email_templates',  StringHelper::normalizeTranslationString($text), array(
                    '{templateArchiveHref}' => Yii::app()->apps->getAppUrl('customer', 'assets/files/example-template.zip', false, true),
                ));
                ?>
             </div>
            <?php 
            $form = $this->beginWidget('CActiveForm', array(
                'action'        => array('campaigns/template', 'campaign_uid' => $campaign->campaign_uid, 'do' => 'upload'),
                'id'            => $templateUp->modelName.'-upload-form',
                'htmlOptions'   => array(
                    'id'        => 'upload-template-form', 
                    'enctype'   => 'multipart/form-data'
                ),
            ));
            ?>
            <div class="form-group">
                <?php echo $form->labelEx($templateUp, 'archive');?>
                <?php echo $form->fileField($templateUp, 'archive', $templateUp->getHtmlOptions('archive')); ?>
                <?php echo $form->error($templateUp, 'archive');?>
            </div>
            <div class="form-group">
                <?php echo $form->labelEx($templateUp, 'inline_css');?>
                <?php echo $form->dropDownList($templateUp, 'inline_css', $templateUp->getInlineCssArray(), $templateUp->getHtmlOptions('inline_css')); ?>
                <div class="help-block"><?php echo $templateUp->getAttributeHelpText('inline_css');?></div>
                <?php echo $form->error($templateUp, 'inline_css');?>
            </div>
            
            <?php if (!empty($campaign->option) && $campaign->option->plain_text_email == CampaignOption::TEXT_YES) { ?>
            <div class="form-group">
                <?php echo $form->labelEx($templateUp, 'auto_plain_text');?>
                <?php echo $form->dropDownList($templateUp, 'auto_plain_text', $templateUp->getAutoPlainTextArray(), $templateUp->getHtmlOptions('auto_plain_text')); ?>
                <div class="help-block"><?php echo $templateUp->getAttributeHelpText('auto_plain_text');?></div>
                <?php echo $form->error($templateUp, 'auto_plain_text');?>
            </div>
            <?php } ?>
            
            <?php $this->endWidget(); ?>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo Yii::t('app', 'Close');?></button>
          <button type="button" class="btn btn-primary btn-submit" data-loading-text="<?php echo Yii::t('app', 'Please wait, processing...');?>" onclick="$('#upload-template-form').submit();"><?php echo Yii::t('email_templates',  'Upload archive');?></button>
        </div>
      </div>
    </div>
</div>