<?php 
if($_SESSION['nivel'] == 1){
  $selectCliente = mysql_query("SELECT empresa FROM crm_cliente WHERE SHA1(id) = '".$_GET['ref']."'");
  $nomeCliente = mysql_result($selectCliente, 0, 0);
 ?>
  <table width="100%" border="0" align="center" cellpadding="5" cellspacing="5" bgcolor="#FFFFFF">
    <tr class="linha-topo">
      <td height="28" align="left" colspan="5" valign="middle" class="titulos-internos">HIST&Oacute;RICO DO CLIENTE: <?php echo utf8_encode($nomeCliente); ?></td>
    </tr>
    <tr class="linha-topo">
      <td align="center"><strong><font size="2" face="Arial, Helvetica, sans-serif">Baixar</font></strong></td>
      <td align="center" width="250"><strong><font size="2" face="Arial, Helvetica, sans-serif">Descri&ccedil;&atilde;o</font></strong></td>
      <td align="center"><strong><font size="2" face="Arial, Helvetica, sans-serif">Data / Hora</font></strong></td>
      <td align="center"><strong><font size="2" face="Arial, Helvetica, sans-serif">Origem</font></strong></td>
      <td align="center"><strong><font size="2" face="Arial, Helvetica, sans-serif">Protocolo</font></strong></td>
    </tr>
    <?php 
    
    $selectArquivos = mysql_query("
                                  SELECT 
                                    * 
                                  FROM
                                    (SELECT 
                                      nomearquivo,
                                      upload,
                                      data_hora,
                                      1 AS origem,
                                      id 
                                    FROM
                                      crm_upload 
                                    WHERE SHA1(codcli) = '".$_GET['ref']."' 
                                      AND statusUpload = 1 
                                    UNION
                                    SELECT 
                                      resposta,
                                      descricao,
                                      data_hora,
                                      2 AS origem,
                                      id_resposta 
                                    FROM
                                      crm_resposta 
                                    WHERE SHA1(id_cliente) = '".$_GET['ref']."'
                                    AND status='1') tabela 
                                  ORDER BY data_hora DESC 
                                  ");
    include 'auxiliares/formata-data.php';
    $aux = 0;
    while($linhaArquivo = mysql_fetch_array($selectArquivos)){
      ?>
      <tr <?php if($aux % 2 == 0){ echo"bgcolor='f2eaea'"; }else{ echo"bgcolor='edeaea'"; } ?>>
        <td align="center" class="dados-historico" ><strong><a href="arquivos/crm/<?php echo $linhaArquivo['0']; ?>">Clique Aqui</a></strong></td>
        <td align="center" class="dados-historico" ><?php echo $linhaArquivo['upload']; ?><br clear="right"></td>
        <td align="center" class="dados-historico" ><?php echo dataPTBRTime($linhaArquivo['2']); ?></td>
        <td align="center" class="dados-historico" >
          <?php
          if($linhaArquivo['3'] == 1) echo utf8_encode($nomeCliente);
          if($linhaArquivo['3'] == 2) echo "Contar Consultoria";
          ?>
        </td>
        <td align="center"  class="dados-historico" ><?php echo str_pad($linhaArquivo['4'], 4, "0", STR_PAD_LEFT);; ?></td>
      </tr>
      <?
      $aux += 1;
    }
    ?>
    </table>
<?php 
}
?>