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

checkLogin('login.php');

if(isset($_GET['id'])){
    $id_cliente = base64_decode($_GET['id']);
    $sql_cliente = "select ds_nome from erpc_clientes where id = $id_cliente";
    $resultado = mysqli_query($conexao, $sql_cliente);
    if(mysqli_num_rows($resultado)){
        while($row = mysqli_fetch_assoc($resultado)){
            $ds_nome = $row['ds_nome'];
        }
    }
}

?>

<div class="block">
  <div class="header">
    <h2>Cliente: <?=$ds_nome; ?></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="50%" >Atendimento</th>
            <th width="20%" >Data</th>
            <th width="15%" >Status</th>
            <th width="10%" class="no-sort">Visualizar</th>
          </tr>
        </thead>
        <tbody>
          <?php
          
          $sql = "select 	 at.id
                    		,ds_titulo
                            ,date_format(dh_inicio_atendimento, '%d/%m/%Y') dh_inicio_atendimento
                            ,ds_status
                            ,id_pedido_liberado
                    	from erpc_crm_atendimentos as at
                    		 inner join erpc_crm_status_atendimento as st 
                             on at.id_status_atendimento = st.id
                    	where id_cliente = $id_cliente
                        order by dh_fim_atendimento desc";
          
          $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_titulo = $row['ds_titulo'];
                  $dh_inicio_atendimento = $row['dh_inicio_atendimento'];
                  $ds_status = $row['ds_status'];
                  $id_pedido_liberado = base64_encode($row['id_pedido_liberado']);
          ?>
          <tr>
            <td><?= sgcws_html($id); ?></td>
            <td><?= sgcws_html($ds_titulo); ?></td>
            <td><?= sgcws_html($dh_inicio_atendimento); ?></td>
            <td><?= sgcws_html($ds_status); ?></td>
            
           
            <span class="glyphicons glyphicons-usd"></span>
            <td class='text-center'>
              <?php 
              if(!empty($id_pedido_liberado)){ ?>
                  <a href="erpc_pedidos_venda_edit.php?id=<?=$id_pedido_liberado; ?>"><i class="glyphicon glyphicon-eye-open" title='Visualizar'><span class="sr-only"></span></i></a>
              <?php 
              }else{
              ?>
              <a href="erpc_crm_atendimento_edit.php?id_at=<?=$id_crip; ?>"><i class="glyphicon glyphicon-eye-open" title='Visualizar'><span class="sr-only"></span></i></a>
			  <?php 
              }
			  ?>
            </td>
          </tr>        
                  
          <?php 
              }
          }  
          ?>
        </tbody>
      </table>
    </div> <!-- table-responsive -->
  </div> <!-- content -->
</div> <!-- block -->

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