<script language="javascript" type="text/javascript">
  var modulo = 'cursos-inscricoes';
</script>
<?php
$modulo = 'cursos-inscricoes';
$arrJafoi = array();
?>
<div class="row-fluid">
  <div class="span12 btn-inverse div-top-borded">
    Inscrições do curso: <?=$this->category->fullname?>
    
    <a href="<?=$this->linkTo('cursos', "list", array());?>" class="btn btn-small btn-info">&laquo; Voltar</a>
    <!-- <a href="<?=$this->linkTo($modulo, "new", array('r'=>$this->category->id));?>" class="btn btn-small btn-primary">Novo »</a> -->
    <form class="search-query pull-right" method="get" action="<?=$this->linkTo($modulo, "list", array('r' => $this->category->id ));?>">
      <input type="text" class="search-query" placeholder="Search" name="s" value="<?php if(!empty($this->filters['s'])) { echo $this->filters['s']; }?>"/>
      <button type="submit" class="btn">Buscar</button>
      <?php if(!empty($this->filters['s'])): ?>
        <input type="button" class="btn" value="Cancelar" onclick="window.location.href = '<?=$this->linkTo($modulo, "list");?>'"/>
      <?php endif; ?>
    </form>
  </div>
</div>
<div class="row-fluid">
  <table class="table table-striped">
    <thead>
      <tr>
        <th width="5">&nbsp;</th>
        <th width="5">#</th>
        
        <th class="">Usuário </th>
        <th class="">CPF </th>
        <th class="">Email </th>
        <th class="">Ficha </th>
        <th class="">Aprovar </th>
        <th class="">Excluir </th>
        
        
        
        
        
        
        
      </tr>
    </thead>
    <tbody>
      <? if(count($this->items) === 0): ?>
        <tr class="first">
          <td colspan="10">Nenhum registro encontrado</td>
        </tr>
      <? endif; ?>
      <? foreach($this->items as $item):

        if(!in_array($item->user_id, $arrJafoi)){
          $arrJafoi[] = $item->user_id;
          $usuario = $item->getUsuario();
          ?>
          <tr class="first order" id="tr-<?= $item->user_id; ?>">
            <td class="updown">&nbsp;</td>
            <td ><input type="checkbox" name="check_inscricao" class="chkbox" data-userid="<?=$item->user_id;?>" data-categoryid="<?=$this->category->id?>"></td>
            
            <td><a href="<?=$this->linkTo($modulo, "edit", array('r'=>$this->category->id,"id" => $item->user_id));?>" title="edit">
              <?=strip_tags($usuario->firstname);?> 
              <?=strip_tags($usuario->lastname);?>
            </a></td> 
            <td><a href="<?=$this->linkTo($modulo, "edit", array('r'=>$this->category->id,"id" => $item->user_id));?>" title="edit">
              <?=strip_tags($usuario->username);?>
            </a></td> 
            <td><a href="<?=$this->linkTo($modulo, "edit", array('r'=>$this->category->id,"id" => $item->user_id));?>" title="edit">
              <?=strip_tags($usuario->email);?>
            </a></td> 
            <td><a href="<?=$this->linkTo($modulo, "edit", array('r'=>$this->category->id,"id" => $item->user_id));?>" title="edit">
              Visualizar Ficha
            </a></td> 
            <td class="btn_inscricao" data-email="<?=strip_tags($usuario->email);?>" data-nome="<?=strip_tags($usuario->firstname);?>" >
              <?php 
              if(!$usuario->verificaInscricaoByCursoId($this->category->id)){
                ?>
                <a href="javascript:aprovarInscricao(<?=$this->category->id.','.$item->user_id;?>)" class="confirm" title="edit">Aprovar inscrição</a>
                <?php
              }else{
                echo "Inscrito";
              }
              ?>
            </td> 
            <td class="tc">
              <ul class="actions">


                <li><a class="ico confirm" href="<?=$this->linkTo($modulo, "delete", array("id" => $item->id,"r" => $this->category->id));?>" title="delete"><img src="css/img/led-ico/delete.png" alt="delete" /></a></li>
              </ul>
            </td>
            
            
            
            
            
            
          </tr>
          <?php 
        }
      endforeach; ?>
    </tbody>
  </table>

  <div class="acoes bottom" style="display: block;margin-left: 26px;">
    <img src="<?=BASE_URL?>cms/img/arrow_ltr.png" />
    <select id="com_marcados" onchange="comMarcados()">
      <option value="">Com marcados:</option>
      <option value="aprovar">Aprovar Inscrição</option>

    </select>
  </div>
  <br>
  <br>

</div>
<div class="row-fluid">
  <!-- pagination -->
  <?php //$this->paginationControl($this->items, null, '_paginator.phtml', array("filters" => $this->filters));?>
</div>

<script type="text/javascript">
  $(document).ready(function(){
    addCheckEvents();
  });


  var lastChecked = null;

  function addCheckEvents(){
    var $chkboxes = $('.chkbox');
    $chkboxes.click(function(e) {
      if(!lastChecked) {
        lastChecked = this;
        return;
      }

      if(e.shiftKey) {
        var start = $chkboxes.index(this);
        var end = $chkboxes.index(lastChecked);

        $chkboxes.slice(Math.min(start,end), Math.max(start,end)+ 1).prop('checked', lastChecked.checked);

      }

      lastChecked = this;
    });
  }

  function comMarcados(){
    var $opt = $('#com_marcados').val();
    var qtdechecked = $('.chkbox:checked').length;

    if($opt!='' && qtdechecked>0){
      if($opt=='aprovar'){
        var ids = '';
        var cc = 0;
        $('.chkbox:checked').each(function(){
          curso_id = $(this).data('categoryid');
          user_id = $(this).data('userid');
          aprovarInscricao(curso_id,user_id);
          $(this).removeAttr('checked');
          cc++;
          
        }); 



      }
    }
    $('#com_marcados').val('');
  }


  
  function aprovarInscricao(curso_id,user_id){
    $('#tr-'+user_id+' .btn_inscricao').html('Aguarde...');
    $.post(BASE_URL+'cms/cursos-inscricoes/inscrever',{'curso_id':curso_id,'user_id':user_id},function(data){
      if(data == 1){
        $('#tr-'+user_id+' .btn_inscricao').html('Inscrito'); 
        var nome = $('#tr-'+user_id+' .btn_inscricao').data('nome'); 
        var email = $('#tr-'+user_id+' .btn_inscricao').data('email'); 
        sendEmail(nome,email);
      }else{
        alert(data);
      }
    });
  }


<?php 
$txtemail = $this->category->texto_email;
if(empty($txtemail)){
  $txtemail = "Sua inscrição no curso ".$this->category->fullname." foi aprovada!";
}
?>

  function sendEmail(nome,email){


    $.post('http://londigital.com.br/mailsender/ceped_ead_sender_inscricao.php',{
      'nome':nome,
      'email':email,
      'texto':'<?= htmlentities($this->category->texto_email, ENT_QUOTES)?>'
    },function(data){

      console.log(email +" | "+ nome+" | "+data);
    })

  }

</script>

