<?
global $db;
ob_start();
?>
  <div class="form-group">
    <label class="col-lg-3 control-label">Wysyłaj tylko raz:</label>
    <div class="col-lg-9 pt5">
      <div class="switch switch-primary round switch-inline">
        <input id="only_once" name="only_once" type="checkbox">
        <label for="only_once"></label>
        <small><i>Dotyczy wysyłki maili z szablonu.</i></small>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label class="col-lg-3 control-label">Zakres dat:</label>
    <div class="col-lg-9">
      <div class="input-group">
        <input type="text" id="dates" name="dates" data="search-column:date_add;search-type:date_range" class="form-control date-range require" value="">
        <span class="input-group-addon cursor date-range-icon">
          <i class="fa fa-calendar"></i>
        </span>
      </div>
    </div>
  </div>
  <div class="form-group">
    <label class="col-lg-3 control-label">lub / i</label>
  </div>
  <? 
    $templates[''] = '---- szablon ----';
    if ( is_array( $this -> templates ) ): foreach ( $this -> templates as $template ):
      $templates[ $template['id'] ] = $template['name'];
    endforeach; endif;
  ?>
  <?= \Html::select(
         array(
          'label'   => 'Szablon',
          'name'    => 'template',
          'id'      => 'template',  
          'values'  => $templates,
          'value'   => $this -> templates['id']  
        
       ));?>
  <div class="form-group">
    <label class="col-lg-3 control-label">Podgląd:</label>
    <div class="col-lg-9">
      <div id="newsletter-preview"></div>
    </div>
  </div>
<?
$out = ob_get_clean();

$grid = new \gridEdit;
$grid -> id               = 'newsletter-prepare';
$grid -> gdb_opt          = $gdb;
$grid -> include_plugins  = true;
$grid -> title            = 'Wysyłka newslettera - przygotowanie';
$grid -> default_buttons  = false;
$grid -> external_code    = $out;
$grid -> buttons          = [
                              [ 
                                'label' => 'Wyślij newsletter',
                                'class' => 'btn-success',
                                'icon'  => 'fa-send',
                                'js'    => 'send_newsletter();'
                              ]
                            ];
echo $grid -> draw();
?>
<script type="text/javascript">
  function send_newsletter()
  {
    var dates = $( '#dates' ).val();
    var template = $('#template').val();
    
    if ( !dates && !template )
      $.prompt( 'Proszę wybrać zakres dat lub szablon.', { title: 'Błąd' } );
    
    $( '#fg-newsletter-prepare' ).submit();
    return false;
  }
  
  $( function()
  {
    $( '#fg-newsletter-prepare' ).attr( 'action', '/admin/newsletter/send/' );
    
    $( 'body' ).on( 'change', '#dates', function()
    {
      var dates = $( this ).val();
      var template = $('#template').val();
      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/ajax.php',
        data: {
          a: 'newsletter-preview',
          dates: dates,
          template: template
        },
        beforeSend: function() 
        {
          $( '#overlay' ).show();
        },
        success: function( response ) 
        {
          $( '#overlay' ).hide();             
          $( '#newsletter-preview' ).html( response );
        }
      });
    });
    
    $( 'body' ).on( 'change', '#template', function()
    {
       var dates = $( '#dates' ).val();
      var template = $( this ).val();
      
      $.ajax(
      {
        type: 'POST',
        cache: false,
        url: '/admin/ajax.php',
        data: {
          a: 'newsletter-preview',
          template: template,
          dates: dates
        },
        beforeSend: function() 
        {
          $( '#overlay' ).show();
        },
        success: function( response ) 
        {
          $( '#overlay' ).hide();             
          $( '#newsletter-preview' ).html( response );
        }
      });
    });
  });
</script>