<?
session_start();
require('../valtiempoinact.php');
require("../conectar.php");
$nom=$_POST["nomb"];
$apa=$_POST["apat"];
$ama=$_POST["amat"];
$tod=$_POST["todos"];
$mis=$_POST["mios"];
$ema=$_POST["mail"];
/*$pag=$_POST["pag"];
$filas=50;
$pagina=$pag*$filas;*/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
     <head>
	 <script>
	 function borrarCliente(id,td)
{
var f=confirm( "En verdad quieres borrar el cliente?");
if(f==true)
	{
	 $.post("borrarcliente.php", {id:id}, function(data,status){
	 if(status=="success")
 		{
 		alert("El registro ha sido borrado");  
		$("#fila"+td).hide();
 		} });
	}
}
	 
	 
	 </script>
     </head>
	 <body>
	 <?
	     if($nom=="" && $apa=="" && $ama=="" && $tod=="no" && $mis=="no" && $ema==""){echo "";}else{
	 ?>
	     <table class="datos" width="850">
		     <tr>
		         <td colspan="7" align="center" class="tl">
				     <b>Prospectos</b>
				 </td>
		     </tr>
		     <tr class="trsb">
			     <td width="40"></td>
			     <td align="center">Nombre cliente</td>
			     <td align="center">Ejecutivo</td>
			     <!--<td align="center">Apellido materno</td>-->
			     <td align="center">e-mail</td>
			     <!--<td align="center">Ver rentas</td> -->
			     <td align="center">nueva renta</td>
				  <?if($_SESSION["usuario_nivel"]>4)
						 {?>
				 <td align="center">Eliminar Renta</td>
				  <?}?>
		     </tr>
			 <?
				
			     if($nom!=""){
				     $no="c.nombre LIKE '$nom%'";			 
				 }
				 if($apa!=""){
				     if($nom!=""){$and="AND";}
				     $ap="$and c.a_pat LIKE '$apa%'";
				 }
				 if($ama!=""){
				     if($nom!=""||$apa!=""){$and="AND";}
				     $am="$and c.a_mat LIKE '$ama%'";				 
				 }
				 if($ema!=""){
				     if($nom!=""||$apa!=""|| $ama!=""){$and="AND";}
				     $em="$and c.email LIKE '$ema%'";				 
				 }
				 if($mis=="si"){
				     if($nom!=""||$apa!="" || $ama!=""){$and="AND";}
				     $mi="$and c.id_emp LIKE '$_SESSION[usuario_id]%'";				 
				 }
				 $cond="$no $ap $am $em $mi";
				 if($tod=="si"){$cond="id_cliente!=''";}
			     $sql="SELECT renta.id_renta,renta.id_ejecutivo, c.id_cliente, c.nombre, c.a_pat, c.a_mat, c.email FROM clientes as c inner join renta on renta.id_cliente=c.id_cliente WHERE $cond and renta.borrar='0' ORDER BY id_cliente desc  limit 60 ";
				 $con=mysql_query($sql);
				 $nr=mysql_num_rows($con);
				 if($nr!=0){
				 $c="";
				 $i=1;
				 while($ls=mysql_fetch_array($con)){
				    $diascita= 5;
                    $f3d=date("Y-m-d", strtotime("$fechaActual -$diascita day"));
                    $sqls="SELECT id_cliente,id_ejecutivo FROM renta WHERE id_renta='$ls[id_renta]' and fecha<='$f3d' AND ultimaact<='$f3d'";
					$ejesema=mysql_query($sqls);
					$semaforo=mysql_fetch_array($ejesema);
					
					$sqlp="SELECT id_cliente,id_ejecutivo FROM renta WHERE id_renta='$ls[id_renta]' and fecha<='$f3d' AND ultimaact<='$f3d' AND id_renta IN (SELECT id_renta FROM pagos)";
					$ejep=mysql_query($sqlp);
					$semaforop=mysql_fetch_array($ejep);
					
					if($semaforo){
					    $img="<img src='images/semaforo-verde.png' width='10' height='10' title='LO PUEDES MODIFICAR'>";
						}else{
						$img="<img src='images/semaforo-rojo.png' width='10' height='10' title='NO PUEDES MODIFICAR'>";
					}
					if($semaforop){
					
					    $p="<b>P </b>";
						}else{
						$p="";
					}
						$ejecut=$ls["id_ejecutivo"];
					$sqle="SELECT usuario FROM empleados WHERE id_empleado='$ejecut'";
					$ejeem=mysql_query($sqle);
					$ej=mysql_fetch_array($ejeem);
					$nombrecliente=$ls["nombre"]." ".$ls["a_pat"]." ".$ls["a_mat"];
				  ?>
				     <tr class="tr<?echo $c?>" id="fila<?echo $i;?>" align="true" onclick="javascript:select(this.id);">
					     <td onclick="javascript:abre('<?echo $ls["id_renta"]?>');"><?echo$p.$img;?></td>
					     <td onclick="javascript:abre('<?echo $ls["id_renta"]?>');"><?echo htmlentities($nombrecliente);?></td>
					     <td onclick="javascript:abre('<?echo $ls["id_renta"]?>');"><?echo htmlentities($ej['usuario']);?></td>
					     <!--<td><?echo htmlentities($ls["a_mat"]);?></td>-->
						 <td onclick="javascript:abre('<?echo $ls["id_renta"]?>');"><?echo htmlentities($ls["email"]);?></td>
					     <!-- <td  onclick="javascript:abre('<?echo $ls["id_renta"]?>');" align="center"><a href="#rentascont" onclick="javascript:abre('<?echo $ls["id_renta"]?>');"><img src="images/zoom.png" width="18" height="18" style="margin-left:-5px;" alt="Ver datos"></a></td> -->
					     <td align="center"><a target="blank" href="regprospecto2.php?op=mod&id=<?echo $ls["id_cliente"];?>"><img src="images/media.png" width="18" height="18" style="margin-left:-5px;" alt="Modificar"></a></td>
						 <?if($_SESSION["usuario_nivel"]>4)
						 {?>
						  <td align="center"><a ><img onclick="borrarCliente('<?echo $ls["id_renta"];?>','<?echo $i;?>');" src="images/delete.png" width="18" height="18" style="margin-left:-5px;" alt="Modificar"></a></td>
						 <?}?>
				     </tr>
				  <?
				     if($c==""){$c="1";}else{$c="";}
					 $i++;
				 }
			 }else{
				     ?>
					 <tr class="tr">
					     <td colspan="6" align="center">No se encontraron registros que coincidan con la busqueda</td>
					 </tr>
					 <?
				 }
			 ?>
	     </table>
		 <?
		}
		 ?>
	 </body>
	 <script>
	     $(".loading").fadeOut('fast');
	 </script>
</html>