<?php
  include_once "seleccion.php";
 include_once "clave.php";
 
$data = array();

if (isset($_POST['password'])){

$fraccionamiento=$_POST['fraccionamiento'];	
$email=$_POST['email'];
$password= md5($_POST['password']);

$wk=mysqli_query($connectionex, "SELECT  `AUTD`,`DBD`, `USUARIOBD`, `PASSBD`, `IPBD`, `URLAP` FROM `datestables` where `PINCODE`= '$fraccionamiento'");
if (mysqli_num_rows($wk) > 0) {
   $userData = $wk->fetch_assoc();
   $usernamebd2=$userData['USUARIOBD'];
   $bdname2=$userData['DBD'];
   $server2=$userData['IPBD'];
   $passwordbd=$userData['PASSBD'];
   $urlapp= $userData['URLAP'];
   $passwordbd2=decrypt($passwordbd,$bdname2);

 //$conn = new mysqli($server2,$usernamebd2,$passwordbd2,$bdname2);
  $connectionex->set_charset("utf8");
  $login=mysqli_query($connectionex, "select `id`, `usuario`, `status`, `online`, `cluster`, `tipousuario` from `guardias-user` where `usuario`='$email' and `clave`='$password'");
if (mysqli_num_rows($login) > 0) {
   $userData = $login->fetch_assoc();
     if ($userData['online'] != '0'){
		$data['status'] = 'error2';
		$data['result'] = '';
		}
          else{
    $w=mysqli_query($connectionex, "UPDATE `guardias-user` SET `online`= '1' WHERE `id` = ".$userData['id']."");
        $data['status'] = 'ok';
        $data['result'] = $userData;
        $data['url']= $urlapp;

}
}
else{
		$data['status'] = 'error';
        $data['result'] = '';
}
}
 else{
		$data['status'] = 'error4';
        $data['result'] = '';
}
} else{

    $idusercaf= $_POST['iduser'];

    //Salida
    $check=mysqli_query($connectionex, "select * from `guardias-user` where `id`='$idusercaf'");
    if (mysqli_num_rows($check) > 0) {
   $userData = $check->fetch_assoc();
        $w=mysqli_query($connectionex,"update `guardias-user` set `online`='0' 
        where `id`=".$userData['id']."");
        
        $data['status'] = 'ok';
        $data['result'] = '';
    }
    else
    {
        $data['status'] = 'error';
        $data['result'] = '';
    }
}


echo json_encode($data);
    
?>