
<?php echo ajax_modal_template_header(TEXT_INFO) ?>

<?php echo form_tag('configuration_form', url_for('ext/email_sending/rules','action=save&entities_id=' . _get::int('entities_id') . (isset($_GET['id']) ? '&id=' . $_GET['id']:'') ),array('class'=>'form-horizontal')) ?>
<?php echo input_hidden_tag('entities_id',_get::int('entities_id')) ?>
<div class="modal-body">
  <div class="form-body">
  
  <ul class="nav nav-tabs">
	  <li class="active"><a href="#general_info"  data-toggle="tab"><?php echo TEXT_GENERAL_INFO ?></a></li>
	  <li><a href="#message_text"  data-toggle="tab"><?php echo TEXT_EXT_MESSAGE_TEXT ?></a></li>	  
	</ul>
	
	<div class="tab-content">
  	<div class="tab-pane fade active in" id="general_info">
   
    <div class="form-group">
	  	<label class="col-md-3 control-label" for="is_active"><?php echo TEXT_IS_ACTIVE ?></label>
	    <div class="col-md-9">	
	  	  <p class="form-control-static"><?php echo input_checkbox_tag('is_active',1,array('checked'=>$obj['is_active'])) ?></p>      
	    </div>			
	  </div>
	  
    <div class="form-group">
    	<label class="col-md-3 control-label" for="type"><?php echo TEXT_EXT_RULE ?></label>
      <div class="col-md-9">	
    	  <?php echo select_tag('action_type',email_rules::get_action_type_choices(), $obj['action_type'],array('class'=>'form-control required','onChange'=>'ext_get_entities_fields()')) ?>        
      </div>			
    </div>
            
    <div id="rules_entities_fields"></div> 
    
    
    <div class="form-group">
		  	<label class="col-md-3 control-label" for="attach_attachments"><?php echo TEXT_EXT_ATTACH_ATTACHMENTS_TO_EMAIL ?></label>
	    <div class="col-md-9">	
	  	  <p class="form-control-static"><?php echo input_checkbox_tag('attach_attachments',1,array('checked'=>$obj['attach_attachments'])) ?></p>      
	    </div>			
	  </div>
	  
  	<div class="form-group">
	  	<label class="col-md-3 control-label" for="cfg_sms_send_to_number_text"><?php echo tooltip_icon(TEXT_ENTER_TEXT_PATTERN_INFO) . TEXT_EMAIL_SUBJECT . fields::get_available_fields_helper(_GET('entities_id'), 'subject'); ?></label>
	    <div class="col-md-9">	
	  	  <?php echo input_tag('subject', $obj['subject'],array('class'=>'form-control input-xlarge textarea-small required')); ?>	  	  	  	       
	    </div>			
	  </div> 
        	  
	  </div>
  	<div class="tab-pane fade" id="message_text">
    	    		 
	    <div class="form-group">
		  	<label class="col-md-3 control-label" for="cfg_sms_send_to_number_text"><?php echo tooltip_icon(TEXT_ENTER_TEXT_PATTERN_INFO) .TEXT_EXT_MESSAGE_TEXT; ?>
		  			  		  	
		  	</label>
		  	<div class="col-md-9">
		  		<div id="available_fields"></div>
		  	</div>
		    <div class="col-md-12" style="padding-top: 5px;">	
		  	  <?php echo textarea_tag('description', $obj['description'],array('class'=>'form-control input-xlarge full-editor','editor-height'=>350)); ?>	  	        
		    </div>			
		  </div> 
	  		  	 
	  </div>
	 </div>
           
   </div>
</div> 
 
<?php echo ajax_modal_template_footer() ?>

</form> 

<script>

  $(function() { 
    $('#configuration_form').validate({ignore:'',
	    	submitHandler: function(form){
					app_prepare_modal_action_loading(form)
					form.submit();
				}
      });
                        
    ext_get_entities_fields(); 

    ext_get_entities_available_fields();
                                                                                
  });


  function ext_get_entities_available_fields()
  { 
  	var entities_id = $('#entities_id').val();
  	  	
    $('#available_fields').html('<div class="ajax-loading"></div>');
     
    $('#available_fields').load('<?php echo url_for("ext/email_sending/rules","action=get_available_fields")?>',{entities_id:entities_id},function(response, status, xhr) {
      if (status == "error") {                                 
         $(this).html('<div class="alert alert-error"><b>Error:</b> ' + xhr.status + ' ' + xhr.statusText+'<div>'+response +'</div></div>')                    
      }
      else
      {
        appHandleUniform();
        jQuery(window).resize();      
      }    
    });      
  }   
  
function ext_get_entities_fields()
{ 
	var entities_id = $('#entities_id').val();
	var action_type = $('#action_type').val();
	
  $('#rules_entities_fields').html('<div class="ajax-loading"></div>');
   
  $('#rules_entities_fields').load('<?php echo url_for("ext/email_sending/rules","action=get_entities_fields")?>',{action_type:action_type, entities_id:entities_id, id:'<?php echo $obj["id"] ?>'},function(response, status, xhr) {
    if (status == "error") {                                 
       $(this).html('<div class="alert alert-error"><b>Error:</b> ' + xhr.status + ' ' + xhr.statusText+'<div>'+response +'</div></div>')                    
    }
    else
    {
      appHandleUniform();
      jQuery(window).resize();      
    }    
  });      
}  

function get_monitor_choices()
{ 
	var entities_id = $('#entities_id').val();
	var action_type = $('#action_type').val();
	var fields_id = $('#monitor_fields_id').val();
	
  $('#monitor_choices_row').html('<div class="ajax-loading"></div>');
   
  $('#monitor_choices_row').load('<?php echo url_for("ext/email_sending/rules","action=get_monitor_choices")?>',{action_type:action_type, fields_id:fields_id, entities_id:entities_id, id:'<?php echo $obj["id"] ?>'},function(response, status, xhr) {
    if (status == "error") {                                 
       $(this).html('<div class="alert alert-error"><b>Error:</b> ' + xhr.status + ' ' + xhr.statusText+'<div>'+response +'</div></div>')                    
    }
    else
    {
      appHandleUniform();
      jQuery(window).resize();
    }    
  });      
} 
</script>   