<?php
session_start();
include 'config.php';

    $uid = $_SESSION['num_usu'];
    $user = $_SESSION['usuario'];

?>

<html>
    
    <head>
        <title>Chat <?=$user?></title>
        <script src="../js/jquery.js" type="text/javascript"></script>
        <script src="../js/chat.js" type="text/javascript"></script>
        <link href="../css/chat.css" rel="stylesheet" type="text/css"/>
    </head>
    
    <body>
   
        <audio id="audio" src="audio/sound_6.mp3"> </audio>
        <div id="titulo" align="center"> Bienvenid@ <?=$user?> </div>
        

<?php
if(isset($_SESSION['usuario']))
{  
    
    ?>
    <div id="users">      
        <div id="contactos">Contactos:</div>
        <ul>
            <li value="0" id="u-0">
                <a value="0" class="userclick">TODOS</a>
            </li>
            <?php
            //$select = "select n_reg,ip from dxacceso where n_reg!='".$uid."'";
            //$select = "select dxopemaex.oper,dxopemaex.nombre from dxopemaex,dxacceso,chat where dxacceso.ip=dxopemaex.nombre and dxopemaex.nombre=chat.ip_rem and dxacceso.ip!='$user' and leido=0";
            //$select = "select a.ip from dxacceso a union select b.ip_rem from chat b where b.ip_rem not in (select a.ip from dxacceso a)";
            /*$select = "select *
                            from 
                            (
                                select b.oper, b.nombre
                                FROM dxopemaex b
                                UNION 
                                select b.oper,a.ip
                                FROM dxacceso a, dxopemaex b
                                     WHERE b.nombre=a.ip
                            ) t1
                            INNER JOIN chat d
                            ON  d.ip_rem = t1.nombre and t1.nombre!='$user'";*/
            //$select = "select a.oper,a.nombre from dxacceso b, dxopemaex a where a.nombre=b.ip and a.nombre!='$user'";
            $select = "select a.oper,a.nombre from dxacceso b, dxopemaex a where a.nombre=b.ip and a.nombre!='$user' union select a.oper,c.ip_rem from dxopemaex a, dxacceso b, chat c where a.nombre=c.ip_rem and c.ip_rem!='$user' and c.ip_rem not in (select b.ip)";
            $query  = mysqli_query($con ,$select,$conex);
            $row = mysqli_num_rows($query);
            if($row>0){
                while($row = mysqli_fetch_array($query)){
                    ?> <li value="<?=$row['oper']?>" id="u-<?=$row['oper']?>" value1="<?=$row['nombre']?>"><a href="#" value="<?=$row['oper']?>" class="userclick" > <?php echo $row['nombre']; ?> </a> </li><?php                
                } 
            }
            ?>
            </ul>
        </ul>
    </div>
          <form method="post" id="form" action="promsj.php">
            <table width="800" height="300">  
            <tr>               
                <td>
                    <div class="mensajes"></div> 
                </td>               
            </tr>
            <tr>
                <td>
                    <input type="text" id="mensaje" name="mensaje"/>
                    <button type="submit" id="enviar" name="enviar">Enviar</button>
                </td>
            </tr>  
          </table>
        </form>
        
        <?php
}else{
    header ("Location: login.php");
    }
?>   
        
    </body> 
</html>

