<?php
if($_SESSION['nivel'] == 1){

	if (!isset($_POST['nomeBusca'])) {
		$query = ("SELECT id, empresa, email, email01, email02, email03 FROM crm_cliente WHERE statusCliente = 1 ORDER BY empresa ASC");
	} else {
		$query = ("SELECT id, empresa, email, email01, email02, email03 FROM crm_cliente WHERE statusCliente = 1 AND empresa LIKE '%".$_POST['nomeBusca']."%' ORDER BY empresa ASC");
	}

	$result = mysql_query($query);

  
  //and credito<>0 
?>

	<style type="text/css">
		#tblBusca { padding-top: 15px; padding-bottom: 15px; margin-bottom: 10px; }
		.v-middle { vertical-align: middle; height: 100px; }
		.center { margin: auto; text-align: center; }
		.font-30 { font-size: 130%; }
	</style>

  <table width="100%" height="58" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
    <!-- <tr>
      <td height="30" align="center" bgcolor="#DDDDDD"><a href="gerenciar.php?arquivo=inserirempresa.php"><strong><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Cadastrar Empresa</font></strong></a></td>
      <td height="30" colspan="7" align="center" bgcolor="#DDDDDD"><a href="datavencida.php" class="titulo"></a><a href="emailsempresa.txt" target="_blank"><strong><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">Gerar Lista  Emails </font></strong></a></td>
    </tr> -->

    <tr class="linha-topo">
    	<td colspan="9">
    		<form action="?miolo=clientes&acao=gerenciar" method="post" name="frmBusca">
	    		<table id="tblBusca">
	    			<tr>
				    	<td>Buscar:</td>
				    	<td><input type="text" size="45" name="nomeBusca" id="nomeBusca"></td>
				    	<td><input type="submit" name="enviar" id="enviar" value="Buscar"></td>
				    	<td><a href="?miolo=clientes&acao=gerenciar"><input type="button" name="lstTodos" value="Listar Todos"></a></td>
              <td width="70">&nbsp;</td>
              <td>
                <a href="miolo.php?miolo=clientes&acao=xls">
                  <input type="button" name="xls" value="Lista para impress&atilde;o">
                </a>
              </td>
	    			</tr>
	    		</table>
	    	</form>
    	</td>
    </tr>

    <tr class="linha-topo">
      <td width="100" height="25" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Nome da Empresa </font></strong></td>
      <!-- <td width="75" height="25" colspan="1" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Chamado</font></strong></td>-->
      <td width="48" height="25" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Recebidos</font></strong></td>
      <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Enviados</font></strong></td>
      <!-- <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Enviar Arquivo</font></strong></td> -->
      <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Certid&otilde;es</font></strong></td>
      <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Documentos</font></strong></td>
      <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Ver Hist&oacute;rico</font></strong></td>
      <td width="48" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Agenda</font></strong></td>
      <td width="74" height="25" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Alterar</font></strong></td>
      <td width="47" height="25" align="center" valign="middle" ><strong><font size="2" face="Arial, Helvetica, sans-serif">Excluir</font></strong></td>
    </tr>
   
    <?php

    if (mysql_num_rows($result) === 0) { ?>

    	<tr bgcolor='#eaeaea'>
    		<td colspan='9'  class="v-middle center font-30">Sua busca não retornou nenhum cliente.</td>
    	</tr>

    <?php } else {

      while($linha = mysql_fetch_array($result)){
      list($cod, $empresa, $email01, $email02, $email03) = $linha;

      // if ($email!=""){
      // 	$listaemails.=$email.";\n ";
      // }
      // if ($email01!=""){
      // 	$listaemails.=$email01.";\n ";
      // }
      // if ($email02!=""){
      // 	$listaemails.=$email.";\n ";
      // }
      // if ($email03!=""){
      // 	$listaemails.=$email.";\n ";
      // }

		  echo "<tr bgcolor='#cccccc'><td colspan='8' height='3'></td></tr>
		        <tr>
		          <td height='35' align='center' width='213'><font size='2' face='Arial, Helvetica, sans-serif'><b>".utf8_encode($empresa)."</b></font> </td>
		          <td height='35' align='center' width='66'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=arquivos&acao=gerenciar&cod=$cod'>Ver Recebidos</a></font></td>
		          <td height='35' align='center' width='66'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=relatorios&acao=gerenciar&cod=$cod'>Ver Enviados</a></font></td>
		          <td height='35' align='center' width='66'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=certidoes&acao=gerenciar&cod=$cod'>Gerenciar Certid&otilde;es</a></font></td>
		          <td height='35' align='center' width='66'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=documentos&acao=gerenciar&cod=$cod'>Gerenciar Documentos</a></font></td>
		          <!-- <td height='35' align='center'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=enviaArquivo&cod=$cod'>Gerenciar</a></font></td> -->
		          <td height='35' align='center'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=clientes&acao=historico&ref=".sha1($cod)."'>Hist&oacute;rico</a></font></td>
		          <td height='35' align='center'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=agenda&acao=gerenciar&cod=".($cod)."'>Gerenciar</a></font></td>
		          <td height='35' align='center'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=clientes&acao=alterar&cod=$cod'>Alterar</a></font></td>
		          <td height='35' align='center'><font size='2' face='Arial, Helvetica, sans-serif'><a href='miolo.php?miolo=clientes&acao=excluir&cod=$cod'>Excluir</a></font></td>
		        </tr>
		        <tr bgcolor='#cccccc'>
		          <td colspan='8' height='3'></td>
		        </tr>";
		  } }

  // $fp = fopen("emailsempresa.txt", "w"); 
  // fwrite($fp, $listaemails); 
  // fclose($fp); 

  ?>

  </table>
<?php }