<?php
include 'header.php';
include 'erpc_crm_atendimento_toolbar.php';
require_once 'inc/clientes.php';
require_once 'inc/usuario.php';

checkLogin('login.php');

?>

<div class="block">
  <div class="header">
    <h2>Cliente - Lista</h2>
  </div>
  <div class="content">
    <?php
    //echo '<pre>' . var_export($_SESSION, true) . '</pre>';
    displayMessagesAndErrors();
    ?>
    <div class="table-responsive">
      <table class="table table-bordered table-striped table-responsible code-sortable" data-order-by="0">
        <thead>
          <tr>
            <!--<th width="3%"><input type="checkbox" class="checkall" /></th>-->
            <th width="05%" >ID</th>
            <th width="30%" >Cliente</th>
            <th width="30%" >Ultimo Atendimento</th>
            <th width="10%" class="no-sort">Visualizar</th>
          </tr>
        </thead>
        <tbody>
          <?php
          
          $sql = "select 	 c.id
                    		,c.ds_nome
                            ,DATE_FORMAT(MAX(at.dh_fim_atendimento), '%d/%m/%Y') as ultimo_atendimento
                    	from erpc_clientes as c
                    		 inner join erpc_crm_atendimentos as at
                    		 on at.id_cliente = c.id
                    	where at.st_ativo = 1
                        group by id";
          
          $result = mysqli_query($conexao, $sql);
          if(mysqli_num_rows($result)>0){
              while($row = mysqli_fetch_assoc($result)){
                  $id = $row['id'];
                  $id_crip = base64_encode($id);
                  $ds_cliente = $row['ds_nome'];
                  $ultimo_atendimento = $row['ultimo_atendimento'];
          ?>
          <tr>
            <td><?= sgcws_html($id); ?></td>
            <td><?= sgcws_html($ds_cliente); ?></td>
            <td><?= sgcws_html($ultimo_atendimento); ?></td>
            
           
            <span class="glyphicons glyphicons-usd"></span>
            <td class='text-center'>
              <a href="erpc_crm_atendimento_cliente_list.php?id=<?=$id_crip; ?>"><i class="glyphicon glyphicon-eye-open" title='Visualizar'><span class="sr-only"></span></i></a>
            </td>
          </tr>        
                  
          <?php 
              }
          }  
          ?>
        </tbody>
      </table>
    </div> <!-- table-responsive -->
  </div> <!-- content -->
</div> <!-- block -->

<?php
include 'footer.php';
?>
